Subject: [PATCH 1/2] import: utils: Add ‘wrap-lines’ procedure.

From: Xinglu Chen <[email-masked]>
Date: Wed, 03 Nov 2021 11:19:16 +0100

* 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 --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 <[email-masked]>
 ;;; Copyright © 2021 Maxim Cournoyer <[email-masked]>
 ;;; Copyright © 2021 Sarah Morgensen <[email-masked]>
+;;; Copyright © 2021 Xinglu Chen <[email-masked]>
 ;;;
 ;;; 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 <[email-masked]>
 ;;; Copyright © 2020 Martin Becze <[email-masked]>
 ;;; Copyright © 2021 Sarah Morgensen <[email-masked]>
+;;; Copyright © 2021 Xinglu Chen <[email-masked]>
 ;;;
 ;;; 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
-- 
2.33.0