-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathMarkdown2HTML.ahk
301 lines (268 loc) · 6.6 KB
/
Markdown2HTML.ahk
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
/*
Markdown2HTML() + StrStartsWith() functions
from GenDocs v3.0-alpha004 - Author: fincs
Source: https://github.com/fincs/GenDocs
+ Table function source:
http://www.autohotkey.com/board/topic/71751-gendocs-v30-alpha002-create-stdlib-documentation-easily/page-2#entry530364
+ KeepHTML (Lintalist addition):
using a ~~~ block HTML-code is included as is
See documentation "Formatted Text Snippets", "Markdown" for more information on tables
and how to use the simplyfied version of the Markdown syntax that is supported by
this function.
*/
;
; File encoding: UTF-8
;
MD_IsMultiP(ByRef htmQ)
{
StringReplace, html, htmQ, <p>, <p>, UseErrorLevel
if ErrorLevel > 1
return true
StringReplace, html, html, <pre, <pre, UseErrorLevel
return !!ErrorLevel
}
Markdown2HTML(ByRef text, simplify=0)
{
if !simplify
return Markdown2HTML_(text)
t := Markdown2HTML_(text)
if !MD_IsMultiP(t)
{
StringReplace, t, t, <p>,, All
StringReplace, t, t, </p>,, All
}
return t
}
Markdown2HTML_(ByRef text)
{
blankLine := 1, inCode := 0, inUList := 0, inOList := 0
Loop, Parse, text, `n, `r
{
isList := false
t := LTrim(A_LoopField)
beg := SubStr(t, 1, 2)
if t =
{
blankLine ++
if blankLine = 1
out .= !inCode ? !inUList ? !inOList ? "</p>`n" : "</ol>`n" : "</ul>`n" : "</pre>`n"
inCode := 0
inUList := 0
inOList := 0
continue
}else
{
if blankLine
{
if RegExMatch(t, "^(#{1,3})\s+(.+)$", o)
{
h := StrLen(o1)
out .= "<h" h " id=""" _ElemID(o2) """>" _HTML(o2) "</h" h ">`n"
continue
}
inCode := (beg = "> ")
inUList := (beg = "* ")
inOList := (t ~= "^([1-9]+|[a-z]+)\.\s+")
if inOList
listTag := t ~= "\d" ? "<ol>`n" : "<ol style=""list-style-type: lower-alpha"">`n", isList := true
out .= !inCode ? !inUList ? !inOList ? "<p>`n" : listTag : "<ul>`n" : "<pre>`n"
}
blankLine := 0
}
_reprocess:
if inCode
{
if (beg = "> ")
{
t := SubStr(t, 3)
t := _HTML(t)
}else
{
out .= "</pre>`n"
SetStatus(out, t, inCode, inUList, inOList)
goto _reprocess
}
}else if inUList
{
if (beg = "* ")
{
t := SubStr(t, 3)
t := "<li>" _MD(t) "</li>"
}else
{
out .= "</ul>`n"
SetStatus(out, t, inCode, inUList, inOList)
goto _reprocess
}
}else if inOList
{
if RegExMatch(t, "O)^([1-9]+|[a-z]+)\.\s+", mObj)
{
StringTrimLeft, t, t, % mObj.Len
t := "<li>" _MD(t) "</li>"
}else
{
out .= "</ol>`n"
SetStatus(out, t, inCode, inUList, inOList)
goto _reprocess
}
}else
{
if (beg != "> ") && (beg != "* ") && !isList
{
t := _MD(t)
if SubStr(t, -1) = " "
{
StringTrimRight, t, t, 2
t .= "<br/>"
}
}else
{
out .= "</p>`n"
SetStatus(out, t, inCode, inUList, inOList)
goto _reprocess
}
}
out .= t "`n"
}
if !blankLine
out .= !inCode ? !inUList ? !inOList ? "</p>`n" : "</ol>`n" : "</ul>`n" : "</pre>`n"
StringTrimRight, out, out, 1
StringReplace, out, out, <p>`n, <p>, All
StringReplace, out, out, `n</p>, </p>, All
; Fix comments
_s := 1
while p := InStr(out, "<pre>", false, _s)
{
p2 := InStr(out, "</pre>", false, p += 5)
code := HighlightCode(SubStr(out, p, p2-p))
out := SubStr(out, 1, p-1) code SubStr(out, p2)
_s := p + StrLen(code)
}
StringReplace, out, out, <pre>`n, <pre class="NoIndent">, All
StringReplace, out, out, `n</pre>, </pre>, All
While RegExMatch(out,"m)\n<p>\|") ; we have a table
_Tables(out)
While RegExMatch(out,"m)\~\~\~") ; we have a html code block (lintalist)
_keepHTML(out)
return out
}
SetStatus(ByRef out, t, ByRef inCode, ByRef inUList, ByRef inOList)
{
beg := SubStr(t, 1, 2)
inCode := beg = "> "
inUList := beg = "* "
inOList := t ~= "^([1-9]+|[a-z]+)\.\s+"
out .= !inCode ? !inUList ? !inOList ? "<p>`n" : "<ol>`n" : "<ul>`n" : "<pre>`n"
}
_MD(ByRef v)
{
static pcre_callout := "_MD_Callout"
t := _HTML(v)
t := RegExReplace(t, "P)\*\*(.+?)\*\*(?C1)", "<strong>$1</strong>")
t := RegExReplace(t, "P)\*(.+?)\*(?C2)", "<em>$1</em>")
t := RegExReplace(t, "P)``(.+?)``(?C3)", "<code>$1</code>")
t := RegExReplace(t, "P)!\[(.*?)\](?C4)\((.+?)\)", "<img src=""$2"" alt=""$1""/>")
t := RegExReplace(t, "P)\[(.+?)\](?C5)\((.+?)\)", "<a href=""$2"">$1</a>")
StringReplace, t, t, \*, *, All
StringReplace, t, t, \``, ``, All
StringReplace, t, t, \[, [, All
StringReplace, t, t, \], ], All
StringReplace, t, t, \!, !, All
StringReplace, t, t, \\, \, All
return t
}
_HTML(ByRef v)
{
StringReplace, t, v, &, &, All
StringReplace, t, t, ", ", All
StringReplace, t, t, <, <, All
StringReplace, t, t, >, >, All
return t
}
_ElemID(ByRef v)
{
t := Trim(_HTML(v))
StringReplace, t, t, ',, All
StringReplace, t, t, ",, All
StringReplace, t, t, (,, All
StringReplace, t, t, ),, All
StringReplace, t, t, [,, All
StringReplace, t, t, ],, All
StringReplace, t, t, ?,, All
StringReplace, t, t, %A_Space%, -, All
StringLower, t, t
return t
}
HighlightCode(ByRef code)
{
Loop, Parse, code, `n, `r
{
line := A_LoopField
tline := Trim(line)
if StrStartsWith(tline, ";")
line := "<em>" line "</em>"
else line := RegExReplace(line, "(\s+)(;.*)$", "$1<em>$2</em>")
StringReplace, line, line, %A_Tab%, %A_Space%%A_Space%%A_Space%%A_Space%, All
out .= line "`n"
}
StringTrimRight, out, out, 1
return out
}
_MD_Callout(m, cId, foundPos, haystack)
{
; cId = 1 -> **text**
; cId = 2 -> *text*
; cId = 3 -> `text`
; cId = 4 -> ![img alt text](img url)
; cId = 5 -> [link text](link url)
p := foundPos, e := 0
while SubStr(haystack, --p, 1) = "\"
e ++
if e & 1
return 1
p := foundPos + m - 1, e := 0
if cId = 1
p --
while SubStr(haystack, --p, 1) = "\"
e ++
if e & 1
return 1
if cId = 4
{
global imglist
imglist._Insert(SubStr(haystack, mPos2, mLen2))
}
return 0
}
_Tables(byref t)
{
RegExMatch(t,"msU)\n<p>\|(.*)</p>",table)
Loop, parse, table1, `n, `r
{
If A_Index = 1
{
htmltable .= "<thead><th>" A_LoopField "</th></thead>`n"
StringReplace, htmltable, htmltable,|,</th><th>,All
Continue
}
htmltable .= "<tr><td>" A_LoopField "</td></tr>`n"
}
StringReplace, htmltable, htmltable,|,</td><td>,All
StringReplace, t, t, <p>|%table1%</p>, <table>`n%htmltable%</table> ; remove unformatted table from t
Return t
}
_KeepHTML(byref t) ; Lintalist addition
{
RegExMatch(t,"iUsm)\~\~\~(.*)\~\~\~",match)
stringreplace, replace, match1, <, <, All
stringreplace, replace, replace, >, >, All
stringreplace, t, t, ~~~%match1%~~~, %replace%, All
Return t
}
; StrStartsWith()
; also from from GenDocs v3.0-alpha004
StrStartsWith(ByRef v, ByRef w)
{
return SubStr(v, 1, StrLen(w)) = w
}