diff mbox series

[bug#51581,1/2] import: utils: Add ‘wrap-lines’ procedure.

Message ID a5deb3aa692385c7ca0b1ed6e4f4436b03b3528c.1635891637.git.public@yoctocell.xyz
State Accepted
Headers show
Series Wrap lines in the description of generated packages | expand

Checks

Context Check Description
cbaines/comparison success View comparision
cbaines/git branch success View Git branch
cbaines/applying patch success View Laminar job
cbaines/issue success View issue

Commit Message

Xinglu Chen Nov. 3, 2021, 10:19 a.m. UTC
* guix/import/utils.scm (wrap-lines): New procedure.
(beautify-description): Use it; add optional ‘length’ argument.
* tests/import-utils.scm ("wrap-lines: 80 characters"): Test it.
---
 guix/import/utils.scm  | 36 +++++++++++++++++++++++++++++++-----
 tests/import-utils.scm |  5 +++++
 2 files changed, 36 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/guix/import/utils.scm b/guix/import/utils.scm
index a180742ca3..103ec2ffe1 100644
--- a/guix/import/utils.scm
+++ b/guix/import/utils.scm
@@ -9,6 +9,7 @@ 
 ;;; Copyright © 2020 Martin Becze <mjbecze@riseup.net>
 ;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
 ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
+;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -66,6 +67,7 @@  (define-module (guix import utils)
             license->symbol
 
             snake-case
+            wrap-lines
             beautify-description
 
             alist->package
@@ -231,9 +233,32 @@  (define (snake-case str)
 with dashes."
   (string-join (string-split (string-downcase str) #\_) "-"))
 
-(define (beautify-description description)
-  "Improve the package DESCRIPTION by turning a beginning sentence fragment
-into a proper sentence and by using two spaces between sentences."
+(define* (wrap-lines str #:optional (length 80))
+  "Given a string STR, wrap lines at LENGTH characters"
+  (define (aux str acc counter)
+    (cond
+     ((string-null? str) acc)
+     ((and (not (= (string-length acc) 0))
+           (= (modulo (string-length acc) length) 0)
+           (not (= counter 1)))
+      (let ((before (substring acc 0 (- counter 1)))
+            (after (substring acc counter)))
+        (aux str (string-append before "\n" after) 1)))
+     ((char=? (string-ref str 0) #\space)
+      (aux (substring str 1)
+           (string-append acc (char-set->string (char-set (string-ref str 0))))
+           (+ (string-length acc) 1)))
+     (else
+      (aux (substring str 1)
+           (string-append acc (char-set->string (char-set (string-ref str 0))))
+           counter))))
+
+  (aux str "" 1))
+
+(define* (beautify-description description #:optional (length 80))
+  "Improve the package DESCRIPTION by turning a beginning sentence fragment into
+a proper sentence and by using two spaces between sentences, and wrap lines at
+LENGTH characters."
   (let ((cleaned (cond
                   ((string-prefix? "A " description)
                    (string-append "This package provides a"
@@ -248,8 +273,9 @@  (define (beautify-description description)
                                              (string-length "Functions"))))
                   (else description))))
     ;; Use double spacing between sentences
-    (regexp-substitute/global #f "\\. \\b"
-                              cleaned 'pre ".  " 'post)))
+    (wrap-lines (regexp-substitute/global #f "\\. \\b"
+                                          cleaned 'pre ".  " 'post)
+                length)))
 
 (define* (package-names->package-inputs names #:optional (output #f))
   "Given a list of PACKAGE-NAMES or (PACKAGE-NAME VERSION) pairs, and an
diff --git a/tests/import-utils.scm b/tests/import-utils.scm
index 7c6c782917..c6790f624a 100644
--- a/tests/import-utils.scm
+++ b/tests/import-utils.scm
@@ -3,6 +3,7 @@ 
 ;;; Copyright © 2016 Ben Woodcroft <donttrustben@gmail.com>
 ;;; Copyright © 2020 Martin Becze <mjbecze@riseup.net>
 ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
+;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -31,6 +32,10 @@  (define-module (test-import-utils)
 
 (test-begin "import-utils")
 
+(test-equal "wrap-lines: 80 characters"
+  "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor\nincididunt ut labore et dolore magna aliqua."
+  (wrap-lines "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."))
+
 (test-equal "beautify-description: use double spacing"
   "This is a package.  It is great.  Trust me Mr.  Hendrix."
   (beautify-description