-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmd.reb
402 lines (368 loc) · 6.33 KB
/
md.reb
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
REBOL[
Title: "Rebol Markdown Parser"
File: %md.reb
Author: "Boleslav Březovský"
Date: 7-3-2014
Type: 'module
Exports: [markdown]
Options: [isolate]
To-do: [
"function to produce rule wheter to continue on start-para or not"
]
Known-bugs: [
]
Notes: ["For mardown specification, see http://johnmacfarlane.net/babelmark2/faq.html"]
]
xml?: true
start-para?: true
end-para?: true
md-buffer: make string! 1000
; FIXME: hacky switch to determine wheter to emit <p> or not (for snippets)
para?: false
set [open-para close-para] either para? [[<p></p>]][["" ""]]
; -----
value: copy "" ; FIXME: leak?
emit: func [data] [
; print "***wrong emit***"
append md-buffer data
]
close-tag: func [tag] [head insert copy tag #"/"]
start-para: does [
if start-para? [
start-para?: false
end-para?: true
emit open-para
]
]
entities: [
#"<" (emit "<")
| #">" (emit ">")
| #"&" (emit "&")
]
escape-set: charset "\`*_{}[]()#+-.!"
escapes: use [escape] [
[
#"\"
(start-para)
set escape escape-set
(emit escape)
]
]
numbers: charset [#"0" - #"9"]
; some "longer, but readable" stuff
plus: #"+"
minus: #"-"
asterisk: #"*"
underscore: #"_"
hash: #"#"
dot: #"."
eq: #"="
lt: #"<"
gt: #">"
header-underscore: use [text tag] [
[
copy text to newline
newline
some [eq (tag: <h1>) | minus (tag: <h2>)]
[newline | end]
(
end-para?: false
start-para?: true
emit ajoin [tag text close-tag tag]
)
]
]
header-hash: use [value continue trailing mark tag] [
[
(
continue: either/only start-para? [not space] [fail]
mark: clear ""
)
continue
copy mark some hash
space
(emit tag: to tag! compose [h (length? mark)])
some [
[
(trailing: "")
[[any space mark] | [opt [2 space (trailing: join newline newline)]]]
[newline | end]
(end-para?: false)
(start-para?: true)
(emit ajoin [close-tag tag trailing])
]
break
| set value skip (emit value)
]
]
]
header-rule: [
header-underscore
| header-hash
]
autolink-rule: use [address] [
[
lt
copy address ; TODO: Parse address to match email
to gt skip
(
start-para
emit ajoin [{<a href="} address {">} address </a>]
)
]
]
link-rule: use [text address value title] [
[
#"["
copy text
to #"]" skip
#"("
(
address: clear ""
title: none
)
any [
not [space | tab | #")"]
set value skip
(append address value)
]
opt [
some [space | tab]
#"^""
copy title to #"^""
skip
]
skip
(
start-para
title: either title [ajoin [space {title="} title {"}]][""]
emit ajoin [{<a href="} address {"} title {>} text </a>]
)
]
]
em-rule: use [mark text] [
[
copy mark ["**" | "__" | "*" | "_"]
not space
copy text
to mark mark
(
start-para
mark: either equal? length? mark 1 <em> <strong>
emit ajoin [mark text close-tag mark]
)
]
]
img-rule: use [text address] [
[
#"!"
#"["
copy text
to #"]" skip
#"("
copy address
to #")" skip
(
start-para
emit ajoin [{<img src="} address {" alt="} text {"} either xml? { /} {} {>}]
)
]
]
; TODO: make it bitset!
horizontal-mark: [minus | asterisk | underscore]
horizontal-rule: [
horizontal-mark
any space
horizontal-mark
any space
horizontal-mark
any [
horizontal-mark
| space
]
(
end-para?: false
emit either xml? <hr /><hr>
)
]
unordered: [any space [asterisk | plus | minus] space]
ordered: [any space some numbers dot space]
; TODO: recursion for lists
list-rule: use [continue tag item] [
[
some [
(
continue: either start-para? [
[
ordered (item: ordered tag: <ol>)
| unordered (item: unordered tag: <ul>)
]
] [
[fail]
]
)
continue
(start-para?: end-para?: false)
(emit ajoin [tag newline <li>])
line-rules
newline
(emit ajoin [</li> newline])
some [
item
(emit <li>)
line-rules
[newline | end]
(emit ajoin [</li> newline])
]
(emit close-tag tag)
]
]
]
blockquote-rule: use [continue] [
[
(
continue: either/only start-para? [gt any space] [fail]
)
continue
(emit ajoin [<blockquote> newline])
line-rules
[[newline (emit newline)] | end]
any [
; FIXME: what an ugly hack
[newline ] (remove back tail md-buffer emit ajoin [close-para newline newline open-para])
| [
continue
opt line-rules
[newline (emit newline) | end]
]
]
(end-para?: false)
(emit ajoin [close-para newline </blockquote>])
]
]
inline-code-rule: use [code value] [
[
[
"``"
(start-para)
(emit <code>)
some [
"``" (emit </code>) break ; end rule
| entities
| set value skip (emit value)
]
]
| [
"`"
(start-para)
(emit <code>)
some [
"`" (emit </code>) break ; end rule
| entities
| set value skip (emit value)
]
]
]
]
code-line: use [value][
[
some [
entities
| [newline | end] (emit newline) break
| set value skip (emit value)
]
]
]
code-rule: use [text] [
[
[4 space | tab]
(emit ajoin [<pre><code>])
code-line
any [
[4 space | tab]
code-line
]
(emit ajoin [</code></pre>])
(end-para?: false)
]
]
asterisk-rule: ["\*" (emit "*")]
newline-rule: [
newline
any [space | tab]
some newline
any [space | tab]
(
emit ajoin [close-para newline newline]
start-para?: true
)
| newline (emit newline)
]
line-break-rule: [
space
some space
newline
(emit ajoin [either xml? <br /> <br> newline])
]
leading-spaces: use [continue] [
[
(continue: either/only start-para? [some space] [fail])
continue
(start-para)
]
]
; simplified rules used as sub-rule in some rules
line-rules: [
some [
em-rule
| link-rule
| header-rule
| not newline set value skip (
start-para
emit value
)
]
]
; main rules
rules: [
; any space
some [
header-rule
| link-rule
| autolink-rule
| img-rule
| list-rule
| blockquote-rule
| inline-code-rule
| code-rule
| asterisk-rule
| em-rule
| horizontal-rule
| entities
| escapes
| line-break-rule
| newline-rule
| end (if end-para? [end-para?: false emit close-para])
| leading-spaces
| set value skip (
start-para
emit value
)
]
]
markdown: func [
"Parse markdown source to HTML or XHTML"
data
; TODO:
/only "Return result without newlines"
; TODO:
/xml "Switch from HTML tags to XML tags (e.g.: <hr /> instead of <hr>)"
] [
start-para?: true
end-para?: true
para?: false
clear head md-buffer
; probe rules
parse data [some rules]
md-buffer
]