-
Notifications
You must be signed in to change notification settings - Fork 14
/
markdown-printer.lisp
314 lines (252 loc) · 10.4 KB
/
markdown-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
(in-package #:3bmd)
(defvar *md-prefix*)
(defvar *md-list-item*)
(defvar *md-in-block*)
(defvar *md-block-seen-p*)
(defmacro with-md-indent ((indent) &body body)
`(let ((*md-prefix* (concatenate 'string *md-prefix*
(make-string ,indent
:initial-element #\space))))
,@body))
(defmacro with-md-prefix ((prefix) &body body)
`(let ((*md-prefix* (concatenate 'string *md-prefix* ,prefix)))
,@body))
(defun md-indent (stream)
(write-string *md-prefix* stream)
(setq *md-in-block* :right-after-indent))
(defun ensure-block (stream)
(unless *md-in-block*
(when *md-block-seen-p*
(format stream "~%"))
(setq *md-in-block* t)
(md-indent stream)
(setq *md-block-seen-p* t)))
(defun end-block (stream)
(when *md-in-block*
(format stream "~%")
(setq *md-in-block* nil)))
;;; These are some of the 3BMD-GRAMMAR::SPECIAL-CHARs. The ! character
;;; is not necessary to escape if [ is. Similary, no need to escape >
;;; if < is. Backslash should never be escaped.
(defparameter *block-chars-to-escape* "*_`&[]<#")
(defparameter *inline-chars-to-escape* (remove #\# *block-chars-to-escape*))
(defun print-md-escaped (string stream)
(loop for char across string
do (when (and (not *in-code*)
(find char
(if (eq *md-in-block* :right-after-indent)
*block-chars-to-escape*
*inline-chars-to-escape*)))
;; TODO: The escaping is overeager. For example, there is
;; no need for the escapes in "\\<->" and "\\&KEY " due
;; to how the parser works, but this needs information
;; about what follows STRING in the parse tree.
(write-char #\\ stream))
(when (eq *md-in-block* :right-after-indent)
(setq *md-in-block* t))
(write-char char stream)
(when (char= char #\Newline)
(md-indent stream))))
(defun print-md (string stream)
(loop for char across string
do (write-char char stream)
(when (eq *md-in-block* :right-after-indent)
(setq *md-in-block* t))
(when (char= char #\Newline)
(md-indent stream))))
(defmethod print-md-tagged-element ((tag (eql :heading)) stream rest)
(ensure-block stream)
(loop repeat (getf rest :level) do (write-char #\# stream))
(write-char #\Space stream)
(dolist (a (getf rest :contents)) (print-md-element a stream))
(end-block stream))
(defmethod print-md-tagged-element ((tag (eql :paragraph)) stream rest)
(ensure-block stream)
(dolist (a rest) (print-md-element a stream))
(end-block stream))
(defmethod print-md-tagged-element ((tag (eql :block-quote)) stream rest)
(with-md-prefix ("> ")
(dolist (a rest)
(print-md-element a stream))))
(defmethod print-md-tagged-element ((tag (eql :plain)) stream rest)
(ensure-block stream)
(dolist (a rest) (print-md-element a stream)))
(defmethod print-md-tagged-element ((tag (eql :emph)) stream rest)
(format stream "*")
(dolist (a rest) (print-md-element a stream))
(format stream "*"))
(defmethod print-md-tagged-element ((tag (eql :strong)) stream rest)
(format stream "**")
(dolist (a rest) (print-md-element a stream))
(format stream "**"))
(defmethod print-md-tagged-element ((tag (eql :link)) stream rest)
(format stream "<")
(dolist (a rest) (print-md-element a stream))
(format stream ">"))
(defmethod print-md-tagged-element ((tag (eql :mailto)) stream rest)
(format stream "<")
(dolist (a rest)
(if (and (stringp a)
(alexandria:starts-with-subseq "mailto:" a))
(print-md-element (subseq a (length "mailto:")) stream)
(print-md-element a stream)))
(format stream ">"))
(defmethod print-md-tagged-element ((tag (eql :explicit-link)) stream rest)
(format stream "[")
(dolist (a (getf rest :label)) (print-md-element a stream))
(format stream "](~a~@[ ~s~])" (getf rest :source) (getf rest :title)))
(defmethod print-md-tagged-element ((tag (eql :reference-link)) stream rest)
(format stream "[")
(dolist (a (getf rest :label)) (print-md-element a stream))
(format stream "][")
(dolist (a (getf rest :definition)) (print-md-element a stream))
(format stream "]"))
(defmethod print-md-tagged-element ((tag (eql :image)) stream rest)
(format stream "!")
(dolist (a rest)
(print-md-element a stream)))
(defmethod print-md-tagged-element ((tag (eql :counted-list)) stream rest)
(let ((*md-list-item* 1))
(dolist (a rest) (print-md-element a stream))))
(defmethod print-md-tagged-element ((tag (eql :bullet-list)) stream rest)
(let ((*md-list-item* "-"))
(dolist (a rest) (print-md-element a stream))))
(defmethod print-md-tagged-element ((tag (eql :list-item)) stream rest)
(ensure-block stream)
(cond ((numberp *md-list-item*)
(format stream "~D. " *md-list-item*)
(incf *md-list-item*))
(t
(format stream "~a " *md-list-item*)))
(print-md-element (first rest) stream)
(with-md-indent (4)
(dolist (a (rest rest)) (print-md-element a stream)))
(end-block stream))
(defmethod print-md-tagged-element ((tag (eql :line-break)) stream rest)
(print-md-escaped "
" stream))
(defmethod print-md-tagged-element ((tag (eql :horizontal-rule)) stream rest)
(ensure-block stream)
(print-md-escaped "---" stream)
(end-block stream))
(defmethod print-md-tagged-element ((tag (eql :html)) stream rest)
(format stream "~{~a~}" rest))
(defmethod print-md-tagged-element ((tag (eql :raw-html)) stream rest)
(format stream "~{~a~}" rest))
(defmethod print-md-tagged-element ((tag (eql :entity)) stream rest)
(format stream "~{~a~}" rest))
(defun remove-ending-newline (string)
(if (alexandria:ends-with #\Newline string)
(subseq string 0 (1- (length string)))
string))
(defmethod print-md-tagged-element ((tag (eql :verbatim)) stream rest)
(with-md-indent (4)
(ensure-block stream)
(dolist (a (butlast rest)) (print-md a stream))
(print-md (remove-ending-newline (first (last rest))) stream)
(end-block stream)))
;;; :UNESCAPED-STRING is not produced by the parser, but applications
;;; that manipulate the parse tree might find it convenient.
(defmethod print-md-tagged-element ((tag (eql :unescaped-string)) stream rest)
(dolist (a rest) (print-md a stream)))
(defun max-n-consecutive-char (char string)
(let ((n 0)
(max 0))
(loop for char-2 across string
do (cond ((char= char char-2)
(incf n)
(setq max (max n max)))
(t
(setq n 0))))
max))
(defun max-n-consecutive-backticks (parse-tree)
(let ((n 0))
(labels ((foo (parse-tree)
(if (stringp parse-tree)
(setq n (max n (max-n-consecutive-char #\` parse-tree)))
(map nil #'foo parse-tree))))
(foo parse-tree))
n))
(defmethod print-md-tagged-element ((tag (eql :code)) stream rest)
(let ((n (max-n-consecutive-backticks rest)))
(loop repeat (1+ n) do (write-char #\` stream))
(let ((*in-code* t))
(dolist (a rest) (print-md-element a stream)))
(loop repeat (1+ n) do (write-char #\` stream))))
(defmacro define-smart-quote-md-translation (name replacement)
`(defmethod print-md-tagged-element ((tag (eql ,name)) stream rest)
(write-string ,replacement stream)))
(define-smart-quote-md-translation :single-quoted "'")
(define-smart-quote-md-translation :double-quoted "\"")
(define-smart-quote-md-translation :en-dash "--")
(define-smart-quote-md-translation :em-dash "---")
(define-smart-quote-md-translation :left-right-single-arrow "<->")
(define-smart-quote-md-translation :left-single-arrow "<-")
(define-smart-quote-md-translation :right-single-arrow "->")
(define-smart-quote-md-translation :left-right-double-arrow "<=>")
(define-smart-quote-md-translation :left-double-arrow "<=")
(define-smart-quote-md-translation :right-double-arrow "=>")
(define-smart-quote-md-translation :ellipsis "...")
(defmethod print-md-tagged-element ((tag (eql :reference)) stream rest)
(format stream "~%[")
(dolist (a (getf rest :label)) (print-md-element a stream))
(format stream "]: ~a~@[ ~s~]~%"
(getf rest :source) (getf rest :title)))
(defmethod print-md-element ((elem (eql :apostrophe)) stream)
(write-string "'" stream))
(defmethod print-md-element ((elem string) stream)
(print-md-escaped elem stream))
(defmethod print-md-element ((elem cons) stream)
(if (symbolp (car elem))
(print-md-tagged-element (car elem) stream (cdr elem))
(error "unknown cons? ~s" elem)))
(defmethod print-doc-to-stream-using-format (doc stream
(format (eql :markdown)))
(let ((*references* (extract-refs doc))
(*md-prefix* "")
(*md-in-block* nil)
(*md-block-seen-p* nil))
(dolist (element doc)
(print-md-element element stream))))
#|
(defparameter *test-cases*
'("\\*notemph\\* \\_notemph2\\_ \\ \\`notcode\\`
"
"### heading
- outer item
dsfdsf *emph* **strong** ***strongemph***
kjdsf [asdf](#xxx \"*title*\") <http://quotenil.com> <mega@retes.hu>
- b1
inside `````co````de`````
codeblock 234
sdfkj kjsdf
- b2
dsfdsf
llll
1. xxx
2. yyy
---
para [ref-link][sfd] ![image](#dsfa \"img\") ![img][32]
sddsf <a href=\"#xxx\">ddd</a> <!-- comment -->
em-dash -- en-dash --- lrsa <-> <- -> <=> <= => ... foo's
[id1]: http://some.link.com/ \"with some title\"
plain
"))
(defun check-roundtrip (original)
(let* ((reconstruction (with-output-to-string (out)
(let ((3bmd-grammar:*smart-quotes* t))
(3bmd:parse-string-and-print-to-stream
original out :format :markdown))))
(position (mismatch reconstruction original)))
(when position
(format t "Mismatch found. Common part:~%~S~%-- Original:~%~S~%~
-- Reconstruction:~%~S~%"
(subseq original 0 position)
(subseq original position)
(subseq reconstruction position))
(error "Mismatch found at position ~S." position))))
(map nil #'check-roundtrip *test-cases*)
(let ((3bmd-grammar:*smart-quotes* t))
(3bmd:parse-string-and-print-to-stream "\\'" *standard-output*))
(3bmd-grammar:parse-doc "[id1]: http://some.link.com/ \"with some title\"")
|#