unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
From: Xinglu Chen <public@yoctocell.xyz>
To: 51581@debbugs.gnu.org
Subject: [bug#51581] [PATCH 1/2] import: utils: Add ‘wrap-lines’ procedure.
Date: Wed, 03 Nov 2021 11:19:16 +0100	[thread overview]
Message-ID: <a5deb3aa692385c7ca0b1ed6e4f4436b03b3528c.1635891637.git.public@yoctocell.xyz> (raw)
In-Reply-To: <cover.1635891637.git.public@yoctocell.xyz>

* 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@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
-- 
2.33.0







  reply	other threads:[~2021-11-03 10:20 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-03 10:17 [bug#51581] [PATCH 0/2] Wrap lines in the description of generated packages Xinglu Chen
2021-11-03 10:19 ` Xinglu Chen [this message]
2021-11-03 10:19 ` [bug#51581] [PATCH 2/2] import: Beautify descriptions when appropriate Xinglu Chen
2021-11-12 22:44 ` [bug#51581] [PATCH 0/2] Wrap lines in the description of generated packages Ludovic Courtès
2021-12-01 15:32   ` Ludovic Courtès
     [not found] ` <cover.1639735009.git.public@yoctocell.xyz>
2021-12-17 15:53   ` bug#51581: [PATCH v2 " Ludovic Courtès

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a5deb3aa692385c7ca0b1ed6e4f4436b03b3528c.1635891637.git.public@yoctocell.xyz \
    --to=public@yoctocell.xyz \
    --cc=51581@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).