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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
local test_directory = arg[0]:match("^(.*)/") or "."
local h = dofile(test_directory .. "/harness.lua")
local script = arg[1]
local mode = arg[2] or ""
local has_lpeg = false
if mode == "nolpeg" then
h.preload_failure("lpeg")
else
has_lpeg = pcall(require, "lpeg")
if not has_lpeg then
io.write("lpeg not installed, markdown and man checks skipped\n")
end
end
h.load(script)
local function render(filename, text)
return h.run({ filename }, { text })
end
local function render_tree(filename, query, text)
return h.run({ filename, "/r/tree/", query }, { text })
end
local out, ret = render("README", "plain <text> & more")
h.contains("no extension renders as plain text", out,
"<pre class='plaintext'>plain <text> & more</pre>")
h.equals("close answers zero", ret, 0)
out = render("notes.txt", "just text")
h.contains("an unknown extension renders as plain text", out, "<pre class='plaintext'>just text</pre>")
out = render(nil, "no name at all")
h.contains("a missing filename renders as plain text", out,
"<pre class='plaintext'>no name at all</pre>")
out = render("README", "before\0after\0")
h.contains("a nul byte shows as a replacement character", out,
"<pre class='plaintext'>before\239\191\189after\239\191\189</pre>")
out = h.run({ "README" }, { "two ", "writes" })
h.contains("writes are joined before rendering", out, "two writes")
if mode == "nolpeg" then
out = render("README.md", "# Title")
h.contains("markdown without lpeg falls back to plain text", out, "<pre class='plaintext'># Title</pre>")
out = render("page.1", ".SH NAME")
h.contains("man without lpeg falls back to plain text", out, "<pre class='plaintext'>.SH NAME</pre>")
h.finish()
end
if not has_lpeg then
h.finish()
end
out = render("README.md", "# Title")
h.contains("a heading renders inside the wrapper", out, "<div class='markdown'><h1 id='title'>Title</h1></div>")
out = render("README.md", "## Sub ##")
h.contains("trailing hashes are stripped from a heading", out, "<h2 id='sub'>Sub</h2>")
out = render("README.md", "# C#")
h.contains("a hash ending a word stays in the heading", out, "<h1 id='c'>C#</h1>")
out = render("README.md", "## The `run` [Command](x), Again!")
h.contains("an anchor is the heading's text in lower case", out, "<h2 id='the-run-command-again'>")
out = render("README.md", "# Twice\n# Twice\n# twice")
h.contains("a repeated anchor is counted", out, "<h1 id='twice'>Twice</h1><h1 id='twice-1'>Twice</h1><h1 id='twice-2'>")
out = render("README.md", "# About")
h.contains("no heading takes an id of the page", out, "<h1 id='about-1'>")
out = render("README.md", "# ???")
h.contains("a heading of punctuation gets no anchor", out, "<h1>???</h1>")
out = render("README.MD", "# Upper")
h.contains("the extension matches whatever its case", out, "<h1 id='upper'>Upper</h1>")
out = render("readme.markdown", "# Long")
h.contains("the long markdown extension dispatches too", out, "<h1 id='long'>Long</h1>")
out = render("README.md", "line one\nline two")
h.contains("the lines of a paragraph run together", out, "<p>line one\nline two</p>")
out = render("README.md", "first para\r\nsecond line")
h.contains("crlf line endings are normalised", out, "<p>first para\nsecond line</p>")
out = render("README.md", "one \ntwo\\\nthree")
h.contains("two trailing spaces break the line", out, "<p>one<br>\ntwo")
h.contains("a trailing backslash breaks the line", out, "two<br>\nthree</p>")
out = render("README.md", "spaces \n\nslash\\")
h.contains("the last line takes no break", out, "<p>spaces</p><p>slash\\</p>")
out = render("README.md", "a **bold** and *leaning* word")
h.contains("double stars embolden", out, "<strong>bold</strong>")
h.contains("single stars lean", out, "<em>leaning</em>")
out = render("README.md", "an __up__ and _down_ word")
h.contains("double underscores embolden", out, "<strong>up</strong>")
h.contains("single underscores lean", out, "<em>down</em>")
out = render("README.md", "snake_case_name and _lean_ and foo__bar__")
h.contains("underscores inside words stay literal", out, "<p>snake_case_name and <em>lean</em> and foo__bar__</p>")
out = render("README.md", "_a_b_ and __c_d__")
h.contains("emphasis spans an underscore inside a word", out, "<p><em>a_b</em> and <strong>c_d</strong></p>")
out = render("README.md", "\\*not leaning\\* and \\# and \\q")
h.contains("a backslash makes punctuation literal", out, "<p>*not leaning* and # and \\q</p>")
out = render("README.md", "\\# not a heading")
h.contains("an escaped hash starts no heading", out, "<p># not a heading</p>")
out = render("README.md", "`a\\*b`")
h.contains("code keeps its backslashes", out, "<code>a\\*b</code>")
out = render("README.md", "© © © &")
h.contains("character references pass through", out, "<p>© © © &</p>")
out = render("README.md", "AT&T & &#xZZ; �")
h.contains("a bare ampersand is escaped", out, "<p>AT&T & &#xZZ; &#12345678;</p>")
out = render("README.md", "`&`")
h.contains("code shows a reference as written", out, "<code>&amp;</code>")
out = render("README.md", "run `x < y` here")
h.contains("inline code is escaped", out, "<code>x < y</code>")
out = render("README.md", "raw <script>alert(1)</script> here")
h.contains("markup in text reaches the page escaped", out, "<script>alert(1)</script>")
h.excludes("no live tag slips through", out, "<script>")
out = render("README.md", "[docs](https://example.com/docs)")
h.contains("an https link renders as an anchor", out, "<a href='https://example.com/docs'>docs</a>")
out = render("README.md", "[docs](docs/page.html)")
h.contains("with no tree a relative link stays as written", out, "<a href='docs/page.html'>docs</a>")
out = render_tree("README.md", "", "[road](ROADMAP.md)")
h.contains("a relative link leads into the tree", out, "<a href='/r/tree/ROADMAP.md'>road</a>")
out = render_tree("docs/README.md", "", "[a](guide.md#setup) [b](../NEWS) [c](/src/x.c)")
h.contains("a link resolves against the readme's directory", out, "href='/r/tree/docs/guide.md#setup'")
h.contains("dot segments fold", out, "href='/r/tree/NEWS'")
h.contains("a leading slash names the root", out, "href='/r/tree/src/x.c'")
out = render_tree("docs/README.md", "", "[up](../../../etc/passwd)")
h.contains("no link climbs above the root", out, "href='/r/tree/etc/passwd'")
out = render_tree("README.md", "?h=next", "[a](NEWS#top)")
h.contains("the ref's query comes before the fragment", out, "href='/r/tree/NEWS?h=next#top'")
out = render_tree("README.md", "&h=next", "[a](NEWS)")
h.contains("the query is escaped for the attribute", out, "href='/r/tree/NEWS&h=next'")
out = render_tree("README.md", "", "[a](my%20file.md) [b](a&b.md) [c](f.md?plain=1)")
h.contains("an encoded path is encoded only once", out, "href='/r/tree/my%20file.md'")
h.contains("a path is escaped for the url", out, "href='/r/tree/a%26b.md'")
h.contains("a query in a link is dropped", out, "href='/r/tree/f.md'")
out = render_tree("README.md", "", "[a](x%00y)")
h.contains("an encoded nul stays encoded", out, "href='/r/tree/x%2500y'")
out = render_tree("README.md", "", "[a](#usage) [b](https://example.com/x)")
h.contains("a fragment stays on the page", out, "href='#usage'")
h.contains("an absolute link is left alone", out, "href='https://example.com/x'")
out = render("README.md", "[mail](mailto:a@example.com)")
h.contains("a mailto link renders as an anchor", out, "<a href='mailto:a@example.com'>mail</a>")
out = render("README.md", "see <https://example.com/a?b=1> or <me@example.com>")
h.contains("a url in angle brackets links", out, "<a href='https://example.com/a?b=1'>https://example.com/a?b=1</a>")
h.contains("an address in angle brackets links", out, "<a href='mailto:me@example.com'>me@example.com</a>")
out = render("README.md", "<javascript:alert(1)> and <b>")
h.excludes("an unsafe autolink renders no anchor", out, "<a ")
h.contains("other angle brackets stay text", out, "<b>")
out = render("README.md", "[bad](javascript:alert(1))")
h.excludes("a javascript link renders no anchor", out, "<a ")
h.contains("its text still reaches the page", out, "bad")
out = render("README.md", "[bad](java\nscript:alert(1))")
h.excludes("a split scheme is still caught", out, "<a ")
out = render("README.md", "[bad](//evil.example/x)")
h.excludes("a scheme relative link renders no anchor", out, "<a ")
out = render("README.md", "[q](https://example.com/a'b)")
h.contains("a quote in a link target is escaped", out, "href='https://example.com/a'b'")
out = render("README.md", "an  image")
h.contains("an image is left out", out, "<p>an image</p>")
out = render("README.md", "a [](https://ci.example/) badge")
h.contains("a badge is left out with its link", out, "<p>a badge</p>")
out = render("README.md", "[](y)\n\n\ntext")
h.contains("a paragraph of badges leaves nothing behind", out, "<div class='markdown'><p>text</p></div>")
out = render("README.md", "```lua\nlocal x = 1 < 2\n```\nafter")
h.contains("a fenced block carries its language", out,
"<pre><code data-lang='lua'>local x = 1 < 2\n</code></pre>")
h.contains("text after the fence renders on", out, "<p>after</p>")
out = render("README.md", "~~~\n**not bold**\n~~~")
h.contains("no inline parsing happens inside a fence", out, "<code>**not bold**\n</code>")
out = render("README.md", "---")
h.contains("a rule of dashes renders a break", out, "<hr>")
out = render("README.md", "- - -")
h.contains("a spaced rule renders a break too", out, "<hr>")
out = render("README.md", "> quoted words")
h.contains("a quote renders as a blockquote", out, "<blockquote><p>quoted words</p></blockquote>")
out = render("README.md", "> > deep")
h.contains("quotes nest", out, "<blockquote><blockquote><p>deep</p></blockquote></blockquote>")
out = render("README.md", string.rep("> ", 30) .. "x")
local _, quote_count = out:gsub("<blockquote>", "")
h.equals("quote nesting stops at the ceiling", quote_count, 24)
h.contains("the innermost text still renders", out, "x")
out = render("README.md", string.rep("*a", 200))
h.check("an emphasis bomb still renders something", #out > 0)
local started = os.clock()
render("README.md", "# a" .. string.rep(" ", 3000) .. "b")
render("README.md", string.rep("# a\n", 50000))
render("README.md", "| a | b |\n|---|---|\n| x" .. string.rep(" ", 40000) .. "y | z |")
h.check("a long run of spaces is trimmed quickly", os.clock() - started < 10)
started = os.clock()
render("page.1", string.rep("\\[", 100000))
render("page.1", string.rep("\\f[", 100000))
render("page.1", string.rep("\\h'", 100000))
render("page.1", string.rep("\\s[", 100000))
h.check("a line of unclosed man escapes renders quickly", os.clock() - started < 10)
out = render("README.md", "Title\n=====\n\nBody")
h.contains("equals signs under a line make a heading", out, "<h1 id='title'>Title</h1><p>Body</p>")
out = render("README.md", "Two\nlines\n---\nafter")
h.contains("dashes under a paragraph make a heading of it all", out,
"<h2 id='two-lines'>Two lines</h2><p>after</p>")
out = render("README.md", "text\n***\nmore")
h.contains("a rule ends the paragraph above it", out, "<p>text</p><hr><p>more</p>")
out = render("README.md", "Para\n\n code line\n\tnext <line>\n\nafter")
h.contains("an indented block renders as code", out,
"<p>Para</p><pre><code>code line\nnext <line>\n</code></pre><p>after</p>")
out = render("README.md", "- item\n\n more about it")
h.excludes("an indented paragraph after a list stays prose", out, "<pre>")
out = render("README.md", "[a](\\\\evil.example/x) [b](\\/evil.example/x)")
h.excludes("a scheme relative link spelt with backslashes renders no anchor", out, "<a ")
out = render("README.md", "| a | b |\n| --- | --- |\n| 1 | 2 |")
h.contains("a pipe table renders its head", out, "<thead><tr><th>a</th><th>b</th></tr></thead>")
h.contains("a pipe table renders its body", out, "<tbody><tr><td>1</td><td>2</td></tr></tbody>")
out = render("README.md", "Pick one:\n| a | b |\n|---|---|\n| 1 | 2 |")
h.contains("a table cuts a paragraph short", out, "<p>Pick one:</p><table><thead><tr><th>a</th>")
out = render("README.md", "a | b in prose")
h.excludes("a pipe without a delimiter row stays prose", out, "<table")
out = render("README.md", "| a | b |\n|---|---|\n| x \\| y | `p \\| q` |")
h.contains("an escaped pipe stays in its cell", out, "<td>x | y</td><td><code>p | q</code></td>")
out = render("README.md", "| a | b |\n|---|---|\n| x \\\\| y |")
h.contains("an escaped backslash still lets the pipe divide", out, "<td>x \\</td><td>y</td>")
out = render("README.md", "- first\n- second")
h.contains("dashes render an unordered list", out, "<ul><li>first</li><li>second</li></ul>")
out = render("README.md", "1. first\n2. second")
h.contains("numbers render an ordered list", out, "<ol><li>first</li><li>second</li></ol>")
out = render("README.md", "- one\n wrapped\n- two")
h.contains("a wrapped item stays one item", out, "<ul><li>one\nwrapped</li><li>two</li></ul>")
out = render("README.md", "1. step\nlazy line\n2. next")
h.contains("an unindented line carries the item on", out, "<ol><li>step\nlazy line</li><li>next</li></ol>")
out = render("README.md", "- item\n---")
h.contains("a rule still ends the list", out, "<ul><li>item</li></ul><hr>")
out = render("README.md", "- plain\n1. numbered")
h.contains("a change of marker splits the list", out, "</ul><ol>")
out = render("README.md", "- a **bold** item")
h.contains("list items parse their text", out, "<li>a <strong>bold</strong> item</li>")
out = render("README.md", string.rep("a", 512 * 1024 + 1))
h.contains("an oversized readme renders as plain text", out, "<pre class='plaintext'>")
out = render("page.1", ".SH NAME")
h.contains("a section heading renders", out, "<div class='markdown manpage'><h2 id='name'>NAME</h2></div>")
out = render("page.man", '.SH "TWO WORDS"')
h.contains("a quoted heading argument is unquoted", out, "<h2 id='two-words'>TWO WORDS</h2>")
out = render("page.3", ".SS Details")
h.contains("a subsection renders one level down", out, "<h3 id='details'>Details</h3>")
out = render("page.1", ".B bold words\nplain after")
h.contains("the bold macro renders strong", out, "<p><strong>bold words</strong>\nplain after</p>")
out = render("page.1", "a \\fBbold\\fR c")
h.contains("font escapes carry across a run", out, "<p>a <strong>bold</strong> c</p>")
out = render("page.1", "x \\(em y \\- z \\(bu \\[co]")
h.contains("character escapes are translated", out, "<p>x — y - z • ©</p>")
out = render("page.1", "\\*(lqquoted\\*(rq\\*R")
h.contains("predefined strings are expanded", out, "<p>“quoted”®</p>")
out = render("page.1", "a\\%b c\\:d \\s-1SMALL\\s0 text \\\" a comment")
h.contains("escapes that only steer the layout draw nothing", out, "<p>ab cd SMALL text </p>")
out = render("page.1", ".nf\ncode <x>\n.fi\nafter")
h.contains("a no fill block renders as code", out, "<pre><code>code <x>\n</code></pre>")
h.contains("filling resumes after it", out, "<p>after</p>")
out = render("page.1", ".nf\n\\fBgit\\fR \\-\\-version\n.fi")
h.contains("a no fill block shows the text of its escapes", out, "<pre><code>git --version\n</code></pre>")
out = render("page.1", ".EX\nline <one>\n.EE")
h.contains("an example block renders as code", out, "<pre><code>line <one>\n</code></pre>")
out = render("page.1", ".TP\n.B \\-v\nBe verbose.")
h.contains("a tag takes a line of its own", out, "<p><strong>-v</strong><br>\nBe verbose.</p>")
out = render("page.1", ".IP \\(bu 2\nAn item.\n.IP \\-v 4\nA flag.")
h.contains("a bullet leads its item", out, "<p>• \nAn item.</p>")
h.contains("a longer tag takes a line of its own", out, "<p>-v<br>\nA flag.</p>")
out = render("page.1", ".BR ls (1),\n.IR file .txt")
h.contains("alternating macros switch fonts between arguments", out,
"<strong>ls</strong>(1),\n<em>file</em>.txt")
out = render("page.1", '.B "two words" three')
h.contains("a quoted argument keeps its blank", out, "<strong>two words three</strong>")
out = render("page.1", "one\n.br\ntwo\n.\nthree")
h.contains("a break request breaks the line", out, "<p>one<br>\ntwo\nthree</p>")
out = render("page.1", table.concat({
".de Vb", ".nf", "..", ".de IX", "..", ".if n .ds Aq '", ".ie \\n(.g \\{\\",
". nr x 1", ".\\}", ".SH NAME", ".IX Header \"NAME\"", ".Vb 2", "\\& code", ".Ve", "after",
}, "\n"))
h.excludes("a page's own macro is not drawn", out, "Header")
h.excludes("a roff request is not drawn", out, "Aq")
h.contains("the heading after the preamble renders", out, "<h2 id='name'>NAME</h2>")
h.contains("pod2man's verbatim macros render code", out, "<pre><code> code\n</code></pre>")
h.contains("text after the definitions renders", out, "<p>after</p>")
out = render("page.1", ".if n \\\n\\{\\\n.ds x y\n.\\}\nafter")
h.contains("a conditional block's requests and braces draw nothing", out,
"<div class='markdown manpage'><p>after</p></div>")
out = render("page.1", "The \\m[blue]\\fBManual\\fR\\m[]\\s-2\\u[1]\\d\\s+2 here")
h.contains("DocBook's link colours and footnote marks draw nothing", out,
"<p>The <strong>Manual</strong>[1] here</p>")
out = render("page.1", ".de x\n.B hidden\n.. \nshown")
h.contains("a definition ends at two dots with blanks after them", out,
"<div class='markdown manpage'><p>shown</p></div>")
out = render("page.1", ".TP\n.PD 0\n.B \\-a\nall\n.PD\nDon\\*(Aqt")
h.excludes("a spacing macro draws nothing", out, ">0")
h.contains("DocBook's apostrophe string expands", out, "Don't")
out = render("page.1", '.\\" a comment\nvisible')
h.excludes("a roff comment is dropped", out, "comment")
h.contains("the line after it renders", out, "visible")
out = render("page.1", "first\n\nsecond")
h.contains("a blank line splits paragraphs", out, "<p>first</p><p>second</p>")
out = render("page.1", ".TH title 1\nbody")
h.excludes("the title macro renders nothing itself", out, "title 1")
h.contains("the body after it renders", out, "<p>body</p>")
h.finish()
|