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