-
Notifications
You must be signed in to change notification settings - Fork 14
/
printer.lisp
361 lines (304 loc) · 11.8 KB
/
printer.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
(in-package #:3bmd)
(defparameter *references* nil)
(defparameter *always-title* nil)
(defparameter *padding* 2)
(defmacro padded ((n s &optional (next 0)) &body body)
(alexandria:once-only (n s next)
`(progn
(loop for ,n from ,n above *padding* do (format ,s "~%"))
(setf *padding* ,n)
(prog1
(progn ,@body)
(setf *padding* ,next)))))
(defun print-label-to-string (label)
(with-output-to-string (s)
(loop with spaces = (coerce '(#\space #\tab #\newline #\return #\linefeed)
'string)
for was-space = nil then space
for c across (with-output-to-string (s)
(loop for i in (alexandria:ensure-list label)
do (print-element i s)))
for space = (find c spaces)
when space
do (unless was-space (write-char #\space s))
else do (write-char c s))))
(defun lookup-reference (ref)
#++(format t "lookup ref ~s -> ~s~%" (print-label-to-string ref)
(gethash (print-label-to-string ref) *references*))
(if *references*
(gethash (print-label-to-string ref) *references*)))
(defun print-escaped (string stream)
(loop for c across string
when (eql c #\&) do (write-string "&" stream)
else when (eql c #\<) do (write-string "<" stream)
else when (eql c #\>) do (write-string ">" stream)
else when (eql c #\") do (write-string """ stream)
;; todo: 'obfuscate' email addresses (when *obfuscate* ...)
else do (write-char c stream)))
(defun escape-string (string)
(when string
(with-output-to-string (s)
(print-escaped string s))))
(defun print-pre-escaped (string stream)
(loop for c across string
when (eql c #\<) do (write-string "<" stream)
else when (eql c #\>) do (write-string ">" stream)
else do (write-char c stream)))
(defun escape-pre-string (string)
(when string
(with-output-to-string (s)
(print-pre-escaped string s))))
(defvar *allowed-id-chars* "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890@$&'()*+.,;:?!=-_/"
"A string of characters that are allowed to appear within an element ID string.")
(defun html-content-id (html)
(with-output-to-string (out)
(loop with state = :text
for c across html
do (case state
(:text
(cond ((eql c #\<)
(setf state :tag))
((eql c #\ )
(write-char #\_ out))
((find c *allowed-id-chars*)
(write-char (char-downcase c) out))))
(:tag
(when (eql c #\>)
(setf state :text)))))))
(defvar *generate-header-ids* NIL
"Whether ID attributes should be generated for header elements.")
;; todo: minimize extra newlines...
(defmethod print-tagged-element ((tag (eql :heading)) stream rest)
(let* ((contents (with-output-to-string (stream)
(dolist (a (getf rest :contents))
(print-element a stream))))
(id (when *generate-header-ids* (html-content-id contents))))
(padded (2 stream)
(format stream "<h~d~@[ id=~s~]>" (getf rest :level) id)
(write-string contents stream)
(format stream "</h~d>" (getf rest :level)))))
(defmethod print-tagged-element ((tag (eql :paragraph)) stream rest)
(padded (2 stream)
(format stream "<p>")
(dolist (a rest)
(print-element a stream))
(format stream "</p>")))
(defmethod print-tagged-element ((tag (eql :block-quote)) stream rest)
(padded (2 stream)
(format stream "<blockquote>~%")
(dolist (a rest)
(print-element a stream))
(format stream "~&</blockquote>")))
(defmethod print-tagged-element ((tag (eql :plain)) stream rest)
(padded (1 stream)
(dolist (a rest)
(print-element a stream))))
(defmethod print-tagged-element ((tag (eql :emph)) stream rest)
(format stream "<em>")
(dolist (a rest)
(print-element a stream))
(format stream "</em>"))
(defmethod print-tagged-element ((tag (eql :strong)) stream rest)
(format stream "<strong>")
(dolist (a rest)
(print-element a stream))
(format stream "</strong>"))
(defmethod print-tagged-element ((tag (eql :link)) stream rest)
(format stream "<a href=\"~a\">~a</a>" (car rest) (car rest)))
(defun encode-email (text)
(with-output-to-string (s)
(loop for i upfrom 0
for char across text
do (cond
((= 0 (mod i 3)) (write-char char s))
;; fixme: make this portable to non-unicode/ascii lisps?
((= 1 (mod i 3)) (format s "&#x~x;" (char-code char)))
(t (format s "&#~d;" (char-code char)))))))
(defmethod print-tagged-element ((tag (eql :mailto)) stream rest)
(format stream "<a href=\"~a\">~a</a>" (encode-email (car rest))
(encode-email (car rest))))
(defmethod print-tagged-element ((tag (eql :explicit-link)) stream rest)
(format stream "<a href=\"~a\" ~@[title=\"~a\"~]>"
(getf rest :source)
(escape-string
(or (getf rest :title) (if *always-title* "" nil))))
(dolist (a (getf rest :label))
(print-element a stream))
(format stream "</a>"))
(defmethod print-tagged-element ((tag (eql :reference-link)) stream rest)
(let* ((label (getf rest :label))
(def (or (getf rest :definition) label))
(ref (lookup-reference def)))
(cond
(ref
(format stream "<a href=\"~a\" ~@[title=\"~a\"~]>" (first ref)
(escape-string (or (second ref) (if *always-title* "" nil))))
(dolist (a label)
(print-element a stream))
(format stream "</a>"))
(t
(warn "Unresolvable reference link ~S~%" rest)
(format stream "[")
(dolist (a label)
(print-element a stream))
(format stream "]")
(when (getf rest :definition)
(format stream "[")
(dolist (a (getf rest :definition))
(print-element a stream))
(format stream "]"))
(format stream "~@[~a~]" (getf rest :tail))))))
(defmethod print-tagged-element ((tag (eql :image)) stream rest)
(setf rest (cdr (first rest)))
(format stream "<img src=\"~a\" ~@[alt=\"~a\"~] ~@[title=\"~a\"~]/>"
(getf rest :source)
(with-output-to-string (s)
(dolist (a (getf rest :label))
(print-element a s)))
(escape-string
(or (getf rest :title) (if *always-title* "" nil)))))
(defmethod print-tagged-element ((tag (eql :counted-list)) stream rest)
(padded (2 stream)
(format stream "<ol>"))
(dolist (a rest)
(print-element a stream))
(padded (1 stream)
(format stream "~&</ol>")))
(defmethod print-tagged-element ((tag (eql :bullet-list)) stream rest)
(padded (2 stream)
(format stream "<ul>"))
(dolist (a rest)
(print-element a stream))
(padded (1 stream)
(format stream "</ul>")))
(defmethod print-tagged-element ((tag (eql :list-item)) stream rest)
(padded (1 stream 2)
(format stream "<li>"))
(dolist (a rest)
(print-element a stream))
(format stream "</li>")
(setf *padding* 0))
(defmethod print-tagged-element ((tag (eql :line-break)) stream rest)
(format stream "<br/>~%"))
(defmethod print-tagged-element ((tag (eql :horizontal-rule)) stream rest)
(padded (2 stream)
(format stream "<hr/>")))
(defmethod print-tagged-element ((tag (eql :html)) stream rest)
(padded (2 stream)
(format stream "~{~a~}" rest)))
(defmethod print-tagged-element ((tag (eql :raw-html)) stream rest)
(format stream "~{~a~}" rest))
(defmethod print-tagged-element ((tag (eql :entity)) stream rest)
(format stream "~{~a~}" rest))
(defmethod print-tagged-element ((tag (eql :verbatim)) stream rest)
(padded (2 stream)
(format stream "<pre><code>")
(dolist (a rest)
(print-element a stream))
(format stream "</code></pre>")))
;;; track whether we are in a code block, so we can avoid smart-quote
;;; junk if it was enabled in the parser
;;;
;;; possibly should just check the *smart-quotes* var from 3bmd-grammar
;;; and bind that to nil inside code blocks?
;;; (or make parser smart enough to not match inside code blocks)
(defparameter *in-code* nil)
(defmethod print-tagged-element ((tag (eql :code)) stream rest)
(format stream "<code>")
(let ((*in-code* t))
(dolist (a rest)
(print-element a stream)))
(format stream "</code>"))
(defmethod print-tagged-element ((tag (eql :single-quoted)) stream rest)
(if *in-code*
(format stream "'")
(format stream "‘"))
(dolist (a rest)
(print-element a stream))
(if *in-code*
(format stream "'")
(format stream "’")))
(defmethod print-tagged-element ((tag (eql :double-quoted)) stream rest)
(if *in-code*
(format stream "\"")
(format stream "“"))
(dolist (a rest)
(print-element a stream))
(if *in-code*
(format stream "\"")
(format stream "”")))
(defmacro define-smart-quote-entity (name replacement)
`(defmethod print-tagged-element ((tag (eql ,name)) stream rest)
(if *in-code*
(format stream "~{~a~}" rest)
(format stream ,replacement))))
(define-smart-quote-entity :em-dash "—")
(define-smart-quote-entity :en-dash "–")
(define-smart-quote-entity :left-right-single-arrow "↔")
(define-smart-quote-entity :left-single-arrow "←")
(define-smart-quote-entity :right-single-arrow "→")
(define-smart-quote-entity :left-right-double-arrow "⇔")
(define-smart-quote-entity :left-double-arrow "⇐")
(define-smart-quote-entity :right-double-arrow "⇒")
(defmethod print-tagged-element ((tag (eql :ellipsis)) stream rest)
(if *in-code*
(format stream "~{~a~}" rest)
(format stream "…")))
(defmethod print-tagged-element ((tag (eql :reference)) stream rest)
)
(defmethod print-element ((elem null) stream)
"")
(defmethod print-element ((elem (eql :apostrophe)) stream)
(if *in-code*
(format stream "'")
(format stream "'")))
(defmethod print-element ((elem string) stream)
#++(format stream "~a" elem)
(print-escaped elem stream))
(defmethod print-element ((elem cons) stream)
(if (symbolp (car elem))
(print-tagged-element (car elem) stream (cdr elem))
(error "unknown cons? ~s" elem)))
(defun extract-refs (doc)
(alexandria:alist-hash-table
(loop for i in doc
when (and (consp i) (eq (car i) :reference))
collect (list (print-label-to-string (getf (cdr i) :label))
(getf (cdr i) :source)
(getf (cdr i) :title)))
:test #'equalp))
(defun expand-tabs (doc &key add-newlines)
(with-output-to-string (s)
(let ((pos 0))
(flet ((out (c)
(incf pos)
(write-char c s)))
(loop
for i across doc
do (case i
((#\newline)
(setf pos 0)
(write-char i s))
((#\tab)
(loop repeat (- 4 (mod pos 4)) do (out #\space)))
(t (out i))))))
(when add-newlines
(format s "~%~%"))))
(defmethod print-doc-to-stream-using-format (doc stream (format (eql :html)))
(let ((*references* (extract-refs doc))
;; Protect the global value.
(*padding* *padding*))
(loop for i in doc
do (print-element i stream))
(format stream "~&")))
(defun print-doc-to-stream (doc stream &key (format :html))
(print-doc-to-stream-using-format doc stream format))
(defun parse-and-print-to-stream (file stream &key (format :html))
(let* ((input (expand-tabs (alexandria:read-file-into-string file)
:add-newlines t))
(doc (3bmd-grammar::parse-doc input)))
(print-doc-to-stream doc stream :format format)))
(defun parse-string-and-print-to-stream (string stream &key (format :html))
(let* ((input (expand-tabs string :add-newlines t)))
(print-doc-to-stream (3bmd-grammar::parse-doc input) stream
:format format)))