-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathletdown.lua
More file actions
319 lines (277 loc) · 8.23 KB
/
letdown.lua
File metadata and controls
319 lines (277 loc) · 8.23 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
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
-- letdown file parsing in lua, emitting html as output
local VERSION = "1.0.0-beta"
local function escape_html(s)
return s:gsub("&", "&"):gsub("<", "<"):gsub(">", ">")
end
local linkdefs = {}
local hashtags = {}
-- remove comments n hashtags unless inside inline code or code blocks
local function strip_comments_and_tags(text)
local seen = {} -- don't record duplicate tags
local out = {}
local i = 1
while i <= #text do
-- code block
if text:sub(i, i+2) == "```" then
local block_end = text:find("\n```", i+3)
if block_end then
table.insert(out, text:sub(i, block_end + 3))
i = block_end + 4
else
table.insert(out, text:sub(i))
break
end
-- inline code
elseif text:sub(i, i) == "`" then
local close_tick = text:find("`", i+1)
if close_tick then
table.insert(out, text:sub(i, close_tick))
i = close_tick + 1
else
table.insert(out, text:sub(i))
break
end
else
-- normal text
local next_special = text:find("[`]", i)
local segment_end = next_special and next_special - 1 or #text
local segment = text:sub(i, segment_end)
-- strip comments
segment = segment:gsub(";;.-;;", "")
-- strip hashtags but store them
segment = segment:gsub("(%s*)#([%w_%-]+)(%s*)", function(pre, tag, post)
if not seen[tag] then
table.insert(hashtags, tag)
seen[tag] = true
end
return pre .. post
end)
table.insert(out, segment)
i = segment_end + 1
end
end
return table.concat(out)
end
local function parse_spans(text)
-- inline code
text = text:gsub("`(.-)`", function (code)
return "<code>" .. escape_html(code) .. "</code>"
end)
-- emphasis
text = text:gsub("%*(.-)%*", "<em>%1</em>")
-- reference-style links
text = text:gsub("%[(.-)%]", function(label)
local trimmed = label:match("^%s*(.-)%s*$")
local url = linkdefs[trimmed]
if url then
-- links defined with a linkdef will open in a new tab
local path = url:gsub("%.let$", ".html") -- if url is ".let", replace with ".html"
return string.format('<a href="%s" target="_blank">%s</a>', escape_html(path), escape_html(label))
else
-- assume the path is "label.html" with spaces replaced by underscores
local safe_label = trimmed:gsub("%s+", "_")
local href = escape_html(safe_label .. ".html")
return string.format('<a href="%s">%s</a>', href, escape_html(trimmed))
end
end)
return text
end
-- emit functions for each block
local function emit_heading(line)
if line:match("^= ") then
return "<h1>" .. escape_html(line:sub(3)) .. "</h1>"
elseif line:match("^== ") then
return "<h2>" .. escape_html(line:sub(4)) .. "</h2>"
elseif line:match("^=== ") then
return "<h3>" .. escape_html(line:sub(5)) .. "</h3>"
end
end
local function emit_list(block)
local html = { "<ul>" }
for line in block:gmatch("[^\n]+") do
local item = line:match("^%- (.*)")
table.insert(html, " <li>" .. parse_spans(item or "") .. "</li>")
end
table.insert(html, "</ul>")
return table.concat(html, "\n")
end
local function emit_image(line)
local path, alt = line:match("^=>%s+(%S+)%s*(.*)")
return string.format('<img src="%s" alt="%s">', escape_html(path or ""), escape_html(alt or ""))
end
local function emit_quote(block)
local lines = {}
for line in block:gmatch("[^\n]+") do
lines[#lines + 1] = line:gsub("^> ", "")
end
local content = table.concat(lines, "\n")
return string.format("<blockquote>%s</blockquote>", parse_spans(content))
end
local function emit_code(block)
local code = block:match("^```%s*\n(.-)\n```")
return string.format("<pre><code>%s</code></pre>", escape_html(code or ""))
end
local function emit_paragraph(block)
return "<p>" .. parse_spans(block) .. "</p>"
end
local function emit_hr()
return "<hr>"
end
-- parse letdown blocks from text
local function parse_letdown(text)
-- strip comments and hashtags first
hashtags = {}
text = strip_comments_and_tags(text)
local blocks = {}
linkdefs = {} -- reset per file
local first_h1 = nil
-- handling and removing linkdefs in first pass
local cleaned_lines = {}
for line in (text .. "\n"):gmatch("([^\n]*)\n") do
local label, url = line:match("^%s*%[(.-)%]%s*:%s*(%S+)%s*$")
if label and url then
linkdefs[label] = url
else
table.insert(cleaned_lines, line)
end
end
text = table.concat(cleaned_lines, "\n")
-- split into blocks by blank line
local temp = {}
for line in (text .. "\n"):gmatch("([^\n]*)\n") do
if line:match("^%s*$") then
if #temp > 0 then
table.insert(blocks, table.concat(temp, "\n"))
temp = {}
end
else
table.insert(temp, line)
end
end
if #temp > 0 then
table.insert(blocks, table.concat(temp, "\n"))
end
-- emit html
local html_blocks = {}
for _, block in ipairs(blocks) do
if block:match("^= ") then
local h1_text = block:sub(3)
if not first_h1 then first_h1 = h1_text end
table.insert(html_blocks, emit_heading(block))
elseif block:match("^== ") or block:match("^=== ") then
table.insert(html_blocks, emit_heading(block))
elseif block:match("^%- ") then
table.insert(html_blocks, emit_list(block))
elseif block:match("^=> ") then
table.insert(html_blocks, emit_image(block))
elseif block:match("^> ") then
table.insert(html_blocks, emit_quote(block))
elseif block:match("^```") then
table.insert(html_blocks, emit_code(block))
elseif block:match("^%-%-%-") then
table.insert(html_blocks, emit_hr())
else
table.insert(html_blocks, emit_paragraph(block))
end
end
return table.concat(html_blocks, "\n\n"), hashtags, first_h1
end
-- cli args/flags
local input_file = nil
local help = false
local version = false
local print_stdout = false
local output_file = nil
local html_template = nil
local function print_usage()
print("Usage: letdown.lua [-h] [-v] [-s] [-o filename] [-t template.html] file.let")
end
-- cli flag parsing
local i = 1
while i <= #arg do
local a = arg[i]
if a == "-h" then
help = true
elseif a == "-v" then
version = true
elseif a == "-p" then
print_stdout = true
elseif a == "-o" then
i = i + 1
output_file = arg[i]
elseif a == "-t" then
i = i + 1
html_template = arg[i]
else
if not input_file then
input_file = a
else
print("Unexpected argument: " .. a)
print_usage()
os.exit(1)
end
end
i = i + 1
end
if help then
print_usage()
print(" -h print help text")
print(" -v print version info")
print(" -o filename write output to filename, regardless of -p tag")
print(" -t template.html use template.html as a template file")
print(" -p write output to stdout only")
os.exit(0)
end
if version then
print("letdown to HTML parser reference implementation\n Version " .. VERSION)
os.exit(0)
end
-- reading input file
if not input_file then
print_usage()
os.exit(1)
end
local f = io.open(input_file, "r")
if not f then
print("Could not open file: " .. input_file .. "\n")
os.exit(1)
end
local raw = f:read("*a")
f:close()
-- parse
local body, tags, first_h1 = parse_letdown(raw)
-- if html template specified, fill it
if html_template then
local tf = io.open(html_template, "r")
if not tf then
print("Could not open template: " .. html_template)
os.exit(1)
end
local template_str = tf:read("*a")
tf:close()
-- get filename without extension
local filebase = input_file:match("([^/\\]+)%.let$")
if not filebase then
filebase = input_file
end
template_str = template_str
:gsub("%%body", function() return body or "" end)
:gsub("%%tags", function() return table.concat(tags, ", ") or "" end)
:gsub("%%file", function() return filebase end)
:gsub("%%h1", function() return first_h1 or "" end)
body = template_str
end
-- output
if not output_file then
if print_stdout then
print(body)
os.exit(0)
else
-- default to just input_file.html
output_file = input_file:gsub("%.let$", "") .. ".html"
end
end
-- outputting the file!
local outf = io.open(output_file, "w")
outf:write(body)
outf:close()