GNU bug report logs - #51581
[PATCH 0/2] Wrap lines in the description of generated packages

Previous Next

Package: guix-patches;

Reported by: Xinglu Chen <public <at> yoctocell.xyz>

Date: Wed, 3 Nov 2021 10:18:02 UTC

Severity: normal

Tags: patch

Done: Ludovic Courtès <ludo <at> gnu.org>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Xinglu Chen <public <at> yoctocell.xyz>
To: 51581 <at> debbugs.gnu.org
Subject: [bug#51581] [PATCH 1/2] import: utils: Add ‘wrap-lines’ procedure.
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 <mjbecze <at> riseup.net>
 ;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer <at> gmail.com>
 ;;; Copyright © 2021 Sarah Morgensen <iskarian <at> mgsn.dev>
+;;; Copyright © 2021 Xinglu Chen <public <at> 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 <at> gmail.com>
 ;;; Copyright © 2020 Martin Becze <mjbecze <at> riseup.net>
 ;;; Copyright © 2021 Sarah Morgensen <iskarian <at> mgsn.dev>
+;;; Copyright © 2021 Xinglu Chen <public <at> 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
-- 
2.33.0







This bug report was last modified 3 years and 158 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.