-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMarkdown.lua
287 lines (260 loc) · 7.82 KB
/
Markdown.lua
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
------------------------------------------------------------------------------------------------------------------------
-- Name: Markdown.lua
-- Version: 1.0 (1/17/2021)
-- Author: Brad Sharp
--
-- Repository: https://github.com/BradSharp/Romarkable
-- License: MIT (https://github.com/BradSharp/Romarkable/blob/main/LICENSE)
--
-- Copyright (c) 2021 Brad Sharp
------------------------------------------------------------------------------------------------------------------------
local Markdown = {}
------------------------------------------------------------------------------------------------------------------------
-- Text Parser
------------------------------------------------------------------------------------------------------------------------
local InlineType = {
Text = 0,
Ref = 1,
}
local ModifierType = {
Bold = 0,
Italic = 1,
Strike = 2,
Code = 3,
}
local function sanitize(s)
return s:gsub("&", "&"):gsub("<", "<"):gsub(">", ">"):gsub("\"", """):gsub("'", "'")
end
local function characters(s)
return s:gmatch(".")
end
local function last(t)
return t[#t]
end
local function getModifiers(stack)
local modifiers = {}
for _, modifierType in pairs(stack) do
modifiers[modifierType] = true
end
return modifiers
end
local function parseModifierTokens(md)
local index = 1
return function ()
local text, newIndex = md:match("^([^%*_~`]+)()", index)
if text then
index = newIndex
return false, text
elseif index <= md:len() then
local text, newIndex = md:match("^(%" .. md:sub(index, index) .. "+)()", index)
index = newIndex
return true, text
end
end
end
local function parseText(md)
end
local richTextLookup = {
["*"] = ModifierType.Bold,
["_"] = ModifierType.Italic,
["~"] = ModifierType.Strike,
["`"] = ModifierType.Code,
}
local function getRichTextModifierType(symbols)
return richTextLookup[symbols:sub(1, 1)]
end
local function richText(md)
md = sanitize(md)
local tags = {
[ModifierType.Bold] = {"<b>", "</b>"},
[ModifierType.Italic] = {"<i>", "</i>"},
[ModifierType.Strike] = {"<s>", "</s>"},
[ModifierType.Code] = {"<font face=\"RobotoMono\">", "</font>"},
}
local state = {}
local output = ""
for token, text in parseModifierTokens(md) do
if token then
local modifierType = getRichTextModifierType(text)
if state[ModifierType.Code] and modifierType ~= ModifierType.Code then
output = output .. text
continue
end
local symbolState = state[modifierType]
if not symbolState then
output = output .. tags[modifierType][1]
state[modifierType] = text
elseif text == symbolState then
output = output .. tags[modifierType][2]
state[modifierType] = nil
else
output = output .. text
end
else
output = output .. text
end
end
for modifierType in pairs(state) do
output = output .. tags[modifierType][2]
end
return output
end
------------------------------------------------------------------------------------------------------------------------
-- Document Parser
------------------------------------------------------------------------------------------------------------------------
local BlockType = {
None = 0,
Paragraph = 1,
Heading = 2,
Code = 3,
List = 4,
Ruler = 5,
Quote = 6,
}
local CombinedBlocks = {
[BlockType.None] = true,
[BlockType.Paragraph] = true,
[BlockType.Code] = true,
[BlockType.List] = true,
[BlockType.Quote] = true,
}
local function cleanup(s)
return s:gsub("\t", " ")
end
local function getTextWithIndentation(line)
local indent, text = line:match("^%s*()(.*)")
return text, math.floor(indent / 2)
end
-- Iterator: Iterates the string line-by-line
local function lines(s)
return (s .. "\n"):gmatch("(.-)\n")
end
-- Iterator: Categorize each line and allows iteration
local function blockLines(md)
local blockType = BlockType.None
local nextLine = lines(md)
local function it()
local line = nextLine()
if not line then
return
end
-- Code
if blockType == BlockType.Code then
if line:match("^```") then
blockType = BlockType.None
end
return BlockType.Code, line
end
-- Blank line
if line:match("^%s*$") then
return BlockType.None, ""
end
-- Ruler
if line:match("^%-%-%-+") or line:match("^===+") then
return BlockType.Ruler, ""
end
-- Heading
if line:match("^#") then
return BlockType.Heading, line
end
-- Code
if line:match("^%s*```") then
blockType = BlockType.Code
return blockType, line
end
-- Quote
if line:match("^%s*>") then
return BlockType.Quote, line
end
-- List
if line:match("^%s*%-%s+") or line:match("^%s*%*%s+") or line:match("^%s*[%u%d]+%.%s+") or line:match("^%s*%+%s+") then
return BlockType.List, line
end
-- Paragraph
return BlockType.Paragraph, line -- should take into account indentation of first-line
end
return it
end
-- Iterator: Joins lines of the same type into a single element
local function textBlocks(md)
local it = blockLines(md)
local lastBlockType, lastLine = it()
return function ()
-- This function works by performing a lookahead at the next line and then deciding what to do with the
-- previous line based on that.
local nextBlockType, nextLine = it()
if nextBlockType == BlockType.Ruler and lastBlockType == BlockType.Paragraph then
-- Combine paragraphs followed by rulers into headers
local text = lastLine
lastBlockType, lastLine = it()
return BlockType.Heading, ("#"):rep(lastLine:sub(1, 1) == "=" and 2 or 1) .. " " .. text
end
local lines = { lastLine }
while CombinedBlocks[nextBlockType] and nextBlockType == lastBlockType do
table.insert(lines, nextLine)
nextBlockType, nextLine = it()
end
local blockType, blockText = lastBlockType, table.concat(lines, "\n")
lastBlockType, lastLine = nextBlockType, nextLine
return blockType, blockText
end
end
-- Iterator: Transforms raw blocks into sections with data
local function blocks(md, markup)
local nextTextBlock = textBlocks(md)
local function it()
local blockType, blockText = nextTextBlock()
if blockType == BlockType.None then
return it() -- skip this block type
end
local block = {}
if blockType then
local text, indent = getTextWithIndentation(blockText)
block.Indent = indent
if blockType == BlockType.Paragraph then
block.Text = markup(text)
elseif blockType == BlockType.Heading then
local level, text = blockText:match("^#+()%s*(.*)")
block.Level, block.Text = level - 1, markup(text)
elseif blockType == BlockType.Code then
local syntax, code = text:match("^```(.-)\n(.*)\n```$")
block.Syntax, block.Code = syntax, syntax == "raw" and code or sanitize(code)
elseif blockType == BlockType.List then
local lines = blockText:split("\n")
for i, line in ipairs(lines) do
local text, indent = getTextWithIndentation(line)
local symbol, text = text:match("^(.-)%s+(.*)")
lines[i] = {
Level = indent,
Text = markup(text),
Symbol = symbol,
}
end
block.Lines = lines
elseif blockType == BlockType.Quote then
local lines = blockText:split("\n")
for i = 1, #lines do
lines[i] = lines[i]:match("^%s*>%s*(.*)")
end
local rawText = table.concat(lines, "\n")
block.RawText, block.Iterator = rawText, blocks(rawText, markup)
end
end
return blockType, block
end
return it
end
local function parseDocument(md, inlineParser)
return blocks(cleanup(md), inlineParser or richText)
end
------------------------------------------------------------------------------------------------------------------------
-- Exports
------------------------------------------------------------------------------------------------------------------------
Markdown.sanitize = sanitize
Markdown.parse = parseDocument
Markdown.parseText = parseText
Markdown.parseTokens = parseModifierTokens
Markdown.BlockType = BlockType
Markdown.InlineType = InlineType
Markdown.ModifierType = ModifierType
return Markdown