-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown.red
More file actions
198 lines (169 loc) · 6.71 KB
/
markdown.red
File metadata and controls
198 lines (169 loc) · 6.71 KB
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
Red [
Title: "markdown parser in red"
Version: 0.3.0
Author: "bitbegin"
Purpose: {
A very small markdown parser written in red.
}
]
context [
debug?: off
debug-print: func [data] [if debug? [print data]]
nochar: charset " ^-^/"
chars: complement nochar
digit: charset [#"0" - #"9"]
hex-char: charset [#"a" - #"f"]
hex-digit: union digit hex-char
space: charset " ^-"
md-buffer: make string! 1000
main-rules: [
lf
| header-rule
| quote-rule
| ulist-rule
| olist-rule
| code-lang-rule
| code-block-rule
| para-rule
| skip thru lf
]
header-rule: [
["#" some space copy text to copy tag [any [some space any "#"] lf] tag (parse text inline-rule emit-h1 text)]
| ["##" some space copy text to copy tag [any [some space any "#"] lf] tag (parse text inline-rule emit-h2 text)]
| ["###" some space copy text to copy tag [any [some space any "#"] lf] tag (parse text inline-rule emit-h3 text)]
| ["####" some space copy text to copy tag [any [some space any "#"] lf] tag (parse text inline-rule emit-h4 text)]
| ["#####" some space copy text to copy tag [any [some space any "#"] lf] tag (parse text inline-rule emit-h5 text)]
| ["######" some space copy text to copy tag [any [some space any "#"] lf] tag (parse text inline-rule emit-h6 text)]
]
para-rule: [copy text to 2 lf (parse text inline-rule emit-para text)]
code-lang-rule: [
["```" any space lf copy text to copy tag "```^/" tag (emit-code text)]
| ["```" copy lang to [any space lf] copy text to copy tag "```^/" tag (emit-code-lang text lang)]
]
code-block-rule: [
copy indent some space (emit "<pre><code>^/") some [copy text thru lf [
[lf (emit text emit "</code></pre>^/") break]
| [indent (emit text)]
| (emit text)]]
]
quote-rule: [
copy indent [">" some space] (emit "<blockquote>") some [copy text to lf [
[lf lf (emit text emit "</blockquote>^/") break]
| [lf indent (emit text)]
| lf (emit text)]]
]
ulist-rule: [
copy indent [["*" | "-" | "+"] some space] (emit "<ul><li>") some [copy text to lf [
[lf lf (emit text emit "</li></ul>^/") break]
| [lf indent (emit text emit "</li><li>")]
| lf (emit text)]]
]
olist-rule: [
digit "." some space (emit "<ol><li>") some [copy text to lf [
[lf lf (emit text emit "</li></ol>^/") break]
| [lf digit "." some space (emit text emit "</li><li>")]
| lf (emit text)]]
]
code-inline-rule: [
to copy tag "`" start: tag to tag :start
remove tag insert "<code>" to tag remove tag insert "</code>"
]
emphasis-inline-rule: [
to copy tag ["*" | "_"] start: tag to tag :start
remove tag insert "<em>" to tag remove tag insert "</em>"
]
strong-inline-rule: [
to copy tag ["**" | "__"] start: tag to tag :start
remove tag insert "<strong>" to tag remove tag insert "</strong>"
]
link-inline-rule: [
to "" ")" (
title: none
if find/tail link space [
title: copy find/tail link space
link: copy/part link find link space])
if (title)][insert {<img src="} insert (link) insert {" alt="} insert (link-text) insert {" title=} insert (title) insert { />}]
| to "" ")" (
title: none
if find/tail link space [
title: copy find/tail link space
link: copy/part link find link space])
if (not title)][insert {<img src="} insert (link) insert {" alt="} insert (link-text) insert {" />}]
| to "[" remove ["[" copy link-text to "](" "](" copy link to ")" ")" (
title: none
if find/tail link space [
title: copy find/tail link space
link: copy/part link find link space])
if (not title)][insert {<a href="} insert (link) insert {">} insert (link-text) insert {</a>}]
| to "[" remove ["[" copy link-text to "](" "](" copy link to ")" ")" (
title: none
if find/tail link space [
title: copy find/tail link space
link: copy/part link find link space])
if (title)][insert {<a href="} insert (link) insert {" title=} insert (title) insert {>} insert (link-text) insert {</a>}]
]
inline-rule: [ahead any code-inline-rule ahead any strong-inline-rule ahead any emphasis-inline-rule any link-inline-rule]
emit: func [value][
append md-buffer value
]
emit-para: func [value][
debug-print "====emit-para"
append md-buffer "<p>"
append md-buffer value
append md-buffer "</p>^/"
]
emit-h1: func [value][
debug-print "====emit-h1"
append md-buffer "<h1>"
append md-buffer value
append md-buffer "</h1>^/"
]
emit-h2: func [value][
debug-print "====emit-h2"
append md-buffer "<h2>"
append md-buffer value
append md-buffer "</h2>^/"
]
emit-h3: func [value][
debug-print "====emit-h3"
append md-buffer "<h3>"
append md-buffer value
append md-buffer "</h3>^/"
]
emit-h4: func [value][
debug-print "====emit-h4"
append md-buffer "<h4>"
append md-buffer value
append md-buffer "</h4>^/"
]
emit-h5: func [value][
debug-print "====emit-h5"
append md-buffer "<h5>"
append md-buffer value
append md-buffer "</h5>^/"
]
emit-h6: func [value][
debug-print "====emit-h6"
append md-buffer "<h6>"
append md-buffer value
append md-buffer "</h6>^/"
]
emit-code: func [value][
debug-print "====emit-code"
append md-buffer "<pre><code>^/"
append md-buffer value
append md-buffer "</code></pre>^/"
]
emit-code-lang: func [value lang][
debug-print "====emit-code-lang"
append md-buffer {<pre><code class="}
append md-buffer lang
append md-buffer {">^/}
append md-buffer value
append md-buffer "</code></pre>^/"
]
set 'parse-markdown function [str [string!] return: [string!]] [
parse str [any main-rules]
md-buffer
]
]