Debian Codegraph @

lib/text/fill.scm

raw
1 ;;;
2 ;;; text.fill - Fill paragraph
3 ;;;
4 ;;; Copyright (c) 2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 (define-module text.fill
35 (use gauche.unicode)
36 (use srfi.13)
37 (use text.tree)
38 (use util.match)
39 (export display-filled-text
40 text->filled-stree)
41 )
42 (select-module text.fill)
43
44 #|
45 width
46 <------------------------------------------------------------------->
47
48 hanging
49 <------>
50 Lorem ipsum dolor sit amet, consectetur adipiscing elit,
51 sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
52 Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
53 nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
54 in reprehenderit in voluptate velit esse cillum dolore eu fugiat
55 nulla pariatur. Excepteur sint occaecat cupidatat non proident,
56 sunt in culpa qui officia deserunt mollit anim id est laborum.
57 <->
58 indent
59
60 NB: The first line does not consider 'indent' argument, but
61 it does consider the starting column if it's available. So even
62 if you start printing a paragraph in the middle of the line, the text
63 still fits within the width.
64
65
66 width
67 <--------------------------------------------------------------------->
68 start column
69 | hanging
70 v<---->
71 Lorem ipsum dolor sit amet, consectetur
72 adipiscing elit, sed do eiusmod tempor incididunt ut labore
73 et dolore magna aliqua. Ut enim ad minim veniam, quis
74 nostrud exercitation ullamco laboris nisi ut aliquip ex ea
75 commodo consequat. Duis aute irure dolor in reprehenderit in
76 voluptate velit esse cillum dolore eu fugiat nulla pariatur.
77 Excepteur sint occaecat cupidatat non proident, sunt in culpa
78 qui officia deserunt mollit anim id est laborum.
79 <-------->
80 indent
81
82 The 'lead-in' is a leading text before paragraph. If it is shorter than
83 (- hanging 1), it becomes run-in heading. Otherwise, the paragraph begins
84 from the next line. Compare the following two examples:
85
86
87 hanging
88 <---------------->
89 THIS IS LEAD-IN. Lorem ipsum dolor sit amet, consectetur
90 adipiscing elit, sed do eiusmod tempor incididunt ut labore
91 et dolore magna aliqua.
92 <----------->
93 indent
94
95
96 hanging
97 <---------------->
98 THIS IS LEAD-IN LONGER THAN HANGING.
99 Lorem ipsum dolor sit amet, consectetur
100 adipiscing elit, sed do eiusmod tempor incididunt ut labore
101 et dolore magna aliqua.
102 <----------->
103 indent
104 |#
105
106 ;; If the input text has double newlines, it marks a paragraph break.
107 ;; The next line starts with hanging indent again.
108
109 ;; TODO:
110 ;; - Customize east-asian-width
111
112 (define *default-width* 65)
113 (define *default-indent* 0)
114 (define *default-hanging* 0)
115
116 (define (display-filled-text text :key (port (current-output-port))
117 (indent #f) (hanging #f) (width #f)
118 (lead-in #f) (start-column #f))
119 ($ write-tree
120 ($ text->filled-stree text
121 :start-column (or start-column (~ port'current-column))
122 :indent indent
123 :hanging hanging
124 :lead-in lead-in
125 :width width)
126 port)
127 (undefined))
128
129 (define (text->filled-stree text :key (indent #f) (hanging #f) (width #f)
130 (start-column 0) (lead-in #f))
131 (let ([indent (or indent *default-indent*)]
132 [hanging (or hanging indent *default-hanging*)]
133 [width (or width *default-width*)])
134 (assume (and (exact-integer? start-column) (>= start-column 0)))
135 (assume (and (exact-integer? indent) (>= indent 0)))
136 (assume (and (exact-integer? hanging) (>= hanging 0)))
137 (assume (and (exact-integer? width) (> width indent)))
138 (assume-type lead-in (<?> <string>))
139
140 (let loop ([par (segment-paragraph text)]
141 [start-column start-column]
142 [lead-in lead-in]
143 [r '()])
144 (match par
145 [()
146 (if (and (null? r) lead-in)
147 (list lead-in) ;edge case - empty text
148 (intersperse "\n" (reverse r)))]
149 [(x . xs)
150 (loop xs 0 #f
151 (cons (fill-paragraph x indent hanging width start-column lead-in)
152 r))]))))
153
154 (define (fill-paragraph text indent hanging width start-column lead-in)
155 ;; NB: This algorithm is similar to pretty printer, and we may integrate
156 ;; the two in future.
157 (let ([indenter (string-append "\n" (make-string indent #\space))]
158 [hanging-indenter (if lead-in
159 (if (< (string-east-asian-width lead-in) hanging)
160 (format "~va" hanging lead-in)
161 (format "~a\n~va" lead-in hanging ""))
162 (make-string hanging #\space))])
163 (let loop ([words (segment-text text)]
164 [column (+ hanging start-column)]
165 [r (list hanging-indenter)])
166 (match words
167 [() (reverse r)]
168 [('s word . rest)
169 (let1 w (string-east-asian-width word)
170 (if (<= (+ column w 1) width)
171 (loop rest (+ column w 1) (list* word " " r))
172 (loop rest (+ indent w) (list* word indenter r))))]
173 [(word . rest)
174 (let1 w (string-east-asian-width word)
175 (if (or (<= (+ column w) width)
176 (length=? r 1)) ;at the very beginning
177 (loop rest (+ column w) (cons word r))
178 (loop rest (+ indent w) (list* word indenter r))))]
179 ))))
180
181 (define (segment-paragraph text)
182 (map string-trim-both (string-split text "\n\n")))
183
184 ;; Split text into unbreakable chunks. We should also consider
185 ;; hyphenations, but that's for future versions.
186 ;;
187 ;; Symbol 's' is inserted where we need to emit #\space if it's in
188 ;; mid-line, but not before/after break.
189 (define (segment-text text)
190 (define (spaces? x) (#/^\s+$/ x))
191 (define (cat s) (and (= (string-length s) 1)
192 (char-general-category (string-ref s 0))))
193 (define (Po? s) (eq? (cat s) 'Po))
194 (define (Pe? s) (eq? (cat s) 'Pe))
195 (let loop ([words (string->words text)]
196 [r '()])
197 (match words
198 [() (reverse r)]
199 [((? spaces? x)) (reverse r)]
200 [(x) (loop '() (cons x r))]
201 [((? spaces? x) . rest)
202 (match r
203 [('s . _) (loop rest r)] ;merge consecutive 's's
204 [_ (loop rest (cons 's r))])]
205 [(x y . rest) (if (or (Po? y) (Pe? y))
206 (loop (cons (string-append x y) rest) r)
207 (loop (cons y rest) (cons x r)))]
208 )))