-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathweave.lisp
445 lines (379 loc) · 17.8 KB
/
weave.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
; Copyright (c) 2022 Justin Meiners
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, version 2.
;
; This program is distributed in the hope that it will be useful, but
; WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
; General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>.
(in-package :srcweave)
; Weave: Make readable documentation from .lit file.
; We have chosen HTML for it's support for many media types and readability
; on many devices.
; DESIGN
; One-to-one correspondence between .lit and .html files.
; The body of an .html file should look the same regardless of whether the .lit is in another context (like a book).
(defstruct weaver
(use-table (make-hash-table) :type hash-table)
(initial-def-table (make-hash-table) :type hash-table)
(anchors (make-hash-table) :type hash-table)
(toc nil :type list)
(title nil :type (or string null))
(filename nil :type (or string null))
(code-type-table (make-hash-table :test #'equal) :type hash-table)
(used-extensions nil :type list)
(used-math nil :type boolean))
(defun make-weaver-default (file-defs)
(let ((all-defs (alexandria-2:mappend #'cdr file-defs)))
(make-weaver
:use-table (make-block-use-table (remove-if-not
(lambda (def) (eq (textblockdef-kind def) :CODE))
all-defs))
:initial-def-table (make-title-to-first-def-table all-defs)
:anchors (make-anchor-table file-defs)
:toc (make-textheading-toc file-defs)
:code-type-table (codetable-create))))
(defun html-file-anchor (block-id &optional file)
(format nil "~a#~a" (or file "") block-id))
(defun textblockdef-weave-file (def)
".lit -> .html"
(filename-replace-extension (textblockdef-file def) ".html"))
(defun filename-if-different (filename current)
"don't append the filename at the beginning of the anchor if it is on the same page as the content."
(if (equal filename current) nil filename))
(defun weave-anchor-for-def (def weaver)
(html-file-anchor (gethash def (weaver-anchors weaver))
(filename-if-different (textblockdef-weave-file def)
(weaver-filename weaver))))
(defun textheading-weave-file (heading)
".lit -> .html"
(filename-replace-extension (textheading-file heading) ".html"))
(defun weave-anchor-for-heading (heading weaver)
(html-file-anchor (gethash heading (weaver-anchors weaver))
(filename-if-different (textheading-weave-file heading)
(weaver-filename weaver))))
(defun format-include (title code-style)
(if code-style (format nil "@{~a}" title) title))
(defun weave-include-unknown (sym error code-style weaver)
(format t "<em class=\"block-link nocode\" title=\"~a\">" error)
(write-string (format-include (symbol-name sym) code-style))
(write-string "</em>"))
(defun weave-include-exists (other code-style weaver)
(format t "<em class=\"block-link nocode\"~a>"
; create hover text which tells the user which file the block is in.
(if (equal (weaver-filename weaver) (textblockdef-weave-file other))
""
(format nil " title=\"~a\"" (textblockdef-file other))))
(format t "<a href=\"~a\">" (weave-anchor-for-def other weaver))
(write-string (format-include (textblockdef-title other) code-style))
(write-string "</a>")
(write-string "</em>"))
(defun weave-include (sym code-style weaver)
; find the first block that uses that title
(let ((other
(gethash sym
(weaver-initial-def-table weaver))))
(cond ((not other)
(weave-include-unknown sym "undefined block" code-style weaver))
((not (textblockdef-weavable other))
(weave-include-unknown sym "block defined, but not weavable." code-style weaver))
(t (weave-include-exists other code-style weaver)))))
(defparameter *html-replace-list*
'((#\& . "&") (#\< . "<") (#\> . ">")))
(defun escape-html (string)
"replace invalid HTML characters with their escaped versions."
(reduce (lambda (string replace-pair)
(ppcre:regex-replace-all (car replace-pair) string (cdr replace-pair)))
*html-replace-list*
:initial-value string))
(defun weave-code-line (weaver line def)
(loop for expr in line do
(cond ((stringp expr) (write-string (escape-html expr)))
((commandp expr)
(case (first expr)
(:INCLUDE (weave-include
(second expr)
t
weaver))
(otherwise
; this shouldn't happen.
; This relates to "block has not been fully resolved" in the tangle.
(error "unknown code command ~S" expr))))
(t (error "internal error. unknown structure ~S" expr)))))
(defun usage-tooltip (def other)
"generate title attribute contents"
(if (equal (textblockdef-file def)
(textblockdef-file other))
(textblockdef-title other)
(format nil "~a. ~a"
(textblockdef-title other)
(textblockdef-file other))))
(defun weave-uses (weaver def)
(let ((uses (gethash (textblockdef-title-sym def) (weaver-use-table weaver))))
(when (not (null uses))
(write-string "<p class=\"block-usages\"><small>Used by ")
(mapnil-indexed (lambda (other i)
(if (textblockdef-weavable other)
(format t "<a href=\"~a\" title=\"~a\">~a</a> "
(weave-anchor-for-def other weaver)
(usage-tooltip def other)
(+ i 1))
(format t "<span title=\"~a\">~a</span> "
(usage-tooltip def other)
(+ i 1)))) uses)
(write-string "</small></p>"))))
(defun language-to-class (language)
(if (equal language "text") "" language))
(defun operation-string (op)
(case op
(:DEFINE nil)
(:APPEND "+=")
(:REDEFINE ":=")
(otherwise (string op))))
(defun weave-operation (weaver def)
(alexandria-2:when-let ((symbol (operation-string (textblockdef-operation def))))
(format t " <a href=\"~a\">~a</a>"
(weave-anchor-for-def
(gethash (textblockdef-title-sym def) (weaver-initial-def-table weaver))
weaver)
symbol)))
(defun weave-codedef (weaver def)
; record extensions
(when (textblockdef-is-file def)
(alexandria-2:when-let ((extension (pathname-type (textblockdef-title def))))
(push extension (weaver-used-extensions weaver))))
(write-line "<div class=\"code-block\">")
; write header
(let* ((title (textblockdef-title def)))
(write-line "<span class=\"block-header\">")
(format t "<strong class=\"block-title\"><em><a id=\"~a\" href=\"~a\">~a</a></em></strong>"
(gethash def (weaver-anchors weaver))
(weave-anchor-for-def def weaver)
title)
(weave-operation weaver def)
(write-line "</span>"))
; write body
(let* ((block (textblockdef-block def))
(lines (textblock-lines block)))
(format t "<pre class=\"prettyprint\"><code class=\"~a\">"
(language-to-class (textblockdef-language def)))
(alexandria-2:if-let ((left (position-if-not #'null lines))
(right (position-if-not #'null lines :from-end t)))
; trim blank lines from start and end
(loop for i from left to right do
(weave-code-line weaver (aref lines i) def)
(write-line ""))
(warn "weaving empty block ~s" (textblockdef-title def)))
(write-line "</code></pre>"))
(when (eq :DEFINE (textblockdef-operation def))
(weave-uses weaver def))
(write-line "</div>"))
(defun weave-prose-line (weaver line def)
(loop for expr in line do
(cond ((stringp expr) (write-string expr))
((commandp expr)
(case (first expr)
(:INCLUDE (weave-include
(second expr)
nil
weaver))
(:TITLE
(setf (weaver-title weaver)
(second expr)))
(:HEADING
; Also can act as a title
(when (null (weaver-title weaver))
(setf (weaver-title weaver)
(textheading-title (second expr))))
(format t "<h~a id=\"~a\">~a</h~a>~%"
(textheading-level (second expr))
(gethash (second expr) (weaver-anchors weaver))
(textheading-title (second expr))
(textheading-level (second expr))))
(:CODE_TYPE
(let* ((args (split-whitespace (second expr)))
(language (first args))
(extension (subseq (second args) 1)))
(setf (gethash extension (weaver-code-type-table weaver)) language)
(push extension (weaver-used-extensions weaver))))
(:MATHBLOCK
; Put <code> tags in block so it displays tex nicely without JS.
(setf (weaver-used-math weaver) t)
(write-string "<div class=\"math-block\"><code>")
(when (not (equal (second expr) "displaymath"))
(format t "\\begin{~a}" (second expr)))
(write-separated-list (third expr) #\newline *standard-output*)
(when (not (equal (second expr) "displaymath"))
(format t "\\end{~a}" (second expr)))
(write-string "</code></div>"))
(:MATH
; Use backticks to prevent markdown from formatting tex,
; for example treating _ as emphasis.
(setf (weaver-used-math weaver) t)
(format t "<span class=\"math\">`~a`</span>"
(second expr)))
(:TOC
(weave-toc weaver))
; These commands are from Zach's Literate.
; We treat them as warnings instead of errors to make migration easier.
; It's possible they may be useful for us in the future.
((:COMMENT_TYPE :ADD_CSS :OVERWRITE_CSS :COLORSCHEME :ERROR_FORMAT)
(warn "deprecated Literate prose command ~s. ignored." (first expr)))
(otherwise (error 'user-error
:format-control "unknown prose command ~S"
:format-arguments (first expr)))))
(t (error "unknown structure ~s" expr)))))
(defun weave-prosedef (weaver def)
(let ((block (textblockdef-block def)))
(setf (weaver-filename weaver) (textblockdef-weave-file def))
(loop for line across (textblock-lines block) do
(weave-prose-line weaver line def)
(write-line ""))))
(defun weave-blocks (weaver source-defs)
(dolist (def source-defs)
(when (textblockdef-weavable def)
(if (eq (textblockdef-kind def) :CODE)
(weave-codedef weaver def)
(weave-prosedef weaver def)))))
(defparameter *attribution* "Generated by srcweave. https://github.com/justinmeiners/srcweave")
(defun weave-html (weaver stream source-defs)
(format stream "<!-- ~a -->~%" *attribution*)
(finish-output stream)
; Run markdown on the entire document so named links work.
(if (stringp *markdown-command*)
(let ((md (uiop:launch-program
(split-whitespace *markdown-command*)
:input :stream
:output stream
:error-output *error-output*)))
(let ((*standard-output* (uiop:process-info-input md)))
(weave-blocks weaver source-defs))
(uiop:close-streams md)
(uiop:wait-process md))
(let ((*standard-output* stream))
(weave-blocks weaver source-defs))))
(defparameter *env-key-types* "LIT_TYPES")
(defparameter *env-key-title* "LIT_TITLE")
(defparameter *env-key-math* "LIT_MATH")
(defun weave-path (weaver source-defs output-path)
(with-open-file (output-stream output-path
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(if (not (stringp *styler-command*))
(weave-html weaver output-stream source-defs)
; we first write to data in RAM to get formatting info for environment variables.
(let ((text (with-output-to-string (s)
(weave-html weaver s source-defs))))
; set environmental variables based on weave results.
(setf (uiop:getenv *env-key-types*)
(extensions-to-type-string (weaver-code-type-table weaver)
(weaver-used-extensions weaver)))
(setf (uiop:getenv *env-key-title*) (or (weaver-title weaver) ""))
(setf (uiop:getenv *env-key-math*) (if (weaver-used-math weaver) "1" ""))
; run styler on weave results.
(uiop:run-program (split-whitespace *styler-command*)
:output output-stream
:error-output t
:input (make-string-input-stream text))))))
(defun weave-build-pathname (file-path base)
(uiop:merge-pathnames*
(make-pathname
:name (pathname-name (uiop:ensure-pathname file-path :want-file t))
:type (if (stringp *markdown-command*) "html" "md"))
base))
(defun weave (file-defs output-dir)
(let* ((output-dir (uiop:ensure-directory-pathname output-dir))
(weaver (make-weaver-default file-defs)))
(map nil (lambda (path-defs-pair)
(let* ((input-path (car path-defs-pair))
(output-path (weave-build-pathname input-path output-dir))
(defs (cdr path-defs-pair)))
(progn
(format t "writing doc: ~a~%" output-path)
(ensure-directories-exist output-path)
(weave-path
weaver
defs
output-path))))
file-defs)))
(defun make-title-to-first-def-table (defs)
"maps titles to their first textblockdef"
(let ((table (make-hash-table)))
(dolist (def defs)
(when (not (gethash (textblockdef-title-sym def) table))
(setf (gethash (textblockdef-title-sym def) table)
def)))
table))
(defun make-block-use-table (defs)
"maps titles to the definition ids that use them"
(let ((table (make-hash-table)))
(dolist (def defs)
(dolist (dependency (textblock-references (textblockdef-block def)))
(push def
(gethash dependency table))))
(maphash (lambda (key val)
(setf (gethash key table) (reverse val))) table)
table))
(defparameter *fragment-invalid-pattern*
(ppcre:create-scanner '(:INVERTED-CHAR-CLASS
#\- #\.
(:RANGE #\a #\z)
(:RANGE #\0 #\9))))
(defun string-to-fragment (string)
(ppcre:regex-replace-all '(:GREEDY-REPETITION 1 nil #\-)
(ppcre:regex-replace-all *fragment-invalid-pattern*
(string-downcase (if (is-file-title string) (subseq string 1) string))
"-")
"-"))
(defun textblockdef-fragment (def)
(format nil ":~a" (string-to-fragment (textblockdef-title def))))
(defun textheading-fragment (def)
(format nil "~a" (string-to-fragment (textheading-title def))))
(defun disambiguate-by-adding-dot-suffix (str i)
(format nil "~a_~a" str (+ i 1)))
(defun make-anchor-table (file-defs)
"Find HTML anchors for a list of defs that will appear on the same page."
(let ((table (make-hash-table)))
(dolist (file-pair file-defs)
; only anchors which appear on the same page need to be disambiguated
(disambiguate-ids-into (cdr file-pair)
table
:key #'textblockdef-fragment
:combine #'disambiguate-by-adding-dot-suffix)
(disambiguate-ids-into (all-headings-from-defs (cdr file-pair))
table
:key #'textheading-fragment
:combine #'disambiguate-by-adding-dot-suffix))
table))
(defun weave-toc-section (section weaver)
(format t "<li><a href=\"~a\">~a</a></li>~%"
(weave-anchor-for-heading section weaver)
(textheading-title section)))
(defun weave-toc-chapter (chapter sections show-chapters weaver)
(when show-chapters
(format t "<li><a href=\"~a\">~a</a>~%"
(weave-anchor-for-heading chapter weaver)
(textheading-title chapter)))
(format t "<ol>~%")
(dolist (section sections)
(weave-toc-section (car section) weaver))
(format t "</ol>~%")
(when show-chapters
(format t "</li>~%")))
(defun weave-toc (weaver)
(let ((show-chapters (> (length (weaver-toc weaver)) 1)))
(when show-chapters
(format t "<ol>~%"))
(dolist (chapter (weaver-toc weaver))
(weave-toc-chapter (car chapter)
(cdr chapter)
show-chapters
weaver))
(when show-chapters
(format t "</ol>"))))