Debian Codegraph @

lib/tools/docprep

raw
1 ;;;
2 ;;; tools/docprep - prepare document
3 ;;;
4 ;;; Copyright (c) 2001-2022 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Permission to use, copy, modify, distribute this software and
7 ;;; accompanying documentation for any purpose is hereby granted,
8 ;;; provided that existing copyright notices are retained in all
9 ;;; copies and that this notice is included verbatim in all
10 ;;; distributions.
11 ;;; This software is provided as is, without express or implied
12 ;;; warranty. In no circumstances the author(s) shall be liable
13 ;;; for any damages arising out of the use of this software.
14 ;;;
15
16 (use file.util)
17 (use gauche.charconv)
18 (use gauche.parseopt)
19 (use srfi.13)
20 (use util.levenshtein)
21
22 (define *version*
23 (string-trim-both (file->string (or (find (cut sys-access <> R_OK)
24 '("../VERSION"
25 "./VERSION"))
26 (error "No VERSION file?")))))
27 (define *node-table* '())
28 (define *header-table* '())
29
30 (define lang (make-parameter 'en))
31 (define srcdir (make-parameter ""))
32
33 ;; TRANSIENT: We use :converion-illegal-output 'replace to read utf8 files,
34 ;; so that docprep won't barf if Gauche is compiled with non-utf8 encoding.
35 ;; (The resulting info file may contain replace chars, but that's unavoidable).
36 ;; After 0.9.13 release and we drop other encoding support, remove
37 ;; :converion-illegal-output option, for it is no longer needed.
38
39 (define (scan-nodes)
40 (let ([current-node #f]
41 [current-header #f])
42 (port-for-each
43 (lambda (line)
44 (rxmatch-case line
45 [#/^@node\s+([^,]+)/ (#f node)
46 (set! current-node (string-trim-right node))]
47 [#/^@(chapter|(sub)*section|appendix\w*)\s+(.*)/ (#f #f #f header)
48 (set! current-header (string-trim-right header))]
49 [#/^@c NODE\s+([^,]*)(,(.*))?/ (#f jnode #f jheader)
50 (let* ([jn (string-trim-right jnode)]
51 [jh (if jheader (string-trim-both jheader) jn)])
52 (push! *node-table* (cons current-node jn))
53 (push! *header-table* (cons current-header jh)))]
54 [#/^@include\s+(\S+)/ (#f file)
55 (with-input-from-file (find-included-file file)
56 (cut scan-nodes)
57 :encoding 'utf8
58 :conversion-illegal-output 'replace)]
59 ))
60 read-line)))
61
62 (define (filter pattern-in pattern-out)
63 ;; This regexp picks potential typo of extract directives.
64 (define suspicious
65 #/^@c ([A-Za-z]{2,7}:?)(?![,-])\s*(.*)/)
66 ;; Loop for 'active' lines.
67 ;; MODTAG tracks whether the previous line was "[SRFI-nn]", "@c MOD ...", or
68 ;; something else. It is to detect the error when those two are reversed.
69 (define (in line modtag)
70 (rxmatch-case line
71 [test eof-object?]
72 [pattern-in () (in (read-line) modtag)]
73 [pattern-out () (out (read-line) modtag)]
74 [#/^@include\s+(\S+)/ (#f file)
75 (finish-srfi-line modtag)
76 (with-input-from-file (find-included-file file)
77 (cut filter pattern-in pattern-out)
78 :encoding 'utf8
79 :conversion-illegal-output 'replace)
80 (in (read-line) #f)]
81 ;; Extract-time include. @include will be tracked by
82 ;; texinfo-multiple-files-update and the includee must have @node
83 ;; at the toplevel, so this can be used to include files without
84 ;; the restriction.
85 [#/^@c xinclude\s+(\S+)/ (#f file)
86 (finish-srfi-line modtag)
87 (with-input-from-file (find-included-file file)
88 (cut filter pattern-in pattern-out)
89 :encoding 'utf8
90 :conversion-illegal-output 'replace)
91 (in (read-line) #f)]
92 [#/^@c COMMON$/ () (finish-srfi-line modtag) (in (read-line) #f)]
93 [#/^@c MOD\s+(\S+)$/ (#f module)
94 (display #"@{@t{~|module|}@}\n")
95 (in (read-line) 'mod)]
96 [#/^@c DEPRECATED$/ ()
97 (display "@b{Deprecated. }\n")
98 (in (read-line) modtag)]
99 [#/^\[SRFI/ (#f)
100 (when (eq? modtag 'mod)
101 (warn #"SRFI tag follows MOD directive: ~|line|\n"))
102 (display line)
103 (in (read-line) 'srfi)]
104 [suspicious (#f word rest)
105 (check-typo word rest line)
106 (finish-srfi-line modtag)
107 (in (read-line) #f)]
108 [test (^_ (eq? (lang) 'en))
109 (finish-srfi-line modtag)
110 (display (regexp-replace-all #/@VERSION@/ line *version*))
111 (newline) (in (read-line) #f)]
112 [#/^@node\s+(.*)$/ (#f nodedesc)
113 (process-node nodedesc) (in (read-line) #f)]
114 [#/^@(chapter|(sub)*section|appendix\w*)\s+(.*)/ (#f cmd #f header)
115 (process-header cmd header) (in (read-line) #f)]
116 [#/^\* ([^:]+)::(.*)?/ (#f node desc)
117 (process-menu node #f desc) (in (read-line) #f)]
118 [#/^\* ([^:]+):\s+([^\)]+\))\.(.*)?/ (#f tag node desc)
119 (process-menu node tag desc) (in (read-line) #f)]
120 [else (finish-srfi-line modtag)
121 (display
122 ($ regexp-replace-all* line
123 #/@VERSION@/ *version*
124 #/(@(?:px|x)?ref)\{([^\}]+)\}/ process-ref))
125 (newline)
126 (in (read-line) #f)]))
127
128 ;; When we see '[SRFI-nn]' line, we don't emit newline imediately,
129 ;; for we don't want space if it is immediately followed by '{module}'.
130 ;; This one emits the delayed newline when the next line isn't the module.
131 (define (finish-srfi-line modtag)
132 (when (eq? modtag 'srfi) (newline)))
133
134
135 ;; Loop for 'inactive' lines.
136 (define (out line modtag)
137 (rxmatch-case line
138 [test eof-object?]
139 [pattern-in () (in (read-line) modtag)]
140 [#/^@c COMMON$/ () (in (read-line) modtag)]
141 [suspicious (#f word rest)
142 (check-typo word rest line)
143 (out (read-line) modtag)]
144 [else (out (read-line) modtag)]))
145
146 (in (read-line) #f))
147
148 ;; Detect potential typo of extract directives.
149 ;; It is a bit complicated, since we need to allow general comment after @c.
150 ;; WORD is a 2-7 letter alphabetic word, and REST is what follows.
151 (define (check-typo word rest line)
152 (when (and (string-any char-upper-case? word)
153 (or (and (equal? rest "")
154 ;; directives that doesn't take argument
155 (let1 ds (re-distances word '("EN" "JP" "COMMON"))
156 (<= 1 (apply min ds) 2)))
157 ;; directives that does take argument
158 (and (>= (string-length word) 3)
159 (let1 ds (re-distances word '("NODE" "MOD" "NOTE"
160 "TODO:" "NB:"))
161 (<= 1 (apply min ds) 2)))))
162 (errorf "Unrecognized extract directive ~s at or near ~s:~d: ~a\n"
163 word (port-name (current-input-port))
164 (port-current-line (current-input-port))
165 line)))
166
167 ;; We search relative to the current directory first, then
168 ;; relative to $srcdir; for FILE may be the generated one.
169 (define (find-included-file file)
170 (if (file-exists? file)
171 file
172 (build-path (srcdir) file)))
173
174 (define (lookup name table)
175 (cond [(assoc (string-trim-both name) table) => cdr]
176 [else name]))
177
178 (define (process-node nodedesc)
179 (display "@node ")
180 (display (string-join
181 (map (cut lookup <> *node-table*) (string-split nodedesc #\,))
182 ", "))
183 (newline))
184
185 (define (process-header cmd header)
186 (format #t "@~a ~a\n" cmd (lookup header *header-table*)))
187
188 (define (process-menu node tag desc)
189 (if tag
190 (format #t "* ~a: ~a. ~a\n" tag (lookup node *node-table*)
191 (string-trim-both (or desc "")))
192 (format #t "* ~a:: ~a\n" (lookup node *node-table*)
193 (string-trim-both (or desc "")))))
194
195 (define (process-ref match)
196 (let ([cmd (rxmatch-substring match 1)]
197 [node (rxmatch-substring match 2)])
198 (format #f "~a{~a}" cmd (lookup node *node-table*))))
199
200 ;; TRANSIENT:
201 ;; This script is used by 0.9.15 to prepare tarball of sources after 0.9.15,
202 ;; so it needs to work without the newly added parseopt's help string feature.
203 ;; We just comment out the help string thingy for now. Revive them after
204 ;; the new release.
205 (define (usage)
206 (print "Usage: gosh tools/docprep [options] infile")
207 ;;(print "Options:")
208 ;;(print (option-parser-help-string))
209 (exit 1))
210
211 ;; TODO: Switch to use 'ja' (language code) instead of 'jp' (country code)
212 (define (main args)
213 (define language 'en)
214 (let-args (cdr args)
215 ([outfile "o=s"
216 ;; TRANSIENT: See above comment
217 ;; "o=s{FILE}"
218 ;; ? "Writes output to {FILE} instead of stdout."
219 ]
220 [#f "en" => (^[] (set! language 'en))
221 ;; TRANSIENT: See above comment
222 ;; ? "Set output language to English"
223 ]
224 [#f "jp" => (^[] (set! language 'jp))
225 ;; TRANSIENT: See above comment
226 ;; ? "Set output language to Japanese"
227 ]
228 [else _ (usage)]
229 . a)
230
231 (define (do-it)
232 (case language
233 [(en) (filter #/^@c EN$/ #/^@c JP$/)]
234 [(jp) (filter #/^@c JP$/ #/^@c EN$/)]))
235
236 (unless (= (length a) 1) (usage))
237
238 (parameterize ([srcdir (sys-normalize-pathname (sys-dirname (car a))
239 :absolute #t)]
240 [lang language])
241 (when (eq? language 'jp)
242 (with-input-from-file (car a) scan-nodes
243 :encoding 'utf8
244 :conversion-illegal-output 'replace))
245 (with-input-from-file (car a)
246 (^[]
247 (if outfile
248 (with-output-to-file outfile do-it :encoding 'utf8)
249 (let1 out (wrap-with-output-conversion
250 (current-output-port) 'utf8)
251 (with-output-to-port out do-it)
252 (close-output-port out))))
253 :encoding 'utf8
254 :conversion-illegal-output 'replace))
255 0))
256
257 ;; Local variables:
258 ;; mode: Scheme
259 ;; end: