Debian Codegraph @

texmf-dist/tex/luatex/markdown/markdown-parser.lua

raw symbols SCIP
1 --
2 -- Copyright (C) 2009-2016 John MacFarlane, Hans Hagen
3 --
4 -- Permission is hereby granted, free of charge, to any person obtaining
5 -- a copy of this software and associated documentation files (the
6 -- "Software"), to deal in the Software without restriction, including
7 -- without limitation the rights to use, copy, modify, merge, publish,
8 -- distribute, sublicense, and/or sell copies of the Software, and to
9 -- permit persons to whom the Software is furnished to do so, subject to
10 -- the following conditions:
11 --
12 -- The above copyright notice and this permission notice shall be
13 -- included in all copies or substantial portions of the Software.
14 --
15 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 -- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 -- MERCHANTABILITY, FITNESS FOR PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 -- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 -- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 -- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 -- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 --
23 -- Copyright (C) 2016-2026 Vít Starý Novotný, Andrej Genčur
24 --
25 -- This work may be distributed and/or modified under the
26 -- conditions of the LaTeX Project Public License, either version 1.3
27 -- of this license or (at your option) any later version.
28 -- The latest version of this license is in
29 --
30 -- http://www.latex-project.org/lppl.txt
31 --
32 -- and version 1.3 or later is part of all distributions of LaTeX
33 -- version 2005/12/01 or later.
34 --
35 -- This work has the LPPL maintenance status `maintained'.
36 -- The Current Maintainer of this work is Vít Starý Novotný.
37 --
38 -- Send bug reports, requests for additions and questions
39 -- either to the GitHub issue tracker at
40 --
41 -- https://github.com/witiko/markdown/issues
42 --
43 -- or to the e-mail address <[email protected]>.
44 --
45 -- MODIFICATION ADVICE:
46 --
47 -- If you want to customize this file, it is best to make a copy of
48 -- the source file(s) from which it was produced. Use a different
49 -- name for your copy(ies) and modify the copy(ies); this will ensure
50 -- that your modifications do not get overwritten when you install a
51 -- new release of the standard system. You should also ensure that
52 -- your modified source file does not generate any modified file with
53 -- the same name as a standard file.
54 --
55 -- You will also need to produce your own, suitably named, .ins file to
56 -- control the generation of files from your source file; this file
57 -- should contain your own preambles for the files it generates, not
58 -- those in the standard .ins files.
59 --
60 local metadata = {
61 version = "3.15.0-0-g58d7bf4d",
62 comment = "A module for the conversion from markdown "
63 .. "to plain TeX",
64 author = "John MacFarlane, Hans Hagen, Vít Starý Novotný, "
65 .. "Andrej Genčur",
66 copyright = {"2009-2016 John MacFarlane, Hans Hagen",
67 "2016-2026 Vít Starý Novotný, Andrej Genčur"},
68 license = "LPPL 1.3c"
69 }
70
71 if not modules then modules = { } end
72 modules['markdown'] = metadata
73 local lpeg = require("lpeg")
74 local md5 = require("md5")
75 if resolvers == nil then
76 (function()
77 local should_initialize = package.loaded.kpse == nil
78 or tex.initialize ~= nil
79 kpse = require("kpse")
80 if should_initialize then
81 kpse.set_program_name("luatex")
82 end
83 end)()
84 end
85 local M = {metadata = metadata}
86 local walkable_syntax = {
87 Block = {
88 "Blockquote",
89 "Verbatim",
90 "ThematicBreak",
91 "BulletList",
92 "OrderedList",
93 "DisplayHtml",
94 "Heading",
95 },
96 BlockOrParagraph = {
97 "Block",
98 "Paragraph",
99 "Plain",
100 },
101 Inline = {
102 "Str",
103 "Space",
104 "Endline",
105 "EndlineBreak",
106 "LinkAndEmph",
107 "Code",
108 "AutoLinkUrl",
109 "AutoLinkEmail",
110 "AutoLinkRelativeReference",
111 "InlineHtml",
112 "HtmlEntity",
113 "EscapedChar",
114 "Smart",
115 "Symbol",
116 },
117 }
118 local defaultOptions = {}
119 local experimentalOptions = {}
120 setmetatable(experimentalOptions, { __index = function (_, key)
121 return defaultOptions[key] end })
122 defaultOptions.eagerCache = true
123 defaultOptions.experimental = false
124 defaultOptions.singletonCache = true
125 local singletonCache = {
126 convert = nil,
127 options = nil,
128 }
129 defaultOptions.unicodeNormalization = true
130 defaultOptions.unicodeNormalizationForm = "nfc"
131 defaultOptions.cacheDir = "."
132 defaultOptions.contentBlocksLanguageMap = "markdown-languages.json"
133 defaultOptions.debugExtensionsFileName = "debug-extensions.json"
134 defaultOptions.frozenCacheFileName = "frozenCache.tex"
135 defaultOptions.acronyms = {}
136 defaultOptions.autoIdentifiers = false
137 defaultOptions.blankBeforeBlockquote = false
138 defaultOptions.blankBeforeCodeFence = false
139 defaultOptions.blankBeforeDivFence = false
140 defaultOptions.blankBeforeHeading = false
141 defaultOptions.blankBeforeHtmlBlock = false
142 defaultOptions.blankBeforeList = false
143 defaultOptions.bracketedSpans = false
144 defaultOptions.breakableBlockquotes = true
145 defaultOptions.citationNbsps = true
146 defaultOptions.citations = false
147 defaultOptions.codeSpans = true
148 defaultOptions.contentBlocks = false
149 defaultOptions.contentLevel = "block"
150 defaultOptions.debugExtensions = false
151 defaultOptions.definitionLists = false
152 defaultOptions.ensureJekyllData = false
153 defaultOptions.expectJekyllData = false
154 metadata.user_extension_api_version = 2
155 metadata.grammar_version = 4
156 defaultOptions.extensions = {}
157 defaultOptions.fancyLists = false
158 defaultOptions.fencedCode = true
159 defaultOptions.fencedCodeAttributes = false
160 defaultOptions.fencedDivs = false
161 defaultOptions.finalizeCache = false
162 defaultOptions.frozenCacheCounter = 0
163 defaultOptions.gfmAutoIdentifiers = false
164 defaultOptions.hashEnumerators = false
165 defaultOptions.headerAttributes = false
166 defaultOptions.html = true
167 defaultOptions.htmlOutput = "basic"
168 defaultOptions.htmlOverLinks = false
169 experimentalOptions.htmlOverLinks = true
170 defaultOptions.hybrid = false
171 defaultOptions.inlineCodeAttributes = false
172 defaultOptions.inlineNotes = false
173 defaultOptions.jekyllData = false
174 defaultOptions.linkAttributes = false
175 defaultOptions.lineBlocks = false
176 defaultOptions.mark = false
177 defaultOptions.notes = false
178 defaultOptions.pipeTables = false
179 defaultOptions.preserveTabs = true
180 defaultOptions.rawAttribute = false
181 defaultOptions.relativeReferences = false
182 defaultOptions.shiftHeadings = 0
183 defaultOptions.slice = "^ $"
184 defaultOptions.smartEllipses = false
185 defaultOptions.startNumber = true
186 defaultOptions.strikeThrough = false
187 defaultOptions.stripIndent = false
188 defaultOptions.subscripts = false
189 defaultOptions.superscripts = false
190 defaultOptions.tableAttributes = false
191 defaultOptions.tableCaptions = false
192 defaultOptions.taskLists = false
193 defaultOptions.texComments = false
194 defaultOptions.texMathDollars = false
195 defaultOptions.texMathDoubleBackslash = false
196 defaultOptions.texMathSingleBackslash = false
197 defaultOptions.tightLists = true
198 defaultOptions.underscores = true
199 local upper, format, length =
200 string.upper, string.format, string.len
201 local P, R, S, V, C, Cg, Cb, Cmt, Cc, Ct, B, Cs, Cp, any =
202 lpeg.P, lpeg.R, lpeg.S, lpeg.V, lpeg.C, lpeg.Cg, lpeg.Cb,
203 lpeg.Cmt, lpeg.Cc, lpeg.Ct, lpeg.B, lpeg.Cs, lpeg.Cp, lpeg.P(1)
204 local early_warnings = {}
205 local unicode_data = require("markdown-unicode-data")
206 if metadata.version ~= unicode_data.metadata.version then
207 table.insert(
208 early_warnings,
209 "markdown.lua " .. metadata.version .. " used with " ..
210 "markdown-unicode-data.lua " .. unicode_data.metadata.version .. "."
211 )
212 end
213 local function depth_first_search(node, path, visit, leave)
214 assert(node._type == "intermediate")
215 visit(node, path)
216 for label, child in pairs(node) do
217 if label == "_type" then
218 goto continue
219 end
220 if child._type ~= "intermediate" then
221 goto continue
222 end
223 depth_first_search(child, path .. label, visit, leave)
224 ::continue::
225 end
226 for label, child in pairs(node) do
227 if label == "_type" then
228 goto continue
229 end
230 if child._type ~= "leaf" then
231 goto continue
232 end
233 visit(child, path)
234 ::continue::
235 end
236 leave(node, path)
237 end
238 local util = {}
239 function util.err(msg, exit_code)
240 io.stderr:write("markdown.lua: " .. msg .. "\n")
241 os.exit(exit_code or 1)
242 end
243 function util.cache(dir, string, salt, transform, suffix)
244 local digest = md5.sumhexa(string .. (salt or ""))
245 local name = util.pathname(dir, digest .. suffix)
246 local file = io.open(name, "r")
247 local result = nil
248 if file == nil then -- If no cache entry exists, create a new one.
249 file = assert(io.open(name, "w"),
250 [[Could not open file "]] .. name .. [[" for writing]])
251 result = string
252 if transform ~= nil then
253 result = transform(result)
254 end
255 assert(file:write(result))
256 assert(file:close())
257 end
258 return name, result
259 end
260 function util.cache_verbatim(dir, string)
261 local name = util.cache(dir, string, nil, nil, ".verbatim")
262 return name
263 end
264 function util.table_copy(t)
265 local u = { }
266 for k, v in pairs(t) do u[k] = v end
267 return setmetatable(u, getmetatable(t))
268 end
269 function util.encode_json_string(s)
270 s = s:gsub([[\]], [[\\]])
271 s = s:gsub([["]], [[\"]])
272 return [["]] .. s .. [["]]
273 end
274 function util.expand_tabs_in_line(s, tabstop)
275 local tab = tabstop or 4
276 local corr = 0
277 return (s:gsub("()\t", function(p)
278 local sp = tab - (p - 1 + corr) % tab
279 corr = corr - 1 + sp
280 return string.rep(" ", sp)
281 end))
282 end
283 function util.walk(t, f)
284 local typ = type(t)
285 if typ == "string" then
286 f(t)
287 elseif typ == "table" then
288 local i = 1
289 local n
290 n = t[i]
291 while n do
292 util.walk(n, f)
293 i = i + 1
294 n = t[i]
295 end
296 elseif typ == "function" then
297 local ok, val = pcall(t)
298 if ok then
299 util.walk(val,f)
300 end
301 else
302 f(tostring(t))
303 end
304 end
305 function util.flatten(ary)
306 local new = {}
307 for _,v in ipairs(ary) do
308 if type(v) == "table" then
309 for _,w in ipairs(util.flatten(v)) do
310 new[#new + 1] = w
311 end
312 else
313 new[#new + 1] = v
314 end
315 end
316 return new
317 end
318 function util.rope_to_string(rope)
319 local buffer = {}
320 util.walk(rope, function(x) buffer[#buffer + 1] = x end)
321 return table.concat(buffer)
322 end
323 function util.rope_last(rope)
324 if #rope == 0 then
325 return nil
326 else
327 local l = rope[#rope]
328 if type(l) == "table" then
329 return util.rope_last(l)
330 else
331 return l
332 end
333 end
334 end
335 function util.intersperse(ary, x)
336 local new = {}
337 local l = #ary
338 for i,v in ipairs(ary) do
339 local n = #new
340 new[n + 1] = v
341 if i ~= l then
342 new[n + 2] = x
343 end
344 end
345 return new
346 end
347 function util.map(ary, f)
348 local new = {}
349 for i,v in ipairs(ary) do
350 new[i] = f(v)
351 end
352 return new
353 end
354 function util.escaper(char_escapes, string_escapes)
355 local char_escapes_list = ""
356 for i,_ in pairs(char_escapes) do
357 char_escapes_list = char_escapes_list .. i
358 end
359 local escapable = S(char_escapes_list) / char_escapes
360 if string_escapes then
361 for k,v in pairs(string_escapes) do
362 escapable = P(k) / v + escapable
363 end
364 end
365 local escape_string = Cs((escapable + any)^0)
366 return function(s)
367 return lpeg.match(escape_string, s)
368 end
369 end
370 function util.pathname(dir, file)
371 if #dir == 0 then
372 return file
373 else
374 return dir .. "/" .. file
375 end
376 end
377 function util.salt(options)
378 local opt_string = {}
379 for k, _ in pairs(defaultOptions) do
380 local v = options[k]
381 if type(v) == "table" then
382 for _, i in ipairs(v) do
383 opt_string[#opt_string+1] = k .. "=" .. tostring(i)
384 end
385 elseif k ~= "cacheDir" then
386 opt_string[#opt_string+1] = k .. "=" .. tostring(v)
387 end
388 end
389 table.sort(opt_string)
390 local salt = table.concat(opt_string, ",")
391 .. "," .. metadata.version
392 return salt
393 end
394 util.warning = (function()
395 local function warning(s)
396 io.stderr:write("Warning: " .. s .. "\n")
397 end
398 for _, message in ipairs(early_warnings) do
399 warning(message)
400 end
401 return warning
402 end)()
403 util.casefold = (function()
404 local fail, any = P(false), P(1)
405 local eof = -any
406 local fold_character = fail
407 for n = 1, 4 do
408 fold_character
409 = fold_character
410 + unicode_data.casefold_mapping[n]
411 end
412 fold_character
413 = fold_character
414 + C(any)
415 local fold_string = Ct(fold_character^0) * eof
416 return function(s, form)
417 local result = table.concat(lpeg.match(fold_string, s))
418 assert(result ~= nil)
419 if form == "nfd" or form == "nfkd" then
420 result = util.normalize(result, form)
421 result = table.concat(lpeg.match(fold_string, result))
422 assert(result ~= nil)
423 result = util.normalize(result, form)
424 end
425 return result
426 end
427 end)()
428 util.canonically_order = (function()
429 local fail, any = P(false), P(1)
430 local eof = -any
431 local cont = R("\128\191")
432 local utf8_character
433 = R("\0\127")
434 + R("\194\223") * cont
435 + R("\224\239") * cont * cont
436 + R("\240\244") * cont * cont * cont
437 local classify_character = fail
438 for n = 1, 4 do
439 classify_character
440 = classify_character
441 + unicode_data.ccc[n]
442 end
443 classify_character
444 = classify_character
445 + utf8_character * Cc(0)
446 local classify_string = Ct(classify_character^0) * eof
447 return function(s)
448 local s_len = utf8.len(s)
449 if s == false or s_len <= 1 then
450 return s
451 end
452 local classes = lpeg.match(classify_string, s)
453 if classes == nil then
454 return s
455 end
456 assert(#classes == s_len)
457 local non_starter_ranges = {}
458 local first_non_starter, last_non_starter = nil, nil
459 for i = 1, #classes do
460 if first_non_starter == nil then
461 if classes[i] ~= 0 then
462 first_non_starter, last_non_starter = i, i
463 end
464 else
465 if classes[i] == 0 then
466 table.insert(
467 non_starter_ranges,
468 {first_non_starter, last_non_starter}
469 )
470 first_non_starter, last_non_starter = nil, nil
471 else
472 last_non_starter = i
473 end
474 end
475 end
476 if first_non_starter ~= nil then
477 table.insert(
478 non_starter_ranges,
479 {first_non_starter, last_non_starter}
480 )
481 end
482 if #non_starter_ranges == 0 then
483 return s
484 end
485 local max_range_length = 0
486 for _, range in ipairs(non_starter_ranges) do
487 local range_start, range_end = table.unpack(range)
488 local range_length = range_end - range_start + 1
489 if range_length > max_range_length then
490 max_range_length = range_length
491 end
492 end
493 if max_range_length <= 1 then
494 return s
495 end
496 local buffer = {}
497 for _, code in utf8.codes(s) do
498 local char = utf8.char(code)
499 table.insert(buffer, char)
500 end
501 assert(#buffer == s_len)
502 for _, range in ipairs(non_starter_ranges) do
503 local range_start, range_end = table.unpack(range)
504 local range_length = range_end - range_start + 1
505 for _ = 1, range_length - 1 do
506 local swapped = false
507 for i = range_start, range_end - 1 do
508 local j = i + 1
509 if classes[i] > classes[j] then
510 classes[i], classes[j] = classes[j], classes[i]
511 buffer[i], buffer[j] = buffer[j], buffer[i]
512 swapped = true
513 end
514 end
515 if not swapped then
516 break
517 end
518 end
519 end
520 return table.concat(buffer, "")
521 end
522 end)()
523 util.decompose = (function()
524 local fail, any = P(false), P(1)
525 local eof = -any
526 local decomposition_types = {"canonical", "compatibility"}
527 local decompose_character = {}
528 for _, decomposition_type in ipairs(decomposition_types) do
529 decompose_character[decomposition_type] = fail
530 for n = 1, 4 do
531 decompose_character[decomposition_type]
532 = decompose_character[decomposition_type]
533 + unicode_data.decomposition_mapping[decomposition_type][n]
534 end
535 decompose_character[decomposition_type]
536 = decompose_character[decomposition_type]
537 + C(any)
538 end
539 local hangul = unicode_data.decomposition_mapping.hangul
540 decompose_character.hangul = {}
541 for syllable_type, _ in pairs(hangul) do
542 decompose_character.hangul[syllable_type] = fail
543 for n = 1, 4 do
544 decompose_character.hangul[syllable_type]
545 = decompose_character.hangul[syllable_type]
546 + hangul[syllable_type][n]
547 end
548 decompose_character.hangul[syllable_type]
549 = decompose_character.hangul[syllable_type]
550 + C(any)
551 end
552 local decompose_string = {}
553 for _, decomposition_type in ipairs(decomposition_types) do
554 decompose_string[decomposition_type]
555 = Ct(decompose_character[decomposition_type]^0) * eof
556 end
557 decompose_string.hangul = {}
558 for syllable_type, _ in pairs(hangul) do
559 decompose_string.hangul[syllable_type]
560 = Ct(decompose_character.hangul[syllable_type]^0) * eof
561 end
562 local function _decompose(s, parser)
563 assert(s ~= nil)
564 local result = table.concat(lpeg.match(parser, s), "")
565 assert(result ~= nil)
566 return result
567 end
568 return function(s, decomposition_type)
569 assert(
570 decomposition_type == "canonical"
571 or decomposition_type == "compatibility"
572 )
573 local prev_s
574 local next_s = s
575 repeat
576 prev_s = next_s
577 local function decompose(...) next_s = _decompose(next_s, ...) end
578 decompose(decompose_string.canonical)
579 if decomposition_type == "compatibility" then
580 decompose(decompose_string.compatibility)
581 end
582 decompose(decompose_string.hangul.LVT)
583 decompose(decompose_string.hangul.LV)
584 until prev_s == next_s
585 return util.canonically_order(next_s)
586 end
587 end)()
588 util.compose = (function()
589 local fail, any = P(false), P(1)
590 local eof = -any
591 local cont = R("\128\191")
592 local utf8_character
593 = R("\0\127")
594 + R("\194\223") * cont
595 + R("\224\239") * cont * cont
596 + R("\240\244") * cont * cont * cont
597 local classify_character = fail
598 for n = 1, 4 do
599 classify_character
600 = classify_character
601 + unicode_data.ccc[n]
602 end
603 classify_character
604 = classify_character
605 + utf8_character * Cc(0)
606 local classify_string = Ct(classify_character^0) * eof
607 local compose_characters = fail
608 for m = 1, 4 do
609 local starter = #unicode_data.composition_mapping.starters[m]
610 local both = fail
611 for n = 1, 4 do
612 both = (
613 both
614 + #unicode_data.composition_mapping.both[m][n]
615 * unicode_data.composition_mapping.both[m][n]
616 )
617 end
618 compose_characters = compose_characters + starter * both
619 end
620 return function(s)
621 local s_len = utf8.len(s)
622 if s == false or s_len <= 1 then
623 return s
624 end
625 local classes = lpeg.match(classify_string, s)
626 if classes == nil then
627 return s
628 end
629 assert(#classes == s_len)
630 local buffer = {}
631 for _, code in utf8.codes(s) do
632 local char = utf8.char(code)
633 table.insert(buffer, char)
634 end
635 assert(#buffer == s_len)
636 local starter = 1
637 while starter <= s_len and classes[starter] ~= 0 do
638 starter = starter + 1
639 end
640 local candidate_combining_mark = starter + 1
641 while candidate_combining_mark <= s_len do
642 local L = buffer[starter]
643 local C = buffer[candidate_combining_mark]
644 local P = lpeg.match(compose_characters, L .. C)
645 if P ~= nil then
646 buffer[starter] = P
647 buffer[candidate_combining_mark] = ""
648 else
649 if classes[candidate_combining_mark] == 0 then
650 starter = candidate_combining_mark
651 end
652 candidate_combining_mark = candidate_combining_mark + 1
653 end
654 assert(starter <= s_len)
655 end
656 for i = 1, s_len - 1 do
657 local last, ch = buffer[i], buffer[i + 1]
658 if last ~= "" and ch ~= "" then
659 local composite = unicode_data.hangul_compose(last, ch)
660 if composite ~= nil then
661 buffer[i] = ""
662 buffer[i + 1] = composite
663 end
664 end
665 end
666 return table.concat(buffer, "")
667 end
668 end)()
669 function util.normalize(s, form)
670 if form == "nfd" then
671 return util.decompose(s, "canonical")
672 elseif form == "nfkd" then
673 return util.decompose(s, "compatibility")
674 elseif form == "nfc" then
675 return util.compose(util.decompose(s, "canonical"))
676 elseif form == "nfkc" then
677 return util.compose(util.decompose(s, "compatibility"))
678 else
679 error(string.format('Unexpected normal form "%s"', form))
680 end
681 end
682 function util.find_file(filename)
683 if resolvers ~= nil then
684 return resolvers.findfile(filename)
685 else
686 return kpse.find_file(filename)
687 end
688 end
689 function util.find_files(filename)
690 if resolvers ~= nil then
691 return resolvers.findfiles(filename)
692 else
693 return {kpse.lookup(filename, {all=true})}
694 end
695 end
696 local entities = {}
697
698 local character_entities = {
699 ["Tab"] = 9,
700 ["NewLine"] = 10,
701 ["excl"] = 33,
702 ["QUOT"] = 34,
703 ["quot"] = 34,
704 ["num"] = 35,
705 ["dollar"] = 36,
706 ["percnt"] = 37,
707 ["AMP"] = 38,
708 ["amp"] = 38,
709 ["apos"] = 39,
710 ["lpar"] = 40,
711 ["rpar"] = 41,
712 ["ast"] = 42,
713 ["midast"] = 42,
714 ["plus"] = 43,
715 ["comma"] = 44,
716 ["period"] = 46,
717 ["sol"] = 47,
718 ["colon"] = 58,
719 ["semi"] = 59,
720 ["LT"] = 60,
721 ["lt"] = 60,
722 ["nvlt"] = {60, 8402},
723 ["bne"] = {61, 8421},
724 ["equals"] = 61,
725 ["GT"] = 62,
726 ["gt"] = 62,
727 ["nvgt"] = {62, 8402},
728 ["quest"] = 63,
729 ["commat"] = 64,
730 ["lbrack"] = 91,
731 ["lsqb"] = 91,
732 ["bsol"] = 92,
733 ["rbrack"] = 93,
734 ["rsqb"] = 93,
735 ["Hat"] = 94,
736 ["UnderBar"] = 95,
737 ["lowbar"] = 95,
738 ["DiacriticalGrave"] = 96,
739 ["grave"] = 96,
740 ["fjlig"] = {102, 106},
741 ["lbrace"] = 123,
742 ["lcub"] = 123,
743 ["VerticalLine"] = 124,
744 ["verbar"] = 124,
745 ["vert"] = 124,
746 ["rbrace"] = 125,
747 ["rcub"] = 125,
748 ["NonBreakingSpace"] = 160,
749 ["nbsp"] = 160,
750 ["iexcl"] = 161,
751 ["cent"] = 162,
752 ["pound"] = 163,
753 ["curren"] = 164,
754 ["yen"] = 165,
755 ["brvbar"] = 166,
756 ["sect"] = 167,
757 ["Dot"] = 168,
758 ["DoubleDot"] = 168,
759 ["die"] = 168,
760 ["uml"] = 168,
761 ["COPY"] = 169,
762 ["copy"] = 169,
763 ["ordf"] = 170,
764 ["laquo"] = 171,
765 ["not"] = 172,
766 ["shy"] = 173,
767 ["REG"] = 174,
768 ["circledR"] = 174,
769 ["reg"] = 174,
770 ["macr"] = 175,
771 ["strns"] = 175,
772 ["deg"] = 176,
773 ["PlusMinus"] = 177,
774 ["plusmn"] = 177,
775 ["pm"] = 177,
776 ["sup2"] = 178,
777 ["sup3"] = 179,
778 ["DiacriticalAcute"] = 180,
779 ["acute"] = 180,
780 ["micro"] = 181,
781 ["para"] = 182,
782 ["CenterDot"] = 183,
783 ["centerdot"] = 183,
784 ["middot"] = 183,
785 ["Cedilla"] = 184,
786 ["cedil"] = 184,
787 ["sup1"] = 185,
788 ["ordm"] = 186,
789 ["raquo"] = 187,
790 ["frac14"] = 188,
791 ["frac12"] = 189,
792 ["half"] = 189,
793 ["frac34"] = 190,
794 ["iquest"] = 191,
795 ["Agrave"] = 192,
796 ["Aacute"] = 193,
797 ["Acirc"] = 194,
798 ["Atilde"] = 195,
799 ["Auml"] = 196,
800 ["Aring"] = 197,
801 ["angst"] = 197,
802 ["AElig"] = 198,
803 ["Ccedil"] = 199,
804 ["Egrave"] = 200,
805 ["Eacute"] = 201,
806 ["Ecirc"] = 202,
807 ["Euml"] = 203,
808 ["Igrave"] = 204,
809 ["Iacute"] = 205,
810 ["Icirc"] = 206,
811 ["Iuml"] = 207,
812 ["ETH"] = 208,
813 ["Ntilde"] = 209,
814 ["Ograve"] = 210,
815 ["Oacute"] = 211,
816 ["Ocirc"] = 212,
817 ["Otilde"] = 213,
818 ["Ouml"] = 214,
819 ["times"] = 215,
820 ["Oslash"] = 216,
821 ["Ugrave"] = 217,
822 ["Uacute"] = 218,
823 ["Ucirc"] = 219,
824 ["Uuml"] = 220,
825 ["Yacute"] = 221,
826 ["THORN"] = 222,
827 ["szlig"] = 223,
828 ["agrave"] = 224,
829 ["aacute"] = 225,
830 ["acirc"] = 226,
831 ["atilde"] = 227,
832 ["auml"] = 228,
833 ["aring"] = 229,
834 ["aelig"] = 230,
835 ["ccedil"] = 231,
836 ["egrave"] = 232,
837 ["eacute"] = 233,
838 ["ecirc"] = 234,
839 ["euml"] = 235,
840 ["igrave"] = 236,
841 ["iacute"] = 237,
842 ["icirc"] = 238,
843 ["iuml"] = 239,
844 ["eth"] = 240,
845 ["ntilde"] = 241,
846 ["ograve"] = 242,
847 ["oacute"] = 243,
848 ["ocirc"] = 244,
849 ["otilde"] = 245,
850 ["ouml"] = 246,
851 ["div"] = 247,
852 ["divide"] = 247,
853 ["oslash"] = 248,
854 ["ugrave"] = 249,
855 ["uacute"] = 250,
856 ["ucirc"] = 251,
857 ["uuml"] = 252,
858 ["yacute"] = 253,
859 ["thorn"] = 254,
860 ["yuml"] = 255,
861 ["Amacr"] = 256,
862 ["amacr"] = 257,
863 ["Abreve"] = 258,
864 ["abreve"] = 259,
865 ["Aogon"] = 260,
866 ["aogon"] = 261,
867 ["Cacute"] = 262,
868 ["cacute"] = 263,
869 ["Ccirc"] = 264,
870 ["ccirc"] = 265,
871 ["Cdot"] = 266,
872 ["cdot"] = 267,
873 ["Ccaron"] = 268,
874 ["ccaron"] = 269,
875 ["Dcaron"] = 270,
876 ["dcaron"] = 271,
877 ["Dstrok"] = 272,
878 ["dstrok"] = 273,
879 ["Emacr"] = 274,
880 ["emacr"] = 275,
881 ["Edot"] = 278,
882 ["edot"] = 279,
883 ["Eogon"] = 280,
884 ["eogon"] = 281,
885 ["Ecaron"] = 282,
886 ["ecaron"] = 283,
887 ["Gcirc"] = 284,
888 ["gcirc"] = 285,
889 ["Gbreve"] = 286,
890 ["gbreve"] = 287,
891 ["Gdot"] = 288,
892 ["gdot"] = 289,
893 ["Gcedil"] = 290,
894 ["Hcirc"] = 292,
895 ["hcirc"] = 293,
896 ["Hstrok"] = 294,
897 ["hstrok"] = 295,
898 ["Itilde"] = 296,
899 ["itilde"] = 297,
900 ["Imacr"] = 298,
901 ["imacr"] = 299,
902 ["Iogon"] = 302,
903 ["iogon"] = 303,
904 ["Idot"] = 304,
905 ["imath"] = 305,
906 ["inodot"] = 305,
907 ["IJlig"] = 306,
908 ["ijlig"] = 307,
909 ["Jcirc"] = 308,
910 ["jcirc"] = 309,
911 ["Kcedil"] = 310,
912 ["kcedil"] = 311,
913 ["kgreen"] = 312,
914 ["Lacute"] = 313,
915 ["lacute"] = 314,
916 ["Lcedil"] = 315,
917 ["lcedil"] = 316,
918 ["Lcaron"] = 317,
919 ["lcaron"] = 318,
920 ["Lmidot"] = 319,
921 ["lmidot"] = 320,
922 ["Lstrok"] = 321,
923 ["lstrok"] = 322,
924 ["Nacute"] = 323,
925 ["nacute"] = 324,
926 ["Ncedil"] = 325,
927 ["ncedil"] = 326,
928 ["Ncaron"] = 327,
929 ["ncaron"] = 328,
930 ["napos"] = 329,
931 ["ENG"] = 330,
932 ["eng"] = 331,
933 ["Omacr"] = 332,
934 ["omacr"] = 333,
935 ["Odblac"] = 336,
936 ["odblac"] = 337,
937 ["OElig"] = 338,
938 ["oelig"] = 339,
939 ["Racute"] = 340,
940 ["racute"] = 341,
941 ["Rcedil"] = 342,
942 ["rcedil"] = 343,
943 ["Rcaron"] = 344,
944 ["rcaron"] = 345,
945 ["Sacute"] = 346,
946 ["sacute"] = 347,
947 ["Scirc"] = 348,
948 ["scirc"] = 349,
949 ["Scedil"] = 350,
950 ["scedil"] = 351,
951 ["Scaron"] = 352,
952 ["scaron"] = 353,
953 ["Tcedil"] = 354,
954 ["tcedil"] = 355,
955 ["Tcaron"] = 356,
956 ["tcaron"] = 357,
957 ["Tstrok"] = 358,
958 ["tstrok"] = 359,
959 ["Utilde"] = 360,
960 ["utilde"] = 361,
961 ["Umacr"] = 362,
962 ["umacr"] = 363,
963 ["Ubreve"] = 364,
964 ["ubreve"] = 365,
965 ["Uring"] = 366,
966 ["uring"] = 367,
967 ["Udblac"] = 368,
968 ["udblac"] = 369,
969 ["Uogon"] = 370,
970 ["uogon"] = 371,
971 ["Wcirc"] = 372,
972 ["wcirc"] = 373,
973 ["Ycirc"] = 374,
974 ["ycirc"] = 375,
975 ["Yuml"] = 376,
976 ["Zacute"] = 377,
977 ["zacute"] = 378,
978 ["Zdot"] = 379,
979 ["zdot"] = 380,
980 ["Zcaron"] = 381,
981 ["zcaron"] = 382,
982 ["fnof"] = 402,
983 ["imped"] = 437,
984 ["gacute"] = 501,
985 ["jmath"] = 567,
986 ["circ"] = 710,
987 ["Hacek"] = 711,
988 ["caron"] = 711,
989 ["Breve"] = 728,
990 ["breve"] = 728,
991 ["DiacriticalDot"] = 729,
992 ["dot"] = 729,
993 ["ring"] = 730,
994 ["ogon"] = 731,
995 ["DiacriticalTilde"] = 732,
996 ["tilde"] = 732,
997 ["DiacriticalDoubleAcute"] = 733,
998 ["dblac"] = 733,
999 ["DownBreve"] = 785,
1000 ["Alpha"] = 913,
1001 ["Beta"] = 914,
1002 ["Gamma"] = 915,
1003 ["Delta"] = 916,
1004 ["Epsilon"] = 917,
1005 ["Zeta"] = 918,
1006 ["Eta"] = 919,
1007 ["Theta"] = 920,
1008 ["Iota"] = 921,
1009 ["Kappa"] = 922,
1010 ["Lambda"] = 923,
1011 ["Mu"] = 924,
1012 ["Nu"] = 925,
1013 ["Xi"] = 926,
1014 ["Omicron"] = 927,
1015 ["Pi"] = 928,
1016 ["Rho"] = 929,
1017 ["Sigma"] = 931,
1018 ["Tau"] = 932,
1019 ["Upsilon"] = 933,
1020 ["Phi"] = 934,
1021 ["Chi"] = 935,
1022 ["Psi"] = 936,
1023 ["Omega"] = 937,
1024 ["ohm"] = 937,
1025 ["alpha"] = 945,
1026 ["beta"] = 946,
1027 ["gamma"] = 947,
1028 ["delta"] = 948,
1029 ["epsi"] = 949,
1030 ["epsilon"] = 949,
1031 ["zeta"] = 950,
1032 ["eta"] = 951,
1033 ["theta"] = 952,
1034 ["iota"] = 953,
1035 ["kappa"] = 954,
1036 ["lambda"] = 955,
1037 ["mu"] = 956,
1038 ["nu"] = 957,
1039 ["xi"] = 958,
1040 ["omicron"] = 959,
1041 ["pi"] = 960,
1042 ["rho"] = 961,
1043 ["sigmaf"] = 962,
1044 ["sigmav"] = 962,
1045 ["varsigma"] = 962,
1046 ["sigma"] = 963,
1047 ["tau"] = 964,
1048 ["upsi"] = 965,
1049 ["upsilon"] = 965,
1050 ["phi"] = 966,
1051 ["chi"] = 967,
1052 ["psi"] = 968,
1053 ["omega"] = 969,
1054 ["thetasym"] = 977,
1055 ["thetav"] = 977,
1056 ["vartheta"] = 977,
1057 ["Upsi"] = 978,
1058 ["upsih"] = 978,
1059 ["phiv"] = 981,
1060 ["straightphi"] = 981,
1061 ["varphi"] = 981,
1062 ["piv"] = 982,
1063 ["varpi"] = 982,
1064 ["Gammad"] = 988,
1065 ["digamma"] = 989,
1066 ["gammad"] = 989,
1067 ["kappav"] = 1008,
1068 ["varkappa"] = 1008,
1069 ["rhov"] = 1009,
1070 ["varrho"] = 1009,
1071 ["epsiv"] = 1013,
1072 ["straightepsilon"] = 1013,
1073 ["varepsilon"] = 1013,
1074 ["backepsilon"] = 1014,
1075 ["bepsi"] = 1014,
1076 ["IOcy"] = 1025,
1077 ["DJcy"] = 1026,
1078 ["GJcy"] = 1027,
1079 ["Jukcy"] = 1028,
1080 ["DScy"] = 1029,
1081 ["Iukcy"] = 1030,
1082 ["YIcy"] = 1031,
1083 ["Jsercy"] = 1032,
1084 ["LJcy"] = 1033,
1085 ["NJcy"] = 1034,
1086 ["TSHcy"] = 1035,
1087 ["KJcy"] = 1036,
1088 ["Ubrcy"] = 1038,
1089 ["DZcy"] = 1039,
1090 ["Acy"] = 1040,
1091 ["Bcy"] = 1041,
1092 ["Vcy"] = 1042,
1093 ["Gcy"] = 1043,
1094 ["Dcy"] = 1044,
1095 ["IEcy"] = 1045,
1096 ["ZHcy"] = 1046,
1097 ["Zcy"] = 1047,
1098 ["Icy"] = 1048,
1099 ["Jcy"] = 1049,
1100 ["Kcy"] = 1050,
1101 ["Lcy"] = 1051,
1102 ["Mcy"] = 1052,
1103 ["Ncy"] = 1053,
1104 ["Ocy"] = 1054,
1105 ["Pcy"] = 1055,
1106 ["Rcy"] = 1056,
1107 ["Scy"] = 1057,
1108 ["Tcy"] = 1058,
1109 ["Ucy"] = 1059,
1110 ["Fcy"] = 1060,
1111 ["KHcy"] = 1061,
1112 ["TScy"] = 1062,
1113 ["CHcy"] = 1063,
1114 ["SHcy"] = 1064,
1115 ["SHCHcy"] = 1065,
1116 ["HARDcy"] = 1066,
1117 ["Ycy"] = 1067,
1118 ["SOFTcy"] = 1068,
1119 ["Ecy"] = 1069,
1120 ["YUcy"] = 1070,
1121 ["YAcy"] = 1071,
1122 ["acy"] = 1072,
1123 ["bcy"] = 1073,
1124 ["vcy"] = 1074,
1125 ["gcy"] = 1075,
1126 ["dcy"] = 1076,
1127 ["iecy"] = 1077,
1128 ["zhcy"] = 1078,
1129 ["zcy"] = 1079,
1130 ["icy"] = 1080,
1131 ["jcy"] = 1081,
1132 ["kcy"] = 1082,
1133 ["lcy"] = 1083,
1134 ["mcy"] = 1084,
1135 ["ncy"] = 1085,
1136 ["ocy"] = 1086,
1137 ["pcy"] = 1087,
1138 ["rcy"] = 1088,
1139 ["scy"] = 1089,
1140 ["tcy"] = 1090,
1141 ["ucy"] = 1091,
1142 ["fcy"] = 1092,
1143 ["khcy"] = 1093,
1144 ["tscy"] = 1094,
1145 ["chcy"] = 1095,
1146 ["shcy"] = 1096,
1147 ["shchcy"] = 1097,
1148 ["hardcy"] = 1098,
1149 ["ycy"] = 1099,
1150 ["softcy"] = 1100,
1151 ["ecy"] = 1101,
1152 ["yucy"] = 1102,
1153 ["yacy"] = 1103,
1154 ["iocy"] = 1105,
1155 ["djcy"] = 1106,
1156 ["gjcy"] = 1107,
1157 ["jukcy"] = 1108,
1158 ["dscy"] = 1109,
1159 ["iukcy"] = 1110,
1160 ["yicy"] = 1111,
1161 ["jsercy"] = 1112,
1162 ["ljcy"] = 1113,
1163 ["njcy"] = 1114,
1164 ["tshcy"] = 1115,
1165 ["kjcy"] = 1116,
1166 ["ubrcy"] = 1118,
1167 ["dzcy"] = 1119,
1168 ["ensp"] = 8194,
1169 ["emsp"] = 8195,
1170 ["emsp13"] = 8196,
1171 ["emsp14"] = 8197,
1172 ["numsp"] = 8199,
1173 ["puncsp"] = 8200,
1174 ["ThinSpace"] = 8201,
1175 ["thinsp"] = 8201,
1176 ["VeryThinSpace"] = 8202,
1177 ["hairsp"] = 8202,
1178 ["NegativeMediumSpace"] = 8203,
1179 ["NegativeThickSpace"] = 8203,
1180 ["NegativeThinSpace"] = 8203,
1181 ["NegativeVeryThinSpace"] = 8203,
1182 ["ZeroWidthSpace"] = 8203,
1183 ["zwnj"] = 8204,
1184 ["zwj"] = 8205,
1185 ["lrm"] = 8206,
1186 ["rlm"] = 8207,
1187 ["dash"] = 8208,
1188 ["hyphen"] = 8208,
1189 ["ndash"] = 8211,
1190 ["mdash"] = 8212,
1191 ["horbar"] = 8213,
1192 ["Verbar"] = 8214,
1193 ["Vert"] = 8214,
1194 ["OpenCurlyQuote"] = 8216,
1195 ["lsquo"] = 8216,
1196 ["CloseCurlyQuote"] = 8217,
1197 ["rsquo"] = 8217,
1198 ["rsquor"] = 8217,
1199 ["lsquor"] = 8218,
1200 ["sbquo"] = 8218,
1201 ["OpenCurlyDoubleQuote"] = 8220,
1202 ["ldquo"] = 8220,
1203 ["CloseCurlyDoubleQuote"] = 8221,
1204 ["rdquo"] = 8221,
1205 ["rdquor"] = 8221,
1206 ["bdquo"] = 8222,
1207 ["ldquor"] = 8222,
1208 ["dagger"] = 8224,
1209 ["Dagger"] = 8225,
1210 ["ddagger"] = 8225,
1211 ["bull"] = 8226,
1212 ["bullet"] = 8226,
1213 ["nldr"] = 8229,
1214 ["hellip"] = 8230,
1215 ["mldr"] = 8230,
1216 ["permil"] = 8240,
1217 ["pertenk"] = 8241,
1218 ["prime"] = 8242,
1219 ["Prime"] = 8243,
1220 ["tprime"] = 8244,
1221 ["backprime"] = 8245,
1222 ["bprime"] = 8245,
1223 ["lsaquo"] = 8249,
1224 ["rsaquo"] = 8250,
1225 ["OverBar"] = 8254,
1226 ["oline"] = 8254,
1227 ["caret"] = 8257,
1228 ["hybull"] = 8259,
1229 ["frasl"] = 8260,
1230 ["bsemi"] = 8271,
1231 ["qprime"] = 8279,
1232 ["MediumSpace"] = 8287,
1233 ["ThickSpace"] = {8287, 8202},
1234 ["NoBreak"] = 8288,
1235 ["ApplyFunction"] = 8289,
1236 ["af"] = 8289,
1237 ["InvisibleTimes"] = 8290,
1238 ["it"] = 8290,
1239 ["InvisibleComma"] = 8291,
1240 ["ic"] = 8291,
1241 ["euro"] = 8364,
1242 ["TripleDot"] = 8411,
1243 ["tdot"] = 8411,
1244 ["DotDot"] = 8412,
1245 ["Copf"] = 8450,
1246 ["complexes"] = 8450,
1247 ["incare"] = 8453,
1248 ["gscr"] = 8458,
1249 ["HilbertSpace"] = 8459,
1250 ["Hscr"] = 8459,
1251 ["hamilt"] = 8459,
1252 ["Hfr"] = 8460,
1253 ["Poincareplane"] = 8460,
1254 ["Hopf"] = 8461,
1255 ["quaternions"] = 8461,
1256 ["planckh"] = 8462,
1257 ["hbar"] = 8463,
1258 ["hslash"] = 8463,
1259 ["planck"] = 8463,
1260 ["plankv"] = 8463,
1261 ["Iscr"] = 8464,
1262 ["imagline"] = 8464,
1263 ["Ifr"] = 8465,
1264 ["Im"] = 8465,
1265 ["image"] = 8465,
1266 ["imagpart"] = 8465,
1267 ["Laplacetrf"] = 8466,
1268 ["Lscr"] = 8466,
1269 ["lagran"] = 8466,
1270 ["ell"] = 8467,
1271 ["Nopf"] = 8469,
1272 ["naturals"] = 8469,
1273 ["numero"] = 8470,
1274 ["copysr"] = 8471,
1275 ["weierp"] = 8472,
1276 ["wp"] = 8472,
1277 ["Popf"] = 8473,
1278 ["primes"] = 8473,
1279 ["Qopf"] = 8474,
1280 ["rationals"] = 8474,
1281 ["Rscr"] = 8475,
1282 ["realine"] = 8475,
1283 ["Re"] = 8476,
1284 ["Rfr"] = 8476,
1285 ["real"] = 8476,
1286 ["realpart"] = 8476,
1287 ["Ropf"] = 8477,
1288 ["reals"] = 8477,
1289 ["rx"] = 8478,
1290 ["TRADE"] = 8482,
1291 ["trade"] = 8482,
1292 ["Zopf"] = 8484,
1293 ["integers"] = 8484,
1294 ["mho"] = 8487,
1295 ["Zfr"] = 8488,
1296 ["zeetrf"] = 8488,
1297 ["iiota"] = 8489,
1298 ["Bernoullis"] = 8492,
1299 ["Bscr"] = 8492,
1300 ["bernou"] = 8492,
1301 ["Cayleys"] = 8493,
1302 ["Cfr"] = 8493,
1303 ["escr"] = 8495,
1304 ["Escr"] = 8496,
1305 ["expectation"] = 8496,
1306 ["Fouriertrf"] = 8497,
1307 ["Fscr"] = 8497,
1308 ["Mellintrf"] = 8499,
1309 ["Mscr"] = 8499,
1310 ["phmmat"] = 8499,
1311 ["order"] = 8500,
1312 ["orderof"] = 8500,
1313 ["oscr"] = 8500,
1314 ["alefsym"] = 8501,
1315 ["aleph"] = 8501,
1316 ["beth"] = 8502,
1317 ["gimel"] = 8503,
1318 ["daleth"] = 8504,
1319 ["CapitalDifferentialD"] = 8517,
1320 ["DD"] = 8517,
1321 ["DifferentialD"] = 8518,
1322 ["dd"] = 8518,
1323 ["ExponentialE"] = 8519,
1324 ["ee"] = 8519,
1325 ["exponentiale"] = 8519,
1326 ["ImaginaryI"] = 8520,
1327 ["ii"] = 8520,
1328 ["frac13"] = 8531,
1329 ["frac23"] = 8532,
1330 ["frac15"] = 8533,
1331 ["frac25"] = 8534,
1332 ["frac35"] = 8535,
1333 ["frac45"] = 8536,
1334 ["frac16"] = 8537,
1335 ["frac56"] = 8538,
1336 ["frac18"] = 8539,
1337 ["frac38"] = 8540,
1338 ["frac58"] = 8541,
1339 ["frac78"] = 8542,
1340 ["LeftArrow"] = 8592,
1341 ["ShortLeftArrow"] = 8592,
1342 ["larr"] = 8592,
1343 ["leftarrow"] = 8592,
1344 ["slarr"] = 8592,
1345 ["ShortUpArrow"] = 8593,
1346 ["UpArrow"] = 8593,
1347 ["uarr"] = 8593,
1348 ["uparrow"] = 8593,
1349 ["RightArrow"] = 8594,
1350 ["ShortRightArrow"] = 8594,
1351 ["rarr"] = 8594,
1352 ["rightarrow"] = 8594,
1353 ["srarr"] = 8594,
1354 ["DownArrow"] = 8595,
1355 ["ShortDownArrow"] = 8595,
1356 ["darr"] = 8595,
1357 ["downarrow"] = 8595,
1358 ["LeftRightArrow"] = 8596,
1359 ["harr"] = 8596,
1360 ["leftrightarrow"] = 8596,
1361 ["UpDownArrow"] = 8597,
1362 ["updownarrow"] = 8597,
1363 ["varr"] = 8597,
1364 ["UpperLeftArrow"] = 8598,
1365 ["nwarr"] = 8598,
1366 ["nwarrow"] = 8598,
1367 ["UpperRightArrow"] = 8599,
1368 ["nearr"] = 8599,
1369 ["nearrow"] = 8599,
1370 ["LowerRightArrow"] = 8600,
1371 ["searr"] = 8600,
1372 ["searrow"] = 8600,
1373 ["LowerLeftArrow"] = 8601,
1374 ["swarr"] = 8601,
1375 ["swarrow"] = 8601,
1376 ["nlarr"] = 8602,
1377 ["nleftarrow"] = 8602,
1378 ["nrarr"] = 8603,
1379 ["nrightarrow"] = 8603,
1380 ["nrarrw"] = {8605, 824},
1381 ["rarrw"] = 8605,
1382 ["rightsquigarrow"] = 8605,
1383 ["Larr"] = 8606,
1384 ["twoheadleftarrow"] = 8606,
1385 ["Uarr"] = 8607,
1386 ["Rarr"] = 8608,
1387 ["twoheadrightarrow"] = 8608,
1388 ["Darr"] = 8609,
1389 ["larrtl"] = 8610,
1390 ["leftarrowtail"] = 8610,
1391 ["rarrtl"] = 8611,
1392 ["rightarrowtail"] = 8611,
1393 ["LeftTeeArrow"] = 8612,
1394 ["mapstoleft"] = 8612,
1395 ["UpTeeArrow"] = 8613,
1396 ["mapstoup"] = 8613,
1397 ["RightTeeArrow"] = 8614,
1398 ["map"] = 8614,
1399 ["mapsto"] = 8614,
1400 ["DownTeeArrow"] = 8615,
1401 ["mapstodown"] = 8615,
1402 ["hookleftarrow"] = 8617,
1403 ["larrhk"] = 8617,
1404 ["hookrightarrow"] = 8618,
1405 ["rarrhk"] = 8618,
1406 ["larrlp"] = 8619,
1407 ["looparrowleft"] = 8619,
1408 ["looparrowright"] = 8620,
1409 ["rarrlp"] = 8620,
1410 ["harrw"] = 8621,
1411 ["leftrightsquigarrow"] = 8621,
1412 ["nharr"] = 8622,
1413 ["nleftrightarrow"] = 8622,
1414 ["Lsh"] = 8624,
1415 ["lsh"] = 8624,
1416 ["Rsh"] = 8625,
1417 ["rsh"] = 8625,
1418 ["ldsh"] = 8626,
1419 ["rdsh"] = 8627,
1420 ["crarr"] = 8629,
1421 ["cularr"] = 8630,
1422 ["curvearrowleft"] = 8630,
1423 ["curarr"] = 8631,
1424 ["curvearrowright"] = 8631,
1425 ["circlearrowleft"] = 8634,
1426 ["olarr"] = 8634,
1427 ["circlearrowright"] = 8635,
1428 ["orarr"] = 8635,
1429 ["LeftVector"] = 8636,
1430 ["leftharpoonup"] = 8636,
1431 ["lharu"] = 8636,
1432 ["DownLeftVector"] = 8637,
1433 ["leftharpoondown"] = 8637,
1434 ["lhard"] = 8637,
1435 ["RightUpVector"] = 8638,
1436 ["uharr"] = 8638,
1437 ["upharpoonright"] = 8638,
1438 ["LeftUpVector"] = 8639,
1439 ["uharl"] = 8639,
1440 ["upharpoonleft"] = 8639,
1441 ["RightVector"] = 8640,
1442 ["rharu"] = 8640,
1443 ["rightharpoonup"] = 8640,
1444 ["DownRightVector"] = 8641,
1445 ["rhard"] = 8641,
1446 ["rightharpoondown"] = 8641,
1447 ["RightDownVector"] = 8642,
1448 ["dharr"] = 8642,
1449 ["downharpoonright"] = 8642,
1450 ["LeftDownVector"] = 8643,
1451 ["dharl"] = 8643,
1452 ["downharpoonleft"] = 8643,
1453 ["RightArrowLeftArrow"] = 8644,
1454 ["rightleftarrows"] = 8644,
1455 ["rlarr"] = 8644,
1456 ["UpArrowDownArrow"] = 8645,
1457 ["udarr"] = 8645,
1458 ["LeftArrowRightArrow"] = 8646,
1459 ["leftrightarrows"] = 8646,
1460 ["lrarr"] = 8646,
1461 ["leftleftarrows"] = 8647,
1462 ["llarr"] = 8647,
1463 ["upuparrows"] = 8648,
1464 ["uuarr"] = 8648,
1465 ["rightrightarrows"] = 8649,
1466 ["rrarr"] = 8649,
1467 ["ddarr"] = 8650,
1468 ["downdownarrows"] = 8650,
1469 ["ReverseEquilibrium"] = 8651,
1470 ["leftrightharpoons"] = 8651,
1471 ["lrhar"] = 8651,
1472 ["Equilibrium"] = 8652,
1473 ["rightleftharpoons"] = 8652,
1474 ["rlhar"] = 8652,
1475 ["nLeftarrow"] = 8653,
1476 ["nlArr"] = 8653,
1477 ["nLeftrightarrow"] = 8654,
1478 ["nhArr"] = 8654,
1479 ["nRightarrow"] = 8655,
1480 ["nrArr"] = 8655,
1481 ["DoubleLeftArrow"] = 8656,
1482 ["Leftarrow"] = 8656,
1483 ["lArr"] = 8656,
1484 ["DoubleUpArrow"] = 8657,
1485 ["Uparrow"] = 8657,
1486 ["uArr"] = 8657,
1487 ["DoubleRightArrow"] = 8658,
1488 ["Implies"] = 8658,
1489 ["Rightarrow"] = 8658,
1490 ["rArr"] = 8658,
1491 ["DoubleDownArrow"] = 8659,
1492 ["Downarrow"] = 8659,
1493 ["dArr"] = 8659,
1494 ["DoubleLeftRightArrow"] = 8660,
1495 ["Leftrightarrow"] = 8660,
1496 ["hArr"] = 8660,
1497 ["iff"] = 8660,
1498 ["DoubleUpDownArrow"] = 8661,
1499 ["Updownarrow"] = 8661,
1500 ["vArr"] = 8661,
1501 ["nwArr"] = 8662,
1502 ["neArr"] = 8663,
1503 ["seArr"] = 8664,
1504 ["swArr"] = 8665,
1505 ["Lleftarrow"] = 8666,
1506 ["lAarr"] = 8666,
1507 ["Rrightarrow"] = 8667,
1508 ["rAarr"] = 8667,
1509 ["zigrarr"] = 8669,
1510 ["LeftArrowBar"] = 8676,
1511 ["larrb"] = 8676,
1512 ["RightArrowBar"] = 8677,
1513 ["rarrb"] = 8677,
1514 ["DownArrowUpArrow"] = 8693,
1515 ["duarr"] = 8693,
1516 ["loarr"] = 8701,
1517 ["roarr"] = 8702,
1518 ["hoarr"] = 8703,
1519 ["ForAll"] = 8704,
1520 ["forall"] = 8704,
1521 ["comp"] = 8705,
1522 ["complement"] = 8705,
1523 ["PartialD"] = 8706,
1524 ["npart"] = {8706, 824},
1525 ["part"] = 8706,
1526 ["Exists"] = 8707,
1527 ["exist"] = 8707,
1528 ["NotExists"] = 8708,
1529 ["nexist"] = 8708,
1530 ["nexists"] = 8708,
1531 ["empty"] = 8709,
1532 ["emptyset"] = 8709,
1533 ["emptyv"] = 8709,
1534 ["varnothing"] = 8709,
1535 ["Del"] = 8711,
1536 ["nabla"] = 8711,
1537 ["Element"] = 8712,
1538 ["in"] = 8712,
1539 ["isin"] = 8712,
1540 ["isinv"] = 8712,
1541 ["NotElement"] = 8713,
1542 ["notin"] = 8713,
1543 ["notinva"] = 8713,
1544 ["ReverseElement"] = 8715,
1545 ["SuchThat"] = 8715,
1546 ["ni"] = 8715,
1547 ["niv"] = 8715,
1548 ["NotReverseElement"] = 8716,
1549 ["notni"] = 8716,
1550 ["notniva"] = 8716,
1551 ["Product"] = 8719,
1552 ["prod"] = 8719,
1553 ["Coproduct"] = 8720,
1554 ["coprod"] = 8720,
1555 ["Sum"] = 8721,
1556 ["sum"] = 8721,
1557 ["minus"] = 8722,
1558 ["MinusPlus"] = 8723,
1559 ["mnplus"] = 8723,
1560 ["mp"] = 8723,
1561 ["dotplus"] = 8724,
1562 ["plusdo"] = 8724,
1563 ["Backslash"] = 8726,
1564 ["setminus"] = 8726,
1565 ["setmn"] = 8726,
1566 ["smallsetminus"] = 8726,
1567 ["ssetmn"] = 8726,
1568 ["lowast"] = 8727,
1569 ["SmallCircle"] = 8728,
1570 ["compfn"] = 8728,
1571 ["Sqrt"] = 8730,
1572 ["radic"] = 8730,
1573 ["Proportional"] = 8733,
1574 ["prop"] = 8733,
1575 ["propto"] = 8733,
1576 ["varpropto"] = 8733,
1577 ["vprop"] = 8733,
1578 ["infin"] = 8734,
1579 ["angrt"] = 8735,
1580 ["ang"] = 8736,
1581 ["angle"] = 8736,
1582 ["nang"] = {8736, 8402},
1583 ["angmsd"] = 8737,
1584 ["measuredangle"] = 8737,
1585 ["angsph"] = 8738,
1586 ["VerticalBar"] = 8739,
1587 ["mid"] = 8739,
1588 ["shortmid"] = 8739,
1589 ["smid"] = 8739,
1590 ["NotVerticalBar"] = 8740,
1591 ["nmid"] = 8740,
1592 ["nshortmid"] = 8740,
1593 ["nsmid"] = 8740,
1594 ["DoubleVerticalBar"] = 8741,
1595 ["par"] = 8741,
1596 ["parallel"] = 8741,
1597 ["shortparallel"] = 8741,
1598 ["spar"] = 8741,
1599 ["NotDoubleVerticalBar"] = 8742,
1600 ["npar"] = 8742,
1601 ["nparallel"] = 8742,
1602 ["nshortparallel"] = 8742,
1603 ["nspar"] = 8742,
1604 ["and"] = 8743,
1605 ["wedge"] = 8743,
1606 ["or"] = 8744,
1607 ["vee"] = 8744,
1608 ["cap"] = 8745,
1609 ["caps"] = {8745, 65024},
1610 ["cup"] = 8746,
1611 ["cups"] = {8746, 65024},
1612 ["Integral"] = 8747,
1613 ["int"] = 8747,
1614 ["Int"] = 8748,
1615 ["iiint"] = 8749,
1616 ["tint"] = 8749,
1617 ["ContourIntegral"] = 8750,
1618 ["conint"] = 8750,
1619 ["oint"] = 8750,
1620 ["Conint"] = 8751,
1621 ["DoubleContourIntegral"] = 8751,
1622 ["Cconint"] = 8752,
1623 ["cwint"] = 8753,
1624 ["ClockwiseContourIntegral"] = 8754,
1625 ["cwconint"] = 8754,
1626 ["CounterClockwiseContourIntegral"] = 8755,
1627 ["awconint"] = 8755,
1628 ["Therefore"] = 8756,
1629 ["there4"] = 8756,
1630 ["therefore"] = 8756,
1631 ["Because"] = 8757,
1632 ["becaus"] = 8757,
1633 ["because"] = 8757,
1634 ["ratio"] = 8758,
1635 ["Colon"] = 8759,
1636 ["Proportion"] = 8759,
1637 ["dotminus"] = 8760,
1638 ["minusd"] = 8760,
1639 ["mDDot"] = 8762,
1640 ["homtht"] = 8763,
1641 ["Tilde"] = 8764,
1642 ["nvsim"] = {8764, 8402},
1643 ["sim"] = 8764,
1644 ["thicksim"] = 8764,
1645 ["thksim"] = 8764,
1646 ["backsim"] = 8765,
1647 ["bsim"] = 8765,
1648 ["race"] = {8765, 817},
1649 ["ac"] = 8766,
1650 ["acE"] = {8766, 819},
1651 ["mstpos"] = 8766,
1652 ["acd"] = 8767,
1653 ["VerticalTilde"] = 8768,
1654 ["wr"] = 8768,
1655 ["wreath"] = 8768,
1656 ["NotTilde"] = 8769,
1657 ["nsim"] = 8769,
1658 ["EqualTilde"] = 8770,
1659 ["NotEqualTilde"] = {8770, 824},
1660 ["eqsim"] = 8770,
1661 ["esim"] = 8770,
1662 ["nesim"] = {8770, 824},
1663 ["TildeEqual"] = 8771,
1664 ["sime"] = 8771,
1665 ["simeq"] = 8771,
1666 ["NotTildeEqual"] = 8772,
1667 ["nsime"] = 8772,
1668 ["nsimeq"] = 8772,
1669 ["TildeFullEqual"] = 8773,
1670 ["cong"] = 8773,
1671 ["simne"] = 8774,
1672 ["NotTildeFullEqual"] = 8775,
1673 ["ncong"] = 8775,
1674 ["TildeTilde"] = 8776,
1675 ["ap"] = 8776,
1676 ["approx"] = 8776,
1677 ["asymp"] = 8776,
1678 ["thickapprox"] = 8776,
1679 ["thkap"] = 8776,
1680 ["NotTildeTilde"] = 8777,
1681 ["nap"] = 8777,
1682 ["napprox"] = 8777,
1683 ["ape"] = 8778,
1684 ["approxeq"] = 8778,
1685 ["apid"] = 8779,
1686 ["napid"] = {8779, 824},
1687 ["backcong"] = 8780,
1688 ["bcong"] = 8780,
1689 ["CupCap"] = 8781,
1690 ["asympeq"] = 8781,
1691 ["nvap"] = {8781, 8402},
1692 ["Bumpeq"] = 8782,
1693 ["HumpDownHump"] = 8782,
1694 ["NotHumpDownHump"] = {8782, 824},
1695 ["bump"] = 8782,
1696 ["nbump"] = {8782, 824},
1697 ["HumpEqual"] = 8783,
1698 ["NotHumpEqual"] = {8783, 824},
1699 ["bumpe"] = 8783,
1700 ["bumpeq"] = 8783,
1701 ["nbumpe"] = {8783, 824},
1702 ["DotEqual"] = 8784,
1703 ["doteq"] = 8784,
1704 ["esdot"] = 8784,
1705 ["nedot"] = {8784, 824},
1706 ["doteqdot"] = 8785,
1707 ["eDot"] = 8785,
1708 ["efDot"] = 8786,
1709 ["fallingdotseq"] = 8786,
1710 ["erDot"] = 8787,
1711 ["risingdotseq"] = 8787,
1712 ["Assign"] = 8788,
1713 ["colone"] = 8788,
1714 ["coloneq"] = 8788,
1715 ["ecolon"] = 8789,
1716 ["eqcolon"] = 8789,
1717 ["ecir"] = 8790,
1718 ["eqcirc"] = 8790,
1719 ["circeq"] = 8791,
1720 ["cire"] = 8791,
1721 ["wedgeq"] = 8793,
1722 ["veeeq"] = 8794,
1723 ["triangleq"] = 8796,
1724 ["trie"] = 8796,
1725 ["equest"] = 8799,
1726 ["questeq"] = 8799,
1727 ["NotEqual"] = 8800,
1728 ["ne"] = 8800,
1729 ["Congruent"] = 8801,
1730 ["bnequiv"] = {8801, 8421},
1731 ["equiv"] = 8801,
1732 ["NotCongruent"] = 8802,
1733 ["nequiv"] = 8802,
1734 ["le"] = 8804,
1735 ["leq"] = 8804,
1736 ["nvle"] = {8804, 8402},
1737 ["GreaterEqual"] = 8805,
1738 ["ge"] = 8805,
1739 ["geq"] = 8805,
1740 ["nvge"] = {8805, 8402},
1741 ["LessFullEqual"] = 8806,
1742 ["lE"] = 8806,
1743 ["leqq"] = 8806,
1744 ["nlE"] = {8806, 824},
1745 ["nleqq"] = {8806, 824},
1746 ["GreaterFullEqual"] = 8807,
1747 ["NotGreaterFullEqual"] = {8807, 824},
1748 ["gE"] = 8807,
1749 ["geqq"] = 8807,
1750 ["ngE"] = {8807, 824},
1751 ["ngeqq"] = {8807, 824},
1752 ["lnE"] = 8808,
1753 ["lneqq"] = 8808,
1754 ["lvertneqq"] = {8808, 65024},
1755 ["lvnE"] = {8808, 65024},
1756 ["gnE"] = 8809,
1757 ["gneqq"] = 8809,
1758 ["gvertneqq"] = {8809, 65024},
1759 ["gvnE"] = {8809, 65024},
1760 ["Lt"] = 8810,
1761 ["NestedLessLess"] = 8810,
1762 ["NotLessLess"] = {8810, 824},
1763 ["ll"] = 8810,
1764 ["nLt"] = {8810, 8402},
1765 ["nLtv"] = {8810, 824},
1766 ["Gt"] = 8811,
1767 ["NestedGreaterGreater"] = 8811,
1768 ["NotGreaterGreater"] = {8811, 824},
1769 ["gg"] = 8811,
1770 ["nGt"] = {8811, 8402},
1771 ["nGtv"] = {8811, 824},
1772 ["between"] = 8812,
1773 ["twixt"] = 8812,
1774 ["NotCupCap"] = 8813,
1775 ["NotLess"] = 8814,
1776 ["nless"] = 8814,
1777 ["nlt"] = 8814,
1778 ["NotGreater"] = 8815,
1779 ["ngt"] = 8815,
1780 ["ngtr"] = 8815,
1781 ["NotLessEqual"] = 8816,
1782 ["nle"] = 8816,
1783 ["nleq"] = 8816,
1784 ["NotGreaterEqual"] = 8817,
1785 ["nge"] = 8817,
1786 ["ngeq"] = 8817,
1787 ["LessTilde"] = 8818,
1788 ["lesssim"] = 8818,
1789 ["lsim"] = 8818,
1790 ["GreaterTilde"] = 8819,
1791 ["gsim"] = 8819,
1792 ["gtrsim"] = 8819,
1793 ["NotLessTilde"] = 8820,
1794 ["nlsim"] = 8820,
1795 ["NotGreaterTilde"] = 8821,
1796 ["ngsim"] = 8821,
1797 ["LessGreater"] = 8822,
1798 ["lessgtr"] = 8822,
1799 ["lg"] = 8822,
1800 ["GreaterLess"] = 8823,
1801 ["gl"] = 8823,
1802 ["gtrless"] = 8823,
1803 ["NotLessGreater"] = 8824,
1804 ["ntlg"] = 8824,
1805 ["NotGreaterLess"] = 8825,
1806 ["ntgl"] = 8825,
1807 ["Precedes"] = 8826,
1808 ["pr"] = 8826,
1809 ["prec"] = 8826,
1810 ["Succeeds"] = 8827,
1811 ["sc"] = 8827,
1812 ["succ"] = 8827,
1813 ["PrecedesSlantEqual"] = 8828,
1814 ["prcue"] = 8828,
1815 ["preccurlyeq"] = 8828,
1816 ["SucceedsSlantEqual"] = 8829,
1817 ["sccue"] = 8829,
1818 ["succcurlyeq"] = 8829,
1819 ["PrecedesTilde"] = 8830,
1820 ["precsim"] = 8830,
1821 ["prsim"] = 8830,
1822 ["NotSucceedsTilde"] = {8831, 824},
1823 ["SucceedsTilde"] = 8831,
1824 ["scsim"] = 8831,
1825 ["succsim"] = 8831,
1826 ["NotPrecedes"] = 8832,
1827 ["npr"] = 8832,
1828 ["nprec"] = 8832,
1829 ["NotSucceeds"] = 8833,
1830 ["nsc"] = 8833,
1831 ["nsucc"] = 8833,
1832 ["NotSubset"] = {8834, 8402},
1833 ["nsubset"] = {8834, 8402},
1834 ["sub"] = 8834,
1835 ["subset"] = 8834,
1836 ["vnsub"] = {8834, 8402},
1837 ["NotSuperset"] = {8835, 8402},
1838 ["Superset"] = 8835,
1839 ["nsupset"] = {8835, 8402},
1840 ["sup"] = 8835,
1841 ["supset"] = 8835,
1842 ["vnsup"] = {8835, 8402},
1843 ["nsub"] = 8836,
1844 ["nsup"] = 8837,
1845 ["SubsetEqual"] = 8838,
1846 ["sube"] = 8838,
1847 ["subseteq"] = 8838,
1848 ["SupersetEqual"] = 8839,
1849 ["supe"] = 8839,
1850 ["supseteq"] = 8839,
1851 ["NotSubsetEqual"] = 8840,
1852 ["nsube"] = 8840,
1853 ["nsubseteq"] = 8840,
1854 ["NotSupersetEqual"] = 8841,
1855 ["nsupe"] = 8841,
1856 ["nsupseteq"] = 8841,
1857 ["subne"] = 8842,
1858 ["subsetneq"] = 8842,
1859 ["varsubsetneq"] = {8842, 65024},
1860 ["vsubne"] = {8842, 65024},
1861 ["supne"] = 8843,
1862 ["supsetneq"] = 8843,
1863 ["varsupsetneq"] = {8843, 65024},
1864 ["vsupne"] = {8843, 65024},
1865 ["cupdot"] = 8845,
1866 ["UnionPlus"] = 8846,
1867 ["uplus"] = 8846,
1868 ["NotSquareSubset"] = {8847, 824},
1869 ["SquareSubset"] = 8847,
1870 ["sqsub"] = 8847,
1871 ["sqsubset"] = 8847,
1872 ["NotSquareSuperset"] = {8848, 824},
1873 ["SquareSuperset"] = 8848,
1874 ["sqsup"] = 8848,
1875 ["sqsupset"] = 8848,
1876 ["SquareSubsetEqual"] = 8849,
1877 ["sqsube"] = 8849,
1878 ["sqsubseteq"] = 8849,
1879 ["SquareSupersetEqual"] = 8850,
1880 ["sqsupe"] = 8850,
1881 ["sqsupseteq"] = 8850,
1882 ["SquareIntersection"] = 8851,
1883 ["sqcap"] = 8851,
1884 ["sqcaps"] = {8851, 65024},
1885 ["SquareUnion"] = 8852,
1886 ["sqcup"] = 8852,
1887 ["sqcups"] = {8852, 65024},
1888 ["CirclePlus"] = 8853,
1889 ["oplus"] = 8853,
1890 ["CircleMinus"] = 8854,
1891 ["ominus"] = 8854,
1892 ["CircleTimes"] = 8855,
1893 ["otimes"] = 8855,
1894 ["osol"] = 8856,
1895 ["CircleDot"] = 8857,
1896 ["odot"] = 8857,
1897 ["circledcirc"] = 8858,
1898 ["ocir"] = 8858,
1899 ["circledast"] = 8859,
1900 ["oast"] = 8859,
1901 ["circleddash"] = 8861,
1902 ["odash"] = 8861,
1903 ["boxplus"] = 8862,
1904 ["plusb"] = 8862,
1905 ["boxminus"] = 8863,
1906 ["minusb"] = 8863,
1907 ["boxtimes"] = 8864,
1908 ["timesb"] = 8864,
1909 ["dotsquare"] = 8865,
1910 ["sdotb"] = 8865,
1911 ["RightTee"] = 8866,
1912 ["vdash"] = 8866,
1913 ["LeftTee"] = 8867,
1914 ["dashv"] = 8867,
1915 ["DownTee"] = 8868,
1916 ["top"] = 8868,
1917 ["UpTee"] = 8869,
1918 ["bot"] = 8869,
1919 ["bottom"] = 8869,
1920 ["perp"] = 8869,
1921 ["models"] = 8871,
1922 ["DoubleRightTee"] = 8872,
1923 ["vDash"] = 8872,
1924 ["Vdash"] = 8873,
1925 ["Vvdash"] = 8874,
1926 ["VDash"] = 8875,
1927 ["nvdash"] = 8876,
1928 ["nvDash"] = 8877,
1929 ["nVdash"] = 8878,
1930 ["nVDash"] = 8879,
1931 ["prurel"] = 8880,
1932 ["LeftTriangle"] = 8882,
1933 ["vartriangleleft"] = 8882,
1934 ["vltri"] = 8882,
1935 ["RightTriangle"] = 8883,
1936 ["vartriangleright"] = 8883,
1937 ["vrtri"] = 8883,
1938 ["LeftTriangleEqual"] = 8884,
1939 ["ltrie"] = 8884,
1940 ["nvltrie"] = {8884, 8402},
1941 ["trianglelefteq"] = 8884,
1942 ["RightTriangleEqual"] = 8885,
1943 ["nvrtrie"] = {8885, 8402},
1944 ["rtrie"] = 8885,
1945 ["trianglerighteq"] = 8885,
1946 ["origof"] = 8886,
1947 ["imof"] = 8887,
1948 ["multimap"] = 8888,
1949 ["mumap"] = 8888,
1950 ["hercon"] = 8889,
1951 ["intcal"] = 8890,
1952 ["intercal"] = 8890,
1953 ["veebar"] = 8891,
1954 ["barvee"] = 8893,
1955 ["angrtvb"] = 8894,
1956 ["lrtri"] = 8895,
1957 ["Wedge"] = 8896,
1958 ["bigwedge"] = 8896,
1959 ["xwedge"] = 8896,
1960 ["Vee"] = 8897,
1961 ["bigvee"] = 8897,
1962 ["xvee"] = 8897,
1963 ["Intersection"] = 8898,
1964 ["bigcap"] = 8898,
1965 ["xcap"] = 8898,
1966 ["Union"] = 8899,
1967 ["bigcup"] = 8899,
1968 ["xcup"] = 8899,
1969 ["Diamond"] = 8900,
1970 ["diam"] = 8900,
1971 ["diamond"] = 8900,
1972 ["sdot"] = 8901,
1973 ["Star"] = 8902,
1974 ["sstarf"] = 8902,
1975 ["divideontimes"] = 8903,
1976 ["divonx"] = 8903,
1977 ["bowtie"] = 8904,
1978 ["ltimes"] = 8905,
1979 ["rtimes"] = 8906,
1980 ["leftthreetimes"] = 8907,
1981 ["lthree"] = 8907,
1982 ["rightthreetimes"] = 8908,
1983 ["rthree"] = 8908,
1984 ["backsimeq"] = 8909,
1985 ["bsime"] = 8909,
1986 ["curlyvee"] = 8910,
1987 ["cuvee"] = 8910,
1988 ["curlywedge"] = 8911,
1989 ["cuwed"] = 8911,
1990 ["Sub"] = 8912,
1991 ["Subset"] = 8912,
1992 ["Sup"] = 8913,
1993 ["Supset"] = 8913,
1994 ["Cap"] = 8914,
1995 ["Cup"] = 8915,
1996 ["fork"] = 8916,
1997 ["pitchfork"] = 8916,
1998 ["epar"] = 8917,
1999 ["lessdot"] = 8918,
2000 ["ltdot"] = 8918,
2001 ["gtdot"] = 8919,
2002 ["gtrdot"] = 8919,
2003 ["Ll"] = 8920,
2004 ["nLl"] = {8920, 824},
2005 ["Gg"] = 8921,
2006 ["ggg"] = 8921,
2007 ["nGg"] = {8921, 824},
2008 ["LessEqualGreater"] = 8922,
2009 ["leg"] = 8922,
2010 ["lesg"] = {8922, 65024},
2011 ["lesseqgtr"] = 8922,
2012 ["GreaterEqualLess"] = 8923,
2013 ["gel"] = 8923,
2014 ["gesl"] = {8923, 65024},
2015 ["gtreqless"] = 8923,
2016 ["cuepr"] = 8926,
2017 ["curlyeqprec"] = 8926,
2018 ["cuesc"] = 8927,
2019 ["curlyeqsucc"] = 8927,
2020 ["NotPrecedesSlantEqual"] = 8928,
2021 ["nprcue"] = 8928,
2022 ["NotSucceedsSlantEqual"] = 8929,
2023 ["nsccue"] = 8929,
2024 ["NotSquareSubsetEqual"] = 8930,
2025 ["nsqsube"] = 8930,
2026 ["NotSquareSupersetEqual"] = 8931,
2027 ["nsqsupe"] = 8931,
2028 ["lnsim"] = 8934,
2029 ["gnsim"] = 8935,
2030 ["precnsim"] = 8936,
2031 ["prnsim"] = 8936,
2032 ["scnsim"] = 8937,
2033 ["succnsim"] = 8937,
2034 ["NotLeftTriangle"] = 8938,
2035 ["nltri"] = 8938,
2036 ["ntriangleleft"] = 8938,
2037 ["NotRightTriangle"] = 8939,
2038 ["nrtri"] = 8939,
2039 ["ntriangleright"] = 8939,
2040 ["NotLeftTriangleEqual"] = 8940,
2041 ["nltrie"] = 8940,
2042 ["ntrianglelefteq"] = 8940,
2043 ["NotRightTriangleEqual"] = 8941,
2044 ["nrtrie"] = 8941,
2045 ["ntrianglerighteq"] = 8941,
2046 ["vellip"] = 8942,
2047 ["ctdot"] = 8943,
2048 ["utdot"] = 8944,
2049 ["dtdot"] = 8945,
2050 ["disin"] = 8946,
2051 ["isinsv"] = 8947,
2052 ["isins"] = 8948,
2053 ["isindot"] = 8949,
2054 ["notindot"] = {8949, 824},
2055 ["notinvc"] = 8950,
2056 ["notinvb"] = 8951,
2057 ["isinE"] = 8953,
2058 ["notinE"] = {8953, 824},
2059 ["nisd"] = 8954,
2060 ["xnis"] = 8955,
2061 ["nis"] = 8956,
2062 ["notnivc"] = 8957,
2063 ["notnivb"] = 8958,
2064 ["barwed"] = 8965,
2065 ["barwedge"] = 8965,
2066 ["Barwed"] = 8966,
2067 ["doublebarwedge"] = 8966,
2068 ["LeftCeiling"] = 8968,
2069 ["lceil"] = 8968,
2070 ["RightCeiling"] = 8969,
2071 ["rceil"] = 8969,
2072 ["LeftFloor"] = 8970,
2073 ["lfloor"] = 8970,
2074 ["RightFloor"] = 8971,
2075 ["rfloor"] = 8971,
2076 ["drcrop"] = 8972,
2077 ["dlcrop"] = 8973,
2078 ["urcrop"] = 8974,
2079 ["ulcrop"] = 8975,
2080 ["bnot"] = 8976,
2081 ["profline"] = 8978,
2082 ["profsurf"] = 8979,
2083 ["telrec"] = 8981,
2084 ["target"] = 8982,
2085 ["ulcorn"] = 8988,
2086 ["ulcorner"] = 8988,
2087 ["urcorn"] = 8989,
2088 ["urcorner"] = 8989,
2089 ["dlcorn"] = 8990,
2090 ["llcorner"] = 8990,
2091 ["drcorn"] = 8991,
2092 ["lrcorner"] = 8991,
2093 ["frown"] = 8994,
2094 ["sfrown"] = 8994,
2095 ["smile"] = 8995,
2096 ["ssmile"] = 8995,
2097 ["cylcty"] = 9005,
2098 ["profalar"] = 9006,
2099 ["topbot"] = 9014,
2100 ["ovbar"] = 9021,
2101 ["solbar"] = 9023,
2102 ["angzarr"] = 9084,
2103 ["lmoust"] = 9136,
2104 ["lmoustache"] = 9136,
2105 ["rmoust"] = 9137,
2106 ["rmoustache"] = 9137,
2107 ["OverBracket"] = 9140,
2108 ["tbrk"] = 9140,
2109 ["UnderBracket"] = 9141,
2110 ["bbrk"] = 9141,
2111 ["bbrktbrk"] = 9142,
2112 ["OverParenthesis"] = 9180,
2113 ["UnderParenthesis"] = 9181,
2114 ["OverBrace"] = 9182,
2115 ["UnderBrace"] = 9183,
2116 ["trpezium"] = 9186,
2117 ["elinters"] = 9191,
2118 ["blank"] = 9251,
2119 ["circledS"] = 9416,
2120 ["oS"] = 9416,
2121 ["HorizontalLine"] = 9472,
2122 ["boxh"] = 9472,
2123 ["boxv"] = 9474,
2124 ["boxdr"] = 9484,
2125 ["boxdl"] = 9488,
2126 ["boxur"] = 9492,
2127 ["boxul"] = 9496,
2128 ["boxvr"] = 9500,
2129 ["boxvl"] = 9508,
2130 ["boxhd"] = 9516,
2131 ["boxhu"] = 9524,
2132 ["boxvh"] = 9532,
2133 ["boxH"] = 9552,
2134 ["boxV"] = 9553,
2135 ["boxdR"] = 9554,
2136 ["boxDr"] = 9555,
2137 ["boxDR"] = 9556,
2138 ["boxdL"] = 9557,
2139 ["boxDl"] = 9558,
2140 ["boxDL"] = 9559,
2141 ["boxuR"] = 9560,
2142 ["boxUr"] = 9561,
2143 ["boxUR"] = 9562,
2144 ["boxuL"] = 9563,
2145 ["boxUl"] = 9564,
2146 ["boxUL"] = 9565,
2147 ["boxvR"] = 9566,
2148 ["boxVr"] = 9567,
2149 ["boxVR"] = 9568,
2150 ["boxvL"] = 9569,
2151 ["boxVl"] = 9570,
2152 ["boxVL"] = 9571,
2153 ["boxHd"] = 9572,
2154 ["boxhD"] = 9573,
2155 ["boxHD"] = 9574,
2156 ["boxHu"] = 9575,
2157 ["boxhU"] = 9576,
2158 ["boxHU"] = 9577,
2159 ["boxvH"] = 9578,
2160 ["boxVh"] = 9579,
2161 ["boxVH"] = 9580,
2162 ["uhblk"] = 9600,
2163 ["lhblk"] = 9604,
2164 ["block"] = 9608,
2165 ["blk14"] = 9617,
2166 ["blk12"] = 9618,
2167 ["blk34"] = 9619,
2168 ["Square"] = 9633,
2169 ["squ"] = 9633,
2170 ["square"] = 9633,
2171 ["FilledVerySmallSquare"] = 9642,
2172 ["blacksquare"] = 9642,
2173 ["squarf"] = 9642,
2174 ["squf"] = 9642,
2175 ["EmptyVerySmallSquare"] = 9643,
2176 ["rect"] = 9645,
2177 ["marker"] = 9646,
2178 ["fltns"] = 9649,
2179 ["bigtriangleup"] = 9651,
2180 ["xutri"] = 9651,
2181 ["blacktriangle"] = 9652,
2182 ["utrif"] = 9652,
2183 ["triangle"] = 9653,
2184 ["utri"] = 9653,
2185 ["blacktriangleright"] = 9656,
2186 ["rtrif"] = 9656,
2187 ["rtri"] = 9657,
2188 ["triangleright"] = 9657,
2189 ["bigtriangledown"] = 9661,
2190 ["xdtri"] = 9661,
2191 ["blacktriangledown"] = 9662,
2192 ["dtrif"] = 9662,
2193 ["dtri"] = 9663,
2194 ["triangledown"] = 9663,
2195 ["blacktriangleleft"] = 9666,
2196 ["ltrif"] = 9666,
2197 ["ltri"] = 9667,
2198 ["triangleleft"] = 9667,
2199 ["loz"] = 9674,
2200 ["lozenge"] = 9674,
2201 ["cir"] = 9675,
2202 ["tridot"] = 9708,
2203 ["bigcirc"] = 9711,
2204 ["xcirc"] = 9711,
2205 ["ultri"] = 9720,
2206 ["urtri"] = 9721,
2207 ["lltri"] = 9722,
2208 ["EmptySmallSquare"] = 9723,
2209 ["FilledSmallSquare"] = 9724,
2210 ["bigstar"] = 9733,
2211 ["starf"] = 9733,
2212 ["star"] = 9734,
2213 ["phone"] = 9742,
2214 ["female"] = 9792,
2215 ["male"] = 9794,
2216 ["spades"] = 9824,
2217 ["spadesuit"] = 9824,
2218 ["clubs"] = 9827,
2219 ["clubsuit"] = 9827,
2220 ["hearts"] = 9829,
2221 ["heartsuit"] = 9829,
2222 ["diamondsuit"] = 9830,
2223 ["diams"] = 9830,
2224 ["sung"] = 9834,
2225 ["flat"] = 9837,
2226 ["natur"] = 9838,
2227 ["natural"] = 9838,
2228 ["sharp"] = 9839,
2229 ["check"] = 10003,
2230 ["checkmark"] = 10003,
2231 ["cross"] = 10007,
2232 ["malt"] = 10016,
2233 ["maltese"] = 10016,
2234 ["sext"] = 10038,
2235 ["VerticalSeparator"] = 10072,
2236 ["lbbrk"] = 10098,
2237 ["rbbrk"] = 10099,
2238 ["bsolhsub"] = 10184,
2239 ["suphsol"] = 10185,
2240 ["LeftDoubleBracket"] = 10214,
2241 ["lobrk"] = 10214,
2242 ["RightDoubleBracket"] = 10215,
2243 ["robrk"] = 10215,
2244 ["LeftAngleBracket"] = 10216,
2245 ["lang"] = 10216,
2246 ["langle"] = 10216,
2247 ["RightAngleBracket"] = 10217,
2248 ["rang"] = 10217,
2249 ["rangle"] = 10217,
2250 ["Lang"] = 10218,
2251 ["Rang"] = 10219,
2252 ["loang"] = 10220,
2253 ["roang"] = 10221,
2254 ["LongLeftArrow"] = 10229,
2255 ["longleftarrow"] = 10229,
2256 ["xlarr"] = 10229,
2257 ["LongRightArrow"] = 10230,
2258 ["longrightarrow"] = 10230,
2259 ["xrarr"] = 10230,
2260 ["LongLeftRightArrow"] = 10231,
2261 ["longleftrightarrow"] = 10231,
2262 ["xharr"] = 10231,
2263 ["DoubleLongLeftArrow"] = 10232,
2264 ["Longleftarrow"] = 10232,
2265 ["xlArr"] = 10232,
2266 ["DoubleLongRightArrow"] = 10233,
2267 ["Longrightarrow"] = 10233,
2268 ["xrArr"] = 10233,
2269 ["DoubleLongLeftRightArrow"] = 10234,
2270 ["Longleftrightarrow"] = 10234,
2271 ["xhArr"] = 10234,
2272 ["longmapsto"] = 10236,
2273 ["xmap"] = 10236,
2274 ["dzigrarr"] = 10239,
2275 ["nvlArr"] = 10498,
2276 ["nvrArr"] = 10499,
2277 ["nvHarr"] = 10500,
2278 ["Map"] = 10501,
2279 ["lbarr"] = 10508,
2280 ["bkarow"] = 10509,
2281 ["rbarr"] = 10509,
2282 ["lBarr"] = 10510,
2283 ["dbkarow"] = 10511,
2284 ["rBarr"] = 10511,
2285 ["RBarr"] = 10512,
2286 ["drbkarow"] = 10512,
2287 ["DDotrahd"] = 10513,
2288 ["UpArrowBar"] = 10514,
2289 ["DownArrowBar"] = 10515,
2290 ["Rarrtl"] = 10518,
2291 ["latail"] = 10521,
2292 ["ratail"] = 10522,
2293 ["lAtail"] = 10523,
2294 ["rAtail"] = 10524,
2295 ["larrfs"] = 10525,
2296 ["rarrfs"] = 10526,
2297 ["larrbfs"] = 10527,
2298 ["rarrbfs"] = 10528,
2299 ["nwarhk"] = 10531,
2300 ["nearhk"] = 10532,
2301 ["hksearow"] = 10533,
2302 ["searhk"] = 10533,
2303 ["hkswarow"] = 10534,
2304 ["swarhk"] = 10534,
2305 ["nwnear"] = 10535,
2306 ["nesear"] = 10536,
2307 ["toea"] = 10536,
2308 ["seswar"] = 10537,
2309 ["tosa"] = 10537,
2310 ["swnwar"] = 10538,
2311 ["nrarrc"] = {10547, 824},
2312 ["rarrc"] = 10547,
2313 ["cudarrr"] = 10549,
2314 ["ldca"] = 10550,
2315 ["rdca"] = 10551,
2316 ["cudarrl"] = 10552,
2317 ["larrpl"] = 10553,
2318 ["curarrm"] = 10556,
2319 ["cularrp"] = 10557,
2320 ["rarrpl"] = 10565,
2321 ["harrcir"] = 10568,
2322 ["Uarrocir"] = 10569,
2323 ["lurdshar"] = 10570,
2324 ["ldrushar"] = 10571,
2325 ["LeftRightVector"] = 10574,
2326 ["RightUpDownVector"] = 10575,
2327 ["DownLeftRightVector"] = 10576,
2328 ["LeftUpDownVector"] = 10577,
2329 ["LeftVectorBar"] = 10578,
2330 ["RightVectorBar"] = 10579,
2331 ["RightUpVectorBar"] = 10580,
2332 ["RightDownVectorBar"] = 10581,
2333 ["DownLeftVectorBar"] = 10582,
2334 ["DownRightVectorBar"] = 10583,
2335 ["LeftUpVectorBar"] = 10584,
2336 ["LeftDownVectorBar"] = 10585,
2337 ["LeftTeeVector"] = 10586,
2338 ["RightTeeVector"] = 10587,
2339 ["RightUpTeeVector"] = 10588,
2340 ["RightDownTeeVector"] = 10589,
2341 ["DownLeftTeeVector"] = 10590,
2342 ["DownRightTeeVector"] = 10591,
2343 ["LeftUpTeeVector"] = 10592,
2344 ["LeftDownTeeVector"] = 10593,
2345 ["lHar"] = 10594,
2346 ["uHar"] = 10595,
2347 ["rHar"] = 10596,
2348 ["dHar"] = 10597,
2349 ["luruhar"] = 10598,
2350 ["ldrdhar"] = 10599,
2351 ["ruluhar"] = 10600,
2352 ["rdldhar"] = 10601,
2353 ["lharul"] = 10602,
2354 ["llhard"] = 10603,
2355 ["rharul"] = 10604,
2356 ["lrhard"] = 10605,
2357 ["UpEquilibrium"] = 10606,
2358 ["udhar"] = 10606,
2359 ["ReverseUpEquilibrium"] = 10607,
2360 ["duhar"] = 10607,
2361 ["RoundImplies"] = 10608,
2362 ["erarr"] = 10609,
2363 ["simrarr"] = 10610,
2364 ["larrsim"] = 10611,
2365 ["rarrsim"] = 10612,
2366 ["rarrap"] = 10613,
2367 ["ltlarr"] = 10614,
2368 ["gtrarr"] = 10616,
2369 ["subrarr"] = 10617,
2370 ["suplarr"] = 10619,
2371 ["lfisht"] = 10620,
2372 ["rfisht"] = 10621,
2373 ["ufisht"] = 10622,
2374 ["dfisht"] = 10623,
2375 ["lopar"] = 10629,
2376 ["ropar"] = 10630,
2377 ["lbrke"] = 10635,
2378 ["rbrke"] = 10636,
2379 ["lbrkslu"] = 10637,
2380 ["rbrksld"] = 10638,
2381 ["lbrksld"] = 10639,
2382 ["rbrkslu"] = 10640,
2383 ["langd"] = 10641,
2384 ["rangd"] = 10642,
2385 ["lparlt"] = 10643,
2386 ["rpargt"] = 10644,
2387 ["gtlPar"] = 10645,
2388 ["ltrPar"] = 10646,
2389 ["vzigzag"] = 10650,
2390 ["vangrt"] = 10652,
2391 ["angrtvbd"] = 10653,
2392 ["ange"] = 10660,
2393 ["range"] = 10661,
2394 ["dwangle"] = 10662,
2395 ["uwangle"] = 10663,
2396 ["angmsdaa"] = 10664,
2397 ["angmsdab"] = 10665,
2398 ["angmsdac"] = 10666,
2399 ["angmsdad"] = 10667,
2400 ["angmsdae"] = 10668,
2401 ["angmsdaf"] = 10669,
2402 ["angmsdag"] = 10670,
2403 ["angmsdah"] = 10671,
2404 ["bemptyv"] = 10672,
2405 ["demptyv"] = 10673,
2406 ["cemptyv"] = 10674,
2407 ["raemptyv"] = 10675,
2408 ["laemptyv"] = 10676,
2409 ["ohbar"] = 10677,
2410 ["omid"] = 10678,
2411 ["opar"] = 10679,
2412 ["operp"] = 10681,
2413 ["olcross"] = 10683,
2414 ["odsold"] = 10684,
2415 ["olcir"] = 10686,
2416 ["ofcir"] = 10687,
2417 ["olt"] = 10688,
2418 ["ogt"] = 10689,
2419 ["cirscir"] = 10690,
2420 ["cirE"] = 10691,
2421 ["solb"] = 10692,
2422 ["bsolb"] = 10693,
2423 ["boxbox"] = 10697,
2424 ["trisb"] = 10701,
2425 ["rtriltri"] = 10702,
2426 ["LeftTriangleBar"] = 10703,
2427 ["NotLeftTriangleBar"] = {10703, 824},
2428 ["NotRightTriangleBar"] = {10704, 824},
2429 ["RightTriangleBar"] = 10704,
2430 ["iinfin"] = 10716,
2431 ["infintie"] = 10717,
2432 ["nvinfin"] = 10718,
2433 ["eparsl"] = 10723,
2434 ["smeparsl"] = 10724,
2435 ["eqvparsl"] = 10725,
2436 ["blacklozenge"] = 10731,
2437 ["lozf"] = 10731,
2438 ["RuleDelayed"] = 10740,
2439 ["dsol"] = 10742,
2440 ["bigodot"] = 10752,
2441 ["xodot"] = 10752,
2442 ["bigoplus"] = 10753,
2443 ["xoplus"] = 10753,
2444 ["bigotimes"] = 10754,
2445 ["xotime"] = 10754,
2446 ["biguplus"] = 10756,
2447 ["xuplus"] = 10756,
2448 ["bigsqcup"] = 10758,
2449 ["xsqcup"] = 10758,
2450 ["iiiint"] = 10764,
2451 ["qint"] = 10764,
2452 ["fpartint"] = 10765,
2453 ["cirfnint"] = 10768,
2454 ["awint"] = 10769,
2455 ["rppolint"] = 10770,
2456 ["scpolint"] = 10771,
2457 ["npolint"] = 10772,
2458 ["pointint"] = 10773,
2459 ["quatint"] = 10774,
2460 ["intlarhk"] = 10775,
2461 ["pluscir"] = 10786,
2462 ["plusacir"] = 10787,
2463 ["simplus"] = 10788,
2464 ["plusdu"] = 10789,
2465 ["plussim"] = 10790,
2466 ["plustwo"] = 10791,
2467 ["mcomma"] = 10793,
2468 ["minusdu"] = 10794,
2469 ["loplus"] = 10797,
2470 ["roplus"] = 10798,
2471 ["Cross"] = 10799,
2472 ["timesd"] = 10800,
2473 ["timesbar"] = 10801,
2474 ["smashp"] = 10803,
2475 ["lotimes"] = 10804,
2476 ["rotimes"] = 10805,
2477 ["otimesas"] = 10806,
2478 ["Otimes"] = 10807,
2479 ["odiv"] = 10808,
2480 ["triplus"] = 10809,
2481 ["triminus"] = 10810,
2482 ["tritime"] = 10811,
2483 ["intprod"] = 10812,
2484 ["iprod"] = 10812,
2485 ["amalg"] = 10815,
2486 ["capdot"] = 10816,
2487 ["ncup"] = 10818,
2488 ["ncap"] = 10819,
2489 ["capand"] = 10820,
2490 ["cupor"] = 10821,
2491 ["cupcap"] = 10822,
2492 ["capcup"] = 10823,
2493 ["cupbrcap"] = 10824,
2494 ["capbrcup"] = 10825,
2495 ["cupcup"] = 10826,
2496 ["capcap"] = 10827,
2497 ["ccups"] = 10828,
2498 ["ccaps"] = 10829,
2499 ["ccupssm"] = 10832,
2500 ["And"] = 10835,
2501 ["Or"] = 10836,
2502 ["andand"] = 10837,
2503 ["oror"] = 10838,
2504 ["orslope"] = 10839,
2505 ["andslope"] = 10840,
2506 ["andv"] = 10842,
2507 ["orv"] = 10843,
2508 ["andd"] = 10844,
2509 ["ord"] = 10845,
2510 ["wedbar"] = 10847,
2511 ["sdote"] = 10854,
2512 ["simdot"] = 10858,
2513 ["congdot"] = 10861,
2514 ["ncongdot"] = {10861, 824},
2515 ["easter"] = 10862,
2516 ["apacir"] = 10863,
2517 ["apE"] = 10864,
2518 ["napE"] = {10864, 824},
2519 ["eplus"] = 10865,
2520 ["pluse"] = 10866,
2521 ["Esim"] = 10867,
2522 ["Colone"] = 10868,
2523 ["Equal"] = 10869,
2524 ["ddotseq"] = 10871,
2525 ["eDDot"] = 10871,
2526 ["equivDD"] = 10872,
2527 ["ltcir"] = 10873,
2528 ["gtcir"] = 10874,
2529 ["ltquest"] = 10875,
2530 ["gtquest"] = 10876,
2531 ["LessSlantEqual"] = 10877,
2532 ["NotLessSlantEqual"] = {10877, 824},
2533 ["leqslant"] = 10877,
2534 ["les"] = 10877,
2535 ["nleqslant"] = {10877, 824},
2536 ["nles"] = {10877, 824},
2537 ["GreaterSlantEqual"] = 10878,
2538 ["NotGreaterSlantEqual"] = {10878, 824},
2539 ["geqslant"] = 10878,
2540 ["ges"] = 10878,
2541 ["ngeqslant"] = {10878, 824},
2542 ["nges"] = {10878, 824},
2543 ["lesdot"] = 10879,
2544 ["gesdot"] = 10880,
2545 ["lesdoto"] = 10881,
2546 ["gesdoto"] = 10882,
2547 ["lesdotor"] = 10883,
2548 ["gesdotol"] = 10884,
2549 ["lap"] = 10885,
2550 ["lessapprox"] = 10885,
2551 ["gap"] = 10886,
2552 ["gtrapprox"] = 10886,
2553 ["lne"] = 10887,
2554 ["lneq"] = 10887,
2555 ["gne"] = 10888,
2556 ["gneq"] = 10888,
2557 ["lnap"] = 10889,
2558 ["lnapprox"] = 10889,
2559 ["gnap"] = 10890,
2560 ["gnapprox"] = 10890,
2561 ["lEg"] = 10891,
2562 ["lesseqqgtr"] = 10891,
2563 ["gEl"] = 10892,
2564 ["gtreqqless"] = 10892,
2565 ["lsime"] = 10893,
2566 ["gsime"] = 10894,
2567 ["lsimg"] = 10895,
2568 ["gsiml"] = 10896,
2569 ["lgE"] = 10897,
2570 ["glE"] = 10898,
2571 ["lesges"] = 10899,
2572 ["gesles"] = 10900,
2573 ["els"] = 10901,
2574 ["eqslantless"] = 10901,
2575 ["egs"] = 10902,
2576 ["eqslantgtr"] = 10902,
2577 ["elsdot"] = 10903,
2578 ["egsdot"] = 10904,
2579 ["el"] = 10905,
2580 ["eg"] = 10906,
2581 ["siml"] = 10909,
2582 ["simg"] = 10910,
2583 ["simlE"] = 10911,
2584 ["simgE"] = 10912,
2585 ["LessLess"] = 10913,
2586 ["NotNestedLessLess"] = {10913, 824},
2587 ["GreaterGreater"] = 10914,
2588 ["NotNestedGreaterGreater"] = {10914, 824},
2589 ["glj"] = 10916,
2590 ["gla"] = 10917,
2591 ["ltcc"] = 10918,
2592 ["gtcc"] = 10919,
2593 ["lescc"] = 10920,
2594 ["gescc"] = 10921,
2595 ["smt"] = 10922,
2596 ["lat"] = 10923,
2597 ["smte"] = 10924,
2598 ["smtes"] = {10924, 65024},
2599 ["late"] = 10925,
2600 ["lates"] = {10925, 65024},
2601 ["bumpE"] = 10926,
2602 ["NotPrecedesEqual"] = {10927, 824},
2603 ["PrecedesEqual"] = 10927,
2604 ["npre"] = {10927, 824},
2605 ["npreceq"] = {10927, 824},
2606 ["pre"] = 10927,
2607 ["preceq"] = 10927,
2608 ["NotSucceedsEqual"] = {10928, 824},
2609 ["SucceedsEqual"] = 10928,
2610 ["nsce"] = {10928, 824},
2611 ["nsucceq"] = {10928, 824},
2612 ["sce"] = 10928,
2613 ["succeq"] = 10928,
2614 ["prE"] = 10931,
2615 ["scE"] = 10932,
2616 ["precneqq"] = 10933,
2617 ["prnE"] = 10933,
2618 ["scnE"] = 10934,
2619 ["succneqq"] = 10934,
2620 ["prap"] = 10935,
2621 ["precapprox"] = 10935,
2622 ["scap"] = 10936,
2623 ["succapprox"] = 10936,
2624 ["precnapprox"] = 10937,
2625 ["prnap"] = 10937,
2626 ["scnap"] = 10938,
2627 ["succnapprox"] = 10938,
2628 ["Pr"] = 10939,
2629 ["Sc"] = 10940,
2630 ["subdot"] = 10941,
2631 ["supdot"] = 10942,
2632 ["subplus"] = 10943,
2633 ["supplus"] = 10944,
2634 ["submult"] = 10945,
2635 ["supmult"] = 10946,
2636 ["subedot"] = 10947,
2637 ["supedot"] = 10948,
2638 ["nsubE"] = {10949, 824},
2639 ["nsubseteqq"] = {10949, 824},
2640 ["subE"] = 10949,
2641 ["subseteqq"] = 10949,
2642 ["nsupE"] = {10950, 824},
2643 ["nsupseteqq"] = {10950, 824},
2644 ["supE"] = 10950,
2645 ["supseteqq"] = 10950,
2646 ["subsim"] = 10951,
2647 ["supsim"] = 10952,
2648 ["subnE"] = 10955,
2649 ["subsetneqq"] = 10955,
2650 ["varsubsetneqq"] = {10955, 65024},
2651 ["vsubnE"] = {10955, 65024},
2652 ["supnE"] = 10956,
2653 ["supsetneqq"] = 10956,
2654 ["varsupsetneqq"] = {10956, 65024},
2655 ["vsupnE"] = {10956, 65024},
2656 ["csub"] = 10959,
2657 ["csup"] = 10960,
2658 ["csube"] = 10961,
2659 ["csupe"] = 10962,
2660 ["subsup"] = 10963,
2661 ["supsub"] = 10964,
2662 ["subsub"] = 10965,
2663 ["supsup"] = 10966,
2664 ["suphsub"] = 10967,
2665 ["supdsub"] = 10968,
2666 ["forkv"] = 10969,
2667 ["topfork"] = 10970,
2668 ["mlcp"] = 10971,
2669 ["Dashv"] = 10980,
2670 ["DoubleLeftTee"] = 10980,
2671 ["Vdashl"] = 10982,
2672 ["Barv"] = 10983,
2673 ["vBar"] = 10984,
2674 ["vBarv"] = 10985,
2675 ["Vbar"] = 10987,
2676 ["Not"] = 10988,
2677 ["bNot"] = 10989,
2678 ["rnmid"] = 10990,
2679 ["cirmid"] = 10991,
2680 ["midcir"] = 10992,
2681 ["topcir"] = 10993,
2682 ["nhpar"] = 10994,
2683 ["parsim"] = 10995,
2684 ["nparsl"] = {11005, 8421},
2685 ["parsl"] = 11005,
2686 ["fflig"] = 64256,
2687 ["filig"] = 64257,
2688 ["fllig"] = 64258,
2689 ["ffilig"] = 64259,
2690 ["ffllig"] = 64260,
2691 ["Ascr"] = 119964,
2692 ["Cscr"] = 119966,
2693 ["Dscr"] = 119967,
2694 ["Gscr"] = 119970,
2695 ["Jscr"] = 119973,
2696 ["Kscr"] = 119974,
2697 ["Nscr"] = 119977,
2698 ["Oscr"] = 119978,
2699 ["Pscr"] = 119979,
2700 ["Qscr"] = 119980,
2701 ["Sscr"] = 119982,
2702 ["Tscr"] = 119983,
2703 ["Uscr"] = 119984,
2704 ["Vscr"] = 119985,
2705 ["Wscr"] = 119986,
2706 ["Xscr"] = 119987,
2707 ["Yscr"] = 119988,
2708 ["Zscr"] = 119989,
2709 ["ascr"] = 119990,
2710 ["bscr"] = 119991,
2711 ["cscr"] = 119992,
2712 ["dscr"] = 119993,
2713 ["fscr"] = 119995,
2714 ["hscr"] = 119997,
2715 ["iscr"] = 119998,
2716 ["jscr"] = 119999,
2717 ["kscr"] = 120000,
2718 ["lscr"] = 120001,
2719 ["mscr"] = 120002,
2720 ["nscr"] = 120003,
2721 ["pscr"] = 120005,
2722 ["qscr"] = 120006,
2723 ["rscr"] = 120007,
2724 ["sscr"] = 120008,
2725 ["tscr"] = 120009,
2726 ["uscr"] = 120010,
2727 ["vscr"] = 120011,
2728 ["wscr"] = 120012,
2729 ["xscr"] = 120013,
2730 ["yscr"] = 120014,
2731 ["zscr"] = 120015,
2732 ["Afr"] = 120068,
2733 ["Bfr"] = 120069,
2734 ["Dfr"] = 120071,
2735 ["Efr"] = 120072,
2736 ["Ffr"] = 120073,
2737 ["Gfr"] = 120074,
2738 ["Jfr"] = 120077,
2739 ["Kfr"] = 120078,
2740 ["Lfr"] = 120079,
2741 ["Mfr"] = 120080,
2742 ["Nfr"] = 120081,
2743 ["Ofr"] = 120082,
2744 ["Pfr"] = 120083,
2745 ["Qfr"] = 120084,
2746 ["Sfr"] = 120086,
2747 ["Tfr"] = 120087,
2748 ["Ufr"] = 120088,
2749 ["Vfr"] = 120089,
2750 ["Wfr"] = 120090,
2751 ["Xfr"] = 120091,
2752 ["Yfr"] = 120092,
2753 ["afr"] = 120094,
2754 ["bfr"] = 120095,
2755 ["cfr"] = 120096,
2756 ["dfr"] = 120097,
2757 ["efr"] = 120098,
2758 ["ffr"] = 120099,
2759 ["gfr"] = 120100,
2760 ["hfr"] = 120101,
2761 ["ifr"] = 120102,
2762 ["jfr"] = 120103,
2763 ["kfr"] = 120104,
2764 ["lfr"] = 120105,
2765 ["mfr"] = 120106,
2766 ["nfr"] = 120107,
2767 ["ofr"] = 120108,
2768 ["pfr"] = 120109,
2769 ["qfr"] = 120110,
2770 ["rfr"] = 120111,
2771 ["sfr"] = 120112,
2772 ["tfr"] = 120113,
2773 ["ufr"] = 120114,
2774 ["vfr"] = 120115,
2775 ["wfr"] = 120116,
2776 ["xfr"] = 120117,
2777 ["yfr"] = 120118,
2778 ["zfr"] = 120119,
2779 ["Aopf"] = 120120,
2780 ["Bopf"] = 120121,
2781 ["Dopf"] = 120123,
2782 ["Eopf"] = 120124,
2783 ["Fopf"] = 120125,
2784 ["Gopf"] = 120126,
2785 ["Iopf"] = 120128,
2786 ["Jopf"] = 120129,
2787 ["Kopf"] = 120130,
2788 ["Lopf"] = 120131,
2789 ["Mopf"] = 120132,
2790 ["Oopf"] = 120134,
2791 ["Sopf"] = 120138,
2792 ["Topf"] = 120139,
2793 ["Uopf"] = 120140,
2794 ["Vopf"] = 120141,
2795 ["Wopf"] = 120142,
2796 ["Xopf"] = 120143,
2797 ["Yopf"] = 120144,
2798 ["aopf"] = 120146,
2799 ["bopf"] = 120147,
2800 ["copf"] = 120148,
2801 ["dopf"] = 120149,
2802 ["eopf"] = 120150,
2803 ["fopf"] = 120151,
2804 ["gopf"] = 120152,
2805 ["hopf"] = 120153,
2806 ["iopf"] = 120154,
2807 ["jopf"] = 120155,
2808 ["kopf"] = 120156,
2809 ["lopf"] = 120157,
2810 ["mopf"] = 120158,
2811 ["nopf"] = 120159,
2812 ["oopf"] = 120160,
2813 ["popf"] = 120161,
2814 ["qopf"] = 120162,
2815 ["ropf"] = 120163,
2816 ["sopf"] = 120164,
2817 ["topf"] = 120165,
2818 ["uopf"] = 120166,
2819 ["vopf"] = 120167,
2820 ["wopf"] = 120168,
2821 ["xopf"] = 120169,
2822 ["yopf"] = 120170,
2823 ["zopf"] = 120171,
2824 }
2825 function entities.dec_entity(s)
2826 local n = tonumber(s)
2827 if n == nil then
2828 return "&#" .. s .. ";" -- fallback for unknown entities
2829 end
2830 return utf8.char(n)
2831 end
2832 function entities.hex_entity(s)
2833 local n = tonumber("0x"..s)
2834 if n == nil then
2835 return "&#x" .. s .. ";" -- fallback for unknown entities
2836 end
2837 return utf8.char(n)
2838 end
2839 function entities.hex_entity_with_x_char(x, s)
2840 local n = tonumber("0x"..s)
2841 if n == nil then
2842 return "&#" .. x .. s .. ";" -- fallback for unknown entities
2843 end
2844 return utf8.char(n)
2845 end
2846 function entities.char_entity(s)
2847 local code_points = character_entities[s]
2848 if code_points == nil then
2849 return "&" .. s .. ";"
2850 end
2851 if type(code_points) ~= 'table' then
2852 code_points = {code_points}
2853 end
2854 local char_table = {}
2855 for _, code_point in ipairs(code_points) do
2856 table.insert(char_table, utf8.char(code_point))
2857 end
2858 return table.concat(char_table)
2859 end
2860 M.writer = {}
2861 local parsers
2862 function M.writer.new(options)
2863 local self = {}
2864 self.options = options
2865 self.flatten_inlines = false
2866 local slice_specifiers = {}
2867 for specifier in options.slice:gmatch("[^%s]+") do
2868 table.insert(slice_specifiers, specifier)
2869 end
2870
2871 if #slice_specifiers == 2 then
2872 self.slice_begin, self.slice_end = table.unpack(slice_specifiers)
2873 local slice_begin_type = self.slice_begin:sub(1, 1)
2874 if slice_begin_type ~= "^" and slice_begin_type ~= "$" then
2875 self.slice_begin = "^" .. self.slice_begin
2876 end
2877 local slice_end_type = self.slice_end:sub(1, 1)
2878 if slice_end_type ~= "^" and slice_end_type ~= "$" then
2879 self.slice_end = "$" .. self.slice_end
2880 end
2881 elseif #slice_specifiers == 1 then
2882 self.slice_begin = "^" .. slice_specifiers[1]
2883 self.slice_end = "$" .. slice_specifiers[1]
2884 end
2885
2886 self.slice_begin_type = self.slice_begin:sub(1, 1)
2887 self.slice_begin_identifier = self.slice_begin:sub(2) or ""
2888 self.slice_end_type = self.slice_end:sub(1, 1)
2889 self.slice_end_identifier = self.slice_end:sub(2) or ""
2890
2891 if self.slice_begin == "^" and self.slice_end ~= "^" then
2892 self.is_writing = true
2893 else
2894 self.is_writing = false
2895 end
2896 self.space = " "
2897 self.nbsp = "\\markdownRendererNbsp{}"
2898 function self.plain(s)
2899 return s
2900 end
2901 function self.paragraph(s)
2902 if not self.is_writing then return "" end
2903 return s
2904 end
2905 self.interblocksep_text = "\\markdownRendererInterblockSeparator\n{}"
2906 function self.interblocksep()
2907 if not self.is_writing then return "" end
2908 return self.interblocksep_text
2909 end
2910 self.paragraphsep_text = "\\markdownRendererParagraphSeparator\n{}"
2911 function self.paragraphsep()
2912 if not self.is_writing then return "" end
2913 return self.paragraphsep_text
2914 end
2915 self.undosep_text = "\\markdownRendererUndoSeparator\n{}"
2916 function self.undosep()
2917 if not self.is_writing then return "" end
2918 return self.undosep_text
2919 end
2920 self.soft_line_break = function()
2921 if self.flatten_inlines then return "\n" end
2922 return "\\markdownRendererSoftLineBreak\n{}"
2923 end
2924 self.hard_line_break = function()
2925 if self.flatten_inlines then return "\n" end
2926 return "\\markdownRendererHardLineBreak\n{}"
2927 end
2928 self.ellipsis = "\\markdownRendererEllipsis{}"
2929 function self.thematic_break()
2930 if not self.is_writing then return "" end
2931 return "\\markdownRendererThematicBreak{}"
2932 end
2933 self.escaped_uri_chars = {
2934 ["{"] = "\\markdownRendererLeftBrace{}",
2935 ["}"] = "\\markdownRendererRightBrace{}",
2936 ["\\"] = "\\markdownRendererBackslash{}",
2937 ["\r"] = " ",
2938 ["\n"] = " ",
2939 }
2940 self.escaped_minimal_strings = {
2941 ["^^"] = "\\markdownRendererCircumflex"
2942 .. "\\markdownRendererCircumflex ",
2943 ["☒"] = "\\markdownRendererTickedBox{}",
2944 ["⌛"] = "\\markdownRendererHalfTickedBox{}",
2945 ["☐"] = "\\markdownRendererUntickedBox{}",
2946 [entities.hex_entity('FFFD')]
2947 = "\\markdownRendererReplacementCharacter{}",
2948 }
2949 self.escaped_strings = util.table_copy(self.escaped_minimal_strings)
2950 self.escaped_strings[entities.hex_entity('00A0')] = self.nbsp
2951 self.escaped_chars = {
2952 ["{"] = "\\markdownRendererLeftBrace{}",
2953 ["}"] = "\\markdownRendererRightBrace{}",
2954 ["%"] = "\\markdownRendererPercentSign{}",
2955 ["\\"] = "\\markdownRendererBackslash{}",
2956 ["#"] = "\\markdownRendererHash{}",
2957 ["$"] = "\\markdownRendererDollarSign{}",
2958 ["&"] = "\\markdownRendererAmpersand{}",
2959 ["_"] = "\\markdownRendererUnderscore{}",
2960 ["^"] = "\\markdownRendererCircumflex{}",
2961 ["~"] = "\\markdownRendererTilde{}",
2962 ["|"] = "\\markdownRendererPipe{}",
2963 [entities.hex_entity('0000')]
2964 = "\\markdownRendererReplacementCharacter{}",
2965 }
2966 local function create_escaper(char_escapes, string_escapes)
2967 local escape = util.escaper(char_escapes, string_escapes)
2968 return function(s)
2969 if self.flatten_inlines then return s end
2970 return escape(s)
2971 end
2972 end
2973 local escape_typographic_text = create_escaper(
2974 self.escaped_chars, self.escaped_strings)
2975 local escape_programmatic_text = create_escaper(
2976 self.escaped_uri_chars, self.escaped_minimal_strings)
2977 local escape_minimal = create_escaper(
2978 {}, self.escaped_minimal_strings)
2979 self.escape = escape_typographic_text
2980 self.math = escape_minimal
2981 if options.hybrid then
2982 self.identifier = escape_minimal
2983 self.string = escape_minimal
2984 self.uri = escape_minimal
2985 self.infostring = escape_minimal
2986 else
2987 self.identifier = escape_programmatic_text
2988 self.string = escape_typographic_text
2989 self.uri = escape_programmatic_text
2990 self.infostring = escape_programmatic_text
2991 end
2992 function self.warning(t, m)
2993 return {"\\markdownRendererWarning{", self.escape(t), "}{",
2994 escape_minimal(t), "}{", self.escape(m or ""), "}{",
2995 escape_minimal(m or ""), "}"}
2996 end
2997 function self.error(t, m)
2998 return {"\\markdownRendererError{", self.escape(t), "}{",
2999 escape_minimal(t), "}{", self.escape(m or ""), "}{",
3000 escape_minimal(m or ""), "}"}
3001 end
3002 function self.code(s, attributes)
3003 if self.flatten_inlines then return s end
3004 local buf = {}
3005 if attributes ~= nil then
3006 table.insert(buf,
3007 "\\markdownRendererCodeSpanAttributeContextBegin\n")
3008 table.insert(buf, self.attributes(attributes))
3009 end
3010 table.insert(buf,
3011 {"\\markdownRendererCodeSpan{", self.escape(s), "}"})
3012 if attributes ~= nil then
3013 table.insert(buf,
3014 "\\markdownRendererCodeSpanAttributeContextEnd{}")
3015 end
3016 return buf
3017 end
3018 function self.link(lab, src, tit, attributes)
3019 if self.flatten_inlines then return lab end
3020 local buf = {}
3021 if attributes ~= nil then
3022 table.insert(buf,
3023 "\\markdownRendererLinkAttributeContextBegin\n")
3024 table.insert(buf, self.attributes(attributes))
3025 end
3026 table.insert(buf, {"\\markdownRendererLink{",lab,"}",
3027 "{",self.escape(src),"}",
3028 "{",self.uri(src),"}",
3029 "{",self.string(tit or ""),"}"})
3030 if attributes ~= nil then
3031 table.insert(buf,
3032 "\\markdownRendererLinkAttributeContextEnd{}")
3033 end
3034 return buf
3035 end
3036 function self.image(lab, src, tit, attributes)
3037 if self.flatten_inlines then return lab end
3038 local buf = {}
3039 if attributes ~= nil then
3040 table.insert(buf,
3041 "\\markdownRendererImageAttributeContextBegin\n")
3042 table.insert(buf, self.attributes(attributes))
3043 end
3044 table.insert(buf, {"\\markdownRendererImage{",lab,"}",
3045 "{",self.string(src),"}",
3046 "{",self.uri(src),"}",
3047 "{",tit,"}"})
3048 if attributes ~= nil then
3049 table.insert(buf,
3050 "\\markdownRendererImageAttributeContextEnd{}")
3051 end
3052 return buf
3053 end
3054 function self.bulletlist(items,tight)
3055 if not self.is_writing then return "" end
3056 local buffer = {}
3057 for _,item in ipairs(items) do
3058 if item ~= "" then
3059 buffer[#buffer + 1] = self.bulletitem(item)
3060 end
3061 end
3062 local contents = util.intersperse(buffer,"\n")
3063 if tight and options.tightLists then
3064 return {"\\markdownRendererUlBeginTight\n",contents,
3065 "\n\\markdownRendererUlEndTight "}
3066 else
3067 return {"\\markdownRendererUlBegin\n",contents,
3068 "\n\\markdownRendererUlEnd "}
3069 end
3070 end
3071 function self.bulletitem(s)
3072 return {"\\markdownRendererUlItem ",s,
3073 "\\markdownRendererUlItemEnd "}
3074 end
3075 function self.orderedlist(items,tight,startnum)
3076 if not self.is_writing then return "" end
3077 local buffer = {}
3078 local num = startnum
3079 for _,item in ipairs(items) do
3080 if item ~= "" then
3081 buffer[#buffer + 1] = self.ordereditem(item,num)
3082 end
3083 if num ~= nil and item ~= "" then
3084 num = num + 1
3085 end
3086 end
3087 local contents = util.intersperse(buffer,"\n")
3088 if tight and options.tightLists then
3089 return {"\\markdownRendererOlBeginTight\n",contents,
3090 "\n\\markdownRendererOlEndTight "}
3091 else
3092 return {"\\markdownRendererOlBegin\n",contents,
3093 "\n\\markdownRendererOlEnd "}
3094 end
3095 end
3096 function self.ordereditem(s,num)
3097 if num ~= nil then
3098 return {"\\markdownRendererOlItemWithNumber{",num,"}",s,
3099 "\\markdownRendererOlItemEnd "}
3100 else
3101 return {"\\markdownRendererOlItem ",s,
3102 "\\markdownRendererOlItemEnd "}
3103 end
3104 end
3105 function self.inline_html_comment(contents)
3106 if self.flatten_inlines then return contents end
3107 return {"\\markdownRendererInlineHtmlComment{",contents,"}"}
3108 end
3109 function self.inline_html_tag(contents)
3110 if self.flatten_inlines then return contents end
3111 return {"\\markdownRendererInlineHtmlTag{",
3112 self.string(contents),"}"}
3113 end
3114 function self.block_html_element(s)
3115 if not self.is_writing then return "" end
3116 local name = util.cache(options.cacheDir, s, nil, nil, ".verbatim")
3117 return {"\\markdownRendererInputBlockHtmlElement{",name,"}"}
3118 end
3119 function self.block_html_cdata_element(s)
3120 if not self.is_writing then return "" end
3121 local name = util.cache(options.cacheDir, s, nil, nil, ".verbatim")
3122 return {"\\markdownRendererInputBlockHtmlCdataElement{",name,"}"}
3123 end
3124 function self.block_html_comment(contents)
3125 if self.flatten_inlines then return contents end
3126 return {"\\markdownRendererBlockHtmlComment{",contents,"}"}
3127 end
3128 function self.block_html_processing_instruction(s)
3129 if not self.is_writing then return "" end
3130 local name = util.cache(options.cacheDir, s, nil, nil, ".verbatim")
3131 return {
3132 "\\markdownRendererInputBlockHtmlProcessingInstruction{",name,"}"}
3133 end
3134 function self.block_html_declaration(s)
3135 if not self.is_writing then return "" end
3136 local name = util.cache(options.cacheDir, s, nil, nil, ".verbatim")
3137 return {
3138 "\\markdownRendererInputBlockHtmlDeclaration{",name,"}"}
3139 end
3140 function self.block_html_cdata_section(s)
3141 if not self.is_writing then return "" end
3142 local name = util.cache(options.cacheDir, s, nil, nil, ".verbatim")
3143 return {"\\markdownRendererInputBlockHtmlCdataSection{",name,"}"}
3144 end
3145 function self.block_html_pcdata_element(s)
3146 if not self.is_writing then return "" end
3147 local name = util.cache(options.cacheDir, s, nil, nil, ".verbatim")
3148 return {"\\markdownRendererInputBlockHtmlPcdataElement{",name,"}"}
3149 end
3150 function self.block_html_standalone_tag(contents)
3151 if self.flatten_inlines then return contents end
3152 return {"\\markdownRendererBlockHtmlStandaloneTag{",
3153 self.string(contents),"}"}
3154 end
3155 function self.inline_html_processing_instruction(contents)
3156 if self.flatten_inlines then return contents end
3157 return {"\\markdownRendererInlineHtmlProcessingInstruction{",
3158 self.string(contents),"}"}
3159 end
3160 function self.inline_html_declaration(contents)
3161 if self.flatten_inlines then return contents end
3162 return {"\\markdownRendererInlineHtmlDeclaration{",
3163 self.string(contents),"}"}
3164 end
3165 function self.inline_html_cdata_section(contents)
3166 if self.flatten_inlines then return contents end
3167 return {"\\markdownRendererInlineHtmlCdataSection{",
3168 self.string(contents),"}"}
3169 end
3170 function self.inline_html_open_tag(contents)
3171 if self.flatten_inlines then return contents end
3172 return {"\\markdownRendererInlineHtmlOpenTag{",
3173 self.string(contents),"}"}
3174 end
3175 function self.inline_html_close_tag(contents)
3176 if self.flatten_inlines then return contents end
3177 return {"\\markdownRendererInlineHtmlCloseTag{",
3178 self.string(contents),"}"}
3179 end
3180 function self.inline_html_empty_tag(contents)
3181 if self.flatten_inlines then return contents end
3182 return {"\\markdownRendererInlineHtmlEmptyTag{",
3183 self.string(contents),"}"}
3184 end
3185 function self.emphasis(s)
3186 if self.flatten_inlines then return s end
3187 return {"\\markdownRendererEmphasis{",s,"}"}
3188 end
3189 function self.strong(s)
3190 if self.flatten_inlines then return s end
3191 return {"\\markdownRendererStrongEmphasis{",s,"}"}
3192 end
3193 function self.tickbox(f)
3194 if f == 1.0 then
3195 return "☒ "
3196 elseif f == 0.0 then
3197 return "☐ "
3198 else
3199 return "⌛ "
3200 end
3201 end
3202 function self.blockquote(s)
3203 if not self.is_writing then return "" end
3204 return {"\\markdownRendererBlockQuoteBegin\n",s,
3205 "\\markdownRendererBlockQuoteEnd "}
3206 end
3207 function self.verbatim(s)
3208 if not self.is_writing then return "" end
3209 s = s:gsub("\n$", "")
3210 local name = util.cache_verbatim(options.cacheDir, s)
3211 return {"\\markdownRendererInputVerbatim{",name,"}"}
3212 end
3213 function self.document(d)
3214 local buf = {"\\markdownRendererDocumentBegin\n"}
3215
3216 -- warn against the `hybrid` option
3217 if options.hybrid then
3218 local text = "The `hybrid` option has been soft-deprecated."
3219 local more = "Consider using one of the following better options "
3220 .. "for mixing TeX and markdown: `contentBlocks`, "
3221 .. "`rawAttribute`, `texComments`, `texMathDollars`, "
3222 .. "`texMathSingleBackslash`, and "
3223 .. "`texMathDoubleBackslash`. "
3224 .. "For more information, see the user manual at "
3225 .. "<https://witiko.github.io/markdown/>."
3226 table.insert(buf, self.warning(text, more))
3227 end
3228
3229 -- insert the text of the document
3230 table.insert(buf, d)
3231
3232 -- pop all attributes
3233 table.insert(buf, self.pop_attributes())
3234
3235 table.insert(buf, "\\markdownRendererDocumentEnd")
3236
3237 return buf
3238 end
3239 local seen_identifiers = {}
3240 local key_value_regex = "([^= ]+)%s*=%s*(.*)"
3241 local function normalize_attributes(attributes, auto_identifiers)
3242 -- normalize attributes
3243 local normalized_attributes = {}
3244 local has_explicit_identifiers = false
3245 local key, value
3246 for _, attribute in ipairs(attributes or {}) do
3247 if attribute:sub(1, 1) == "#" then
3248 table.insert(normalized_attributes, attribute)
3249 has_explicit_identifiers = true
3250 seen_identifiers[attribute:sub(2)] = true
3251 elseif attribute:sub(1, 1) == "." then
3252 table.insert(normalized_attributes, attribute)
3253 else
3254 key, value = attribute:match(key_value_regex)
3255 if key:lower() == "id" then
3256 table.insert(normalized_attributes, "#" .. value)
3257 elseif key:lower() == "class" then
3258 local classes = {}
3259 for class in value:gmatch("%S+") do
3260 table.insert(classes, class)
3261 end
3262 table.sort(classes)
3263 for _, class in ipairs(classes) do
3264 table.insert(normalized_attributes, "." .. class)
3265 end
3266 else
3267 table.insert(normalized_attributes, attribute)
3268 end
3269 end
3270 end
3271
3272 -- if no explicit identifiers exist, add auto identifiers
3273 if not has_explicit_identifiers and auto_identifiers ~= nil then
3274 local seen_auto_identifiers = {}
3275 for _, auto_identifier in ipairs(auto_identifiers) do
3276 if seen_auto_identifiers[auto_identifier] == nil then
3277 seen_auto_identifiers[auto_identifier] = true
3278 if seen_identifiers[auto_identifier] == nil then
3279 seen_identifiers[auto_identifier] = true
3280 table.insert(normalized_attributes,
3281 "#" .. auto_identifier)
3282 else
3283 local auto_identifier_number = 1
3284 while true do
3285 local numbered_auto_identifier = auto_identifier .. "-"
3286 .. auto_identifier_number
3287 if seen_identifiers[numbered_auto_identifier] == nil then
3288 seen_identifiers[numbered_auto_identifier] = true
3289 table.insert(normalized_attributes,
3290 "#" .. numbered_auto_identifier)
3291 break
3292 end
3293 auto_identifier_number = auto_identifier_number + 1
3294 end
3295 end
3296 end
3297 end
3298 end
3299
3300 -- sort and deduplicate normalized attributes
3301 table.sort(normalized_attributes)
3302 local seen_normalized_attributes = {}
3303 local deduplicated_normalized_attributes = {}
3304 for _, attribute in ipairs(normalized_attributes) do
3305 if seen_normalized_attributes[attribute] == nil then
3306 seen_normalized_attributes[attribute] = true
3307 table.insert(deduplicated_normalized_attributes, attribute)
3308 end
3309 end
3310
3311 return deduplicated_normalized_attributes
3312 end
3313
3314 function self.attributes(attributes, should_normalize_attributes)
3315 local normalized_attributes
3316 if should_normalize_attributes == false then
3317 normalized_attributes = attributes
3318 else
3319 normalized_attributes = normalize_attributes(attributes)
3320 end
3321
3322 local buf = {}
3323 local key, value
3324 for _, attribute in ipairs(normalized_attributes) do
3325 if attribute:sub(1, 1) == "#" then
3326 table.insert(buf, {"\\markdownRendererAttributeIdentifier{",
3327 attribute:sub(2), "}"})
3328 elseif attribute:sub(1, 1) == "." then
3329 table.insert(buf, {"\\markdownRendererAttributeClassName{",
3330 attribute:sub(2), "}"})
3331 else
3332 key, value = attribute:match(key_value_regex)
3333 table.insert(buf, {"\\markdownRendererAttributeKeyValue{",
3334 key, "}{", value, "}"})
3335 end
3336 end
3337
3338 return buf
3339 end
3340 self.active_attributes = {}
3341 self.attribute_type_levels = {}
3342 setmetatable(self.attribute_type_levels,
3343 { __index = function() return 0 end })
3344 local function apply_attributes()
3345 local buf = {}
3346 for i = 1, #self.active_attributes do
3347 local start_output = self.active_attributes[i][3]
3348 if start_output ~= nil then
3349 table.insert(buf, start_output)
3350 end
3351 end
3352 return buf
3353 end
3354
3355 local function tear_down_attributes()
3356 local buf = {}
3357 for i = #self.active_attributes, 1, -1 do
3358 local end_output = self.active_attributes[i][4]
3359 if end_output ~= nil then
3360 table.insert(buf, end_output)
3361 end
3362 end
3363 return buf
3364 end
3365 function self.push_attributes(attribute_type, attributes,
3366 start_output, end_output)
3367 local attribute_type_level
3368 = self.attribute_type_levels[attribute_type]
3369 self.attribute_type_levels[attribute_type]
3370 = attribute_type_level + 1
3371
3372 -- index attributes in a hash table for easy lookup
3373 attributes = attributes or {}
3374 for i = 1, #attributes do
3375 attributes[attributes[i]] = true
3376 end
3377
3378 local buf = {}
3379 -- handle slicing
3380 if attributes["#" .. self.slice_end_identifier] ~= nil and
3381 self.slice_end_type == "^" then
3382 if self.is_writing then
3383 table.insert(buf, self.undosep())
3384 table.insert(buf, tear_down_attributes())
3385 end
3386 self.is_writing = false
3387 end
3388 if attributes["#" .. self.slice_begin_identifier] ~= nil and
3389 self.slice_begin_type == "^" then
3390 table.insert(buf, apply_attributes())
3391 self.is_writing = true
3392 end
3393 if self.is_writing and start_output ~= nil then
3394 table.insert(buf, start_output)
3395 end
3396 table.insert(self.active_attributes,
3397 {attribute_type, attributes,
3398 start_output, end_output})
3399 return buf
3400 end
3401
3402 function self.pop_attributes(attribute_type)
3403 local buf = {}
3404 -- pop attributes until we find attributes of correct type
3405 -- or until no attributes remain
3406 local current_attribute_type = false
3407 while current_attribute_type ~= attribute_type and
3408 #self.active_attributes > 0 do
3409 local attributes, _, end_output
3410 current_attribute_type, attributes, _, end_output = table.unpack(
3411 self.active_attributes[#self.active_attributes])
3412 local attribute_type_level
3413 = self.attribute_type_levels[current_attribute_type]
3414 self.attribute_type_levels[current_attribute_type]
3415 = attribute_type_level - 1
3416 if self.is_writing and end_output ~= nil then
3417 table.insert(buf, end_output)
3418 end
3419 table.remove(self.active_attributes, #self.active_attributes)
3420 -- handle slicing
3421 if attributes["#" .. self.slice_end_identifier] ~= nil
3422 and self.slice_end_type == "$" then
3423 if self.is_writing then
3424 table.insert(buf, self.undosep())
3425 table.insert(buf, tear_down_attributes())
3426 end
3427 self.is_writing = false
3428 end
3429 if attributes["#" .. self.slice_begin_identifier] ~= nil and
3430 self.slice_begin_type == "$" then
3431 self.is_writing = true
3432 table.insert(buf, apply_attributes())
3433 end
3434 end
3435 return buf
3436 end
3437 local function create_auto_identifier(s)
3438 local buffer = {}
3439 local prev_space = false
3440 local letter_found = false
3441 local normalized_s = s
3442 if not options.unicodeNormalization
3443 or options.unicodeNormalizationForm ~= "nfc" then
3444 normalized_s = util.normalize(normalized_s, "nfc")
3445 end
3446
3447 for _, code in utf8.codes(normalized_s) do
3448 local char = utf8.char(code)
3449
3450 -- Remove everything up to the first letter.
3451 if not letter_found then
3452 local is_letter = lpeg.match(
3453 parsers.unicode.alpha,
3454 char
3455 )
3456 if is_letter then
3457 letter_found = true
3458 else
3459 goto continue
3460 end
3461 end
3462
3463 -- Remove all non-alphanumeric characters, except underscores,
3464 -- hyphens, and periods.
3465 if not lpeg.match(
3466 ( parsers.underscore
3467 + parsers.dash
3468 + parsers.period
3469 + parsers.unicode.word
3470 + #parsers.unicode.whitespace ),
3471 char
3472 ) then
3473 goto continue
3474 end
3475
3476 -- Replace all spaces and newlines with hyphens.
3477 if lpeg.match(
3478 ( parsers.newline
3479 + #parsers.unicode.whitespace ),
3480 char
3481 ) then
3482 char = "-"
3483 if prev_space then
3484 goto continue
3485 else
3486 prev_space = true
3487 end
3488 else
3489 -- Case-fold all alphabetic characters.
3490 local form = nil
3491 if options.unicodeNormalization then
3492 form = options.unicodeNormalizationForm
3493 end
3494 char = util.casefold(char, form)
3495 prev_space = false
3496 end
3497
3498 table.insert(buffer, char)
3499
3500 ::continue::
3501 end
3502
3503 if prev_space then
3504 table.remove(buffer)
3505 end
3506
3507 local identifier = #buffer == 0 and "section"
3508 or table.concat(buffer, "")
3509 return identifier
3510 end
3511 local function create_gfm_auto_identifier(s)
3512 local buffer = {}
3513 local prev_space = false
3514 local letter_found = false
3515 local normalized_s = s
3516 if not options.unicodeNormalization
3517 or options.unicodeNormalizationForm ~= "nfc" then
3518 normalized_s = util.normalize(normalized_s, "nfc")
3519 end
3520
3521 for _, code in utf8.codes(normalized_s) do
3522 local char = utf8.char(code)
3523
3524 -- Remove everything up to the first non-space.
3525 if not letter_found then
3526 local is_letter = not lpeg.match(
3527 #parsers.unicode.whitespace,
3528 char
3529 )
3530 if is_letter then
3531 letter_found = true
3532 else
3533 goto continue
3534 end
3535 end
3536
3537 -- Remove all non-alphanumeric characters, except underscores
3538 -- and hyphens.
3539 if not lpeg.match(
3540 ( parsers.underscore
3541 + parsers.dash
3542 + parsers.unicode.word
3543 + #parsers.unicode.whitespace ),
3544 char
3545 ) then
3546 prev_space = false
3547 goto continue
3548 end
3549
3550 -- Replace all spaces and newlines with hyphens.
3551 if lpeg.match(
3552 ( parsers.newline
3553 + #parsers.unicode.whitespace ),
3554 char
3555 ) then
3556 char = "-"
3557 if prev_space then
3558 goto continue
3559 else
3560 prev_space = true
3561 end
3562 else
3563 -- Case-fold all alphabetic characters.
3564 local form = nil
3565 if options.unicodeNormalization then
3566 form = options.unicodeNormalizationForm
3567 end
3568 char = util.casefold(char, form)
3569 prev_space = false
3570 end
3571
3572 table.insert(buffer, char)
3573
3574 ::continue::
3575 end
3576
3577 if prev_space then
3578 table.remove(buffer)
3579 end
3580
3581 local identifier = #buffer == 0 and "section"
3582 or table.concat(buffer, "")
3583 return identifier
3584 end
3585 self.secbegin_text = "\\markdownRendererSectionBegin\n"
3586 self.secend_text = "\n\\markdownRendererSectionEnd "
3587 function self.heading(s, level, attributes)
3588 local buf = {}
3589 local flat_text, inlines = table.unpack(s)
3590
3591 -- push empty attributes for implied sections
3592 while self.attribute_type_levels["heading"] < level - 1 do
3593 table.insert(buf,
3594 self.push_attributes("heading",
3595 nil,
3596 self.secbegin_text,
3597 self.secend_text))
3598 end
3599
3600 -- pop attributes for sections that have ended
3601 while self.attribute_type_levels["heading"] >= level do
3602 table.insert(buf, self.pop_attributes("heading"))
3603 end
3604
3605 -- construct attributes for the new section
3606 local auto_identifiers = {}
3607 if self.options.autoIdentifiers then
3608 table.insert(auto_identifiers, create_auto_identifier(flat_text))
3609 end
3610 if self.options.gfmAutoIdentifiers then
3611 table.insert(auto_identifiers,
3612 create_gfm_auto_identifier(flat_text))
3613 end
3614 local normalized_attributes = normalize_attributes(attributes,
3615 auto_identifiers)
3616
3617 -- push attributes for the new section
3618 local start_output = {}
3619 local end_output = {}
3620 table.insert(start_output, self.secbegin_text)
3621 table.insert(end_output, self.secend_text)
3622
3623 table.insert(buf, self.push_attributes("heading",
3624 normalized_attributes,
3625 start_output,
3626 end_output))
3627 assert(self.attribute_type_levels["heading"] == level)
3628
3629 -- render the heading and its attributes
3630 if self.is_writing and #normalized_attributes > 0 then
3631 table.insert(buf,
3632 "\\markdownRendererHeaderAttributeContextBegin\n")
3633 table.insert(buf, self.attributes(normalized_attributes, false))
3634 end
3635
3636 local cmd
3637 level = level + options.shiftHeadings
3638 if level <= 1 then
3639 cmd = "\\markdownRendererHeadingOne"
3640 elseif level == 2 then
3641 cmd = "\\markdownRendererHeadingTwo"
3642 elseif level == 3 then
3643 cmd = "\\markdownRendererHeadingThree"
3644 elseif level == 4 then
3645 cmd = "\\markdownRendererHeadingFour"
3646 elseif level == 5 then
3647 cmd = "\\markdownRendererHeadingFive"
3648 elseif level >= 6 then
3649 cmd = "\\markdownRendererHeadingSix"
3650 else
3651 cmd = ""
3652 end
3653 if self.is_writing then
3654 table.insert(buf, {cmd, "{", inlines, "}"})
3655 end
3656
3657 if self.is_writing and #normalized_attributes > 0 then
3658 table.insert(buf, "\\markdownRendererHeaderAttributeContextEnd{}")
3659 end
3660
3661 return buf
3662 end
3663 function self.get_state()
3664 return {
3665 is_writing=self.is_writing,
3666 flatten_inlines=self.flatten_inlines,
3667 active_attributes={table.unpack(self.active_attributes)},
3668 }
3669 end
3670 function self.set_state(s)
3671 local previous_state = self.get_state()
3672 for key, value in pairs(s) do
3673 self[key] = value
3674 end
3675 return previous_state
3676 end
3677 function self.defer_call(f)
3678 local previous_state = self.get_state()
3679 return function(...)
3680 local state = self.set_state(previous_state)
3681 local return_value = f(...)
3682 self.set_state(state)
3683 return return_value
3684 end
3685 end
3686
3687 return self
3688 end
3689 parsers = {}
3690 parsers.percent = P("%")
3691 parsers.at = P("@")
3692 parsers.comma = P(",")
3693 parsers.asterisk = P("*")
3694 parsers.dash = P("-")
3695 parsers.plus = P("+")
3696 parsers.underscore = P("_")
3697 parsers.period = P(".")
3698 parsers.hash = P("#")
3699 parsers.dollar = P("$")
3700 parsers.ampersand = P("&")
3701 parsers.backtick = P("`")
3702 parsers.less = P("<")
3703 parsers.more = P(">")
3704 parsers.space = P(" ")
3705 parsers.squote = P("'")
3706 parsers.dquote = P('"')
3707 parsers.lparent = P("(")
3708 parsers.rparent = P(")")
3709 parsers.lbracket = P("[")
3710 parsers.rbracket = P("]")
3711 parsers.lbrace = P("{")
3712 parsers.rbrace = P("}")
3713 parsers.circumflex = P("^")
3714 parsers.slash = P("/")
3715 parsers.equal = P("=")
3716 parsers.colon = P(":")
3717 parsers.semicolon = P(";")
3718 parsers.exclamation = P("!")
3719 parsers.pipe = P("|")
3720 parsers.tilde = P("~")
3721 parsers.backslash = P("\\")
3722 parsers.tab = P("\t")
3723 parsers.newline = P("\n")
3724
3725 parsers.digit = R("09")
3726 parsers.hexdigit = R("09","af","AF")
3727 parsers.letter = R("AZ","az")
3728 parsers.alphanumeric = R("AZ","az","09")
3729 parsers.keyword = parsers.letter
3730 * (parsers.alphanumeric + parsers.dash)^0
3731
3732 parsers.doubleasterisks = P("**")
3733 parsers.doubleunderscores = P("__")
3734 parsers.doubletildes = P("~~")
3735 parsers.fourspaces = P(" ")
3736
3737 parsers.any = P(1)
3738 parsers.eof = P(-1)
3739 parsers.succeed = P(true)
3740 parsers.fail = P(false)
3741
3742 parsers.internal_punctuation = S(":;,.?")
3743 parsers.ascii_punctuation = S("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~")
3744
3745 parsers.escapable = parsers.ascii_punctuation
3746 parsers.anyescaped = parsers.backslash / ""
3747 * parsers.escapable
3748 + parsers.any
3749
3750 parsers.spacechar = S("\t ")
3751 parsers.spacing = S(" \n\r\t")
3752 parsers.nonspacechar = parsers.any - parsers.spacing
3753 parsers.optionalspace = parsers.spacechar^0
3754
3755 parsers.normalchar = parsers.any - (V("SpecialChar")
3756 + parsers.spacing)
3757 parsers.nonindentspace = parsers.space^-3 * - parsers.spacechar
3758 parsers.indent = parsers.space^-3 * parsers.tab
3759 + parsers.fourspaces / ""
3760 parsers.linechar = P(1 - parsers.newline)
3761
3762 parsers.blankline = parsers.optionalspace
3763 * parsers.newline / "\n"
3764 parsers.blanklines = parsers.blankline^0
3765 parsers.skipblanklines = ( parsers.optionalspace
3766 * parsers.newline)^0
3767 parsers.indentedline = parsers.indent /""
3768 * C( parsers.linechar^1
3769 * parsers.newline^-1)
3770 parsers.optionallyindentedline = parsers.indent^-1 /""
3771 * C( parsers.linechar^1
3772 * parsers.newline^-1)
3773 parsers.sp = parsers.spacing^0
3774 parsers.spnl = parsers.optionalspace
3775 * ( parsers.newline
3776 * parsers.optionalspace)^-1
3777 parsers.line = parsers.linechar^0 * parsers.newline
3778 parsers.nonemptyline = parsers.line - parsers.blankline
3779
3780 parsers.unicode = {}
3781 parsers.unicode.preceding_punctuation = parsers.fail
3782 parsers.unicode.punctuation = parsers.fail
3783 parsers.unicode.alpha = parsers.fail
3784 parsers.unicode.numeric = parsers.fail
3785 parsers.unicode.word = parsers.fail
3786 parsers.unicode.preceding_whitespace = parsers.fail
3787 parsers.unicode.whitespace = parsers.fail
3788 for n = 1, 4 do
3789 local punctuation_of_length_n
3790 = unicode_data.categories.P[n]
3791 + unicode_data.categories.S[n]
3792 parsers.unicode.preceding_punctuation
3793 = parsers.unicode.preceding_punctuation
3794 + B(punctuation_of_length_n)
3795 parsers.unicode.punctuation
3796 = parsers.unicode.punctuation
3797 + punctuation_of_length_n
3798 local alpha_of_length_n = unicode_data.categories.L[n]
3799 parsers.unicode.alpha
3800 = parsers.unicode.alpha
3801 + alpha_of_length_n
3802 local numeric_of_length_n = unicode_data.categories.N[n]
3803 parsers.unicode.numeric
3804 = parsers.unicode.numeric
3805 + numeric_of_length_n
3806 local word_of_length_n
3807 = alpha_of_length_n
3808 + numeric_of_length_n
3809 + unicode_data.categories.Pc[n]
3810 parsers.unicode.word
3811 = parsers.unicode.word
3812 + word_of_length_n
3813 local whitespace_of_length_n = unicode_data.categories.Z[n]
3814 if n == 1 then
3815 whitespace_of_length_n
3816 = whitespace_of_length_n
3817 + R("\t\r")
3818 end
3819 parsers.unicode.preceding_whitespace
3820 = parsers.unicode.preceding_whitespace
3821 + B(whitespace_of_length_n)
3822 parsers.unicode.whitespace
3823 = parsers.unicode.whitespace
3824 + whitespace_of_length_n
3825 end
3826
3827 parsers.leader = parsers.space^-3
3828
3829 local function has_trail(indent_table)
3830 return indent_table ~= nil and
3831 indent_table.trail ~= nil and
3832 next(indent_table.trail) ~= nil
3833 end
3834
3835 local function has_indents(indent_table)
3836 return indent_table ~= nil and
3837 indent_table.indents ~= nil and
3838 next(indent_table.indents) ~= nil
3839 end
3840
3841 local function add_trail(indent_table, trail_info)
3842 indent_table.trail = trail_info
3843 return indent_table
3844 end
3845
3846 local function remove_trail(indent_table)
3847 indent_table.trail = nil
3848 return indent_table
3849 end
3850
3851 local function update_indent_table(indent_table, new_indent, add)
3852 indent_table = remove_trail(indent_table)
3853
3854 if not has_indents(indent_table) then
3855 indent_table.indents = {}
3856 end
3857
3858 if add then
3859 indent_table.indents[#indent_table.indents + 1] = new_indent
3860 else
3861 if indent_table.indents[#indent_table.indents].name
3862 == new_indent.name then
3863 indent_table.indents[#indent_table.indents] = nil
3864 end
3865 end
3866
3867 return indent_table
3868 end
3869
3870 local function remove_indent(name)
3871 local remove_indent_level =
3872 function(s, i, indent_table) -- luacheck: ignore s i
3873 indent_table = update_indent_table(indent_table, {name=name},
3874 false)
3875 return true, indent_table
3876 end
3877
3878 return Cg(Cmt(Cb("indent_info"), remove_indent_level), "indent_info")
3879 end
3880
3881 local function process_starter_spacing(indent, spacing,
3882 minimum, left_strip_length)
3883 left_strip_length = left_strip_length or 0
3884
3885 local count = 0
3886 local tab_value = 4 - (indent) % 4
3887
3888 local code_started, minimum_found = false, false
3889 local code_start, minimum_remainder = "", ""
3890
3891 local left_total_stripped = 0
3892 local full_remainder = ""
3893
3894 if spacing ~= nil then
3895 for i = 1, #spacing do
3896 local character = spacing:sub(i, i)
3897
3898 if character == "\t" then
3899 count = count + tab_value
3900 tab_value = 4
3901 elseif character == " " then
3902 count = count + 1
3903 tab_value = 4 - (1 - tab_value) % 4
3904 end
3905
3906 if (left_strip_length ~= 0) then
3907 local possible_to_strip = math.min(count, left_strip_length)
3908 count = count - possible_to_strip
3909 left_strip_length = left_strip_length - possible_to_strip
3910 left_total_stripped = left_total_stripped + possible_to_strip
3911 else
3912 full_remainder = full_remainder .. character
3913 end
3914
3915 if (minimum_found) then
3916 minimum_remainder = minimum_remainder .. character
3917 elseif (count >= minimum) then
3918 minimum_found = true
3919 minimum_remainder = minimum_remainder
3920 .. string.rep(" ", count - minimum)
3921 end
3922
3923 if (code_started) then
3924 code_start = code_start .. character
3925 elseif (count >= minimum + 4) then
3926 code_started = true
3927 code_start = code_start
3928 .. string.rep(" ", count - (minimum + 4))
3929 end
3930 end
3931 end
3932
3933 local remainder
3934 if (code_started) then
3935 remainder = code_start
3936 else
3937 remainder = string.rep(" ", count - minimum)
3938 end
3939
3940 local is_minimum = count >= minimum
3941 return {
3942 is_code = code_started,
3943 remainder = remainder,
3944 left_total_stripped = left_total_stripped,
3945 is_minimum = is_minimum,
3946 minimum_remainder = minimum_remainder,
3947 total_length = count,
3948 full_remainder = full_remainder
3949 }
3950 end
3951
3952 local function count_indent_tab_level(indent_table)
3953 local count = 0
3954 if not has_indents(indent_table) then
3955 return count
3956 end
3957
3958 for i=1, #indent_table.indents do
3959 count = count + indent_table.indents[i].length
3960 end
3961 return count
3962 end
3963
3964 local function total_delimiter_length(delimiter)
3965 local count = 0
3966 if type(delimiter) == "string" then return #delimiter end
3967 for _, value in pairs(delimiter) do
3968 count = count + total_delimiter_length(value)
3969 end
3970 return count
3971 end
3972
3973 local function process_starter_indent(_, _, indent_table, starter,
3974 is_blank, indent_type, breakable)
3975 local last_trail = starter[1]
3976 local delimiter = starter[2]
3977 local raw_new_trail = starter[3]
3978
3979 if indent_type == "bq" and not breakable then
3980 indent_table.ignore_blockquote_blank = true
3981 end
3982
3983 if has_trail(indent_table) then
3984 local trail = indent_table.trail
3985 if trail.is_code then
3986 return false
3987 end
3988 last_trail = trail.remainder
3989 else
3990 local sp = process_starter_spacing(0, last_trail, 0, 0)
3991
3992 if sp.is_code then
3993 return false
3994 end
3995 last_trail = sp.remainder
3996 end
3997
3998 local preceding_indentation = count_indent_tab_level(indent_table) % 4
3999 local last_trail_length = #last_trail
4000 local delimiter_length = total_delimiter_length(delimiter)
4001
4002 local total_indent_level = preceding_indentation + last_trail_length
4003 + delimiter_length
4004
4005 local sp = {}
4006 if not is_blank then
4007 sp = process_starter_spacing(total_indent_level, raw_new_trail,
4008 0, 1)
4009 end
4010
4011 local del_trail_length = sp.left_total_stripped
4012 if is_blank then
4013 del_trail_length = 1
4014 elseif not sp.is_code then
4015 del_trail_length = del_trail_length + #sp.remainder
4016 end
4017
4018 local indent_length = last_trail_length + delimiter_length
4019 + del_trail_length
4020 local new_indent_info = {name=indent_type, length=indent_length}
4021
4022 indent_table = update_indent_table(indent_table, new_indent_info,
4023 true)
4024 indent_table = add_trail(indent_table,
4025 {is_code=sp.is_code,
4026 remainder=sp.remainder,
4027 total_length=sp.total_length,
4028 full_remainder=sp.full_remainder})
4029
4030 return true, indent_table
4031 end
4032
4033 local function decode_pattern(name)
4034 local delimeter = parsers.succeed
4035 if name == "bq" then
4036 delimeter = parsers.more
4037 end
4038
4039 return C(parsers.optionalspace) * C(delimeter)
4040 * C(parsers.optionalspace) * Cp()
4041 end
4042
4043 local function left_blank_starter(indent_table)
4044 local blank_starter_index
4045
4046 if not has_indents(indent_table) then
4047 return
4048 end
4049
4050 for i = #indent_table.indents,1,-1 do
4051 local value = indent_table.indents[i]
4052 if value.name == "li" then
4053 blank_starter_index = i
4054 else
4055 break
4056 end
4057 end
4058
4059 return blank_starter_index
4060 end
4061
4062 local function traverse_indent(s, i, indent_table, is_optional,
4063 is_blank, current_line_indents)
4064 local new_index = i
4065
4066 local preceding_indentation = 0
4067 local current_trail = {}
4068
4069 local blank_starter = left_blank_starter(indent_table)
4070
4071 if current_line_indents == nil then
4072 current_line_indents = {}
4073 end
4074
4075 for index = 1,#indent_table.indents do
4076 local value = indent_table.indents[index]
4077 local pattern = decode_pattern(value.name)
4078
4079 -- match decoded pattern
4080 local new_indent_info = lpeg.match(Ct(pattern), s, new_index)
4081 if new_indent_info == nil then
4082 local blankline_end = lpeg.match(
4083 Ct(parsers.blankline * Cg(Cp(), "pos")), s, new_index)
4084 if is_optional or not indent_table.ignore_blockquote_blank
4085 or not blankline_end then
4086 return is_optional, new_index, current_trail,
4087 current_line_indents
4088 end
4089
4090 return traverse_indent(s, tonumber(blankline_end.pos),
4091 indent_table, is_optional, is_blank,
4092 current_line_indents)
4093 end
4094
4095 local raw_last_trail = new_indent_info[1]
4096 local delimiter = new_indent_info[2]
4097 local raw_new_trail = new_indent_info[3]
4098 local next_index = new_indent_info[4]
4099
4100 local space_only = delimiter == ""
4101
4102 -- check previous trail
4103 if not space_only and next(current_trail) == nil then
4104 local sp = process_starter_spacing(0, raw_last_trail, 0, 0)
4105 current_trail = {is_code=sp.is_code, remainder=sp.remainder,
4106 total_length=sp.total_length,
4107 full_remainder=sp.full_remainder}
4108 end
4109
4110 if next(current_trail) ~= nil then
4111 if not space_only and current_trail.is_code then
4112 return is_optional, new_index, current_trail,
4113 current_line_indents
4114 end
4115 if current_trail.internal_remainder ~= nil then
4116 raw_last_trail = current_trail.internal_remainder
4117 end
4118 end
4119
4120 local raw_last_trail_length = 0
4121 local delimiter_length = 0
4122
4123 if not space_only then
4124 delimiter_length = #delimiter
4125 raw_last_trail_length = #raw_last_trail
4126 end
4127
4128 local total_indent_level = preceding_indentation
4129 + raw_last_trail_length + delimiter_length
4130
4131 local spacing_to_process
4132 local minimum = 0
4133 local left_strip_length = 0
4134
4135 if not space_only then
4136 spacing_to_process = raw_new_trail
4137 left_strip_length = 1
4138 else
4139 spacing_to_process = raw_last_trail
4140 minimum = value.length
4141 end
4142
4143 local sp = process_starter_spacing(total_indent_level,
4144 spacing_to_process, minimum,
4145 left_strip_length)
4146
4147 if space_only and not sp.is_minimum then
4148 return is_optional or (is_blank and blank_starter <= index),
4149 new_index, current_trail, current_line_indents
4150 end
4151
4152 local indent_length = raw_last_trail_length + delimiter_length
4153 + sp.left_total_stripped
4154
4155 -- update info for the next pattern
4156 if not space_only then
4157 preceding_indentation = preceding_indentation + indent_length
4158 else
4159 preceding_indentation = preceding_indentation + value.length
4160 end
4161
4162 current_trail = {is_code=sp.is_code, remainder=sp.remainder,
4163 internal_remainder=sp.minimum_remainder,
4164 total_length=sp.total_length,
4165 full_remainder=sp.full_remainder}
4166
4167 current_line_indents[#current_line_indents + 1] = new_indent_info
4168 new_index = next_index
4169 end
4170
4171 return true, new_index, current_trail, current_line_indents
4172 end
4173
4174 local function check_trail(expect_code, is_code)
4175 return (expect_code and is_code) or (not expect_code and not is_code)
4176 end
4177
4178 local check_trail_joined =
4179 function(s, i, indent_table, -- luacheck: ignore s i
4180 spacing, expect_code, omit_remainder)
4181 local is_code
4182 local remainder
4183
4184 if has_trail(indent_table) then
4185 local trail = indent_table.trail
4186 is_code = trail.is_code
4187 if is_code then
4188 remainder = trail.remainder
4189 else
4190 remainder = trail.full_remainder
4191 end
4192 else
4193 local sp = process_starter_spacing(0, spacing, 0, 0)
4194 is_code = sp.is_code
4195 if is_code then
4196 remainder = sp.remainder
4197 else
4198 remainder = sp.full_remainder
4199 end
4200 end
4201
4202 local result = check_trail(expect_code, is_code)
4203 if omit_remainder then
4204 return result
4205 end
4206 return result, remainder
4207 end
4208
4209 local check_trail_length =
4210 function(s, i, indent_table, -- luacheck: ignore s i
4211 spacing, min, max)
4212 local trail
4213
4214 if has_trail(indent_table) then
4215 trail = indent_table.trail
4216 else
4217 trail = process_starter_spacing(0, spacing, 0, 0)
4218 end
4219
4220 local total_length = trail.total_length
4221 if total_length == nil then
4222 return false
4223 end
4224
4225 return min <= total_length and total_length <= max
4226 end
4227
4228 local function check_continuation_indentation(s, i, indent_table,
4229 is_optional, is_blank)
4230 if not has_indents(indent_table) then
4231 return true
4232 end
4233
4234 local passes, new_index, current_trail, current_line_indents =
4235 traverse_indent(s, i, indent_table, is_optional, is_blank)
4236
4237 if passes then
4238 indent_table.current_line_indents = current_line_indents
4239 indent_table = add_trail(indent_table, current_trail)
4240 return new_index, indent_table
4241 end
4242 return false
4243 end
4244
4245 local function get_last_indent_name(indent_table)
4246 if has_indents(indent_table) then
4247 return indent_table.indents[#indent_table.indents].name
4248 end
4249 end
4250
4251 local function remove_remainder_if_blank(indent_table, remainder)
4252 if get_last_indent_name(indent_table) == "li" then
4253 return ""
4254 end
4255 return remainder
4256 end
4257
4258 local check_trail_type =
4259 function(s, i, -- luacheck: ignore s i
4260 trail, spacing, trail_type)
4261 if trail == nil then
4262 trail = process_starter_spacing(0, spacing, 0, 0)
4263 end
4264
4265 if trail_type == "non-code" then
4266 return check_trail(false, trail.is_code)
4267 end
4268 if trail_type == "code" then
4269 return check_trail(true, trail.is_code)
4270 end
4271 if trail_type == "full-code" then
4272 if (trail.is_code) then
4273 return i, trail.remainder
4274 end
4275 return i, ""
4276 end
4277 if trail_type == "full-any" then
4278 return i, trail.internal_remainder
4279 end
4280 end
4281
4282 local trail_freezing =
4283 function(s, i, -- luacheck: ignore s i
4284 indent_table, is_freezing)
4285 if is_freezing then
4286 if indent_table.is_trail_frozen then
4287 indent_table.trail = indent_table.frozen_trail
4288 else
4289 indent_table.frozen_trail = indent_table.trail
4290 indent_table.is_trail_frozen = true
4291 end
4292 else
4293 indent_table.frozen_trail = nil
4294 indent_table.is_trail_frozen = false
4295 end
4296 return true, indent_table
4297 end
4298
4299 local check_continuation_indentation_and_trail =
4300 function (s, i, indent_table, is_optional, is_blank, trail_type,
4301 reset_rem, omit_remainder)
4302 if not has_indents(indent_table) then
4303 local spacing, new_index = lpeg.match( C(parsers.spacechar^0)
4304 * Cp(), s, i)
4305 local result, remainder = check_trail_type(s, i,
4306 indent_table.trail, spacing, trail_type)
4307 if remainder == nil then
4308 if result then
4309 return new_index
4310 end
4311 return false
4312 end
4313 if result then
4314 return new_index, remainder
4315 end
4316 return false
4317 end
4318
4319 local passes, new_index, current_trail = traverse_indent(s, i,
4320 indent_table, is_optional, is_blank)
4321
4322 if passes then
4323 local spacing
4324 if current_trail == nil then
4325 local newer_spacing, newer_index = lpeg.match(
4326 C(parsers.spacechar^0) * Cp(), s, i)
4327 current_trail = process_starter_spacing(0, newer_spacing, 0, 0)
4328 new_index = newer_index
4329 spacing = newer_spacing
4330 else
4331 spacing = current_trail.remainder
4332 end
4333 local result, remainder = check_trail_type(s, new_index,
4334 current_trail, spacing, trail_type)
4335 if remainder == nil or omit_remainder then
4336 if result then
4337 return new_index
4338 end
4339 return false
4340 end
4341
4342 if is_blank and reset_rem then
4343 remainder = remove_remainder_if_blank(indent_table, remainder)
4344 end
4345 if result then
4346 return new_index, remainder
4347 end
4348 return false
4349 end
4350 return false
4351 end
4352
4353 parsers.check_trail = Cmt( Cb("indent_info") * C(parsers.spacechar^0)
4354 * Cc(false), check_trail_joined)
4355
4356 parsers.check_trail_no_rem = Cmt( Cb("indent_info")
4357 * C(parsers.spacechar^0) * Cc(false)
4358 * Cc(true), check_trail_joined)
4359
4360 parsers.check_code_trail = Cmt( Cb("indent_info")
4361 * C(parsers.spacechar^0)
4362 * Cc(true), check_trail_joined)
4363
4364 parsers.check_trail_length_range = function(min, max)
4365 return Cmt( Cb("indent_info") * C(parsers.spacechar^0) * Cc(min)
4366 * Cc(max), check_trail_length)
4367 end
4368
4369 parsers.check_trail_length = function(n)
4370 return parsers.check_trail_length_range(n, n)
4371 end
4372
4373 parsers.freeze_trail = Cg( Cmt(Cb("indent_info")
4374 * Cc(true), trail_freezing), "indent_info")
4375
4376 parsers.unfreeze_trail = Cg(Cmt(Cb("indent_info") * Cc(false),
4377 trail_freezing), "indent_info")
4378
4379 parsers.check_minimal_indent = Cmt(Cb("indent_info") * Cc(false),
4380 check_continuation_indentation)
4381
4382 parsers.check_optional_indent = Cmt(Cb("indent_info") * Cc(true),
4383 check_continuation_indentation)
4384
4385 parsers.check_minimal_blank_indent
4386 = Cmt( Cb("indent_info") * Cc(false)
4387 * Cc(true)
4388 , check_continuation_indentation)
4389
4390
4391 parsers.check_minimal_indent_and_trail =
4392 Cmt( Cb("indent_info")
4393 * Cc(false) * Cc(false) * Cc("non-code") * Cc(true)
4394 , check_continuation_indentation_and_trail)
4395
4396 parsers.check_minimal_indent_and_code_trail =
4397 Cmt( Cb("indent_info")
4398 * Cc(false) * Cc(false) * Cc("code") * Cc(false)
4399 , check_continuation_indentation_and_trail)
4400
4401 parsers.check_minimal_blank_indent_and_full_code_trail =
4402 Cmt( Cb("indent_info")
4403 * Cc(false) * Cc(true) * Cc("full-code") * Cc(true)
4404 , check_continuation_indentation_and_trail)
4405
4406 parsers.check_minimal_indent_and_any_trail =
4407 Cmt( Cb("indent_info")
4408 * Cc(false) * Cc(false) * Cc("full-any") * Cc(true) * Cc(false)
4409 , check_continuation_indentation_and_trail)
4410
4411 parsers.check_minimal_blank_indent_and_any_trail =
4412 Cmt( Cb("indent_info")
4413 * Cc(false) * Cc(true) * Cc("full-any") * Cc(true) * Cc(false)
4414 , check_continuation_indentation_and_trail)
4415
4416 parsers.check_minimal_blank_indent_and_any_trail_no_rem =
4417 Cmt( Cb("indent_info")
4418 * Cc(false) * Cc(true) * Cc("full-any") * Cc(true) * Cc(true)
4419 , check_continuation_indentation_and_trail)
4420
4421 parsers.check_optional_indent_and_any_trail =
4422 Cmt( Cb("indent_info")
4423 * Cc(true) * Cc(false) * Cc("full-any") * Cc(true) * Cc(false)
4424 , check_continuation_indentation_and_trail)
4425
4426 parsers.check_optional_blank_indent_and_any_trail =
4427 Cmt( Cb("indent_info")
4428 * Cc(true) * Cc(true) * Cc("full-any") * Cc(true) * Cc(false)
4429 , check_continuation_indentation_and_trail)
4430
4431
4432 parsers.spnlc_noexc = parsers.optionalspace
4433 * ( parsers.newline
4434 * parsers.check_minimal_indent_and_any_trail)^-1
4435
4436 parsers.spnlc = parsers.optionalspace
4437 * (V("EndlineNoSub"))^-1
4438
4439 parsers.spnlc_sep = parsers.optionalspace * V("EndlineNoSub")
4440 + parsers.spacechar^1
4441
4442 parsers.only_blank = parsers.spacechar^0
4443 * (parsers.newline + parsers.eof)
4444
4445 parsers.commented_line_letter = parsers.linechar
4446 + parsers.newline
4447 - parsers.backslash
4448 - parsers.percent
4449 parsers.commented_line = Cg(Cc(""), "backslashes")
4450 * Cg(Cc(true), "pure_comment")
4451 * ((#(parsers.commented_line_letter
4452 - parsers.newline)
4453 * Cb("backslashes")
4454 * Cs(parsers.commented_line_letter
4455 - parsers.newline)^1 -- initial
4456 * Cg(Cc(""), "backslashes")
4457 * Cg(Cc(false), "pure_comment"))
4458 + #( parsers.backslash
4459 * (parsers.backslash + parsers.newline))
4460 * Cg((parsers.backslash -- even backslash
4461 * ( parsers.backslash
4462 + #parsers.newline))^1, "backslashes")
4463 * Cg(Cc(false), "pure_comment")
4464 + (parsers.backslash
4465 * (#parsers.percent
4466 * Cb("backslashes")
4467 / function(backslashes)
4468 return string.rep("\\", #backslashes / 2)
4469 end
4470 * C(parsers.percent)
4471 + #parsers.commented_line_letter
4472 * Cb("backslashes")
4473 * Cc("\\")
4474 * C(parsers.commented_line_letter))
4475 * Cg(Cc(""), "backslashes")
4476 * Cg(Cc(false), "pure_comment")))^0
4477 * (#parsers.percent
4478 * Cb("backslashes")
4479 / function(backslashes)
4480 return string.rep("\\", #backslashes / 2)
4481 end
4482 * ((Cb("pure_comment")
4483 * parsers.percent -- comment
4484 * parsers.line
4485 * #parsers.blankline) -- blank line
4486 / function(is_pure)
4487 return is_pure and "" or "\n"
4488 end
4489 + parsers.percent -- comment
4490 * parsers.line
4491 * parsers.optionalspace) -- leading spaces
4492 + #(parsers.newline)
4493 * Cb("backslashes")
4494 * C(parsers.newline))
4495
4496 parsers.chunk = parsers.line * (parsers.optionallyindentedline
4497 - parsers.blankline)^0
4498
4499 parsers.attribute_key_char = parsers.unicode.alpha
4500 + parsers.unicode.numeric
4501 + S("-_:.")
4502 parsers.attribute_raw_char = parsers.unicode.alpha
4503 + parsers.unicode.numeric
4504 + S("-_")
4505 parsers.attribute_key = (parsers.attribute_key_char
4506 - parsers.dash - parsers.digit)
4507 * parsers.attribute_key_char^0
4508 parsers.attribute_value = ( (parsers.dquote / "")
4509 * (parsers.anyescaped - parsers.dquote)^0
4510 * (parsers.dquote / ""))
4511 + ( (parsers.squote / "")
4512 * (parsers.anyescaped - parsers.squote)^0
4513 * (parsers.squote / ""))
4514 + ( parsers.anyescaped
4515 - parsers.dquote
4516 - parsers.rbrace
4517 - parsers.space)^0
4518 parsers.attribute_identifier = parsers.attribute_key_char^1
4519 parsers.attribute_classname = parsers.unicode.alpha
4520 * parsers.attribute_key_char^0
4521 parsers.attribute_raw = parsers.attribute_raw_char^1
4522
4523 parsers.attribute = (parsers.dash * Cc(".unnumbered"))
4524 + C( parsers.hash
4525 * parsers.attribute_identifier)
4526 + C( parsers.period
4527 * parsers.attribute_classname)
4528 + Cs( parsers.attribute_key
4529 * parsers.optionalspace
4530 * parsers.equal
4531 * parsers.optionalspace
4532 * parsers.attribute_value)
4533 parsers.attributes = parsers.lbrace
4534 * parsers.optionalspace
4535 * parsers.attribute
4536 * (parsers.spacechar^1
4537 * parsers.attribute)^0
4538 * parsers.optionalspace
4539 * parsers.rbrace
4540
4541 parsers.raw_attribute = parsers.lbrace
4542 * parsers.optionalspace
4543 * parsers.equal
4544 * C(parsers.attribute_raw)
4545 * parsers.optionalspace
4546 * parsers.rbrace
4547
4548 -- block followed by 0 or more optionally
4549 -- indented blocks with first line indented.
4550 parsers.indented_blocks = function(bl)
4551 return Cs( bl
4552 * ( parsers.blankline^1
4553 * parsers.indent
4554 * -parsers.blankline
4555 * bl)^0
4556 * (parsers.blankline^1 + parsers.eof) )
4557 end
4558 local function repeat_between(pattern, min, max)
4559 return -pattern^(max + 1) * pattern^min
4560 end
4561
4562 parsers.hexentity = parsers.ampersand * parsers.hash * C(S("Xx"))
4563 * C(repeat_between(parsers.hexdigit, 1, 6))
4564 * parsers.semicolon
4565 parsers.decentity = parsers.ampersand * parsers.hash
4566 * C(repeat_between(parsers.digit, 1, 7))
4567 * parsers.semicolon
4568 parsers.tagentity = parsers.ampersand * C(parsers.alphanumeric^1)
4569 * parsers.semicolon
4570
4571 parsers.html_entities
4572 = parsers.hexentity / entities.hex_entity_with_x_char
4573 + parsers.decentity / entities.dec_entity
4574 + parsers.tagentity / entities.char_entity
4575 parsers.html_space_entity_char
4576 = Cmt(parsers.html_entities, function(_, i, entity)
4577 return entity == " " and i or nil
4578 end)
4579 parsers.bullet = function(bullet_char, interrupting)
4580 local allowed_end
4581 if interrupting then
4582 allowed_end = C(parsers.spacechar^1) * #parsers.linechar
4583 else
4584 allowed_end = C(parsers.spacechar^1)
4585 + #(parsers.newline + parsers.eof)
4586 end
4587 return parsers.check_trail
4588 * Ct(C(bullet_char) * Cc(""))
4589 * allowed_end
4590 end
4591
4592 local function tickbox(interior)
4593 return parsers.optionalspace * parsers.lbracket
4594 * interior * parsers.rbracket * parsers.spacechar^1
4595 end
4596
4597 parsers.ticked_box = tickbox(S("xX")) * Cc(1.0)
4598 parsers.halfticked_box = tickbox(S("./")) * Cc(0.5)
4599 parsers.unticked_box = tickbox(parsers.spacechar^1) * Cc(0.0)
4600
4601 parsers.openticks = Cg(parsers.backtick^1, "ticks")
4602
4603 local function captures_equal_length(_,i,a,b)
4604 return #a == #b and i
4605 end
4606
4607 parsers.closeticks = Cmt(C(parsers.backtick^1)
4608 * Cb("ticks"), captures_equal_length)
4609
4610 parsers.intickschar = (parsers.any - S("\n\r`"))
4611 + V("NoSoftLineBreakEndline")
4612 + (parsers.backtick^1 - parsers.closeticks)
4613
4614 local function process_inticks(s)
4615 s = s:gsub("\n", " ")
4616 s = s:gsub("^ (.*) $", "%1")
4617 return s
4618 end
4619
4620 parsers.inticks = parsers.openticks
4621 * C(parsers.space^0)
4622 * parsers.closeticks
4623 + parsers.openticks
4624 * Cs(Cs(parsers.intickschar^0) / process_inticks)
4625 * parsers.closeticks
4626
4627 -- case-insensitive match (we assume s is lowercase)
4628 -- must be single byte encoding
4629 parsers.keyword_exact = function(s)
4630 local parser = P(0)
4631 for i=1,#s do
4632 local c = s:sub(i,i)
4633 local m = c .. upper(c)
4634 parser = parser * S(m)
4635 end
4636 return parser
4637 end
4638
4639 parsers.special_block_keyword =
4640 parsers.keyword_exact("pre") +
4641 parsers.keyword_exact("script") +
4642 parsers.keyword_exact("style") +
4643 parsers.keyword_exact("textarea")
4644
4645 parsers.block_keyword =
4646 parsers.keyword_exact("address") +
4647 parsers.keyword_exact("article") +
4648 parsers.keyword_exact("aside") +
4649 parsers.keyword_exact("base") +
4650 parsers.keyword_exact("basefont") +
4651 parsers.keyword_exact("blockquote") +
4652 parsers.keyword_exact("body") +
4653 parsers.keyword_exact("caption") +
4654 parsers.keyword_exact("center") +
4655 parsers.keyword_exact("col") +
4656 parsers.keyword_exact("colgroup") +
4657 parsers.keyword_exact("dd") +
4658 parsers.keyword_exact("details") +
4659 parsers.keyword_exact("dialog") +
4660 parsers.keyword_exact("dir") +
4661 parsers.keyword_exact("div") +
4662 parsers.keyword_exact("dl") +
4663 parsers.keyword_exact("dt") +
4664 parsers.keyword_exact("fieldset") +
4665 parsers.keyword_exact("figcaption") +
4666 parsers.keyword_exact("figure") +
4667 parsers.keyword_exact("footer") +
4668 parsers.keyword_exact("form") +
4669 parsers.keyword_exact("frame") +
4670 parsers.keyword_exact("frameset") +
4671 parsers.keyword_exact("h1") +
4672 parsers.keyword_exact("h2") +
4673 parsers.keyword_exact("h3") +
4674 parsers.keyword_exact("h4") +
4675 parsers.keyword_exact("h5") +
4676 parsers.keyword_exact("h6") +
4677 parsers.keyword_exact("head") +
4678 parsers.keyword_exact("header") +
4679 parsers.keyword_exact("hr") +
4680 parsers.keyword_exact("html") +
4681 parsers.keyword_exact("iframe") +
4682 parsers.keyword_exact("legend") +
4683 parsers.keyword_exact("li") +
4684 parsers.keyword_exact("link") +
4685 parsers.keyword_exact("main") +
4686 parsers.keyword_exact("menu") +
4687 parsers.keyword_exact("menuitem") +
4688 parsers.keyword_exact("nav") +
4689 parsers.keyword_exact("noframes") +
4690 parsers.keyword_exact("ol") +
4691 parsers.keyword_exact("optgroup") +
4692 parsers.keyword_exact("option") +
4693 parsers.keyword_exact("p") +
4694 parsers.keyword_exact("param") +
4695 parsers.keyword_exact("section") +
4696 parsers.keyword_exact("source") +
4697 parsers.keyword_exact("summary") +
4698 parsers.keyword_exact("table") +
4699 parsers.keyword_exact("tbody") +
4700 parsers.keyword_exact("td") +
4701 parsers.keyword_exact("tfoot") +
4702 parsers.keyword_exact("th") +
4703 parsers.keyword_exact("thead") +
4704 parsers.keyword_exact("title") +
4705 parsers.keyword_exact("tr") +
4706 parsers.keyword_exact("track") +
4707 parsers.keyword_exact("ul")
4708
4709 -- end conditions
4710 parsers.html_blankline_end_condition
4711 = parsers.linechar^0
4712 * ( parsers.newline
4713 * (parsers.check_minimal_blank_indent_and_any_trail
4714 * #parsers.blankline
4715 + parsers.check_minimal_indent_and_any_trail)
4716 * parsers.linechar^1)^0
4717 * (parsers.newline^-1 / "")
4718
4719 local function remove_trailing_blank_lines(s)
4720 return s:gsub("[\n\r]+%s*$", "")
4721 end
4722
4723 parsers.html_until_end = function(end_marker)
4724 return (
4725 Cs(C( parsers.newline
4726 * (parsers.check_minimal_blank_indent_and_any_trail
4727 * #parsers.blankline
4728 + parsers.check_minimal_indent_and_any_trail)
4729 + parsers.linechar - end_marker)^0)
4730 * (C(parsers.linechar^0 * parsers.newline^-1)
4731 / remove_trailing_blank_lines)
4732 )
4733 end
4734
4735 -- attributes
4736 parsers.html_attribute_spacing = parsers.optionalspace
4737 * V("NoSoftLineBreakEndline")
4738 * parsers.optionalspace
4739 + parsers.spacechar^1
4740
4741 parsers.html_attribute_name = ( parsers.letter
4742 + parsers.colon
4743 + parsers.underscore)
4744 * ( parsers.alphanumeric
4745 + parsers.colon
4746 + parsers.underscore
4747 + parsers.period
4748 + parsers.dash)^0
4749
4750 parsers.html_attribute_value = parsers.squote
4751 * (parsers.linechar - parsers.squote)^0
4752 * parsers.squote
4753 + parsers.dquote
4754 * (parsers.linechar - parsers.dquote)^0
4755 * parsers.dquote
4756 + ( parsers.any
4757 - parsers.spacechar
4758 - parsers.newline
4759 - parsers.dquote
4760 - parsers.squote
4761 - parsers.backtick
4762 - parsers.equal
4763 - parsers.less
4764 - parsers.more)^1
4765
4766 parsers.html_inline_attribute_value = parsers.squote
4767 * (V("NoSoftLineBreakEndline")
4768 + parsers.any
4769 - parsers.blankline^2
4770 - parsers.squote)^0
4771 * parsers.squote
4772 + parsers.dquote
4773 * (V("NoSoftLineBreakEndline")
4774 + parsers.any
4775 - parsers.blankline^2
4776 - parsers.dquote)^0
4777 * parsers.dquote
4778 + (parsers.any
4779 - parsers.spacechar
4780 - parsers.newline
4781 - parsers.dquote
4782 - parsers.squote
4783 - parsers.backtick
4784 - parsers.equal
4785 - parsers.less
4786 - parsers.more)^1
4787
4788 parsers.html_attribute_value_specification
4789 = parsers.optionalspace
4790 * parsers.equal
4791 * parsers.optionalspace
4792 * parsers.html_attribute_value
4793
4794 parsers.html_spnl = parsers.optionalspace
4795 * (V("NoSoftLineBreakEndline")
4796 * parsers.optionalspace)^-1
4797
4798 parsers.html_inline_attribute_value_specification
4799 = parsers.html_spnl
4800 * parsers.equal
4801 * parsers.html_spnl
4802 * parsers.html_inline_attribute_value
4803
4804 parsers.html_attribute
4805 = parsers.html_attribute_spacing
4806 * parsers.html_attribute_name
4807 * parsers.html_inline_attribute_value_specification^-1
4808
4809 parsers.html_non_newline_attribute
4810 = parsers.spacechar^1
4811 * parsers.html_attribute_name
4812 * parsers.html_attribute_value_specification^-1
4813
4814 parsers.nested_breaking_blank = parsers.newline
4815 * parsers.check_minimal_blank_indent
4816 * parsers.blankline
4817
4818 parsers.html_comment_start = P("<!--")
4819 * parsers.optionalspace
4820
4821 parsers.html_comment_end = parsers.optionalspace
4822 * P("-->")
4823
4824 parsers.html_comment
4825 = parsers.html_comment_start
4826 * parsers.html_until_end(parsers.html_comment_end)
4827
4828 parsers.html_inline_comment = (parsers.html_comment_start / "")
4829 * -P(">") * -P("->")
4830 * Cs(( parsers.any
4831 - parsers.nested_breaking_blank
4832 - parsers.html_comment_end)^0)
4833 * (parsers.html_comment_end / "")
4834
4835 parsers.html_cdatasection_start = P("<![CDATA[")
4836
4837 parsers.html_cdatasection_end = P("]]>")
4838
4839 parsers.html_cdatasection
4840 = Cs( parsers.html_cdatasection_start
4841 * parsers.html_until_end(parsers.html_cdatasection_end))
4842 / remove_trailing_blank_lines
4843
4844 parsers.html_inline_cdatasection
4845 = parsers.html_cdatasection_start
4846 * Cs(V("NoSoftLineBreakEndline") + parsers.any
4847 - parsers.nested_breaking_blank - parsers.html_cdatasection_end)^0
4848 * parsers.html_cdatasection_end
4849
4850 parsers.html_declaration_start = P("<!") * parsers.letter
4851
4852 parsers.html_declaration_end = P(">")
4853
4854 parsers.html_declaration
4855 = Cs( parsers.html_declaration_start
4856 * parsers.html_until_end(parsers.html_declaration_end))
4857 / remove_trailing_blank_lines
4858
4859 parsers.html_inline_declaration
4860 = parsers.html_declaration_start
4861 * Cs(V("NoSoftLineBreakEndline") + parsers.any
4862 - parsers.nested_breaking_blank - parsers.html_declaration_end)^0
4863 * parsers.html_declaration_end
4864
4865 parsers.html_instruction_start = P("<?")
4866
4867 parsers.html_instruction_end = P("?>")
4868
4869 parsers.html_instruction
4870 = Cs( parsers.html_instruction_start
4871 * parsers.html_until_end(parsers.html_instruction_end))
4872 / remove_trailing_blank_lines
4873
4874 parsers.html_inline_instruction = parsers.html_instruction_start
4875 * Cs( V("NoSoftLineBreakEndline")
4876 + parsers.any
4877 - parsers.nested_breaking_blank
4878 - parsers.html_instruction_end)^0
4879 * parsers.html_instruction_end
4880
4881 parsers.html_blankline = parsers.newline
4882 * parsers.optionalspace
4883 * parsers.newline
4884
4885 parsers.html_tag_start = parsers.less
4886
4887 parsers.html_tag_closing_start = parsers.less
4888 * parsers.slash
4889
4890 parsers.html_tag_end = parsers.html_spnl
4891 * parsers.more
4892
4893 parsers.html_empty_tag_end = parsers.html_spnl
4894 * parsers.slash
4895 * parsers.more
4896
4897 -- opening tags
4898 parsers.html_any_open_inline_tag = parsers.html_tag_start
4899 * parsers.keyword
4900 * parsers.html_attribute^0
4901 * parsers.html_tag_end
4902
4903 parsers.html_any_open_tag = parsers.html_tag_start
4904 * parsers.keyword
4905 * parsers.html_non_newline_attribute^0
4906 * parsers.html_tag_end
4907
4908 parsers.html_open_tag = parsers.html_tag_start
4909 * parsers.block_keyword
4910 * parsers.html_attribute^0
4911 * parsers.html_tag_end
4912
4913 parsers.html_open_special_tag = parsers.html_tag_start
4914 * parsers.special_block_keyword
4915 * parsers.html_attribute^0
4916 * parsers.html_tag_end
4917
4918 -- incomplete tags
4919 parsers.incomplete_tag_following = parsers.spacechar
4920 + parsers.more
4921 + parsers.slash * parsers.more
4922 + #(parsers.newline + parsers.eof)
4923
4924 parsers.incomplete_special_tag_following = parsers.spacechar
4925 + parsers.more
4926 + #( parsers.newline
4927 + parsers.eof)
4928
4929 parsers.html_incomplete_open_tag = parsers.html_tag_start
4930 * parsers.block_keyword
4931 * parsers.incomplete_tag_following
4932
4933 parsers.html_incomplete_open_special_tag
4934 = parsers.html_tag_start
4935 * parsers.special_block_keyword
4936 * parsers.incomplete_special_tag_following
4937
4938 parsers.html_incomplete_close_tag = parsers.html_tag_closing_start
4939 * parsers.block_keyword
4940 * parsers.incomplete_tag_following
4941
4942 parsers.html_incomplete_close_special_tag
4943 = parsers.html_tag_closing_start
4944 * parsers.special_block_keyword
4945 * parsers.incomplete_tag_following
4946
4947 -- closing tags
4948 parsers.html_close_tag = parsers.html_tag_closing_start
4949 * parsers.block_keyword
4950 * parsers.html_tag_end
4951
4952 parsers.html_any_close_tag = parsers.html_tag_closing_start
4953 * parsers.keyword
4954 * parsers.html_tag_end
4955
4956 parsers.html_close_special_tag = parsers.html_tag_closing_start
4957 * parsers.special_block_keyword
4958 * parsers.html_tag_end
4959
4960 -- empty tags
4961 parsers.html_any_empty_inline_tag = parsers.html_tag_start
4962 * parsers.keyword
4963 * parsers.html_attribute^0
4964 * parsers.html_empty_tag_end
4965
4966 parsers.html_any_empty_tag = parsers.html_tag_start
4967 * parsers.keyword
4968 * parsers.html_non_newline_attribute^0
4969 * parsers.optionalspace
4970 * parsers.slash
4971 * parsers.more
4972
4973 parsers.html_empty_tag = parsers.html_tag_start
4974 * parsers.block_keyword
4975 * parsers.html_attribute^0
4976 * parsers.html_empty_tag_end
4977
4978 parsers.html_empty_special_tag = parsers.html_tag_start
4979 * parsers.special_block_keyword
4980 * parsers.html_attribute^0
4981 * parsers.html_empty_tag_end
4982
4983 parsers.html_incomplete_blocks
4984 = parsers.html_incomplete_open_tag
4985 + parsers.html_incomplete_open_special_tag
4986 + parsers.html_incomplete_close_tag
4987
4988 -- parse special html blocks
4989 parsers.html_blankline_ending_special_block_opening
4990 = ( parsers.html_close_special_tag
4991 + parsers.html_empty_special_tag)
4992 * #( parsers.optionalspace
4993 * (parsers.newline + parsers.eof))
4994
4995 parsers.html_blankline_ending_special_block
4996 = parsers.html_blankline_ending_special_block_opening
4997 * parsers.html_blankline_end_condition
4998
4999 parsers.html_special_block_opening
5000 = parsers.html_incomplete_open_special_tag
5001 - parsers.html_empty_special_tag
5002
5003 parsers.html_closing_special_block
5004 = Cs( parsers.html_special_block_opening
5005 * parsers.html_until_end(parsers.html_close_special_tag))
5006 / remove_trailing_blank_lines
5007
5008 parsers.html_special_block
5009 = parsers.html_blankline_ending_special_block
5010 + parsers.html_closing_special_block
5011
5012 -- parse html blocks
5013 parsers.html_block_opening = parsers.html_incomplete_open_tag
5014 + parsers.html_incomplete_close_tag
5015
5016 parsers.html_block = parsers.html_block_opening
5017 * parsers.html_blankline_end_condition
5018
5019 -- parse any html blocks
5020 parsers.html_any_block_opening
5021 = ( parsers.html_any_open_tag
5022 + parsers.html_any_close_tag
5023 + parsers.html_any_empty_tag)
5024 * #(parsers.optionalspace * (parsers.newline + parsers.eof))
5025
5026 parsers.html_any_block = parsers.html_any_block_opening
5027 * parsers.html_blankline_end_condition
5028
5029 parsers.html_inline_comment_full = parsers.html_comment_start
5030 * -P(">") * -P("->")
5031 * Cs(( parsers.any - P("--")
5032 - parsers.nested_breaking_blank
5033 - parsers.html_comment_end)^0)
5034 * parsers.html_comment_end
5035
5036 parsers.html_inline_tags = parsers.html_inline_comment_full
5037 + parsers.html_any_empty_inline_tag
5038 + parsers.html_inline_instruction
5039 + parsers.html_inline_cdatasection
5040 + parsers.html_inline_declaration
5041 + parsers.html_any_open_inline_tag
5042 + parsers.html_any_close_tag
5043
5044 parsers.urlchar = parsers.anyescaped
5045 - parsers.newline
5046 - parsers.more
5047
5048 parsers.auto_link_scheme_part = parsers.alphanumeric
5049 + parsers.plus
5050 + parsers.period
5051 + parsers.dash
5052
5053 parsers.auto_link_scheme = parsers.letter
5054 * parsers.auto_link_scheme_part
5055 * parsers.auto_link_scheme_part^-30
5056
5057 parsers.absolute_uri = parsers.auto_link_scheme * parsers.colon
5058 * ( parsers.any - parsers.spacing
5059 - parsers.less - parsers.more)^0
5060
5061 parsers.printable_characters = S(".!#$%&'*+/=?^_`{|}~-")
5062
5063 parsers.email_address_local_part_char = parsers.alphanumeric
5064 + parsers.printable_characters
5065
5066 parsers.email_address_local_part
5067 = parsers.email_address_local_part_char^1
5068
5069 parsers.email_address_dns_label = parsers.alphanumeric
5070 * ( parsers.alphanumeric
5071 + parsers.dash)^-62
5072 * B(parsers.alphanumeric)
5073
5074 parsers.email_address_domain = parsers.email_address_dns_label
5075 * ( parsers.period
5076 * parsers.email_address_dns_label)^0
5077
5078 parsers.email_address = parsers.email_address_local_part
5079 * parsers.at
5080 * parsers.email_address_domain
5081
5082 parsers.auto_link_url = parsers.less
5083 * C(parsers.absolute_uri)
5084 * parsers.more
5085
5086 parsers.auto_link_email = parsers.less
5087 * C(parsers.email_address)
5088 * parsers.more
5089
5090 parsers.auto_link_relative_reference = parsers.less
5091 * C(parsers.urlchar^1)
5092 * parsers.more
5093
5094 parsers.autolink = parsers.auto_link_url
5095 + parsers.auto_link_email
5096
5097 -- content in balanced brackets, parentheses, or quotes:
5098 parsers.bracketed = P{ parsers.lbracket
5099 * (( parsers.backslash / "" * parsers.rbracket
5100 + parsers.any - (parsers.lbracket
5101 + parsers.rbracket
5102 + parsers.blankline^2)
5103 ) + V(1))^0
5104 * parsers.rbracket }
5105
5106 parsers.inparens = P{ parsers.lparent
5107 * ((parsers.anyescaped - (parsers.lparent
5108 + parsers.rparent
5109 + parsers.blankline^2)
5110 ) + V(1))^0
5111 * parsers.rparent }
5112
5113 parsers.squoted = P{ parsers.squote * parsers.alphanumeric
5114 * ((parsers.anyescaped - (parsers.squote
5115 + parsers.blankline^2)
5116 ) + V(1))^0
5117 * parsers.squote }
5118
5119 parsers.dquoted = P{ parsers.dquote * parsers.alphanumeric
5120 * ((parsers.anyescaped - (parsers.dquote
5121 + parsers.blankline^2)
5122 ) + V(1))^0
5123 * parsers.dquote }
5124
5125 parsers.link_text = parsers.lbracket
5126 * Cs((parsers.alphanumeric^1
5127 + parsers.bracketed
5128 + parsers.inticks
5129 + parsers.autolink
5130 + V("InlineHtml")
5131 + ( parsers.backslash * parsers.backslash)
5132 + ( parsers.backslash
5133 * ( parsers.lbracket
5134 + parsers.rbracket)
5135 + V("Space")
5136 + V("NoSoftLineBreakEndline")
5137 + (parsers.any
5138 - ( parsers.newline
5139 + parsers.lbracket
5140 + parsers.rbracket
5141 + parsers.blankline^2))))^0)
5142 * parsers.rbracket
5143
5144 parsers.link_label_body = -#(parsers.sp * parsers.rbracket)
5145 * #( ( parsers.any
5146 - parsers.rbracket)^-999
5147 * parsers.rbracket)
5148 * Cs((parsers.alphanumeric^1
5149 + parsers.inticks
5150 + parsers.autolink
5151 + V("InlineHtml")
5152 + ( parsers.backslash * parsers.backslash)
5153 + ( parsers.backslash
5154 * ( parsers.lbracket
5155 + parsers.rbracket)
5156 + V("Space")
5157 + V("NoSoftLineBreakEndline")
5158 + (parsers.any
5159 - ( parsers.newline
5160 + parsers.lbracket
5161 + parsers.rbracket
5162 + parsers.blankline^2))))^1)
5163
5164 parsers.link_label = parsers.lbracket
5165 * parsers.link_label_body
5166 * parsers.rbracket
5167
5168 parsers.inparens_url = P{ parsers.lparent
5169 * ((parsers.anyescaped - (parsers.lparent
5170 + parsers.rparent
5171 + parsers.spacing)
5172 ) + V(1))^0
5173 * parsers.rparent }
5174
5175 -- url for markdown links, allowing nested brackets:
5176 parsers.url = parsers.less * Cs((parsers.anyescaped
5177 - parsers.newline
5178 - parsers.less
5179 - parsers.more)^0)
5180 * parsers.more
5181 + -parsers.less
5182 * Cs((parsers.inparens_url + (parsers.anyescaped
5183 - parsers.spacing
5184 - parsers.lparent
5185 - parsers.rparent))^1)
5186
5187 -- quoted text:
5188 parsers.title_s = parsers.squote
5189 * Cs((parsers.html_entities
5190 + V("Space")
5191 + V("NoSoftLineBreakEndline")
5192 + ( parsers.anyescaped
5193 - parsers.newline
5194 - parsers.squote
5195 - parsers.blankline^2))^0)
5196 * parsers.squote
5197
5198 parsers.title_d = parsers.dquote
5199 * Cs((parsers.html_entities
5200 + V("Space")
5201 + V("NoSoftLineBreakEndline")
5202 + ( parsers.anyescaped
5203 - parsers.newline
5204 - parsers.dquote
5205 - parsers.blankline^2))^0)
5206 * parsers.dquote
5207
5208 parsers.title_p = parsers.lparent
5209 * Cs((parsers.html_entities
5210 + V("Space")
5211 + V("NoSoftLineBreakEndline")
5212 + ( parsers.anyescaped
5213 - parsers.newline
5214 - parsers.lparent
5215 - parsers.rparent
5216 - parsers.blankline^2))^0)
5217 * parsers.rparent
5218
5219 parsers.title
5220 = parsers.title_d + parsers.title_s + parsers.title_p
5221
5222 parsers.optionaltitle
5223 = parsers.spnlc * parsers.title * parsers.spacechar^0 + Cc("")
5224
5225 -- parse a reference definition: [foo]: /bar "title"
5226 parsers.define_reference_parser = (parsers.check_trail / "")
5227 * parsers.link_label * parsers.colon
5228 * parsers.spnlc * parsers.url
5229 * ( parsers.spnlc_sep * parsers.title
5230 * parsers.only_blank
5231 + Cc("") * parsers.only_blank)
5232 parsers.Inline = V("Inline")
5233
5234 -- parse many p between starter and ender
5235 parsers.between = function(p, starter, ender)
5236 local ender2 = B(parsers.nonspacechar) * ender
5237 return ( starter
5238 * #parsers.nonspacechar
5239 * Ct(p * (p - ender2)^0)
5240 * ender2)
5241 end
5242
5243 parsers.lineof = function(c)
5244 return ( parsers.check_trail_no_rem
5245 * (P(c) * parsers.optionalspace)^3
5246 * (parsers.newline + parsers.eof))
5247 end
5248
5249 parsers.thematic_break_lines = parsers.lineof(parsers.asterisk)
5250 + parsers.lineof(parsers.dash)
5251 + parsers.lineof(parsers.underscore)
5252 -- parse Atx heading start and return level
5253 parsers.heading_start = #parsers.hash * C(parsers.hash^-6)
5254 * -parsers.hash / length
5255
5256 -- parse setext header ending and return level
5257 parsers.heading_level
5258 = parsers.nonindentspace * parsers.equal^1
5259 * parsers.optionalspace * #parsers.newline * Cc(1)
5260 + parsers.nonindentspace * parsers.dash^1
5261 * parsers.optionalspace * #parsers.newline * Cc(2)
5262
5263 local function strip_atx_end(s)
5264 return s:gsub("%s+#*%s*\n$","")
5265 end
5266
5267 parsers.atx_heading = parsers.check_trail_no_rem
5268 * Cg(parsers.heading_start, "level")
5269 * (C( parsers.optionalspace
5270 * parsers.hash^0
5271 * parsers.optionalspace
5272 * parsers.newline)
5273 + parsers.spacechar^1
5274 * C(parsers.line))
5275 M.reader = {}
5276 function M.reader.new(writer, options)
5277 local self = {}
5278 self.writer = writer
5279 self.options = options
5280 self.parsers = {}
5281 (function(parsers)
5282 setmetatable(self.parsers, {
5283 __index = function (_, key)
5284 return parsers[key]
5285 end
5286 })
5287 end)(parsers)
5288 local parsers = self.parsers
5289 function self.normalize_tag(tag)
5290 tag = util.rope_to_string(tag)
5291 tag = tag:gsub("[ \n\r\t]+", " ")
5292 tag = tag:gsub("^ ", ""):gsub(" $", "")
5293 local form = nil
5294 if options.unicodeNormalization then
5295 form = options.unicodeNormalizationForm
5296 end
5297 tag = util.casefold(tag, form)
5298 return tag
5299 end
5300 local function iterlines(s, f)
5301 local rope = lpeg.match(Ct((parsers.line / f)^1), s)
5302 return util.rope_to_string(rope)
5303 end
5304 if options.preserveTabs then
5305 self.expandtabs = function(s) return s end
5306 else
5307 self.expandtabs = function(s)
5308 if s:find("\t") then
5309 return iterlines(s, util.expand_tabs_in_line)
5310 else
5311 return s
5312 end
5313 end
5314 end
5315 self.parser_functions = {}
5316 self.create_parser = function(name, grammar, toplevel)
5317 self.parser_functions[name] = function(str)
5318 if toplevel and options.stripIndent then
5319 local min_prefix_length, min_prefix = nil, ''
5320 str = iterlines(str, function(line)
5321 if lpeg.match(parsers.nonemptyline, line) == nil then
5322 return line
5323 end
5324 line = util.expand_tabs_in_line(line)
5325 local prefix = lpeg.match(C(parsers.optionalspace), line)
5326 local prefix_length = #prefix
5327 local is_shorter = min_prefix_length == nil
5328 if not is_shorter then
5329 is_shorter = prefix_length < min_prefix_length
5330 end
5331 if is_shorter then
5332 min_prefix_length, min_prefix = prefix_length, prefix
5333 end
5334 return line
5335 end)
5336 str = str:gsub('^' .. min_prefix, '')
5337 end
5338 if toplevel and (options.texComments or options.hybrid) then
5339 str = lpeg.match(Ct(parsers.commented_line^1), str)
5340 str = util.rope_to_string(str)
5341 end
5342 local res = lpeg.match(grammar(), str)
5343 if res == nil then
5344 return writer.error(
5345 format("Parser `%s` failed to process the input text.", name),
5346 format("Here are the first 20 characters of the remaining "
5347 .. "unprocessed text: `%s`.", str:sub(1,20))
5348 )
5349 else
5350 return res
5351 end
5352 end
5353 end
5354
5355 self.create_parser("parse_blocks",
5356 function()
5357 return parsers.blocks
5358 end, true)
5359
5360 self.create_parser("parse_blocks_nested",
5361 function()
5362 return parsers.blocks_nested
5363 end, false)
5364
5365 self.create_parser("parse_inlines",
5366 function()
5367 return parsers.inlines
5368 end, false)
5369
5370 self.create_parser("parse_inlines_no_inline_note",
5371 function()
5372 return parsers.inlines_no_inline_note
5373 end, false)
5374
5375 self.create_parser("parse_inlines_no_html",
5376 function()
5377 return parsers.inlines_no_html
5378 end, false)
5379
5380 self.create_parser("parse_inlines_nbsp",
5381 function()
5382 return parsers.inlines_nbsp
5383 end, false)
5384
5385 self.create_parser("parse_inlines_no_link_or_emphasis",
5386 function()
5387 return parsers.inlines_no_link_or_emphasis
5388 end, false)
5389
5390 self.create_parser("parse_inlines_identity",
5391 function()
5392 return parsers.inlines_identity
5393 end, false)
5394
5395 self.create_parser("parse_inlines_string",
5396 function()
5397 return parsers.inlines_string
5398 end, false)
5399
5400 self.create_parser("parse_inlines_math",
5401 function()
5402 return parsers.inlines_math
5403 end, false)
5404 parsers.minimally_indented_blankline
5405 = parsers.check_minimal_indent * (parsers.blankline / "")
5406
5407 parsers.minimally_indented_block
5408 = parsers.check_minimal_indent * V("Block")
5409
5410 parsers.minimally_indented_block_or_paragraph
5411 = parsers.check_minimal_indent * V("BlockOrParagraph")
5412
5413 parsers.minimally_indented_paragraph
5414 = parsers.check_minimal_indent * V("Paragraph")
5415
5416 parsers.minimally_indented_plain
5417 = parsers.check_minimal_indent * V("Plain")
5418
5419 parsers.minimally_indented_par_or_plain
5420 = parsers.minimally_indented_paragraph
5421 + parsers.minimally_indented_plain
5422
5423 parsers.minimally_indented_par_or_plain_no_blank
5424 = parsers.minimally_indented_par_or_plain
5425 - parsers.minimally_indented_blankline
5426
5427 parsers.minimally_indented_ref
5428 = parsers.check_minimal_indent * V("Reference")
5429
5430 parsers.minimally_indented_blank
5431 = parsers.check_minimal_indent * V("Blank")
5432
5433 parsers.conditionally_indented_blankline
5434 = parsers.check_minimal_blank_indent * (parsers.blankline / "")
5435
5436 parsers.minimally_indented_ref_or_block
5437 = parsers.minimally_indented_ref
5438 + parsers.minimally_indented_block
5439 - parsers.minimally_indented_blankline
5440
5441 parsers.minimally_indented_ref_or_block_or_par
5442 = parsers.minimally_indented_ref
5443 + parsers.minimally_indented_block_or_paragraph
5444 - parsers.minimally_indented_blankline
5445
5446
5447 function parsers.separator_loop(separated_block, paragraph,
5448 block_separator, paragraph_separator)
5449 return separated_block
5450 + block_separator
5451 * paragraph
5452 * separated_block
5453 + paragraph_separator
5454 * paragraph
5455 end
5456
5457 function parsers.create_loop_body_pair(separated_block, paragraph,
5458 block_separator,
5459 paragraph_separator)
5460 return {
5461 block = parsers.separator_loop(separated_block, paragraph,
5462 block_separator, block_separator),
5463 par = parsers.separator_loop(separated_block, paragraph,
5464 block_separator, paragraph_separator)
5465 }
5466 end
5467
5468 parsers.block_sep_group = function(blank)
5469 return blank^0 * parsers.eof
5470 + ( blank^2 / writer.paragraphsep
5471 + blank^0 / writer.interblocksep
5472 )
5473 end
5474
5475 parsers.par_sep_group = function(blank)
5476 return blank^0 * parsers.eof
5477 + blank^0 / writer.paragraphsep
5478 end
5479
5480 parsers.sep_group_no_output = function(blank)
5481 return blank^0 * parsers.eof
5482 + blank^0
5483 end
5484
5485 parsers.content_blank = parsers.minimally_indented_blankline
5486
5487 parsers.ref_or_block_separated
5488 = parsers.sep_group_no_output(parsers.content_blank)
5489 * ( parsers.minimally_indented_ref
5490 - parsers.content_blank)
5491 + parsers.block_sep_group(parsers.content_blank)
5492 * ( parsers.minimally_indented_block
5493 - parsers.content_blank)
5494
5495 parsers.loop_body_pair =
5496 parsers.create_loop_body_pair(
5497 parsers.ref_or_block_separated,
5498 parsers.minimally_indented_par_or_plain_no_blank,
5499 parsers.block_sep_group(parsers.content_blank),
5500 parsers.par_sep_group(parsers.content_blank))
5501
5502 parsers.content_loop = ( V("Block")
5503 * parsers.loop_body_pair.block^0
5504 + (V("Paragraph") + V("Plain"))
5505 * parsers.ref_or_block_separated
5506 * parsers.loop_body_pair.block^0
5507 + (V("Paragraph") + V("Plain"))
5508 * parsers.loop_body_pair.par^0)
5509 * parsers.content_blank^0
5510
5511 parsers.indented_content = function()
5512 return Ct( (V("Reference") + (parsers.blankline / ""))
5513 * parsers.content_blank^0
5514 * parsers.check_minimal_indent
5515 * parsers.content_loop
5516 + (V("Reference") + (parsers.blankline / ""))
5517 * parsers.content_blank^0
5518 + parsers.content_loop)
5519 end
5520
5521 parsers.add_indent = function(pattern, name, breakable)
5522 return Cg(Cmt( Cb("indent_info")
5523 * Ct(pattern)
5524 * ( #parsers.linechar -- check if starter is blank
5525 * Cc(false) + Cc(true))
5526 * Cc(name)
5527 * Cc(breakable),
5528 process_starter_indent), "indent_info")
5529 end
5530
5531 if options.hashEnumerators then
5532 parsers.dig = parsers.digit + parsers.hash
5533 else
5534 parsers.dig = parsers.digit
5535 end
5536
5537 parsers.enumerator = function(delimiter_type, interrupting)
5538 local delimiter_range
5539 local allowed_end
5540 if interrupting then
5541 delimiter_range = P("1")
5542 allowed_end = C(parsers.spacechar^1) * #parsers.linechar
5543 else
5544 delimiter_range = parsers.dig * parsers.dig^-8
5545 allowed_end = C(parsers.spacechar^1)
5546 + #(parsers.newline + parsers.eof)
5547 end
5548
5549 return parsers.check_trail
5550 * Ct(C(delimiter_range) * C(delimiter_type))
5551 * allowed_end
5552 end
5553
5554 parsers.starter = parsers.bullet(parsers.dash)
5555 + parsers.bullet(parsers.asterisk)
5556 + parsers.bullet(parsers.plus)
5557 + parsers.enumerator(parsers.period)
5558 + parsers.enumerator(parsers.rparent)
5559
5560 parsers.blockquote_start
5561 = parsers.check_trail
5562 * C(parsers.more)
5563 * C(parsers.spacechar^0)
5564
5565 parsers.blockquote_body
5566 = parsers.add_indent(parsers.blockquote_start, "bq", true)
5567 * parsers.indented_content()
5568 * remove_indent("bq")
5569
5570 if not options.breakableBlockquotes then
5571 parsers.blockquote_body
5572 = parsers.add_indent(parsers.blockquote_start, "bq", false)
5573 * parsers.indented_content()
5574 * remove_indent("bq")
5575 end
5576 local function parse_content_part(content_part)
5577 local rope = util.rope_to_string(content_part)
5578 local parsed
5579 = self.parser_functions.parse_inlines_no_link_or_emphasis(rope)
5580 parsed.indent_info = nil
5581 return parsed
5582 end
5583
5584 local collect_emphasis_content =
5585 function(t, opening_index, closing_index)
5586 local content = {}
5587
5588 local content_part = {}
5589 for i = opening_index, closing_index do
5590 local value = t[i]
5591
5592 if value.rendered ~= nil then
5593 content[#content + 1] = parse_content_part(content_part)
5594 content_part = {}
5595 content[#content + 1] = value.rendered
5596 value.rendered = nil
5597 else
5598 if value.warning ~= nil then
5599 if next(content_part) ~= nil then
5600 content[#content + 1] = parse_content_part(content_part)
5601 end
5602 content_part = {}
5603
5604 content[#content + 1] = value.warning
5605 value.warning = nil
5606 end
5607 if value.type == "delimiter"
5608 and value.element == "emphasis" then
5609 if value.is_active then
5610 content_part[#content_part + 1]
5611 = string.rep(value.character, value.current_count)
5612 end
5613 else
5614 content_part[#content_part + 1] = value.content
5615 end
5616 value.content = ''
5617 value.is_active = false
5618 end
5619 end
5620
5621 if next(content_part) ~= nil then
5622 content[#content + 1] = parse_content_part(content_part)
5623 end
5624
5625 return content
5626 end
5627
5628 local function fill_emph(t, opening_index, closing_index)
5629 local content
5630 = collect_emphasis_content(t, opening_index + 1,
5631 closing_index - 1)
5632 t[opening_index + 1].is_active = true
5633 t[opening_index + 1].rendered = writer.emphasis(content)
5634 end
5635
5636 local function fill_strong(t, opening_index, closing_index)
5637 local content
5638 = collect_emphasis_content(t, opening_index + 1,
5639 closing_index - 1)
5640 t[opening_index + 1].is_active = true
5641 t[opening_index + 1].rendered = writer.strong(content)
5642 end
5643
5644 local function breaks_three_rule(opening_delimiter, closing_delimiter)
5645 return ( opening_delimiter.is_closing
5646 or closing_delimiter.is_opening)
5647 and (( opening_delimiter.original_count
5648 + closing_delimiter.original_count) % 3 == 0)
5649 and ( opening_delimiter.original_count % 3 ~= 0
5650 or closing_delimiter.original_count % 3 ~= 0)
5651 end
5652
5653 local find_emphasis_opener = function(t, bottom_index, latest_index,
5654 character, closing_delimiter)
5655 for i = latest_index, bottom_index, -1 do
5656 local value = t[i]
5657 if value.is_active and
5658 value.is_opening and
5659 value.type == "delimiter" and
5660 value.element == "emphasis" and
5661 (value.character == character) and
5662 (value.current_count > 0) then
5663 if not breaks_three_rule(value, closing_delimiter) then
5664 return i
5665 end
5666 end
5667 end
5668 end
5669
5670 local function process_emphasis(t, opening_index, closing_index)
5671 for i = opening_index, closing_index do
5672 local value = t[i]
5673 if value.type == "delimiter" and value.element == "emphasis" then
5674 local delimiter_length = string.len(value.content)
5675 value.character = string.sub(value.content, 1, 1)
5676 value.current_count = delimiter_length
5677 value.original_count = delimiter_length
5678 end
5679 end
5680
5681 local openers_bottom = {
5682 ['*'] = {
5683 [true] = {opening_index, opening_index, opening_index},
5684 [false] = {opening_index, opening_index, opening_index}
5685 },
5686 ['_'] = {
5687 [true] = {opening_index, opening_index, opening_index},
5688 [false] = {opening_index, opening_index, opening_index}
5689 }
5690 }
5691
5692 local current_position = opening_index
5693 local max_position = closing_index
5694
5695 while current_position <= max_position do
5696 local value = t[current_position]
5697
5698 if value.type ~= "delimiter" or
5699 value.element ~= "emphasis" or
5700 not value.is_active or
5701 not value.is_closing or
5702 (value.current_count <= 0) then
5703 current_position = current_position + 1
5704 goto continue
5705 end
5706
5707 local character = value.character
5708 local is_opening = value.is_opening
5709 local closing_length_modulo_three = value.original_count % 3
5710
5711 local current_openers_bottom
5712 = openers_bottom[character][is_opening]
5713 [closing_length_modulo_three + 1]
5714
5715 local opener_position
5716 = find_emphasis_opener(t, current_openers_bottom,
5717 current_position - 1, character, value)
5718
5719 if (opener_position == nil) then
5720 openers_bottom[character][is_opening]
5721 [closing_length_modulo_three + 1]
5722 = current_position
5723 current_position = current_position + 1
5724 goto continue
5725 end
5726
5727 local opening_delimiter = t[opener_position]
5728
5729 local current_opening_count = opening_delimiter.current_count
5730 local current_closing_count = t[current_position].current_count
5731
5732 if (current_opening_count >= 2)
5733 and (current_closing_count >= 2) then
5734 opening_delimiter.current_count = current_opening_count - 2
5735 t[current_position].current_count = current_closing_count - 2
5736 fill_strong(t, opener_position, current_position)
5737 else
5738 opening_delimiter.current_count = current_opening_count - 1
5739 t[current_position].current_count = current_closing_count - 1
5740 fill_emph(t, opener_position, current_position)
5741 end
5742
5743 ::continue::
5744 end
5745 end
5746
5747 parsers.delimiter_run = function(character)
5748 return (B(parsers.backslash * character) + -B(character))
5749 * character^1
5750 * -#character
5751 end
5752
5753 parsers.left_flanking_delimiter_run = function(character)
5754 return (B( parsers.any)
5755 * ( parsers.unicode.preceding_punctuation
5756 + parsers.unicode.preceding_whitespace)
5757 + -B(parsers.any))
5758 * parsers.delimiter_run(character)
5759 * #parsers.unicode.punctuation
5760 + parsers.delimiter_run(character)
5761 * -#( #parsers.unicode.punctuation
5762 + #parsers.unicode.whitespace
5763 + parsers.eof)
5764 end
5765
5766 parsers.right_flanking_delimiter_run = function(character)
5767 return parsers.unicode.preceding_punctuation
5768 * parsers.delimiter_run(character)
5769 * ( #parsers.unicode.punctuation
5770 + #parsers.unicode.whitespace
5771 + parsers.eof)
5772 + (B(parsers.any)
5773 * -( parsers.unicode.preceding_punctuation
5774 + parsers.unicode.preceding_whitespace))
5775 * parsers.delimiter_run(character)
5776 end
5777
5778 if options.underscores then
5779 parsers.emph_start
5780 = parsers.left_flanking_delimiter_run(parsers.asterisk)
5781 + ( -#parsers.right_flanking_delimiter_run(parsers.underscore)
5782 + ( parsers.unicode.preceding_punctuation
5783 * #parsers.right_flanking_delimiter_run(parsers.underscore)))
5784 * parsers.left_flanking_delimiter_run(parsers.underscore)
5785
5786 parsers.emph_end
5787 = parsers.right_flanking_delimiter_run(parsers.asterisk)
5788 + ( -#parsers.left_flanking_delimiter_run(parsers.underscore)
5789 + #( parsers.left_flanking_delimiter_run(parsers.underscore)
5790 * #parsers.unicode.punctuation))
5791 * parsers.right_flanking_delimiter_run(parsers.underscore)
5792 else
5793 parsers.emph_start
5794 = parsers.left_flanking_delimiter_run(parsers.asterisk)
5795
5796 parsers.emph_end
5797 = parsers.right_flanking_delimiter_run(parsers.asterisk)
5798 end
5799
5800 parsers.emph_capturing_open_and_close
5801 = #parsers.emph_start * #parsers.emph_end
5802 * Ct( Cg(Cc("delimiter"), "type")
5803 * Cg(Cc("emphasis"), "element")
5804 * Cg(C(parsers.emph_start), "content")
5805 * Cg(Cc(true), "is_opening")
5806 * Cg(Cc(true), "is_closing"))
5807
5808 parsers.emph_capturing_open = Ct( Cg(Cc("delimiter"), "type")
5809 * Cg(Cc("emphasis"), "element")
5810 * Cg(C(parsers.emph_start), "content")
5811 * Cg(Cc(true), "is_opening")
5812 * Cg(Cc(false), "is_closing"))
5813
5814 parsers.emph_capturing_close = Ct( Cg(Cc("delimiter"), "type")
5815 * Cg(Cc("emphasis"), "element")
5816 * Cg(C(parsers.emph_end), "content")
5817 * Cg(Cc(false), "is_opening")
5818 * Cg(Cc(true), "is_closing"))
5819
5820 parsers.emph_open_or_close = parsers.emph_capturing_open_and_close
5821 + parsers.emph_capturing_open
5822 + parsers.emph_capturing_close
5823
5824 parsers.emph_open = parsers.emph_capturing_open_and_close
5825 + parsers.emph_capturing_open
5826
5827 parsers.emph_close = parsers.emph_capturing_open_and_close
5828 + parsers.emph_capturing_close
5829
5830 -- List of references defined in the document
5831 local references
5832
5833 -- List of note references defined in the document
5834 parsers.rawnotes = {}
5835
5836 function self.register_link(_, tag, url, title,
5837 attributes)
5838 local normalized_tag = self.normalize_tag(tag)
5839 if references[normalized_tag] == nil then
5840 references[normalized_tag] = {
5841 url = url,
5842 title = title,
5843 attributes = attributes
5844 }
5845 return ""
5846 else
5847 local text
5848 = string.format('Multiply defined link reference "%s"', tag)
5849 local more = string.format("Look for the text `[%s]: ...`.", tag)
5850 return writer.warning(text, more)
5851 end
5852 end
5853
5854 function self.lookup_reference(tag)
5855 return references[self.normalize_tag(tag)]
5856 end
5857
5858 function self.lookup_note_reference(tag)
5859 return parsers.rawnotes[self.normalize_tag(tag)]
5860 end
5861
5862 parsers.title_s_direct_ref = parsers.squote
5863 * Cs((parsers.html_entities
5864 + ( parsers.anyescaped
5865 - parsers.squote
5866 - parsers.blankline^2))^0)
5867 * parsers.squote
5868
5869 parsers.title_d_direct_ref = parsers.dquote
5870 * Cs((parsers.html_entities
5871 + ( parsers.anyescaped
5872 - parsers.dquote
5873 - parsers.blankline^2))^0)
5874 * parsers.dquote
5875
5876 parsers.title_p_direct_ref = parsers.lparent
5877 * Cs((parsers.html_entities
5878 + ( parsers.anyescaped
5879 - parsers.lparent
5880 - parsers.rparent
5881 - parsers.blankline^2))^0)
5882 * parsers.rparent
5883
5884 parsers.title_direct_ref = parsers.title_s_direct_ref
5885 + parsers.title_d_direct_ref
5886 + parsers.title_p_direct_ref
5887
5888 parsers.inline_direct_ref_inside = parsers.lparent * parsers.spnl
5889 * Cg(parsers.url + Cc(""), "url")
5890 * parsers.spnl
5891 * Cg( parsers.title_direct_ref
5892 + Cc(""), "title")
5893 * parsers.spnl * parsers.rparent
5894
5895 parsers.inline_direct_ref = parsers.lparent * parsers.spnlc
5896 * Cg(parsers.url + Cc(""), "url")
5897 * parsers.spnlc
5898 * Cg(parsers.title + Cc(""), "title")
5899 * parsers.spnlc * parsers.rparent
5900
5901 parsers.empty_link = parsers.lbracket
5902 * parsers.rbracket
5903
5904 parsers.inline_link = parsers.link_text
5905 * parsers.inline_direct_ref
5906
5907 parsers.full_link = parsers.link_text
5908 * parsers.link_label
5909
5910 parsers.shortcut_link = parsers.link_label
5911 * -(parsers.empty_link + parsers.link_label)
5912
5913 parsers.collapsed_link = parsers.link_label
5914 * parsers.empty_link
5915
5916 parsers.image_opening = #(parsers.exclamation * parsers.inline_link)
5917 * Cg(Cc("inline"), "link_type")
5918 + #(parsers.exclamation * parsers.full_link)
5919 * Cg(Cc("full"), "link_type")
5920 + #( parsers.exclamation
5921 * parsers.collapsed_link)
5922 * Cg(Cc("collapsed"), "link_type")
5923 + #(parsers.exclamation * parsers.shortcut_link)
5924 * Cg(Cc("shortcut"), "link_type")
5925 + #(parsers.exclamation * parsers.empty_link)
5926 * Cg(Cc("empty"), "link_type")
5927
5928 parsers.link_opening = #parsers.inline_link
5929 * Cg(Cc("inline"), "link_type")
5930 + #parsers.full_link
5931 * Cg(Cc("full"), "link_type")
5932 + #parsers.collapsed_link
5933 * Cg(Cc("collapsed"), "link_type")
5934 + #parsers.shortcut_link
5935 * Cg(Cc("shortcut"), "link_type")
5936 + #parsers.empty_link
5937 * Cg(Cc("empty_link"), "link_type")
5938 + #parsers.link_text
5939 * Cg(Cc("link_text"), "link_type")
5940
5941 parsers.note_opening = #(parsers.circumflex * parsers.link_text)
5942 * Cg(Cc("note_inline"), "link_type")
5943
5944 parsers.raw_note_opening = #( parsers.lbracket
5945 * parsers.circumflex
5946 * parsers.link_label_body
5947 * parsers.rbracket)
5948 * Cg(Cc("raw_note"), "link_type")
5949
5950 local inline_note_element = Cg(Cc("note"), "element")
5951 * parsers.note_opening
5952 * Cg( parsers.circumflex
5953 * parsers.lbracket, "content")
5954
5955 local image_element = Cg(Cc("image"), "element")
5956 * parsers.image_opening
5957 * Cg( parsers.exclamation
5958 * parsers.lbracket, "content")
5959
5960 local note_element = Cg(Cc("note"), "element")
5961 * parsers.raw_note_opening
5962 * Cg( parsers.lbracket
5963 * parsers.circumflex, "content")
5964
5965 local link_element = Cg(Cc("link"), "element")
5966 * parsers.link_opening
5967 * Cg(parsers.lbracket, "content")
5968
5969 local opening_elements = parsers.fail
5970
5971 if options.inlineNotes then
5972 opening_elements = opening_elements + inline_note_element
5973 end
5974
5975 opening_elements = opening_elements + image_element
5976
5977 if options.notes then
5978 opening_elements = opening_elements + note_element
5979 end
5980
5981 opening_elements = opening_elements + link_element
5982
5983 parsers.link_image_opening = Ct( Cg(Cc("delimiter"), "type")
5984 * Cg(Cc(true), "is_opening")
5985 * Cg(Cc(false), "is_closing")
5986 * opening_elements)
5987
5988 parsers.link_image_closing = Ct( Cg(Cc("delimiter"), "type")
5989 * Cg(Cc("link"), "element")
5990 * Cg(Cc(false), "is_opening")
5991 * Cg(Cc(true), "is_closing")
5992 * ( Cg(Cc(true), "is_direct")
5993 * Cg( parsers.rbracket
5994 * #parsers.inline_direct_ref,
5995 "content")
5996 + Cg(Cc(false), "is_direct")
5997 * Cg(parsers.rbracket, "content")))
5998
5999 parsers.link_image_open_or_close = parsers.link_image_opening
6000 + parsers.link_image_closing
6001
6002 if options.html then
6003 parsers.link_emph_precedence = parsers.inticks
6004 + parsers.autolink
6005 + parsers.html_inline_tags
6006 else
6007 parsers.link_emph_precedence = parsers.inticks
6008 + parsers.autolink
6009 end
6010
6011 parsers.link_and_emph_endline = parsers.newline
6012 * ((parsers.check_minimal_indent
6013 * -V("EndlineExceptions")
6014 + parsers.check_optional_indent
6015 * -V("EndlineExceptions")
6016 * -V("ListStarter")) / "")
6017 * parsers.spacechar^0 / "\n"
6018
6019 parsers.link_and_emph_content
6020 = Ct( Cg(Cc("content"), "type")
6021 * Cg(Cs(( parsers.link_emph_precedence
6022 + parsers.backslash * parsers.linechar
6023 + parsers.link_and_emph_endline
6024 + (parsers.linechar
6025 - parsers.blankline^2
6026 - parsers.link_image_open_or_close
6027 - parsers.emph_open_or_close))^0), "content"))
6028
6029 parsers.link_and_emph_table
6030 = (parsers.link_image_opening + parsers.emph_open)
6031 * parsers.link_and_emph_content
6032 * ((parsers.link_image_open_or_close + parsers.emph_open_or_close)
6033 * parsers.link_and_emph_content)^1
6034
6035 local function collect_link_content(t, opening_index, closing_index)
6036 local content = {}
6037 for i = opening_index, closing_index do
6038 content[#content + 1] = t[i].content
6039 end
6040 return util.rope_to_string(content)
6041 end
6042
6043 local function find_link_opener(t, bottom_index, latest_index)
6044 for i = latest_index, bottom_index, -1 do
6045 local value = t[i]
6046 if value.type == "delimiter" and
6047 value.is_opening and
6048 ( value.element == "link"
6049 or value.element == "image"
6050 or value.element == "note")
6051 and not value.removed then
6052 if value.is_active then
6053 return i
6054 end
6055 value.removed = true
6056 return nil
6057 end
6058 end
6059 end
6060
6061 local function find_next_link_closing_index(t, latest_index)
6062 for i = latest_index, #t do
6063 local value = t[i]
6064 if value.is_closing and
6065 value.element == "link" and
6066 not value.removed then
6067 return i
6068 end
6069 end
6070 end
6071
6072 local function disable_previous_link_openers(t, opening_index)
6073 if t[opening_index].element == "image" then
6074 return
6075 end
6076
6077 for i = opening_index, 1, -1 do
6078 local value = t[i]
6079 if value.is_active and
6080 value.type == "delimiter" and
6081 value.is_opening and
6082 value.element == "link" then
6083 value.is_active = false
6084 end
6085 end
6086 end
6087
6088 local function disable_range(t, opening_index, closing_index)
6089 for i = opening_index, closing_index do
6090 local value = t[i]
6091 if value.is_active then
6092 value.is_active = false
6093 if value.type == "delimiter" then
6094 value.removed = true
6095 end
6096 end
6097 end
6098 end
6099
6100 local delete_parsed_content_in_range =
6101 function(t, opening_index, closing_index)
6102 for i = opening_index, closing_index do
6103 t[i].rendered = nil
6104 end
6105 end
6106
6107 local function empty_content_in_range(t, opening_index, closing_index)
6108 for i = opening_index, closing_index do
6109 t[i].content = ''
6110 end
6111 end
6112
6113 local function join_attributes(reference_attributes, own_attributes)
6114 local merged_attributes = {}
6115 for _, attribute in ipairs(reference_attributes or {}) do
6116 table.insert(merged_attributes, attribute)
6117 end
6118 for _, attribute in ipairs(own_attributes or {}) do
6119 table.insert(merged_attributes, attribute)
6120 end
6121 if next(merged_attributes) == nil then
6122 merged_attributes = nil
6123 end
6124 return merged_attributes
6125 end
6126
6127 local render_link_or_image =
6128 function(t, opening_index, closing_index, content_end_index,
6129 reference)
6130 process_emphasis(t, opening_index, content_end_index)
6131 local mapped = collect_emphasis_content(t, opening_index + 1,
6132 content_end_index - 1)
6133
6134 local rendered = {}
6135 if (t[opening_index].element == "link") then
6136 rendered = writer.link(mapped, reference.url,
6137 reference.title, reference.attributes)
6138 end
6139
6140 if (t[opening_index].element == "image") then
6141 rendered = writer.image(mapped, reference.url,
6142 self.parser_functions.parse_inlines_string(reference.title),
6143 reference.attributes)
6144 end
6145
6146 if (t[opening_index].element == "note") then
6147 if (t[opening_index].link_type == "note_inline") then
6148 rendered = writer.note(mapped)
6149 end
6150 if (t[opening_index].link_type == "raw_note") then
6151 rendered = writer.note(reference)
6152 end
6153 end
6154
6155 t[opening_index].rendered = rendered
6156 delete_parsed_content_in_range(t, opening_index + 1,
6157 closing_index)
6158 empty_content_in_range(t, opening_index, closing_index)
6159 disable_previous_link_openers(t, opening_index)
6160 disable_range(t, opening_index, closing_index)
6161 end
6162
6163 local resolve_inline_following_content =
6164 function(t, closing_index, match_reference, match_link_attributes)
6165 local content = ""
6166 for i = closing_index + 1, #t do
6167 content = content .. t[i].content
6168 end
6169
6170 local matching_content = parsers.succeed
6171
6172 if match_reference then
6173 matching_content = matching_content
6174 * parsers.inline_direct_ref_inside
6175 end
6176
6177 if match_link_attributes then
6178 matching_content = matching_content
6179 * Cg(Ct(parsers.attributes^-1), "attributes")
6180 end
6181
6182 local matched = lpeg.match(Ct( matching_content
6183 * Cg(Cp(), "end_position")), content)
6184
6185 local matched_count = matched.end_position - 1
6186 for i = closing_index + 1, #t do
6187 local value = t[i]
6188
6189 local chars_left = matched_count
6190 matched_count = matched_count - #value.content
6191
6192 if matched_count <= 0 then
6193 value.content = value.content:sub(chars_left + 1)
6194 break
6195 end
6196
6197 value.content = ''
6198 value.is_active = false
6199 end
6200
6201 local attributes = matched.attributes
6202 if attributes == nil or next(attributes) == nil then
6203 attributes = nil
6204 end
6205
6206 return {
6207 url = matched.url or "",
6208 title = matched.title or "",
6209 attributes = attributes
6210 }
6211 end
6212
6213 local function resolve_inline_link(t, opening_index, closing_index)
6214 local inline_content
6215 = resolve_inline_following_content(t, closing_index, true,
6216 t.match_link_attributes)
6217 render_link_or_image(t, opening_index, closing_index,
6218 closing_index, inline_content)
6219 end
6220
6221 local resolve_note_inline_link =
6222 function(t, opening_index, closing_index)
6223 local inline_content
6224 = resolve_inline_following_content(t, closing_index,
6225 false, false)
6226 render_link_or_image(t, opening_index, closing_index,
6227 closing_index, inline_content)
6228 end
6229
6230 local function resolve_shortcut_link(t, opening_index, closing_index)
6231 local content
6232 = collect_link_content(t, opening_index + 1, closing_index - 1)
6233 local r = self.lookup_reference(content)
6234
6235 if r then
6236 local inline_content
6237 = resolve_inline_following_content(t, closing_index, false,
6238 t.match_link_attributes)
6239 r.attributes
6240 = join_attributes(r.attributes, inline_content.attributes)
6241 render_link_or_image(t, opening_index, closing_index,
6242 closing_index, r)
6243 end
6244 end
6245
6246 local function resolve_raw_note_link(t, opening_index, closing_index)
6247 local content
6248 = collect_link_content(t, opening_index + 1, closing_index - 1)
6249 local r = self.lookup_note_reference(content)
6250
6251 if r then
6252 local parsed_ref = self.parser_functions.parse_blocks_nested(r)
6253 render_link_or_image(t, opening_index, closing_index,
6254 closing_index, parsed_ref)
6255 end
6256 end
6257
6258 local function resolve_full_link(t, opening_index, closing_index)
6259 local next_link_closing_index
6260 = find_next_link_closing_index(t, closing_index + 4)
6261 local next_link_content
6262 = collect_link_content(t, closing_index + 3,
6263 next_link_closing_index - 1)
6264 local r = self.lookup_reference(next_link_content)
6265
6266 if r then
6267 local inline_content
6268 = resolve_inline_following_content(t, next_link_closing_index,
6269 false,
6270 t.match_link_attributes)
6271 r.attributes
6272 = join_attributes(r.attributes, inline_content.attributes)
6273 render_link_or_image(t, opening_index, next_link_closing_index,
6274 closing_index, r)
6275 else
6276 local text = string.format('Undefined link reference "%s"',
6277 next_link_content)
6278 local more = string.format("Look for the text `[...][%s]`.",
6279 next_link_content)
6280 t[opening_index].warning = writer.warning(text, more)
6281 end
6282 end
6283
6284 local function resolve_collapsed_link(t, opening_index, closing_index)
6285 local next_link_closing_index
6286 = find_next_link_closing_index(t, closing_index + 4)
6287 local content
6288 = collect_link_content(t, opening_index + 1, closing_index - 1)
6289 local r = self.lookup_reference(content)
6290
6291 if r then
6292 local inline_content
6293 = resolve_inline_following_content(t, closing_index, false,
6294 t.match_link_attributes)
6295 r.attributes
6296 = join_attributes(r.attributes, inline_content.attributes)
6297 render_link_or_image(t, opening_index, next_link_closing_index,
6298 closing_index, r)
6299 else
6300 local text = string.format('Undefined link reference "%s"',
6301 content)
6302 local more = string.format("Look for the text `[%s][]`.",
6303 content)
6304 t[opening_index].warning = writer.warning(text, more)
6305 end
6306 end
6307
6308 local function process_links_and_emphasis(t)
6309 for _,value in ipairs(t) do
6310 value.is_active = true
6311 end
6312
6313 for i,value in ipairs(t) do
6314 if not value.is_closing
6315 or value.type ~= "delimiter"
6316 or not ( value.element == "link"
6317 or value.element == "image"
6318 or value.element == "note")
6319 or value.removed then
6320 goto continue
6321 end
6322
6323 local opener_position = find_link_opener(t, 1, i - 1)
6324 if (opener_position == nil) then
6325 goto continue
6326 end
6327
6328 local opening_delimiter = t[opener_position]
6329 opening_delimiter.removed = true
6330
6331 local link_type = opening_delimiter.link_type
6332
6333 if (link_type == "inline") then
6334 resolve_inline_link(t, opener_position, i)
6335 end
6336 if (link_type == "shortcut") then
6337 resolve_shortcut_link(t, opener_position, i)
6338 end
6339 if (link_type == "full") then
6340 resolve_full_link(t, opener_position, i)
6341 end
6342 if (link_type == "collapsed") then
6343 resolve_collapsed_link(t, opener_position, i)
6344 end
6345 if (link_type == "note_inline") then
6346 resolve_note_inline_link(t, opener_position, i)
6347 end
6348 if (link_type == "raw_note") then
6349 resolve_raw_note_link(t, opener_position, i)
6350 end
6351
6352 ::continue::
6353 end
6354
6355 t[#t].content = t[#t].content:gsub("%s*$","")
6356
6357 process_emphasis(t, 1, #t)
6358 local final_result = collect_emphasis_content(t, 1, #t)
6359 return final_result
6360 end
6361
6362 function self.defer_link_and_emphasis_processing(delimiter_table)
6363 return writer.defer_call(function()
6364 return process_links_and_emphasis(delimiter_table)
6365 end)
6366 end
6367
6368 parsers.Str = ( parsers.normalchar
6369 * (parsers.normalchar + parsers.at)^0)
6370 / writer.string
6371
6372 parsers.Symbol = (parsers.backtick^1 + V("SpecialChar"))
6373 / writer.string
6374
6375 parsers.Ellipsis = P("...") / writer.ellipsis
6376
6377 parsers.Smart = parsers.Ellipsis
6378
6379 parsers.Code = parsers.inticks / writer.code
6380
6381 if options.blankBeforeBlockquote then
6382 parsers.bqstart = parsers.fail
6383 else
6384 parsers.bqstart = parsers.blockquote_start
6385 end
6386
6387 if options.blankBeforeHeading then
6388 parsers.headerstart = parsers.fail
6389 else
6390 parsers.headerstart = parsers.atx_heading
6391 end
6392
6393 if options.blankBeforeList then
6394 parsers.interrupting_bullets = parsers.fail
6395 parsers.interrupting_enumerators = parsers.fail
6396 else
6397 parsers.interrupting_bullets
6398 = parsers.bullet(parsers.dash, true)
6399 + parsers.bullet(parsers.asterisk, true)
6400 + parsers.bullet(parsers.plus, true)
6401
6402 parsers.interrupting_enumerators
6403 = parsers.enumerator(parsers.period, true)
6404 + parsers.enumerator(parsers.rparent, true)
6405 end
6406
6407 if options.html then
6408 parsers.html_interrupting
6409 = parsers.check_trail
6410 * ( parsers.html_incomplete_open_tag
6411 + parsers.html_incomplete_close_tag
6412 + parsers.html_incomplete_open_special_tag
6413 + parsers.html_comment_start
6414 + parsers.html_cdatasection_start
6415 + parsers.html_declaration_start
6416 + parsers.html_instruction_start
6417 - parsers.html_close_special_tag
6418 - parsers.html_empty_special_tag)
6419 else
6420 parsers.html_interrupting = parsers.fail
6421 end
6422
6423 if options.blankBeforeHtmlBlock then
6424 parsers.html_interrupting = parsers.fail
6425 end
6426
6427 parsers.ListStarter = parsers.starter
6428
6429 parsers.EndlineExceptions
6430 = parsers.blankline -- paragraph break
6431 + parsers.eof -- end of document
6432 + parsers.bqstart
6433 + parsers.thematic_break_lines
6434 + parsers.interrupting_bullets
6435 + parsers.interrupting_enumerators
6436 + parsers.headerstart
6437 + parsers.html_interrupting
6438
6439 parsers.NoSoftLineBreakEndlineExceptions = parsers.EndlineExceptions
6440
6441 parsers.endline = parsers.newline
6442 * (parsers.check_minimal_indent
6443 * -V("EndlineExceptions")
6444 + parsers.check_optional_indent
6445 * -V("EndlineExceptions")
6446 * -V("ListStarter")) / function(_) return end
6447 * parsers.spacechar^0
6448
6449 parsers.Endline = parsers.endline
6450 / writer.soft_line_break
6451
6452 parsers.EndlineNoSub = parsers.endline
6453
6454 parsers.NoSoftLineBreakEndline
6455 = parsers.newline
6456 * (parsers.check_minimal_indent
6457 * -V("NoSoftLineBreakEndlineExceptions")
6458 + parsers.check_optional_indent
6459 * -V("NoSoftLineBreakEndlineExceptions")
6460 * -V("ListStarter"))
6461 * parsers.spacechar^0
6462 / writer.space
6463
6464 parsers.EndlineBreak = parsers.backslash * parsers.endline
6465 / writer.hard_line_break
6466
6467 parsers.OptionalIndent
6468 = parsers.spacechar^1 / writer.space
6469
6470 parsers.Space = parsers.spacechar^2 * parsers.endline
6471 / writer.hard_line_break
6472 + parsers.spacechar^1
6473 * parsers.endline^-1
6474 * parsers.eof / self.expandtabs
6475 + parsers.spacechar^1 * parsers.endline
6476 / writer.soft_line_break
6477 + parsers.spacechar^1
6478 * -parsers.newline / self.expandtabs
6479 + parsers.spacechar^1
6480 + ( parsers.spacechar
6481 + parsers.html_space_entity_char)^1
6482 * parsers.endline / writer.soft_line_break
6483
6484 parsers.NonbreakingEndline
6485 = parsers.endline
6486 / writer.nbsp
6487
6488 parsers.NonbreakingSpace
6489 = parsers.spacechar^2 * parsers.endline
6490 / writer.nbsp
6491 + parsers.spacechar^1
6492 * parsers.endline^-1 * parsers.eof / ""
6493 + parsers.spacechar^1 * parsers.endline
6494 * parsers.optionalspace
6495 / writer.nbsp
6496 + parsers.spacechar^1 * parsers.optionalspace
6497 / writer.nbsp
6498
6499 function self.auto_link_url(url, attributes)
6500 return writer.link(writer.escape(url),
6501 url, nil, attributes)
6502 end
6503 function self.auto_link_email(email, attributes)
6504 return writer.link(writer.escape(email),
6505 "mailto:"..email,
6506 nil, attributes)
6507 end
6508
6509 parsers.AutoLinkUrl = parsers.auto_link_url
6510 / self.auto_link_url
6511
6512 parsers.AutoLinkEmail
6513 = parsers.auto_link_email
6514 / self.auto_link_email
6515
6516 parsers.AutoLinkRelativeReference
6517 = parsers.auto_link_relative_reference
6518 / self.auto_link_url
6519
6520 parsers.LinkAndEmph = Ct(parsers.link_and_emph_table)
6521 / self.defer_link_and_emphasis_processing
6522
6523 parsers.EscapedChar = parsers.backslash
6524 * C(parsers.escapable) / writer.string
6525
6526 if options.htmlOutput == "commonmark" then
6527 parsers.InlineHtml
6528 = Cs(parsers.html_inline_comment)
6529 / self.parser_functions.parse_inlines_no_html
6530 / writer.inline_html_comment
6531 + Cs(parsers.html_any_empty_inline_tag)
6532 / writer.inline_html_empty_tag
6533 + Cs(parsers.html_inline_instruction)
6534 / writer.inline_html_processing_instruction
6535 + Cs(parsers.html_inline_cdatasection)
6536 / writer.inline_html_cdata_section
6537 + Cs(parsers.html_inline_declaration)
6538 / writer.inline_html_declaration
6539 + Cs(parsers.html_any_open_inline_tag)
6540 / writer.inline_html_open_tag
6541 + Cs(parsers.html_any_close_tag)
6542 / writer.inline_html_close_tag
6543 else
6544 parsers.InlineHtml = Cs(parsers.html_inline_comment)
6545 / self.parser_functions.parse_inlines_no_html
6546 / writer.inline_html_comment
6547 + Cs( parsers.html_any_empty_inline_tag
6548 + parsers.html_inline_instruction
6549 + parsers.html_inline_cdatasection
6550 + parsers.html_inline_declaration
6551 + parsers.html_any_open_inline_tag
6552 + parsers.html_any_close_tag)
6553 / writer.inline_html_tag
6554 end
6555
6556 parsers.HtmlEntity = parsers.html_entities / writer.string
6557 if options.htmlOutput == "commonmark" then
6558 parsers.DisplayHtml
6559 = parsers.check_trail
6560 * ( parsers.html_comment
6561 / self.parser_functions.parse_blocks_nested
6562 / writer.block_html_comment)
6563 + Cs(parsers.check_trail * parsers.html_special_block)
6564 / writer.block_html_cdata_element
6565 + Cs(parsers.check_trail * parsers.html_block)
6566 / writer.block_html_pcdata_element
6567 + Cs(parsers.check_trail * parsers.html_any_block)
6568 / writer.block_html_standalone_tag
6569 + Cs(parsers.check_trail * parsers.html_instruction)
6570 / writer.block_html_processing_instruction
6571 + Cs(parsers.check_trail * parsers.html_cdatasection)
6572 / writer.block_html_cdata_section
6573 + Cs(parsers.check_trail * parsers.html_declaration)
6574 / writer.block_html_declaration
6575 else
6576 parsers.DisplayHtml = Cs( parsers.check_trail
6577 * ( parsers.html_comment
6578 + parsers.html_special_block
6579 + parsers.html_block
6580 + parsers.html_any_block
6581 + parsers.html_instruction
6582 + parsers.html_cdatasection
6583 + parsers.html_declaration))
6584 / writer.block_html_element
6585 end
6586
6587 parsers.indented_non_blank_line = parsers.indentedline
6588 - parsers.blankline
6589
6590 parsers.Verbatim
6591 = Cs( parsers.check_code_trail
6592 * (parsers.line - parsers.blankline)
6593 * (( parsers.check_minimal_blank_indent_and_full_code_trail
6594 * parsers.blankline)^0
6595 * ( (parsers.check_minimal_indent / "")
6596 * parsers.check_code_trail
6597 * (parsers.line - parsers.blankline))^1)^0)
6598 / self.expandtabs / writer.verbatim
6599
6600 parsers.Blockquote = parsers.blockquote_body
6601 / writer.blockquote
6602
6603 parsers.ThematicBreak = parsers.thematic_break_lines
6604 / writer.thematic_break
6605
6606 parsers.Reference = parsers.define_reference_parser
6607 / self.register_link
6608
6609 parsers.Paragraph = parsers.freeze_trail
6610 * (Ct((parsers.Inline)^1)
6611 * (parsers.newline + parsers.eof)
6612 * parsers.unfreeze_trail
6613 / writer.paragraph)
6614
6615 parsers.Plain = parsers.nonindentspace * Ct(parsers.Inline^1)
6616 / writer.plain
6617
6618 if options.taskLists then
6619 parsers.tickbox = ( parsers.ticked_box
6620 + parsers.halfticked_box
6621 + parsers.unticked_box
6622 ) / writer.tickbox
6623 else
6624 parsers.tickbox = parsers.fail
6625 end
6626
6627 parsers.list_blank = parsers.conditionally_indented_blankline
6628
6629 parsers.ref_or_block_list_separated
6630 = parsers.sep_group_no_output(parsers.list_blank)
6631 * parsers.minimally_indented_ref
6632 + parsers.block_sep_group(parsers.list_blank)
6633 * parsers.minimally_indented_block
6634
6635 parsers.ref_or_block_non_separated
6636 = parsers.minimally_indented_ref
6637 + (parsers.succeed / writer.interblocksep)
6638 * parsers.minimally_indented_block
6639 - parsers.minimally_indented_blankline
6640
6641 parsers.tight_list_loop_body_pair =
6642 parsers.create_loop_body_pair(
6643 parsers.ref_or_block_non_separated,
6644 parsers.minimally_indented_par_or_plain_no_blank,
6645 (parsers.succeed / writer.interblocksep),
6646 (parsers.succeed / writer.paragraphsep))
6647
6648 parsers.loose_list_loop_body_pair =
6649 parsers.create_loop_body_pair(
6650 parsers.ref_or_block_list_separated,
6651 parsers.minimally_indented_par_or_plain,
6652 parsers.block_sep_group(parsers.list_blank),
6653 parsers.par_sep_group(parsers.list_blank))
6654
6655 parsers.tight_list_content_loop
6656 = V("Block")
6657 * parsers.tight_list_loop_body_pair.block^0
6658 + (V("Paragraph") + V("Plain"))
6659 * parsers.ref_or_block_non_separated
6660 * parsers.tight_list_loop_body_pair.block^0
6661 + (V("Paragraph") + V("Plain"))
6662 * parsers.tight_list_loop_body_pair.par^0
6663
6664 parsers.loose_list_content_loop
6665 = V("Block")
6666 * parsers.loose_list_loop_body_pair.block^0
6667 + (V("Paragraph") + V("Plain"))
6668 * parsers.ref_or_block_list_separated
6669 * parsers.loose_list_loop_body_pair.block^0
6670 + (V("Paragraph") + V("Plain"))
6671 * parsers.loose_list_loop_body_pair.par^0
6672
6673 parsers.list_item_tightness_condition
6674 = -#( parsers.list_blank^0
6675 * parsers.minimally_indented_ref_or_block_or_par)
6676 * remove_indent("li")
6677 + remove_indent("li")
6678 * parsers.fail
6679
6680 parsers.indented_content_tight
6681 = Ct( (parsers.blankline / "")
6682 * #parsers.list_blank
6683 * remove_indent("li")
6684 + ( (V("Reference") + (parsers.blankline / ""))
6685 * parsers.check_minimal_indent
6686 * parsers.tight_list_content_loop
6687 + (V("Reference") + (parsers.blankline / ""))
6688 + (parsers.tickbox^-1 / writer.escape)
6689 * parsers.tight_list_content_loop
6690 )
6691 * parsers.list_item_tightness_condition)
6692
6693 parsers.indented_content_loose
6694 = Ct( (parsers.blankline / "")
6695 * #parsers.list_blank
6696 + ( (V("Reference") + (parsers.blankline / ""))
6697 * parsers.check_minimal_indent
6698 * parsers.loose_list_content_loop
6699 + (V("Reference") + (parsers.blankline / ""))
6700 + (parsers.tickbox^-1 / writer.escape)
6701 * parsers.loose_list_content_loop))
6702
6703 parsers.TightListItem = function(starter)
6704 return -parsers.ThematicBreak
6705 * parsers.add_indent(starter, "li")
6706 * parsers.indented_content_tight
6707 end
6708
6709 parsers.LooseListItem = function(starter)
6710 return -parsers.ThematicBreak
6711 * parsers.add_indent(starter, "li")
6712 * parsers.indented_content_loose
6713 * remove_indent("li")
6714 end
6715
6716 parsers.BulletListOfType = function(bullet_type)
6717 local bullet = parsers.bullet(bullet_type)
6718 return ( Ct( parsers.TightListItem(bullet)
6719 * ( (parsers.check_minimal_indent / "")
6720 * parsers.TightListItem(bullet)
6721 )^0
6722 )
6723 * Cc(true)
6724 * -#( (parsers.list_blank^0 / "")
6725 * parsers.check_minimal_indent
6726 * (bullet - parsers.ThematicBreak)
6727 )
6728 + Ct( parsers.LooseListItem(bullet)
6729 * ( (parsers.list_blank^0 / "")
6730 * (parsers.check_minimal_indent / "")
6731 * parsers.LooseListItem(bullet)
6732 )^0
6733 )
6734 * Cc(false)
6735 ) / writer.bulletlist
6736 end
6737
6738 parsers.BulletList = parsers.BulletListOfType(parsers.dash)
6739 + parsers.BulletListOfType(parsers.asterisk)
6740 + parsers.BulletListOfType(parsers.plus)
6741
6742 local function ordered_list(items,tight,starter)
6743 local startnum = starter[2][1]
6744 if options.startNumber then
6745 startnum = tonumber(startnum) or 1 -- fallback for '#'
6746 if startnum ~= nil then
6747 startnum = math.floor(startnum)
6748 end
6749 else
6750 startnum = nil
6751 end
6752 return writer.orderedlist(items,tight,startnum)
6753 end
6754
6755 parsers.OrderedListOfType = function(delimiter_type)
6756 local enumerator = parsers.enumerator(delimiter_type)
6757 return Cg(enumerator, "listtype")
6758 * (Ct( parsers.TightListItem(Cb("listtype"))
6759 * ( (parsers.check_minimal_indent / "")
6760 * parsers.TightListItem(enumerator))^0)
6761 * Cc(true)
6762 * -#((parsers.list_blank^0 / "")
6763 * parsers.check_minimal_indent * enumerator)
6764 + Ct( parsers.LooseListItem(Cb("listtype"))
6765 * ((parsers.list_blank^0 / "")
6766 * (parsers.check_minimal_indent / "")
6767 * parsers.LooseListItem(enumerator))^0)
6768 * Cc(false)
6769 ) * Ct(Cb("listtype")) / ordered_list
6770 end
6771
6772 parsers.OrderedList = parsers.OrderedListOfType(parsers.period)
6773 + parsers.OrderedListOfType(parsers.rparent)
6774 parsers.Blank = parsers.blankline / ""
6775 + V("Reference")
6776 function parsers.parse_heading_text(s)
6777 local inlines = self.parser_functions.parse_inlines(s)
6778 local flatten_inlines = self.writer.flatten_inlines
6779 self.writer.flatten_inlines = true
6780 local flat_text = self.parser_functions.parse_inlines(s)
6781 flat_text = util.rope_to_string(flat_text)
6782 self.writer.flatten_inlines = flatten_inlines
6783 return {flat_text, inlines}
6784 end
6785
6786 -- parse atx header
6787 parsers.AtxHeading = parsers.check_trail_no_rem
6788 * Cg(parsers.heading_start, "level")
6789 * ((C( parsers.optionalspace
6790 * parsers.hash^0
6791 * parsers.optionalspace
6792 * parsers.newline)
6793 + parsers.spacechar^1
6794 * C(parsers.line))
6795 / strip_atx_end
6796 / parsers.parse_heading_text)
6797 * Cb("level")
6798 / writer.heading
6799
6800 parsers.heading_line = parsers.linechar^1
6801 - parsers.thematic_break_lines
6802
6803 parsers.heading_text = parsers.heading_line
6804 * ( (V("Endline") / "\n")
6805 * ( parsers.heading_line
6806 - parsers.heading_level))^0
6807 * parsers.newline^-1
6808
6809 parsers.SetextHeading = parsers.freeze_trail
6810 * parsers.check_trail_no_rem
6811 * #( parsers.heading_text
6812 * parsers.check_minimal_indent
6813 * parsers.check_trail
6814 * parsers.heading_level)
6815 * Cs(parsers.heading_text)
6816 / parsers.parse_heading_text
6817 * parsers.check_minimal_indent_and_trail
6818 * parsers.heading_level
6819 * parsers.newline
6820 * parsers.unfreeze_trail
6821 / writer.heading
6822
6823 parsers.Heading = parsers.AtxHeading + parsers.SetextHeading
6824 function self.finalize_grammar(extensions)
6825 local walkable_syntax = (function(global_walkable_syntax)
6826 local local_walkable_syntax = {}
6827 for lhs, rule in pairs(global_walkable_syntax) do
6828 local_walkable_syntax[lhs] = util.table_copy(rule)
6829 end
6830 return local_walkable_syntax
6831 end)(walkable_syntax)
6832 local current_extension_name = nil
6833 self.insert_pattern = function(selector, pattern, pattern_name)
6834 assert(pattern_name == nil or type(pattern_name) == "string")
6835 local _, _, lhs, pos, rhs
6836 = selector:find("^(%a+)%s+([%a%s]+%a+)%s+(%a+)$")
6837 assert(lhs ~= nil,
6838 [[Expected selector in form ]]
6839 .. [["LHS (before|after|instead of) RHS", not "]]
6840 .. selector .. [["]])
6841 assert(walkable_syntax[lhs] ~= nil,
6842 [[Rule ]] .. lhs
6843 .. [[ -> ... does not exist in markdown grammar]])
6844 assert(pos == "before" or pos == "after" or pos == "instead of",
6845 [[Expected positional specifier "before", "after", ]]
6846 .. [[or "instead of", not "]]
6847 .. pos .. [["]])
6848 local rule = walkable_syntax[lhs]
6849 local index = nil
6850 for current_index, current_rhs in ipairs(rule) do
6851 if type(current_rhs) == "string" and current_rhs == rhs then
6852 index = current_index
6853 if pos == "after" then
6854 index = index + 1
6855 end
6856 break
6857 end
6858 end
6859 assert(index ~= nil,
6860 [[Rule ]] .. lhs .. [[ -> ]] .. rhs
6861 .. [[ does not exist in markdown grammar]])
6862 local accountable_pattern
6863 if current_extension_name then
6864 accountable_pattern
6865 = {pattern, current_extension_name, pattern_name}
6866 else
6867 assert(type(pattern) == "string",
6868 [[reader->insert_pattern() was called outside ]]
6869 .. [[an extension with ]]
6870 .. [[a PEG pattern instead of a rule name]])
6871 accountable_pattern = pattern
6872 end
6873 if pos == "instead of" then
6874 rule[index] = accountable_pattern
6875 else
6876 table.insert(rule, index, accountable_pattern)
6877 end
6878 -- TODO: Remove all occurrences of `pattern` after `index`
6879 -- to improve speed?
6880 end
6881 if options.htmlOverLinks then
6882 self.insert_pattern("Inline before AutoLinkUrl", "InlineHtml")
6883 end
6884 local syntax =
6885 { "Blocks",
6886
6887 Blocks = V("InitializeState")
6888 * V("ExpectedJekyllData")
6889 * V("Blank")^0
6890 * ( V("Block")
6891 * ( V("Blank")^0 * parsers.eof
6892 + ( V("Blank")^2 / writer.paragraphsep
6893 + V("Blank")^0 / writer.interblocksep
6894 )
6895 )
6896 + ( V("Paragraph") + V("Plain") )
6897 * ( V("Blank")^0 * parsers.eof
6898 + ( V("Blank")^2 / writer.paragraphsep
6899 + V("Blank")^0 / writer.interblocksep
6900 )
6901 )
6902 * V("Block")
6903 * ( V("Blank")^0 * parsers.eof
6904 + ( V("Blank")^2 / writer.paragraphsep
6905 + V("Blank")^0 / writer.interblocksep
6906 )
6907 )
6908 + ( V("Paragraph") + V("Plain") )
6909 * ( V("Blank")^0 * parsers.eof
6910 + V("Blank")^0 / writer.paragraphsep
6911 )
6912 )^0,
6913
6914 ExpectedJekyllData = parsers.succeed,
6915
6916 Blank = parsers.Blank,
6917 Reference = parsers.Reference,
6918
6919 Blockquote = parsers.Blockquote,
6920 Verbatim = parsers.Verbatim,
6921 ThematicBreak = parsers.ThematicBreak,
6922 BulletList = parsers.BulletList,
6923 OrderedList = parsers.OrderedList,
6924 DisplayHtml = parsers.DisplayHtml,
6925 Heading = parsers.Heading,
6926 Paragraph = parsers.Paragraph,
6927 Plain = parsers.Plain,
6928
6929 ListStarter = parsers.ListStarter,
6930 EndlineExceptions = parsers.EndlineExceptions,
6931 NoSoftLineBreakEndlineExceptions
6932 = parsers.NoSoftLineBreakEndlineExceptions,
6933
6934 Str = parsers.Str,
6935 Space = parsers.Space,
6936 OptionalIndent = parsers.OptionalIndent,
6937 Endline = parsers.Endline,
6938 EndlineNoSub = parsers.EndlineNoSub,
6939 NoSoftLineBreakEndline
6940 = parsers.NoSoftLineBreakEndline,
6941 EndlineBreak = parsers.EndlineBreak,
6942 LinkAndEmph = parsers.LinkAndEmph,
6943 Code = parsers.Code,
6944 AutoLinkUrl = parsers.AutoLinkUrl,
6945 AutoLinkEmail = parsers.AutoLinkEmail,
6946 AutoLinkRelativeReference
6947 = parsers.AutoLinkRelativeReference,
6948 InlineHtml = parsers.InlineHtml,
6949 HtmlEntity = parsers.HtmlEntity,
6950 EscapedChar = parsers.EscapedChar,
6951 Smart = parsers.Smart,
6952 Symbol = parsers.Symbol,
6953 SpecialChar = parsers.fail,
6954 InitializeState = parsers.succeed,
6955 }
6956 self.update_rule = function(rule_name, get_pattern)
6957 assert(current_extension_name ~= nil)
6958 assert(syntax[rule_name] ~= nil,
6959 [[Rule ]] .. rule_name
6960 .. [[ -> ... does not exist in markdown grammar]])
6961 local previous_pattern
6962 local extension_name
6963 if walkable_syntax[rule_name] then
6964 local previous_accountable_pattern
6965 = walkable_syntax[rule_name][1]
6966 previous_pattern = previous_accountable_pattern[1]
6967 extension_name
6968 = previous_accountable_pattern[2]
6969 .. ", " .. current_extension_name
6970 else
6971 previous_pattern = nil
6972 extension_name = current_extension_name
6973 end
6974 local pattern
6975 if type(get_pattern) == "function" then
6976 pattern = get_pattern(previous_pattern)
6977 else
6978 assert(previous_pattern == nil,
6979 [[Rule ]] .. rule_name ..
6980 [[ has already been updated by ]] .. extension_name)
6981 pattern = get_pattern
6982 end
6983 local accountable_pattern = { pattern, extension_name, rule_name }
6984 walkable_syntax[rule_name] = { accountable_pattern }
6985 end
6986 local special_characters = {}
6987 self.add_special_character = function(c)
6988 table.insert(special_characters, c)
6989 syntax.SpecialChar = S(table.concat(special_characters, ""))
6990 end
6991
6992 self.add_special_character("*")
6993 self.add_special_character("[")
6994 self.add_special_character("]")
6995 self.add_special_character("<")
6996 self.add_special_character("!")
6997 self.add_special_character("\\")
6998 self.initialize_named_group = function(name, value)
6999 local pattern = Ct("")
7000 if value ~= nil then
7001 pattern = pattern / value
7002 end
7003 syntax.InitializeState = syntax.InitializeState
7004 * Cg(pattern, name)
7005 end
7006 self.initialize_named_group("indent_info")
7007 for _, extension in ipairs(extensions) do
7008 current_extension_name = extension.name
7009 extension.extend_writer(writer)
7010 extension.extend_reader(self)
7011 end
7012 current_extension_name = nil
7013 if options.debugExtensions then
7014 local sorted_lhs = {}
7015 for lhs, _ in pairs(walkable_syntax) do
7016 table.insert(sorted_lhs, lhs)
7017 end
7018 table.sort(sorted_lhs)
7019
7020 local output_lines = {"{"}
7021 for lhs_index, lhs in ipairs(sorted_lhs) do
7022 local encoded_lhs = util.encode_json_string(lhs)
7023 table.insert(output_lines, [[ ]] ..encoded_lhs .. [[: []])
7024 local rule = walkable_syntax[lhs]
7025 for rhs_index, rhs in ipairs(rule) do
7026 local human_readable_rhs
7027 if type(rhs) == "string" then
7028 human_readable_rhs = rhs
7029 else
7030 local pattern_name
7031 if rhs[3] then
7032 pattern_name = rhs[3]
7033 else
7034 pattern_name = "Anonymous Pattern"
7035 end
7036 local extension_name = rhs[2]
7037 human_readable_rhs = pattern_name .. [[ (]]
7038 .. extension_name .. [[)]]
7039 end
7040 local encoded_rhs
7041 = util.encode_json_string(human_readable_rhs)
7042 local output_line = [[ ]] .. encoded_rhs
7043 if rhs_index < #rule then
7044 output_line = output_line .. ","
7045 end
7046 table.insert(output_lines, output_line)
7047 end
7048 local output_line = " ]"
7049 if lhs_index < #sorted_lhs then
7050 output_line = output_line .. ","
7051 end
7052 table.insert(output_lines, output_line)
7053 end
7054 table.insert(output_lines, "}")
7055
7056 local output = table.concat(output_lines, "\n")
7057 local output_filename = options.debugExtensionsFileName
7058 local output_file = assert(io.open(output_filename, "w"),
7059 [[Could not open file "]] .. output_filename
7060 .. [[" for writing]])
7061 assert(output_file:write(output))
7062 assert(output_file:close())
7063 end
7064 for lhs, rule in pairs(walkable_syntax) do
7065 syntax[lhs] = parsers.fail
7066 for _, rhs in ipairs(rule) do
7067 local pattern
7068 if type(rhs) == "string" then
7069 pattern = V(rhs)
7070 else
7071 pattern = rhs[1]
7072 if type(pattern) == "string" then
7073 pattern = V(pattern)
7074 end
7075 end
7076 syntax[lhs] = syntax[lhs] + pattern
7077 end
7078 end
7079 if options.underscores then
7080 self.add_special_character("_")
7081 end
7082
7083 if not options.codeSpans then
7084 syntax.Code = parsers.fail
7085 else
7086 self.add_special_character("`")
7087 end
7088
7089 if not options.html then
7090 syntax.DisplayHtml = parsers.fail
7091 syntax.InlineHtml = parsers.fail
7092 syntax.HtmlEntity = parsers.fail
7093 else
7094 self.add_special_character("&")
7095 end
7096
7097 if options.preserveTabs then
7098 options.stripIndent = false
7099 end
7100
7101 if not options.smartEllipses then
7102 syntax.Smart = parsers.fail
7103 else
7104 self.add_special_character(".")
7105 end
7106
7107 if not options.relativeReferences then
7108 syntax.AutoLinkRelativeReference = parsers.fail
7109 end
7110
7111 if options.contentLevel == "inline" then
7112 syntax[1] = "Inlines"
7113 syntax.Inlines = V("InitializeState")
7114 * parsers.Inline^0
7115 * ( parsers.spacing^0
7116 * parsers.eof / "")
7117 syntax.Space = parsers.Space + parsers.blankline / writer.space
7118 end
7119
7120 local blocks_nested_t = util.table_copy(syntax)
7121 blocks_nested_t.ExpectedJekyllData = parsers.succeed
7122 parsers.blocks_nested = Ct(blocks_nested_t)
7123
7124 parsers.blocks = Ct(syntax)
7125
7126 local inlines_t = util.table_copy(syntax)
7127 inlines_t[1] = "Inlines"
7128 inlines_t.Inlines = V("InitializeState")
7129 * parsers.Inline^0
7130 * ( parsers.spacing^0
7131 * parsers.eof / "")
7132 parsers.inlines = Ct(inlines_t)
7133
7134 local inlines_no_inline_note_t = util.table_copy(inlines_t)
7135 inlines_no_inline_note_t.InlineNote = parsers.fail
7136 parsers.inlines_no_inline_note = Ct(inlines_no_inline_note_t)
7137
7138 local inlines_no_html_t = util.table_copy(inlines_t)
7139 inlines_no_html_t.DisplayHtml = parsers.fail
7140 inlines_no_html_t.InlineHtml = parsers.fail
7141 inlines_no_html_t.HtmlEntity = parsers.fail
7142 parsers.inlines_no_html = Ct(inlines_no_html_t)
7143
7144 local inlines_nbsp_t = util.table_copy(inlines_t)
7145 inlines_nbsp_t.Endline = parsers.NonbreakingEndline
7146 inlines_nbsp_t.Space = parsers.NonbreakingSpace
7147 parsers.inlines_nbsp = Ct(inlines_nbsp_t)
7148
7149 local inlines_identity_t = util.table_copy(inlines_t)
7150 inlines_identity_t.Str = parsers.any / function(s) return s end
7151 parsers.inlines_identity = Ct(inlines_identity_t)
7152
7153 local inlines_string_t = util.table_copy(inlines_t)
7154 inlines_string_t.Str = parsers.any / writer.string
7155 parsers.inlines_string = Ct(inlines_string_t)
7156
7157 local inlines_math_t = util.table_copy(inlines_t)
7158 inlines_math_t.Str = parsers.any / writer.math
7159 parsers.inlines_math = Ct(inlines_math_t)
7160
7161 local inlines_no_link_or_emphasis_t = util.table_copy(inlines_t)
7162 inlines_no_link_or_emphasis_t.LinkAndEmph = parsers.fail
7163 inlines_no_link_or_emphasis_t.EndlineExceptions
7164 = parsers.EndlineExceptions - parsers.eof
7165 parsers.inlines_no_link_or_emphasis
7166 = Ct(inlines_no_link_or_emphasis_t)
7167 return function(input)
7168 if options.unicodeNormalization then
7169 local form = options.unicodeNormalizationForm
7170 input = util.normalize(input, form)
7171 end
7172 input = input:gsub("\r\n?", "\n")
7173 if input:sub(-1) ~= "\n" then
7174 input = input .. "\n"
7175 end
7176 references = {}
7177 local document = self.parser_functions.parse_blocks(input)
7178 local output = util.rope_to_string(writer.document(document))
7179 local undosep_start, undosep_end
7180 local potential_secend_start, secend_start
7181 local potential_sep_start, sep_start
7182 while true do
7183 -- find a `writer->undosep`
7184 undosep_start, undosep_end
7185 = output:find(writer.undosep_text, 1, true)
7186 if undosep_start == nil then break end
7187 -- skip any preceding section ends
7188 secend_start = undosep_start
7189 while true do
7190 potential_secend_start = secend_start - #writer.secend_text
7191 if potential_secend_start < 1
7192 or output:sub(potential_secend_start,
7193 secend_start - 1) ~= writer.secend_text
7194 then
7195 break
7196 end
7197 secend_start = potential_secend_start
7198 end
7199 -- find an immediately preceding
7200 -- block element / paragraph separator
7201 sep_start = secend_start
7202 potential_sep_start = sep_start - #writer.interblocksep_text
7203 if potential_sep_start >= 1
7204 and output:sub(potential_sep_start,
7205 sep_start - 1) == writer.interblocksep_text
7206 then
7207 sep_start = potential_sep_start
7208 else
7209 potential_sep_start = sep_start - #writer.paragraphsep_text
7210 if potential_sep_start >= 1
7211 and output:sub(potential_sep_start,
7212 sep_start - 1) == writer.paragraphsep_text
7213 then
7214 sep_start = potential_sep_start
7215 end
7216 end
7217 -- remove `writer->undosep` and immediately preceding
7218 -- block element / paragraph separator
7219 output = output:sub(1, sep_start - 1)
7220 .. output:sub(secend_start, undosep_start - 1)
7221 .. output:sub(undosep_end + 1)
7222 end
7223 return output
7224 end
7225 end
7226 return self
7227 end
7228 M.extensions = {}
7229 M.extensions.acronyms = function(acronyms)
7230 assert(#acronyms > 0)
7231 return {
7232 name = "built-in acronyms syntax extension",
7233 extend_writer = function(self)
7234 function self.acronym(s)
7235 if self.flatten_inlines then return s end
7236 return {"\\markdownRendererAcronym{",s,"}"}
7237 end
7238 end, extend_reader = function(self)
7239 local parsers = self.parsers
7240 local options = self.options
7241 local writer = self.writer
7242 local prefix_tree = {_type = "intermediate"}
7243 for _, acronym in ipairs(acronyms) do
7244 local node = prefix_tree
7245 if options.unicodeNormalization then
7246 local form = options.unicodeNormalizationForm
7247 acronym = util.normalize(acronym, form)
7248 end
7249 for i = 1, #acronym do
7250 local acronym_byte = acronym:sub(i, i)
7251 if i < #acronym then
7252 if node[acronym_byte] == nil then
7253 node[acronym_byte] = {_type = "intermediate"}
7254 end
7255 node = node[acronym_byte]
7256 else
7257 table.insert(node, {acronym_byte, _type = "leaf"})
7258 end
7259 end
7260 end
7261 local subparsers = {}
7262 depth_first_search(prefix_tree, "", function(node, path)
7263 if node._type == "leaf" then
7264 local acronym_byte = table.unpack(node)
7265 if subparsers[path] == nil then
7266 subparsers[path] = parsers.fail
7267 end
7268 subparsers[path] = subparsers[path] + P(acronym_byte)
7269 end
7270 end, function(_, path)
7271 if #path > 0 then
7272 local byte = path:sub(#path, #path)
7273 local parent_path = path:sub(1, #path-1)
7274 local prefix = P(byte)
7275 local suffix = prefix * subparsers[path]
7276 if subparsers[parent_path] == nil then
7277 subparsers[parent_path] = parsers.fail
7278 end
7279 subparsers[parent_path] = subparsers[parent_path] + suffix
7280 end
7281 end)
7282 assert(subparsers[""] ~= nil)
7283 local Acronym = ( parsers.unicode.punctuation
7284 - V("SpecialChar")
7285 + parsers.unicode.whitespace
7286 - V("EndlineExceptions"))^0
7287 / writer.string
7288 * (
7289 subparsers[""]
7290 * #(-parsers.normalchar
7291 + parsers.unicode.punctuation
7292 + parsers.unicode.whitespace
7293 + parsers.eof)
7294 / writer.acronym)
7295 self.insert_pattern("Inline before Str", Acronym, "Acronym")
7296 end
7297 }
7298 end
7299 M.extensions.bracketed_spans = function()
7300 return {
7301 name = "built-in bracketed_spans syntax extension",
7302 extend_writer = function(self)
7303 function self.span(s, attr)
7304 if self.flatten_inlines then return s end
7305 return {"\\markdownRendererBracketedSpanAttributeContextBegin",
7306 self.attributes(attr),
7307 "\\markdownRendererBracketedSpan{",s,"}",
7308 "\\markdownRendererBracketedSpanAttributeContextEnd{}"}
7309 end
7310 end, extend_reader = function(self)
7311 local parsers = self.parsers
7312 local writer = self.writer
7313
7314 local span_label = parsers.lbracket
7315 * (Cs((parsers.alphanumeric^1
7316 + parsers.inticks
7317 + parsers.autolink
7318 + V("InlineHtml")
7319 + ( parsers.backslash * parsers.backslash)
7320 + ( parsers.backslash
7321 * (parsers.lbracket + parsers.rbracket)
7322 + V("Space") + V("Endline")
7323 + (parsers.any
7324 - ( parsers.newline
7325 + parsers.lbracket
7326 + parsers.rbracket
7327 + parsers.blankline^2))))^1)
7328 / self.parser_functions.parse_inlines)
7329 * parsers.rbracket
7330
7331 local Span = span_label
7332 * Ct(parsers.attributes)
7333 / writer.span
7334
7335 self.insert_pattern("Inline before LinkAndEmph",
7336 Span, "Span")
7337 end
7338 }
7339 end
7340 M.extensions.citations = function(citation_nbsps)
7341 return {
7342 name = "built-in citations syntax extension",
7343 extend_writer = function(self)
7344 function self.citations(text_cites, cites)
7345 local buffer = {}
7346 if self.flatten_inlines then
7347 for _,cite in ipairs(cites) do
7348 if cite.prenote then
7349 table.insert(buffer, {cite.prenote, " "})
7350 end
7351 table.insert(buffer, cite.name)
7352 if cite.postnote then
7353 table.insert(buffer, {" ", cite.postnote})
7354 end
7355 end
7356 else
7357 table.insert(buffer,
7358 {"\\markdownRenderer",
7359 text_cites and "TextCite" or "Cite",
7360 "{", #cites, "}"})
7361 for _,cite in ipairs(cites) do
7362 table.insert(buffer,
7363 {cite.suppress_author and "-" or "+", "{",
7364 cite.prenote or "", "}{",
7365 cite.postnote or "", "}{", cite.name, "}"})
7366 end
7367 end
7368 return buffer
7369 end
7370 end, extend_reader = function(self)
7371 local parsers = self.parsers
7372 local writer = self.writer
7373
7374 local citation_chars
7375 = parsers.alphanumeric
7376 + S("#$%&-+<>~/_")
7377
7378 local citation_name
7379 = Cs(parsers.dash^-1) * parsers.at
7380 * Cs(citation_chars
7381 * ((( citation_chars
7382 + parsers.internal_punctuation
7383 - parsers.comma - parsers.semicolon)
7384 * -#(( parsers.internal_punctuation
7385 - parsers.comma
7386 - parsers.semicolon)^0
7387 * -( citation_chars
7388 + parsers.internal_punctuation
7389 - parsers.comma
7390 - parsers.semicolon)))^0
7391 * citation_chars)^-1)
7392
7393 local citation_body_prenote
7394 = Cs((parsers.alphanumeric^1
7395 + parsers.bracketed
7396 + parsers.inticks
7397 + parsers.autolink
7398 + V("InlineHtml")
7399 + V("Space") + V("EndlineNoSub")
7400 + (parsers.anyescaped
7401 - ( parsers.newline
7402 + parsers.rbracket
7403 + parsers.blankline^2))
7404 - ( parsers.spnl
7405 * parsers.dash^-1
7406 * parsers.at))^1)
7407
7408 local citation_body_postnote
7409 = Cs((parsers.alphanumeric^1
7410 + parsers.bracketed
7411 + parsers.inticks
7412 + parsers.autolink
7413 + V("InlineHtml")
7414 + V("Space") + V("EndlineNoSub")
7415 + (parsers.anyescaped
7416 - ( parsers.newline
7417 + parsers.rbracket
7418 + parsers.semicolon
7419 + parsers.blankline^2))
7420 - (parsers.spnl * parsers.rbracket))^1)
7421
7422 local citation_body_chunk
7423 = ( citation_body_prenote
7424 * parsers.spnlc_sep
7425 + Cc("")
7426 * parsers.spnlc
7427 )
7428 * citation_name
7429 * ( parsers.internal_punctuation
7430 - parsers.semicolon)^-1
7431 * ( parsers.spnlc / function(_) return end
7432 * citation_body_postnote
7433 + Cc("")
7434 * parsers.spnlc
7435 )
7436
7437 local citation_body
7438 = citation_body_chunk
7439 * ( parsers.semicolon
7440 * parsers.spnlc
7441 * citation_body_chunk
7442 )^0
7443
7444 local citation_headless_body_postnote
7445 = Cs((parsers.alphanumeric^1
7446 + parsers.bracketed
7447 + parsers.inticks
7448 + parsers.autolink
7449 + V("InlineHtml")
7450 + V("Space") + V("Endline")
7451 + (parsers.anyescaped
7452 - ( parsers.newline
7453 + parsers.rbracket
7454 + parsers.at
7455 + parsers.semicolon + parsers.blankline^2))
7456 - (parsers.spnl * parsers.rbracket))^0)
7457
7458 local citation_headless_body
7459 = citation_headless_body_postnote
7460 * ( parsers.semicolon
7461 * parsers.spnlc
7462 * citation_body_chunk
7463 )^0
7464
7465 local citations
7466 = function(text_cites, raw_cites)
7467 local function normalize(str)
7468 if str == "" then
7469 str = nil
7470 else
7471 str = (citation_nbsps and
7472 self.parser_functions.parse_inlines_nbsp or
7473 self.parser_functions.parse_inlines)(str)
7474 end
7475 return str
7476 end
7477
7478 local cites = {}
7479 for i = 1,#raw_cites,4 do
7480 cites[#cites+1] = {
7481 prenote = normalize(raw_cites[i]),
7482 suppress_author = raw_cites[i+1] == "-",
7483 name = writer.identifier(raw_cites[i+2]),
7484 postnote = normalize(raw_cites[i+3]),
7485 }
7486 end
7487 return writer.citations(text_cites, cites)
7488 end
7489
7490 local TextCitations
7491 = Ct((parsers.spnlc
7492 * Cc("")
7493 * citation_name
7494 * ((parsers.spnlc
7495 * parsers.lbracket
7496 * citation_headless_body
7497 * parsers.rbracket) + Cc("")))^1)
7498 / function(raw_cites)
7499 return citations(true, raw_cites)
7500 end
7501
7502 local ParenthesizedCitations
7503 = Ct((parsers.spnlc
7504 * parsers.lbracket
7505 * citation_body
7506 * parsers.rbracket)^1)
7507 / function(raw_cites)
7508 return citations(false, raw_cites)
7509 end
7510
7511 local Citations = TextCitations + ParenthesizedCitations
7512
7513 self.insert_pattern("Inline before LinkAndEmph",
7514 Citations, "Citations")
7515
7516 self.add_special_character("@")
7517 self.add_special_character("-")
7518 end
7519 }
7520 end
7521 M.extensions.content_blocks = function(language_map)
7522 local languages_json = (function()
7523 local base, prev, curr
7524 for _, pathname in ipairs(util.find_files(language_map)) do
7525 local file = io.open(pathname, "r")
7526 if not file then goto continue end
7527 local input = assert(file:read("*a"))
7528 assert(file:close())
7529 local json = input:gsub('("[^\n]-"):','[%1]=')
7530 curr = load("_ENV = {}; return "..json)()
7531 if type(curr) == "table" then
7532 if base == nil then
7533 base = curr
7534 else
7535 setmetatable(prev, { __index = curr })
7536 end
7537 prev = curr
7538 end
7539 ::continue::
7540 end
7541 return base or {}
7542 end)()
7543
7544 return {
7545 name = "built-in content_blocks syntax extension",
7546 extend_writer = function(self)
7547 function self.contentblock(src,suf,type,tit)
7548 if not self.is_writing then return "" end
7549 src = src.."."..suf
7550 suf = suf:lower()
7551 if type == "onlineimage" then
7552 return {"\\markdownRendererContentBlockOnlineImage{",suf,"}",
7553 "{",self.string(src),"}",
7554 "{",self.uri(src),"}",
7555 "{",self.string(tit or ""),"}"}
7556 elseif languages_json[suf] then
7557 return {"\\markdownRendererContentBlockCode{",suf,"}",
7558 "{",self.string(languages_json[suf]),"}",
7559 "{",self.string(src),"}",
7560 "{",self.uri(src),"}",
7561 "{",self.string(tit or ""),"}"}
7562 else
7563 return {"\\markdownRendererContentBlock{",suf,"}",
7564 "{",self.string(src),"}",
7565 "{",self.uri(src),"}",
7566 "{",self.string(tit or ""),"}"}
7567 end
7568 end
7569 end, extend_reader = function(self)
7570 local parsers = self.parsers
7571 local writer = self.writer
7572
7573 local contentblock_tail
7574 = parsers.optionaltitle
7575 * (parsers.newline + parsers.eof)
7576
7577 -- case insensitive online image suffix:
7578 local onlineimagesuffix
7579 = (function(...)
7580 local parser = nil
7581 for _, suffix in ipairs({...}) do
7582 local pattern=nil
7583 for i=1,#suffix do
7584 local char=suffix:sub(i,i)
7585 char = S(char:lower()..char:upper())
7586 if pattern == nil then
7587 pattern = char
7588 else
7589 pattern = pattern * char
7590 end
7591 end
7592 if parser == nil then
7593 parser = pattern
7594 else
7595 parser = parser + pattern
7596 end
7597 end
7598 return parser
7599 end)("png", "jpg", "jpeg", "gif", "tif", "tiff")
7600
7601 -- online image url for iA Writer content blocks with
7602 -- mandatory suffix, allowing nested brackets:
7603 local onlineimageurl
7604 = (parsers.less
7605 * Cs((parsers.anyescaped
7606 - parsers.more
7607 - parsers.spacing
7608 - #(parsers.period
7609 * onlineimagesuffix
7610 * parsers.more
7611 * contentblock_tail))^0)
7612 * parsers.period
7613 * Cs(onlineimagesuffix)
7614 * parsers.more
7615 + (Cs((parsers.inparens
7616 + (parsers.anyescaped
7617 - parsers.spacing
7618 - parsers.rparent
7619 - #(parsers.period
7620 * onlineimagesuffix
7621 * contentblock_tail)))^0)
7622 * parsers.period
7623 * Cs(onlineimagesuffix))
7624 ) * Cc("onlineimage")
7625
7626 -- filename for iA Writer content blocks with mandatory suffix:
7627 local localfilepath
7628 = parsers.slash
7629 * Cs((parsers.anyescaped
7630 - parsers.tab
7631 - parsers.newline
7632 - #(parsers.period
7633 * parsers.alphanumeric^1
7634 * contentblock_tail))^1)
7635 * parsers.period
7636 * Cs(parsers.alphanumeric^1)
7637 * Cc("localfile")
7638
7639 local ContentBlock
7640 = parsers.check_trail_no_rem
7641 * (localfilepath + onlineimageurl)
7642 * contentblock_tail
7643 / writer.contentblock
7644
7645 self.insert_pattern("Block before Blockquote",
7646 ContentBlock, "ContentBlock")
7647 end
7648 }
7649 end
7650 M.extensions.definition_lists = function(tight_lists)
7651 return {
7652 name = "built-in definition_lists syntax extension",
7653 extend_writer = function(self)
7654 local function dlitem(term, defs)
7655 local retVal = {"\\markdownRendererDlItem{",term,"}"}
7656 for _, def in ipairs(defs) do
7657 retVal[#retVal+1]
7658 = {"\\markdownRendererDlDefinitionBegin ",def,
7659 "\\markdownRendererDlDefinitionEnd "}
7660 end
7661 retVal[#retVal+1] = "\\markdownRendererDlItemEnd "
7662 return retVal
7663 end
7664
7665 function self.definitionlist(items,tight)
7666 if not self.is_writing then return "" end
7667 local buffer = {}
7668 for _,item in ipairs(items) do
7669 buffer[#buffer + 1] = dlitem(item.term, item.definitions)
7670 end
7671 if tight and tight_lists then
7672 return {"\\markdownRendererDlBeginTight\n", buffer,
7673 "\n\\markdownRendererDlEndTight"}
7674 else
7675 return {"\\markdownRendererDlBegin\n", buffer,
7676 "\n\\markdownRendererDlEnd"}
7677 end
7678 end
7679 end, extend_reader = function(self)
7680 local parsers = self.parsers
7681 local writer = self.writer
7682
7683 local defstartchar = S("~:")
7684
7685 local defstart
7686 = parsers.check_trail_length(0) * defstartchar
7687 * #parsers.spacing
7688 * (parsers.tab + parsers.space^-3)
7689 + parsers.check_trail_length(1)
7690 * defstartchar * #parsers.spacing
7691 * (parsers.tab + parsers.space^-2)
7692 + parsers.check_trail_length(2)
7693 * defstartchar * #parsers.spacing
7694 * (parsers.tab + parsers.space^-1)
7695 + parsers.check_trail_length(3)
7696 * defstartchar * #parsers.spacing
7697
7698 local indented_line
7699 = (parsers.check_minimal_indent / "")
7700 * parsers.check_code_trail * parsers.line
7701
7702 local blank
7703 = parsers.check_minimal_blank_indent_and_any_trail
7704 * parsers.optionalspace * parsers.newline
7705
7706 local dlchunk = Cs(parsers.line * (indented_line - blank)^0)
7707
7708 local indented_blocks = function(bl)
7709 return Cs( bl
7710 * (blank^1 * (parsers.check_minimal_indent / "")
7711 * parsers.check_code_trail * -parsers.blankline * bl)^0
7712 * (blank^1 + parsers.eof))
7713 end
7714
7715 local function definition_list_item(term, defs, _)
7716 return { term = self.parser_functions.parse_inlines(term),
7717 definitions = defs }
7718 end
7719
7720 local DefinitionListItemLoose
7721 = C(parsers.line) * blank^0
7722 * Ct((parsers.check_minimal_indent * (defstart
7723 * indented_blocks(dlchunk)
7724 / self.parser_functions.parse_blocks_nested))^1)
7725 * Cc(false) / definition_list_item
7726
7727 local DefinitionListItemTight
7728 = C(parsers.line)
7729 * Ct((parsers.check_minimal_indent * (defstart * dlchunk
7730 / self.parser_functions.parse_blocks_nested))^1)
7731 * Cc(true) / definition_list_item
7732
7733 local DefinitionList
7734 = ( Ct(DefinitionListItemLoose^1) * Cc(false)
7735 + Ct(DefinitionListItemTight^1)
7736 * (blank^0
7737 * -DefinitionListItemLoose * Cc(true))
7738 ) / writer.definitionlist
7739
7740 self.insert_pattern("Block after Heading",
7741 DefinitionList, "DefinitionList")
7742 end
7743 }
7744 end
7745 M.extensions.fancy_lists = function()
7746 return {
7747 name = "built-in fancy_lists syntax extension",
7748 extend_writer = function(self)
7749 local options = self.options
7750
7751 function self.fancylist(items,tight,startnum,numstyle,numdelim)
7752 if not self.is_writing then return "" end
7753 local buffer = {}
7754 local num = startnum
7755 for _,item in ipairs(items) do
7756 if item ~= "" then
7757 buffer[#buffer + 1] = self.fancyitem(item,num)
7758 end
7759 if num ~= nil and item ~= "" then
7760 num = num + 1
7761 end
7762 end
7763 local contents = util.intersperse(buffer,"\n")
7764 if tight and options.tightLists then
7765 return {"\\markdownRendererFancyOlBeginTight{",
7766 numstyle,"}{",numdelim,"}",contents,
7767 "\n\\markdownRendererFancyOlEndTight "}
7768 else
7769 return {"\\markdownRendererFancyOlBegin{",
7770 numstyle,"}{",numdelim,"}",contents,
7771 "\n\\markdownRendererFancyOlEnd "}
7772 end
7773 end
7774 function self.fancyitem(s,num)
7775 if num ~= nil then
7776 return {"\\markdownRendererFancyOlItemWithNumber{",num,"}",s,
7777 "\\markdownRendererFancyOlItemEnd "}
7778 else
7779 return {"\\markdownRendererFancyOlItem ",s,
7780 "\\markdownRendererFancyOlItemEnd "}
7781 end
7782 end
7783 end, extend_reader = function(self)
7784 local parsers = self.parsers
7785 local options = self.options
7786 local writer = self.writer
7787
7788 local function combine_markers_and_delims(markers, delims)
7789 local markers_table = {}
7790 for _,marker in ipairs(markers) do
7791 local start_marker
7792 local continuation_marker
7793 if type(marker) == "table" then
7794 start_marker = marker[1]
7795 continuation_marker = marker[2]
7796 else
7797 start_marker = marker
7798 continuation_marker = marker
7799 end
7800 for _,delim in ipairs(delims) do
7801 table.insert(markers_table,
7802 {start_marker, continuation_marker, delim})
7803 end
7804 end
7805 return markers_table
7806 end
7807
7808 local function join_table_with_func(func, markers_table)
7809 local pattern = func(table.unpack(markers_table[1]))
7810 for i = 2, #markers_table do
7811 pattern = pattern + func(table.unpack(markers_table[i]))
7812 end
7813 return pattern
7814 end
7815
7816 local lowercase_letter_marker = R("az")
7817 local uppercase_letter_marker = R("AZ")
7818
7819 local roman_marker = function(chars)
7820 local m, d, c = P(chars[1]), P(chars[2]), P(chars[3])
7821 local l, x, v, i
7822 = P(chars[4]), P(chars[5]), P(chars[6]), P(chars[7])
7823 return m^-3
7824 * (c*m + c*d + d^-1 * c^-3)
7825 * (x*c + x*l + l^-1 * x^-3)
7826 * (i*x + i*v + v^-1 * i^-3)
7827 end
7828
7829 local lowercase_roman_marker
7830 = roman_marker({"m", "d", "c", "l", "x", "v", "i"})
7831 local uppercase_roman_marker
7832 = roman_marker({"M", "D", "C", "L", "X", "V", "I"})
7833
7834 local lowercase_opening_roman_marker = P("i")
7835 local uppercase_opening_roman_marker = P("I")
7836
7837 local digit_marker = parsers.dig * parsers.dig^-8
7838
7839 local markers = {
7840 {lowercase_opening_roman_marker, lowercase_roman_marker},
7841 {uppercase_opening_roman_marker, uppercase_roman_marker},
7842 lowercase_letter_marker,
7843 uppercase_letter_marker,
7844 lowercase_roman_marker,
7845 uppercase_roman_marker,
7846 digit_marker
7847 }
7848
7849 local delims = {
7850 parsers.period,
7851 parsers.rparent
7852 }
7853
7854 local markers_table = combine_markers_and_delims(markers, delims)
7855
7856 local function enumerator(start_marker, _,
7857 delimiter_type, interrupting)
7858 local delimiter_range
7859 local allowed_end
7860 if interrupting then
7861 delimiter_range = P("1")
7862 allowed_end = C(parsers.spacechar^1) * #parsers.linechar
7863 else
7864 delimiter_range = start_marker
7865 allowed_end = C(parsers.spacechar^1)
7866 + #(parsers.newline + parsers.eof)
7867 end
7868
7869 return parsers.check_trail
7870 * Ct(C(delimiter_range) * C(delimiter_type))
7871 * allowed_end
7872 end
7873
7874 local starter = join_table_with_func(enumerator, markers_table)
7875
7876 local TightListItem = function(starter)
7877 return parsers.add_indent(starter, "li")
7878 * parsers.indented_content_tight
7879 end
7880
7881 local LooseListItem = function(starter)
7882 return parsers.add_indent(starter, "li")
7883 * parsers.indented_content_loose
7884 * remove_indent("li")
7885 end
7886
7887 local function roman2number(roman)
7888 local romans = { ["M"] = 1000, ["D"] = 500, ["C"] = 100,
7889 ["L"] = 50, ["X"] = 10, ["V"] = 5, ["I"] = 1 }
7890 local numeral = 0
7891
7892 local i = 1
7893 local len = string.len(roman)
7894 while i < len do
7895 local z1, z2 = romans[ string.sub(roman, i, i) ],
7896 romans[ string.sub(roman, i+1, i+1) ]
7897 if z1 < z2 then
7898 numeral = numeral + (z2 - z1)
7899 i = i + 2
7900 else
7901 numeral = numeral + z1
7902 i = i + 1
7903 end
7904 end
7905 if i <= len then
7906 numeral = numeral + romans[ string.sub(roman,i,i) ]
7907 end
7908 return numeral
7909 end
7910
7911 local function sniffstyle(numstr, delimend)
7912 local numdelim
7913 if delimend == ")" then
7914 numdelim = "OneParen"
7915 elseif delimend == "." then
7916 numdelim = "Period"
7917 else
7918 numdelim = "Default"
7919 end
7920
7921 local num
7922 num = numstr:match("^([I])$")
7923 if num then
7924 return roman2number(num), "UpperRoman", numdelim
7925 end
7926 num = numstr:match("^([i])$")
7927 if num then
7928 return roman2number(string.upper(num)), "LowerRoman", numdelim
7929 end
7930 num = numstr:match("^([A-Z])$")
7931 if num then
7932 return string.byte(num) - string.byte("A") + 1,
7933 "UpperAlpha", numdelim
7934 end
7935 num = numstr:match("^([a-z])$")
7936 if num then
7937 return string.byte(num) - string.byte("a") + 1,
7938 "LowerAlpha", numdelim
7939 end
7940 num = numstr:match("^([IVXLCDM]+)")
7941 if num then
7942 return roman2number(num), "UpperRoman", numdelim
7943 end
7944 num = numstr:match("^([ivxlcdm]+)")
7945 if num then
7946 return roman2number(string.upper(num)), "LowerRoman", numdelim
7947 end
7948 return math.floor(tonumber(numstr) or 1), "Decimal", numdelim
7949 end
7950
7951 local function fancylist(items,tight,start)
7952 local startnum, numstyle, numdelim
7953 = sniffstyle(start[2][1], start[2][2])
7954 return writer.fancylist(items,tight,
7955 options.startNumber and startnum or 1,
7956 numstyle or "Decimal",
7957 numdelim or "Default")
7958 end
7959
7960 local FancyListOfType
7961 = function(start_marker, continuation_marker, delimiter_type)
7962 local enumerator_start
7963 = enumerator(start_marker, continuation_marker,
7964 delimiter_type)
7965 local enumerator_cont
7966 = enumerator(continuation_marker, continuation_marker,
7967 delimiter_type)
7968 return Cg(enumerator_start, "listtype")
7969 * (Ct( TightListItem(Cb("listtype"))
7970 * ((parsers.check_minimal_indent / "")
7971 * TightListItem(enumerator_cont))^0)
7972 * Cc(true)
7973 * -#((parsers.conditionally_indented_blankline^0 / "")
7974 * parsers.check_minimal_indent * enumerator_cont)
7975 + Ct( LooseListItem(Cb("listtype"))
7976 * ((parsers.conditionally_indented_blankline^0 / "")
7977 * (parsers.check_minimal_indent / "")
7978 * LooseListItem(enumerator_cont))^0)
7979 * Cc(false)
7980 ) * Ct(Cb("listtype")) / fancylist
7981 end
7982
7983 local FancyList
7984 = join_table_with_func(FancyListOfType, markers_table)
7985
7986 local ListStarter = starter
7987
7988 self.update_rule("OrderedList", FancyList)
7989 self.update_rule("ListStarter", ListStarter)
7990 end
7991 }
7992 end
7993 M.extensions.fenced_code = function(blank_before_code_fence,
7994 allow_attributes,
7995 allow_raw_blocks)
7996 return {
7997 name = "built-in fenced_code syntax extension",
7998 extend_writer = function(self)
7999 local options = self.options
8000
8001 function self.fencedCode(s, i, attr)
8002 if not self.is_writing then return "" end
8003 s = s:gsub("\n$", "")
8004 local buf = {}
8005 if attr ~= nil then
8006 table.insert(buf,
8007 {"\\markdownRendererFencedCodeAttributeContextBegin",
8008 self.attributes(attr)})
8009 end
8010 local name = util.cache_verbatim(options.cacheDir, s)
8011 table.insert(buf,
8012 {"\\markdownRendererInputFencedCode{",
8013 name,"}{",self.string(i),"}{",self.infostring(i),"}"})
8014 if attr ~= nil then
8015 table.insert(buf,
8016 "\\markdownRendererFencedCodeAttributeContextEnd{}")
8017 end
8018 return buf
8019 end
8020
8021 if allow_raw_blocks then
8022 function self.rawBlock(s, attr)
8023 if not self.is_writing then return "" end
8024 s = s:gsub("\n$", "")
8025 local name = util.cache_verbatim(options.cacheDir, s)
8026 return {"\\markdownRendererInputRawBlock{",
8027 name,"}{", self.string(attr),"}"}
8028 end
8029 end
8030 end, extend_reader = function(self)
8031 local parsers = self.parsers
8032 local writer = self.writer
8033
8034 local function captures_geq_length(_,i,a,b)
8035 return #a >= #b and i
8036 end
8037
8038 local function strip_enclosing_whitespaces(str)
8039 return str:gsub("^%s*(.-)%s*$", "%1")
8040 end
8041
8042 local tilde_infostring = Cs(Cs((V("HtmlEntity")
8043 + parsers.anyescaped
8044 - parsers.newline)^0)
8045 / strip_enclosing_whitespaces)
8046
8047 local backtick_infostring
8048 = Cs( Cs((V("HtmlEntity")
8049 + ( -#(parsers.backslash * parsers.backtick)
8050 * parsers.anyescaped)
8051 - parsers.newline
8052 - parsers.backtick)^0)
8053 / strip_enclosing_whitespaces)
8054
8055 local fenceindent
8056
8057 local function has_trail(indent_table)
8058 return indent_table ~= nil and
8059 indent_table.trail ~= nil and
8060 next(indent_table.trail) ~= nil
8061 end
8062
8063 local function has_indents(indent_table)
8064 return indent_table ~= nil and
8065 indent_table.indents ~= nil and
8066 next(indent_table.indents) ~= nil
8067 end
8068
8069 local function get_last_indent_name(indent_table)
8070 if has_indents(indent_table) then
8071 return indent_table.indents[#indent_table.indents].name
8072 end
8073 end
8074
8075 local count_fenced_start_indent =
8076 function(_, _, indent_table, trail)
8077 local last_indent_name = get_last_indent_name(indent_table)
8078 fenceindent = 0
8079 if last_indent_name ~= "li" then
8080 fenceindent = #trail
8081 end
8082 return true
8083 end
8084
8085 local fencehead = function(char, infostring)
8086 return Cmt( Cb("indent_info")
8087 * parsers.check_trail, count_fenced_start_indent)
8088 * Cg(char^3, "fencelength")
8089 * parsers.optionalspace
8090 * infostring
8091 * (parsers.newline + parsers.eof)
8092 end
8093
8094 local fencetail = function(char)
8095 return parsers.check_trail_no_rem
8096 * Cmt(C(char^3) * Cb("fencelength"), captures_geq_length)
8097 * parsers.optionalspace * (parsers.newline + parsers.eof)
8098 + parsers.eof
8099 end
8100
8101 local process_fenced_line =
8102 function(s, i, -- luacheck: ignore s i
8103 indent_table, line_content, is_blank)
8104 local remainder = ""
8105 if has_trail(indent_table) then
8106 remainder = indent_table.trail.internal_remainder
8107 end
8108
8109 if is_blank
8110 and get_last_indent_name(indent_table) == "li" then
8111 remainder = ""
8112 end
8113
8114 local str = remainder .. line_content
8115 local index = 1
8116 local remaining = fenceindent
8117
8118 while true do
8119 local c = str:sub(index, index)
8120 if c == " " and remaining > 0 then
8121 remaining = remaining - 1
8122 index = index + 1
8123 elseif c == "\t" and remaining > 3 then
8124 remaining = remaining - 4
8125 index = index + 1
8126 else
8127 break
8128 end
8129 end
8130
8131 return true, str:sub(index)
8132 end
8133
8134 local fencedline = function(char)
8135 return Cmt( Cb("indent_info")
8136 * C(parsers.line - fencetail(char))
8137 * Cc(false), process_fenced_line)
8138 end
8139
8140 local blankfencedline
8141 = Cmt( Cb("indent_info")
8142 * C(parsers.blankline)
8143 * Cc(true), process_fenced_line)
8144
8145 local TildeFencedCode
8146 = fencehead(parsers.tilde, tilde_infostring)
8147 * Cs(( (parsers.check_minimal_blank_indent / "")
8148 * blankfencedline
8149 + ( parsers.check_minimal_indent / "")
8150 * fencedline(parsers.tilde))^0)
8151 * ( (parsers.check_minimal_indent / "")
8152 * fencetail(parsers.tilde) + parsers.succeed)
8153
8154 local BacktickFencedCode
8155 = fencehead(parsers.backtick, backtick_infostring)
8156 * Cs(( (parsers.check_minimal_blank_indent / "")
8157 * blankfencedline
8158 + (parsers.check_minimal_indent / "")
8159 * fencedline(parsers.backtick))^0)
8160 * ( (parsers.check_minimal_indent / "")
8161 * fencetail(parsers.backtick) + parsers.succeed)
8162
8163 local infostring_with_attributes
8164 = Ct(C((parsers.linechar
8165 - ( parsers.optionalspace
8166 * parsers.attributes))^0)
8167 * parsers.optionalspace
8168 * Ct(parsers.attributes))
8169
8170 local FencedCode
8171 = ((TildeFencedCode + BacktickFencedCode)
8172 / function(infostring, code)
8173 local expanded_code = self.expandtabs(code)
8174
8175 if allow_raw_blocks then
8176 local raw_attr = lpeg.match(parsers.raw_attribute,
8177 infostring)
8178 if raw_attr then
8179 return writer.rawBlock(expanded_code, raw_attr)
8180 end
8181 end
8182
8183 local attr = nil
8184 if allow_attributes then
8185 local match = lpeg.match(infostring_with_attributes,
8186 infostring)
8187 if match then
8188 infostring, attr = table.unpack(match)
8189 end
8190 end
8191 return writer.fencedCode(expanded_code, infostring, attr)
8192 end)
8193
8194 self.insert_pattern("Block after Verbatim",
8195 FencedCode, "FencedCode")
8196
8197 local fencestart
8198 if blank_before_code_fence then
8199 fencestart = parsers.fail
8200 else
8201 fencestart = fencehead(parsers.backtick, backtick_infostring)
8202 + fencehead(parsers.tilde, tilde_infostring)
8203 end
8204
8205 self.update_rule("EndlineExceptions", function(previous_pattern)
8206 if previous_pattern == nil then
8207 previous_pattern = parsers.EndlineExceptions
8208 end
8209 return previous_pattern + fencestart
8210 end)
8211
8212 self.add_special_character("`")
8213 self.add_special_character("~")
8214 end
8215 }
8216 end
8217 M.extensions.fenced_divs = function(blank_before_div_fence)
8218 return {
8219 name = "built-in fenced_divs syntax extension",
8220 extend_writer = function(self)
8221 function self.div_begin(attributes)
8222 local start_output
8223 = {"\\markdownRendererFencedDivAttributeContextBegin\n",
8224 self.attributes(attributes)}
8225 local end_output
8226 = {"\\markdownRendererFencedDivAttributeContextEnd{}"}
8227 return self.push_attributes(
8228 "div", attributes, start_output, end_output)
8229 end
8230 function self.div_end()
8231 return self.pop_attributes("div")
8232 end
8233 end, extend_reader = function(self)
8234 local parsers = self.parsers
8235 local writer = self.writer
8236 local fenced_div_infostring
8237 = Ct(parsers.attributes)
8238 + (
8239 C( parsers.attribute_classname
8240 - parsers.colon)^1
8241 / function (infostring)
8242 return {"." .. infostring}
8243 end
8244 )
8245
8246 local fenced_div_begin = parsers.nonindentspace
8247 * parsers.colon^3
8248 * parsers.optionalspace
8249 * fenced_div_infostring
8250 * ( parsers.spacechar^1
8251 * parsers.colon^1)^0
8252 * parsers.optionalspace
8253 * (parsers.newline + parsers.eof)
8254
8255 local fenced_div_end = parsers.nonindentspace
8256 * parsers.colon^3
8257 * parsers.optionalspace
8258 * (parsers.newline + parsers.eof)
8259 self.initialize_named_group("fenced_div_level", "0")
8260 self.initialize_named_group("fenced_div_num_opening_indents")
8261
8262 local function increment_div_level()
8263 local push_indent_table =
8264 function(s, i, indent_table, -- luacheck: ignore s i
8265 fenced_div_num_opening_indents, fenced_div_level)
8266 fenced_div_level = tonumber(fenced_div_level) + 1
8267 local num_opening_indents = 0
8268 if indent_table.indents ~= nil then
8269 num_opening_indents = #indent_table.indents
8270 end
8271 fenced_div_num_opening_indents[fenced_div_level]
8272 = num_opening_indents
8273 return true, fenced_div_num_opening_indents
8274 end
8275
8276 local increment_level =
8277 function(s, i, fenced_div_level) -- luacheck: ignore s i
8278 fenced_div_level = tonumber(fenced_div_level) + 1
8279 return true, tostring(fenced_div_level)
8280 end
8281
8282 return Cg( Cmt( Cb("indent_info")
8283 * Cb("fenced_div_num_opening_indents")
8284 * Cb("fenced_div_level"), push_indent_table)
8285 , "fenced_div_num_opening_indents")
8286 * Cg( Cmt( Cb("fenced_div_level"), increment_level)
8287 , "fenced_div_level")
8288 end
8289
8290 local function decrement_div_level()
8291 local pop_indent_table =
8292 function(s, i, -- luacheck: ignore s i
8293 fenced_div_indent_table, fenced_div_level)
8294 fenced_div_level = tonumber(fenced_div_level)
8295 fenced_div_indent_table[fenced_div_level] = nil
8296 return true, tostring(fenced_div_level - 1)
8297 end
8298
8299 return Cg( Cmt( Cb("fenced_div_num_opening_indents")
8300 * Cb("fenced_div_level"), pop_indent_table)
8301 , "fenced_div_level")
8302 end
8303
8304 local non_fenced_div_block
8305 = parsers.check_minimal_indent * V("Block")
8306 - parsers.check_minimal_indent_and_trail * fenced_div_end
8307
8308 local non_fenced_div_paragraph
8309 = parsers.check_minimal_indent * V("Paragraph")
8310 - parsers.check_minimal_indent_and_trail * fenced_div_end
8311
8312 local blank = parsers.minimally_indented_blank
8313
8314 local block_separated = parsers.block_sep_group(blank)
8315 * non_fenced_div_block
8316
8317 local loop_body_pair
8318 = parsers.create_loop_body_pair(block_separated,
8319 non_fenced_div_paragraph,
8320 parsers.block_sep_group(blank),
8321 parsers.par_sep_group(blank))
8322
8323 local content_loop = ( non_fenced_div_block
8324 * loop_body_pair.block^0
8325 + non_fenced_div_paragraph
8326 * block_separated
8327 * loop_body_pair.block^0
8328 + non_fenced_div_paragraph
8329 * loop_body_pair.par^0)
8330 * blank^0
8331
8332 local FencedDiv = fenced_div_begin
8333 / writer.div_begin
8334 * increment_div_level()
8335 * parsers.skipblanklines
8336 * Ct(content_loop)
8337 * parsers.minimally_indented_blank^0
8338 * parsers.check_minimal_indent_and_trail
8339 * fenced_div_end
8340 * decrement_div_level()
8341 * (Cc("") / writer.div_end)
8342
8343 self.insert_pattern("Block after Verbatim",
8344 FencedDiv, "FencedDiv")
8345
8346 self.add_special_character(":")
8347
8348 local function is_inside_div()
8349 local check_div_level =
8350 function(s, i, fenced_div_level) -- luacheck: ignore s i
8351 fenced_div_level = tonumber(fenced_div_level)
8352 return fenced_div_level > 0
8353 end
8354
8355 return Cmt(Cb("fenced_div_level"), check_div_level)
8356 end
8357
8358 local function check_indent()
8359 local compare_indent =
8360 function(s, i, indent_table, -- luacheck: ignore s i
8361 fenced_div_num_opening_indents, fenced_div_level)
8362 fenced_div_level = tonumber(fenced_div_level)
8363 local num_current_indents
8364 = ( indent_table.current_line_indents ~= nil and
8365 #indent_table.current_line_indents) or 0
8366 local num_opening_indents
8367 = fenced_div_num_opening_indents[fenced_div_level]
8368 return num_current_indents == num_opening_indents
8369 end
8370
8371 return Cmt( Cb("indent_info")
8372 * Cb("fenced_div_num_opening_indents")
8373 * Cb("fenced_div_level"), compare_indent)
8374 end
8375
8376 local fencestart = is_inside_div()
8377 * fenced_div_end
8378 * check_indent()
8379
8380 if not blank_before_div_fence then
8381 self.update_rule("EndlineExceptions", function(previous_pattern)
8382 if previous_pattern == nil then
8383 previous_pattern = parsers.EndlineExceptions
8384 end
8385 return previous_pattern + fencestart
8386 end)
8387 end
8388 end
8389 }
8390 end
8391 M.extensions.header_attributes = function()
8392 return {
8393 name = "built-in header_attributes syntax extension",
8394 extend_writer = function()
8395 end, extend_reader = function(self)
8396 local parsers = self.parsers
8397 local writer = self.writer
8398
8399 local function strip_atx_end(s)
8400 return s:gsub("%s+#*%s*$","")
8401 end
8402
8403 local AtxHeading = Cg(parsers.heading_start, "level")
8404 * parsers.optionalspace
8405 * (C(((parsers.linechar
8406 - (parsers.attributes
8407 * parsers.optionalspace
8408 * parsers.newline))
8409 * (parsers.linechar
8410 - parsers.lbrace)^0)^1)
8411 / strip_atx_end
8412 / parsers.parse_heading_text)
8413 * Cg(Ct(parsers.newline
8414 + (parsers.attributes
8415 * parsers.optionalspace
8416 * parsers.newline)), "attributes")
8417 * Cb("level")
8418 * Cb("attributes")
8419 / writer.heading
8420
8421 local function strip_trailing_spaces(s)
8422 return s:gsub("%s*$","")
8423 end
8424
8425 local heading_line = (parsers.linechar
8426 - (parsers.attributes
8427 * parsers.optionalspace
8428 * parsers.newline))^1
8429 - parsers.thematic_break_lines
8430
8431 local heading_text
8432 = heading_line
8433 * ( (V("Endline") / "\n")
8434 * (heading_line - parsers.heading_level))^0
8435 * parsers.newline^-1
8436
8437 local SetextHeading
8438 = parsers.freeze_trail * parsers.check_trail_no_rem
8439 * #(heading_text
8440 * (parsers.attributes
8441 * parsers.optionalspace
8442 * parsers.newline)^-1
8443 * parsers.check_minimal_indent
8444 * parsers.check_trail
8445 * parsers.heading_level)
8446 * Cs(heading_text) / strip_trailing_spaces
8447 / parsers.parse_heading_text
8448 * Cg(Ct((parsers.attributes
8449 * parsers.optionalspace
8450 * parsers.newline)^-1), "attributes")
8451 * parsers.check_minimal_indent_and_trail * parsers.heading_level
8452 * Cb("attributes")
8453 * parsers.newline
8454 * parsers.unfreeze_trail
8455 / writer.heading
8456
8457 local Heading = AtxHeading + SetextHeading
8458 self.update_rule("Heading", Heading)
8459 end
8460 }
8461 end
8462 M.extensions.inline_code_attributes = function()
8463 return {
8464 name = "built-in inline_code_attributes syntax extension",
8465 extend_writer = function()
8466 end, extend_reader = function(self)
8467 local writer = self.writer
8468
8469 local CodeWithAttributes = parsers.inticks
8470 * Ct(parsers.attributes)
8471 / writer.code
8472
8473 self.insert_pattern("Inline before Code",
8474 CodeWithAttributes,
8475 "CodeWithAttributes")
8476 end
8477 }
8478 end
8479 M.extensions.line_blocks = function()
8480 return {
8481 name = "built-in line_blocks syntax extension",
8482 extend_writer = function(self)
8483 function self.lineblock(lines)
8484 if not self.is_writing then return "" end
8485 local buffer = {}
8486 for i = 1, #lines - 1 do
8487 buffer[#buffer + 1] = { lines[i], self.hard_line_break }
8488 end
8489 buffer[#buffer + 1] = lines[#lines]
8490
8491 return {"\\markdownRendererLineBlockBegin\n"
8492 ,buffer,
8493 "\n\\markdownRendererLineBlockEnd "}
8494 end
8495 end, extend_reader = function(self)
8496 local parsers = self.parsers
8497 local writer = self.writer
8498
8499 local LineBlock
8500 = Ct((Cs(( (parsers.pipe * parsers.space) / ""
8501 * ((parsers.space)/entities.char_entity("nbsp"))^0
8502 * parsers.linechar^0 * (parsers.newline/""))
8503 * (-parsers.pipe
8504 * (parsers.space^1/" ")
8505 * parsers.linechar^1
8506 * (parsers.newline/"")
8507 )^0)
8508 / self.parser_functions.parse_inlines)^1)
8509 / writer.lineblock
8510
8511 self.insert_pattern("Block after Blockquote",
8512 LineBlock, "LineBlock")
8513 end
8514 }
8515 end
8516 M.extensions.mark = function()
8517 return {
8518 name = "built-in mark syntax extension",
8519 extend_writer = function(self)
8520 function self.mark(s)
8521 if self.flatten_inlines then return s end
8522 return {"\\markdownRendererMark{", s, "}"}
8523 end
8524 end, extend_reader = function(self)
8525 local parsers = self.parsers
8526 local writer = self.writer
8527
8528 local doubleequals = P("==")
8529
8530 local Mark
8531 = parsers.between(V("Inline"), doubleequals, doubleequals)
8532 / function (inlines) return writer.mark(inlines) end
8533
8534 self.add_special_character("=")
8535 self.insert_pattern("Inline before LinkAndEmph",
8536 Mark, "Mark")
8537 end
8538 }
8539 end
8540 M.extensions.link_attributes = function()
8541 return {
8542 name = "built-in link_attributes syntax extension",
8543 extend_writer = function()
8544 end, extend_reader = function(self)
8545 local parsers = self.parsers
8546 local options = self.options
8547
8548 local define_reference_parser
8549 = (parsers.check_trail / "")
8550 * parsers.link_label
8551 * parsers.colon
8552 * parsers.spnlc * parsers.url
8553 * ( parsers.spnlc_sep * parsers.title
8554 * (parsers.spnlc * Ct(parsers.attributes))
8555 * parsers.only_blank
8556 + parsers.spnlc_sep * parsers.title * parsers.only_blank
8557 + Cc("") * (parsers.spnlc * Ct(parsers.attributes))
8558 * parsers.only_blank
8559 + Cc("") * parsers.only_blank)
8560
8561 local ReferenceWithAttributes = define_reference_parser
8562 / self.register_link
8563
8564 self.update_rule("Reference", ReferenceWithAttributes)
8565
8566
8567 local LinkWithAttributesAndEmph
8568 = Ct(parsers.link_and_emph_table * Cg(Cc(true),
8569 "match_link_attributes"))
8570 / self.defer_link_and_emphasis_processing
8571
8572 self.update_rule("LinkAndEmph", LinkWithAttributesAndEmph)
8573
8574 local AutoLinkUrlWithAttributes
8575 = parsers.auto_link_url
8576 * Ct(parsers.attributes)
8577 / self.auto_link_url
8578
8579 self.insert_pattern("Inline before AutoLinkUrl",
8580 AutoLinkUrlWithAttributes,
8581 "AutoLinkUrlWithAttributes")
8582
8583 local AutoLinkEmailWithAttributes
8584 = parsers.auto_link_email
8585 * Ct(parsers.attributes)
8586 / self.auto_link_email
8587
8588 self.insert_pattern("Inline before AutoLinkEmail",
8589 AutoLinkEmailWithAttributes,
8590 "AutoLinkEmailWithAttributes")
8591
8592 if options.relativeReferences then
8593
8594 local AutoLinkRelativeReferenceWithAttributes
8595 = parsers.auto_link_relative_reference
8596 * Ct(parsers.attributes)
8597 / self.auto_link_url
8598
8599 self.insert_pattern(
8600 "Inline before AutoLinkRelativeReference",
8601 AutoLinkRelativeReferenceWithAttributes,
8602 "AutoLinkRelativeReferenceWithAttributes")
8603
8604 end
8605
8606 end
8607 }
8608 end
8609 M.extensions.notes = function(notes, inline_notes)
8610 assert(notes or inline_notes)
8611 return {
8612 name = "built-in notes syntax extension",
8613 extend_writer = function(self)
8614 function self.note(s)
8615 if self.flatten_inlines then return "" end
8616 return {"\\markdownRendererNote{",s,"}"}
8617 end
8618 end, extend_reader = function(self)
8619 local parsers = self.parsers
8620 local writer = self.writer
8621
8622 local rawnotes = parsers.rawnotes
8623
8624 if inline_notes then
8625 local InlineNote
8626 = parsers.circumflex
8627 * ( parsers.link_label
8628 / self.parser_functions.parse_inlines_no_inline_note)
8629 / writer.note
8630
8631 self.insert_pattern("Inline after LinkAndEmph",
8632 InlineNote, "InlineNote")
8633 end
8634 if notes then
8635 local function strip_first_char(s)
8636 return s:sub(2)
8637 end
8638
8639 local RawNoteRef
8640 = #(parsers.lbracket * parsers.circumflex)
8641 * parsers.link_label / strip_first_char
8642
8643 -- like indirect_link
8644 local function lookup_note(ref)
8645 return writer.defer_call(function()
8646 local found = rawnotes[self.normalize_tag(ref)]
8647 if found then
8648 return writer.note(
8649 self.parser_functions.parse_blocks_nested(found))
8650 else
8651 local text = string.format(
8652 'Undefined note reference "%s"', ref)
8653 local more = string.format(
8654 "Look for the text `[^%s]`.", ref)
8655 return {writer.warning(text, more), "[",
8656 self.parser_functions.parse_inlines("^" .. ref), "]"}
8657 end
8658 end)
8659 end
8660
8661 local function register_note(ref,rawnote)
8662 local normalized_tag = self.normalize_tag(ref)
8663 if rawnotes[normalized_tag] == nil then
8664 rawnotes[normalized_tag] = rawnote
8665 return ""
8666 else
8667 local text
8668 = string.format('Multiply defined note reference "%s"',
8669 ref)
8670 local more
8671 = string.format("Look for the text `[^%s]: ...`.", ref)
8672 return writer.warning(text, more)
8673 end
8674 end
8675
8676 local NoteRef = RawNoteRef / lookup_note
8677
8678 local optionally_indented_line
8679 = parsers.check_optional_indent_and_any_trail * parsers.line
8680
8681 local blank
8682 = parsers.check_optional_blank_indent_and_any_trail
8683 * parsers.optionalspace * parsers.newline
8684
8685 local chunk
8686 = Cs(parsers.line
8687 * (optionally_indented_line - blank)^0)
8688
8689 local indented_blocks = function(bl)
8690 return Cs( bl
8691 * ( blank^1 * (parsers.check_optional_indent / "")
8692 * parsers.check_code_trail
8693 * -parsers.blankline * bl)^0)
8694 end
8695
8696 local NoteBlock
8697 = parsers.check_trail_no_rem
8698 * RawNoteRef * parsers.colon
8699 * parsers.spnlc * indented_blocks(chunk)
8700 / register_note
8701
8702 self.update_rule("Reference", function(previous_pattern)
8703 if previous_pattern == nil then
8704 previous_pattern = parsers.Reference
8705 end
8706 return NoteBlock + previous_pattern
8707 end)
8708
8709 self.insert_pattern("Inline before LinkAndEmph",
8710 NoteRef, "NoteRef")
8711 end
8712
8713 self.add_special_character("^")
8714 end
8715 }
8716 end
8717 M.extensions.pipe_tables = function(table_captions, table_attributes)
8718
8719 local function make_pipe_table_rectangular(rows)
8720 local num_columns = #rows[2]
8721 local rectangular_rows = {}
8722 for i = 1, #rows do
8723 local row = rows[i]
8724 local rectangular_row = {}
8725 for j = 1, num_columns do
8726 rectangular_row[j] = row[j] or ""
8727 end
8728 table.insert(rectangular_rows, rectangular_row)
8729 end
8730 return rectangular_rows
8731 end
8732
8733 local function pipe_table_row(allow_empty_first_column
8734 , nonempty_column
8735 , column_separator
8736 , column)
8737 local row_beginning
8738 if allow_empty_first_column then
8739 row_beginning = -- empty first column
8740 #(parsers.spacechar^4
8741 * column_separator)
8742 * parsers.optionalspace
8743 * column
8744 * parsers.optionalspace
8745 -- non-empty first column
8746 + parsers.nonindentspace
8747 * nonempty_column^-1
8748 * parsers.optionalspace
8749 else
8750 row_beginning = parsers.nonindentspace
8751 * nonempty_column^-1
8752 * parsers.optionalspace
8753 end
8754
8755 return Ct(row_beginning
8756 * (-- single column with no leading pipes
8757 #(column_separator
8758 * parsers.optionalspace
8759 * parsers.newline)
8760 * column_separator
8761 * parsers.optionalspace
8762 -- single column with leading pipes or
8763 -- more than a single column
8764 + (column_separator
8765 * parsers.optionalspace
8766 * column
8767 * parsers.optionalspace)^1
8768 * (column_separator
8769 * parsers.optionalspace)^-1))
8770 end
8771
8772 return {
8773 name = "built-in pipe_tables syntax extension",
8774 extend_writer = function(self)
8775 function self.table(rows, caption, attributes)
8776 if not self.is_writing then return "" end
8777 local buffer = {}
8778 if attributes ~= nil then
8779 table.insert(buffer,
8780 "\\markdownRendererTableAttributeContextBegin\n")
8781 table.insert(buffer, self.attributes(attributes))
8782 end
8783 table.insert(buffer,
8784 {"\\markdownRendererTable{",
8785 caption or "", "}{", #rows - 1, "}{",
8786 #rows[1], "}"})
8787 local temp = rows[2] -- put alignments on the first row
8788 rows[2] = rows[1]
8789 rows[1] = temp
8790 for i, row in ipairs(rows) do
8791 table.insert(buffer, "{")
8792 for _, column in ipairs(row) do
8793 if i > 1 then -- do not use braces for alignments
8794 table.insert(buffer, "{")
8795 end
8796 table.insert(buffer, column)
8797 if i > 1 then
8798 table.insert(buffer, "}")
8799 end
8800 end
8801 table.insert(buffer, "}")
8802 end
8803 if attributes ~= nil then
8804 table.insert(buffer,
8805 "\\markdownRendererTableAttributeContextEnd{}")
8806 end
8807 return buffer
8808 end
8809 end, extend_reader = function(self)
8810 local parsers = self.parsers
8811 local writer = self.writer
8812
8813 local table_hline_separator = parsers.pipe + parsers.plus
8814
8815 local table_hline_column = (parsers.dash
8816 - #(parsers.dash
8817 * (parsers.spacechar
8818 + table_hline_separator
8819 + parsers.newline)))^1
8820 * (parsers.colon * Cc("r")
8821 + parsers.dash * Cc("d"))
8822 + parsers.colon
8823 * (parsers.dash
8824 - #(parsers.dash
8825 * (parsers.spacechar
8826 + table_hline_separator
8827 + parsers.newline)))^1
8828 * (parsers.colon * Cc("c")
8829 + parsers.dash * Cc("l"))
8830
8831 local table_hline = pipe_table_row(false
8832 , table_hline_column
8833 , table_hline_separator
8834 , table_hline_column)
8835
8836 local table_caption_beginning
8837 = ( parsers.check_minimal_blank_indent_and_any_trail_no_rem
8838 * parsers.optionalspace * parsers.newline)^0
8839 * parsers.check_minimal_indent_and_trail
8840 * (P("Table")^-1 * parsers.colon)
8841 * parsers.optionalspace
8842
8843 local function strip_trailing_spaces(s)
8844 return s:gsub("%s*$","")
8845 end
8846
8847 local table_row
8848 = pipe_table_row(true
8849 , (C((parsers.linechar - parsers.pipe)^1)
8850 / strip_trailing_spaces
8851 / self.parser_functions.parse_inlines)
8852 , parsers.pipe
8853 , (C((parsers.linechar - parsers.pipe)^0)
8854 / strip_trailing_spaces
8855 / self.parser_functions.parse_inlines))
8856
8857 local table_caption
8858 if table_captions then
8859 table_caption = #table_caption_beginning
8860 * table_caption_beginning
8861 if table_attributes then
8862 table_caption = table_caption
8863 * (C(((( parsers.linechar
8864 - (parsers.attributes
8865 * parsers.optionalspace
8866 * parsers.newline
8867 * -#( parsers.optionalspace
8868 * parsers.linechar)))
8869 + ( parsers.newline
8870 * #( parsers.optionalspace
8871 * parsers.linechar)
8872 * C(parsers.optionalspace)
8873 / writer.space))
8874 * (parsers.linechar
8875 - parsers.lbrace)^0)^1)
8876 / strip_trailing_spaces
8877 / self.parser_functions.parse_inlines)
8878 * (parsers.newline
8879 + ( Ct(parsers.attributes)
8880 * parsers.optionalspace
8881 * parsers.newline))
8882 else
8883 table_caption = table_caption
8884 * C(( parsers.linechar
8885 + ( parsers.newline
8886 * #( parsers.optionalspace
8887 * parsers.linechar)
8888 * C(parsers.optionalspace)
8889 / writer.space))^1)
8890 / self.parser_functions.parse_inlines
8891 * parsers.newline
8892 end
8893 else
8894 table_caption = parsers.fail
8895 end
8896
8897 local PipeTable
8898 = Ct( table_row * parsers.newline
8899 * (parsers.check_minimal_indent_and_trail / {})
8900 * table_hline * parsers.newline
8901 * ( (parsers.check_minimal_indent / {})
8902 * table_row * parsers.newline)^0)
8903 / make_pipe_table_rectangular
8904 * table_caption^-1
8905 / writer.table
8906
8907 self.insert_pattern("Block after Blockquote",
8908 PipeTable, "PipeTable")
8909 end
8910 }
8911 end
8912 M.extensions.raw_inline = function()
8913 return {
8914 name = "built-in raw_inline syntax extension",
8915 extend_writer = function(self)
8916 local options = self.options
8917
8918 function self.rawInline(s, attr)
8919 if not self.is_writing then return "" end
8920 if self.flatten_inlines then return s end
8921 local name = util.cache_verbatim(options.cacheDir, s)
8922 return {"\\markdownRendererInputRawInline{",
8923 name,"}{", self.string(attr),"}"}
8924 end
8925 end, extend_reader = function(self)
8926 local writer = self.writer
8927
8928 local RawInline = parsers.inticks
8929 * parsers.raw_attribute
8930 / writer.rawInline
8931
8932 self.insert_pattern("Inline before Code",
8933 RawInline, "RawInline")
8934 end
8935 }
8936 end
8937 M.extensions.strike_through = function()
8938 return {
8939 name = "built-in strike_through syntax extension",
8940 extend_writer = function(self)
8941 function self.strike_through(s)
8942 if self.flatten_inlines then return s end
8943 return {"\\markdownRendererStrikeThrough{",s,"}"}
8944 end
8945 end, extend_reader = function(self)
8946 local parsers = self.parsers
8947 local writer = self.writer
8948
8949 local StrikeThrough = (
8950 parsers.between(parsers.Inline, parsers.doubletildes,
8951 parsers.doubletildes)
8952 ) / writer.strike_through
8953
8954 self.insert_pattern("Inline after LinkAndEmph",
8955 StrikeThrough, "StrikeThrough")
8956
8957 self.add_special_character("~")
8958 end
8959 }
8960 end
8961 M.extensions.subscripts = function()
8962 return {
8963 name = "built-in subscripts syntax extension",
8964 extend_writer = function(self)
8965 function self.subscript(s, parsed)
8966 if self.flatten_inlines then return s end
8967 return {"\\markdownRendererSubscript{",parsed,"}"}
8968 end
8969 end, extend_reader = function(self)
8970 local parsers = self.parsers
8971 local writer = self.writer
8972
8973 local Subscript = (
8974 parsers.between(parsers.Str, parsers.tilde, parsers.tilde)
8975 ) / function(s)
8976 return writer.subscript(
8977 s, self.parser_functions.parse_inlines_identity(s[1])
8978 )
8979 end
8980
8981 self.insert_pattern("Inline after LinkAndEmph",
8982 Subscript, "Subscript")
8983
8984 self.add_special_character("~")
8985 end
8986 }
8987 end
8988 M.extensions.superscripts = function()
8989 return {
8990 name = "built-in superscripts syntax extension",
8991 extend_writer = function(self)
8992 function self.superscript(s, parsed)
8993 if self.flatten_inlines then return s end
8994 return {"\\markdownRendererSuperscript{",parsed,"}"}
8995 end
8996 end, extend_reader = function(self)
8997 local parsers = self.parsers
8998 local writer = self.writer
8999
9000 local Superscript = (
9001 parsers.between(parsers.Str, parsers.circumflex,
9002 parsers.circumflex)
9003 ) / function(s)
9004 return writer.superscript(
9005 s, self.parser_functions.parse_inlines_identity(s[1])
9006 )
9007 end
9008
9009 self.insert_pattern("Inline after LinkAndEmph",
9010 Superscript, "Superscript")
9011
9012 self.add_special_character("^")
9013 end
9014 }
9015 end
9016 M.extensions.tex_math = function(tex_math_dollars,
9017 tex_math_single_backslash,
9018 tex_math_double_backslash)
9019 return {
9020 name = "built-in tex_math syntax extension",
9021 extend_writer = function(self)
9022 function self.display_math(s, parsed)
9023 if self.flatten_inlines then return s end
9024 return {"\\markdownRendererDisplayMath{",parsed,"}"}
9025 end
9026 function self.inline_math(s, parsed)
9027 if self.flatten_inlines then return s end
9028 return {"\\markdownRendererInlineMath{",parsed,"}"}
9029 end
9030 end, extend_reader = function(self)
9031 local parsers = self.parsers
9032 local writer = self.writer
9033
9034 local function between(p, starter, ender)
9035 return (starter * Cs(p * (p - ender)^0) * ender)
9036 end
9037
9038 local function strip_preceding_whitespaces(str)
9039 return str:gsub("^%s*(.-)$", "%1")
9040 end
9041
9042 local allowed_before_closing
9043 = B( parsers.backslash * parsers.any
9044 + parsers.any * (parsers.any - parsers.backslash))
9045
9046 local allowed_before_closing_no_space
9047 = B( parsers.backslash * parsers.any
9048 + parsers.any * (parsers.nonspacechar - parsers.backslash))
9049
9050 local dollar_math_content
9051 = (parsers.newline * (parsers.check_optional_indent / "")
9052 + parsers.backslash^-1
9053 * parsers.linechar)
9054 - parsers.blankline^2
9055 - parsers.dollar
9056
9057 local inline_math_opening_dollars = parsers.dollar
9058 * #(parsers.nonspacechar)
9059
9060 local inline_math_closing_dollars
9061 = allowed_before_closing_no_space
9062 * parsers.dollar
9063 * -#(parsers.digit)
9064
9065 local inline_math_dollars = between(Cs( dollar_math_content),
9066 inline_math_opening_dollars,
9067 inline_math_closing_dollars)
9068
9069 local display_math_opening_dollars = parsers.dollar
9070 * parsers.dollar
9071
9072 local display_math_closing_dollars = parsers.dollar
9073 * parsers.dollar
9074
9075 local display_math_dollars = between(Cs( dollar_math_content),
9076 display_math_opening_dollars,
9077 display_math_closing_dollars)
9078 local backslash_math_content
9079 = (parsers.newline * (parsers.check_optional_indent / "")
9080 + parsers.linechar)
9081 - parsers.blankline^2
9082 local inline_math_opening_double = parsers.backslash
9083 * parsers.backslash
9084 * parsers.lparent
9085
9086 local inline_math_closing_double = allowed_before_closing
9087 * parsers.spacechar^0
9088 * parsers.backslash
9089 * parsers.backslash
9090 * parsers.rparent
9091
9092 local inline_math_double = between(Cs( backslash_math_content),
9093 inline_math_opening_double,
9094 inline_math_closing_double)
9095 / strip_preceding_whitespaces
9096
9097 local display_math_opening_double = parsers.backslash
9098 * parsers.backslash
9099 * parsers.lbracket
9100
9101 local display_math_closing_double = allowed_before_closing
9102 * parsers.spacechar^0
9103 * parsers.backslash
9104 * parsers.backslash
9105 * parsers.rbracket
9106
9107 local display_math_double = between(Cs( backslash_math_content),
9108 display_math_opening_double,
9109 display_math_closing_double)
9110 / strip_preceding_whitespaces
9111 local inline_math_opening_single = parsers.backslash
9112 * parsers.lparent
9113
9114 local inline_math_closing_single = allowed_before_closing
9115 * parsers.spacechar^0
9116 * parsers.backslash
9117 * parsers.rparent
9118
9119 local inline_math_single = between(Cs( backslash_math_content),
9120 inline_math_opening_single,
9121 inline_math_closing_single)
9122 / strip_preceding_whitespaces
9123
9124 local display_math_opening_single = parsers.backslash
9125 * parsers.lbracket
9126
9127 local display_math_closing_single = allowed_before_closing
9128 * parsers.spacechar^0
9129 * parsers.backslash
9130 * parsers.rbracket
9131
9132 local display_math_single = between(Cs( backslash_math_content),
9133 display_math_opening_single,
9134 display_math_closing_single)
9135 / strip_preceding_whitespaces
9136
9137 local display_math = parsers.fail
9138
9139 local inline_math = parsers.fail
9140
9141 if tex_math_dollars then
9142 display_math = display_math + display_math_dollars
9143 inline_math = inline_math + inline_math_dollars
9144 end
9145
9146 if tex_math_double_backslash then
9147 display_math = display_math + display_math_double
9148 inline_math = inline_math + inline_math_double
9149 end
9150
9151 if tex_math_single_backslash then
9152 display_math = display_math + display_math_single
9153 inline_math = inline_math + inline_math_single
9154 end
9155
9156 local TexMath = display_math
9157 / function(s)
9158 return writer.display_math(
9159 s, self.parser_functions.parse_inlines_math(s)
9160 )
9161 end
9162 + inline_math
9163 / function(s)
9164 return writer.inline_math(
9165 s, self.parser_functions.parse_inlines_math(s)
9166 )
9167 end
9168
9169 self.insert_pattern("Inline after LinkAndEmph",
9170 TexMath, "TexMath")
9171
9172 if tex_math_dollars then
9173 self.add_special_character("$")
9174 end
9175
9176 if tex_math_single_backslash or tex_math_double_backslash then
9177 self.add_special_character("\\")
9178 self.add_special_character("[")
9179 self.add_special_character("]")
9180 self.add_special_character(")")
9181 self.add_special_character("(")
9182 end
9183 end
9184 }
9185 end
9186 M.extensions.jekyll_data = function(expect_jekyll_data,
9187 ensure_jekyll_data)
9188 return {
9189 name = "built-in jekyll_data syntax extension",
9190 extend_writer = function(self)
9191 function self.jekyllData(d, t, p)
9192 if not self.is_writing then return "" end
9193
9194 local buf = {}
9195
9196 local keys = {}
9197 for k, _ in pairs(d) do
9198 table.insert(keys, k)
9199 end
9200 table.sort(keys, function(first, second)
9201 if type(first) ~= type(second) then
9202 return type(first) < type(second)
9203 else
9204 return first < second
9205 end
9206 end)
9207
9208 if not p then
9209 table.insert(buf, "\\markdownRendererJekyllDataBegin")
9210 end
9211
9212 local is_sequence = false
9213 if #d > 0 and #d == #keys then
9214 for i=1, #d do
9215 if d[i] == nil then
9216 goto not_a_sequence
9217 end
9218 end
9219 is_sequence = true
9220 end
9221 ::not_a_sequence::
9222
9223 if is_sequence then
9224 table.insert(buf,
9225 "\\markdownRendererJekyllDataSequenceBegin{")
9226 table.insert(buf, self.identifier(p or "null"))
9227 table.insert(buf, "}{")
9228 table.insert(buf, #keys)
9229 table.insert(buf, "}")
9230 else
9231 table.insert(buf, "\\markdownRendererJekyllDataMappingBegin{")
9232 table.insert(buf, self.identifier(p or "null"))
9233 table.insert(buf, "}{")
9234 table.insert(buf, #keys)
9235 table.insert(buf, "}")
9236 end
9237
9238 for _, k in ipairs(keys) do
9239 local v = d[k]
9240 local typ = type(v)
9241 k = tostring(k or "null")
9242 if typ == "table" and next(v) ~= nil then
9243 table.insert(
9244 buf,
9245 self.jekyllData(v, t, k)
9246 )
9247 else
9248 k = self.identifier(k)
9249 v = tostring(v)
9250 if typ == "boolean" then
9251 table.insert(buf, "\\markdownRendererJekyllDataBoolean{")
9252 table.insert(buf, k)
9253 table.insert(buf, "}{")
9254 table.insert(buf, v)
9255 table.insert(buf, "}")
9256 elseif typ == "number" then
9257 table.insert(buf, "\\markdownRendererJekyllDataNumber{")
9258 table.insert(buf, k)
9259 table.insert(buf, "}{")
9260 table.insert(buf, v)
9261 table.insert(buf, "}")
9262 elseif typ == "string" then
9263 table.insert(buf,
9264 "\\markdownRendererJekyllDataProgrammaticString{")
9265 table.insert(buf, k)
9266 table.insert(buf, "}{")
9267 table.insert(buf, self.identifier(v))
9268 table.insert(buf, "}")
9269 table.insert(buf,
9270 "\\markdownRendererJekyllDataTypographicString{")
9271 table.insert(buf, k)
9272 table.insert(buf, "}{")
9273 table.insert(buf, t(v))
9274 table.insert(buf, "}")
9275 elseif typ == "table" then
9276 table.insert(buf, "\\markdownRendererJekyllDataEmpty{")
9277 table.insert(buf, k)
9278 table.insert(buf, "}")
9279 else
9280 local error = self.error(format(
9281 "Unexpected type %s for value of "
9282 .. "YAML key %s.", typ, k))
9283 table.insert(buf, error)
9284 end
9285 end
9286 end
9287
9288 if is_sequence then
9289 table.insert(buf, "\\markdownRendererJekyllDataSequenceEnd")
9290 else
9291 table.insert(buf, "\\markdownRendererJekyllDataMappingEnd")
9292 end
9293
9294 if not p then
9295 table.insert(buf, "\\markdownRendererJekyllDataEnd")
9296 end
9297
9298 return buf
9299 end
9300 end, extend_reader = function(self)
9301 local parsers = self.parsers
9302 local writer = self.writer
9303
9304 local JekyllData
9305 = Cmt( C((parsers.line - P("---") - P("..."))^0)
9306 , function(s, i, text) -- luacheck: ignore s i
9307 local data
9308 local ran_ok, _ = pcall(function()
9309 local tinyyaml = require("tinyyaml")
9310 data = tinyyaml.parse(text, {timestamps=false})
9311 end)
9312 if ran_ok and data ~= nil then
9313 return true, writer.jekyllData(data, function(s)
9314 return self.parser_functions.parse_blocks_nested(s)
9315 end, nil)
9316 else
9317 return false
9318 end
9319 end
9320 )
9321
9322 local UnexpectedJekyllData
9323 = P("---")
9324 * parsers.blankline / 0
9325 -- if followed by blank, it's thematic break
9326 * #(-parsers.blankline)
9327 * JekyllData
9328 * (P("---") + P("..."))
9329
9330 local ExpectedJekyllData
9331 = ( P("---")
9332 * parsers.blankline / 0
9333 -- if followed by blank, it's thematic break
9334 * #(-parsers.blankline)
9335 )^-1
9336 * JekyllData
9337 * (P("---") + P("..."))^-1
9338
9339 if ensure_jekyll_data then
9340 ExpectedJekyllData = ExpectedJekyllData
9341 * parsers.eof
9342 else
9343 ExpectedJekyllData = ( ExpectedJekyllData
9344 * (V("Blank")^0 / writer.interblocksep)
9345 )^-1
9346 end
9347
9348 self.insert_pattern("Block before Blockquote",
9349 UnexpectedJekyllData, "UnexpectedJekyllData")
9350 if expect_jekyll_data then
9351 self.update_rule("ExpectedJekyllData", ExpectedJekyllData)
9352 end
9353 end
9354 }
9355 end
9356 function M.new(options)
9357 options = options or {}
9358 setmetatable(options, { __index = function (_, key)
9359 return defaultOptions[key] end })
9360 if options.experimental then
9361 setmetatable(options, { __index = function (_, key)
9362 return experimentalOptions[key] end })
9363 end
9364 if options.singletonCache and singletonCache.convert then
9365 for k, v in pairs(defaultOptions) do
9366 if type(v) == "table" then
9367 for i = 1, math.max(#singletonCache.options[k], #options[k]) do
9368 if singletonCache.options[k][i] ~= options[k][i] then
9369 goto miss
9370 end
9371 end
9372 elseif k ~= "cacheDir"
9373 and singletonCache.options[k] ~= options[k] then
9374 goto miss
9375 end
9376 end
9377 return singletonCache.convert
9378 end
9379 ::miss::
9380 local extensions = {}
9381
9382 if #options.acronyms > 0 then
9383 local acronyms_extension = M.extensions.acronyms(options.acronyms)
9384 table.insert(extensions, acronyms_extension)
9385 end
9386
9387 if options.bracketedSpans then
9388 local bracketed_spans_extension = M.extensions.bracketed_spans()
9389 table.insert(extensions, bracketed_spans_extension)
9390 end
9391
9392 if options.contentBlocks then
9393 local content_blocks_extension = M.extensions.content_blocks(
9394 options.contentBlocksLanguageMap)
9395 table.insert(extensions, content_blocks_extension)
9396 end
9397
9398 if options.definitionLists then
9399 local definition_lists_extension = M.extensions.definition_lists(
9400 options.tightLists)
9401 table.insert(extensions, definition_lists_extension)
9402 end
9403
9404 if options.fencedCode then
9405 local fenced_code_extension = M.extensions.fenced_code(
9406 options.blankBeforeCodeFence,
9407 options.fencedCodeAttributes,
9408 options.rawAttribute)
9409 table.insert(extensions, fenced_code_extension)
9410 end
9411
9412 if options.fencedDivs then
9413 local fenced_div_extension = M.extensions.fenced_divs(
9414 options.blankBeforeDivFence)
9415 table.insert(extensions, fenced_div_extension)
9416 end
9417
9418 if options.headerAttributes then
9419 local header_attributes_extension = M.extensions.header_attributes()
9420 table.insert(extensions, header_attributes_extension)
9421 end
9422
9423 if options.inlineCodeAttributes then
9424 local inline_code_attributes_extension =
9425 M.extensions.inline_code_attributes()
9426 table.insert(extensions, inline_code_attributes_extension)
9427 end
9428
9429 if options.jekyllData then
9430 local jekyll_data_extension = M.extensions.jekyll_data(
9431 options.expectJekyllData, options.ensureJekyllData)
9432 table.insert(extensions, jekyll_data_extension)
9433 end
9434
9435 if options.linkAttributes then
9436 local link_attributes_extension =
9437 M.extensions.link_attributes()
9438 table.insert(extensions, link_attributes_extension)
9439 end
9440
9441 if options.lineBlocks then
9442 local line_block_extension = M.extensions.line_blocks()
9443 table.insert(extensions, line_block_extension)
9444 end
9445
9446 if options.mark then
9447 local mark_extension = M.extensions.mark()
9448 table.insert(extensions, mark_extension)
9449 end
9450
9451 if options.pipeTables then
9452 local pipe_tables_extension = M.extensions.pipe_tables(
9453 options.tableCaptions, options.tableAttributes)
9454 table.insert(extensions, pipe_tables_extension)
9455 end
9456
9457 if options.rawAttribute then
9458 local raw_inline_extension = M.extensions.raw_inline()
9459 table.insert(extensions, raw_inline_extension)
9460 end
9461
9462 if options.strikeThrough then
9463 local strike_through_extension = M.extensions.strike_through()
9464 table.insert(extensions, strike_through_extension)
9465 end
9466
9467 if options.subscripts then
9468 local subscript_extension = M.extensions.subscripts()
9469 table.insert(extensions, subscript_extension)
9470 end
9471
9472 if options.superscripts then
9473 local superscript_extension = M.extensions.superscripts()
9474 table.insert(extensions, superscript_extension)
9475 end
9476
9477 if options.texMathDollars or
9478 options.texMathSingleBackslash or
9479 options.texMathDoubleBackslash then
9480 local tex_math_extension = M.extensions.tex_math(
9481 options.texMathDollars,
9482 options.texMathSingleBackslash,
9483 options.texMathDoubleBackslash)
9484 table.insert(extensions, tex_math_extension)
9485 end
9486
9487 if options.notes or options.inlineNotes then
9488 local notes_extension = M.extensions.notes(
9489 options.notes, options.inlineNotes)
9490 table.insert(extensions, notes_extension)
9491 end
9492
9493 if options.citations then
9494 local citations_extension
9495 = M.extensions.citations(options.citationNbsps)
9496 table.insert(extensions, citations_extension)
9497 end
9498
9499 if options.fancyLists then
9500 local fancy_lists_extension = M.extensions.fancy_lists()
9501 table.insert(extensions, fancy_lists_extension)
9502 end
9503 for _, user_extension_filename in ipairs(options.extensions) do
9504 local user_extension = (function(filename)
9505 local pathname = assert(util.find_file(filename),
9506 [[Could not locate user-defined syntax extension "]]
9507 .. filename)
9508 local input_file = assert(io.open(pathname, "r"),
9509 [[Could not open user-defined syntax extension "]]
9510 .. pathname .. [[" for reading]])
9511 local input = assert(input_file:read("*a"))
9512 assert(input_file:close())
9513 local user_extension, err = load([[
9514 local sandbox = {}
9515 setmetatable(sandbox, {__index = _G})
9516 _ENV = sandbox
9517 ]] .. input)()
9518 assert(user_extension,
9519 [[Failed to compile user-defined syntax extension "]]
9520 .. pathname .. [[": ]] .. (err or [[]]))
9521 assert(user_extension.api_version ~= nil,
9522 [[User-defined syntax extension "]] .. pathname
9523 .. [[" does not specify mandatory field "api_version"]])
9524 assert(type(user_extension.api_version) == "number",
9525 [[User-defined syntax extension "]] .. pathname
9526 .. [[" specifies field "api_version" of type "]]
9527 .. type(user_extension.api_version)
9528 .. [[" but "number" was expected]])
9529 assert(user_extension.api_version > 0
9530 and user_extension.api_version
9531 <= metadata.user_extension_api_version,
9532 [[User-defined syntax extension "]] .. pathname
9533 .. [[" uses syntax extension API version "]]
9534 .. user_extension.api_version .. [[ but markdown.lua ]]
9535 .. metadata.version .. [[ uses API version ]]
9536 .. metadata.user_extension_api_version
9537 .. [[, which is incompatible]])
9538
9539 assert(user_extension.grammar_version ~= nil,
9540 [[User-defined syntax extension "]] .. pathname
9541 .. [[" does not specify mandatory field "grammar_version"]])
9542 assert(type(user_extension.grammar_version) == "number",
9543 [[User-defined syntax extension "]] .. pathname
9544 .. [[" specifies field "grammar_version" of type "]]
9545 .. type(user_extension.grammar_version)
9546 .. [[" but "number" was expected]])
9547 assert(user_extension.grammar_version == metadata.grammar_version,
9548 [[User-defined syntax extension "]] .. pathname
9549 .. [[" uses grammar version "]]
9550 .. user_extension.grammar_version
9551 .. [[ but markdown.lua ]] .. metadata.version
9552 .. [[ uses grammar version ]] .. metadata.grammar_version
9553 .. [[, which is incompatible]])
9554
9555 assert(user_extension.finalize_grammar ~= nil,
9556 [[User-defined syntax extension "]] .. pathname
9557 .. [[" does not specify mandatory "finalize_grammar" field]])
9558 assert(type(user_extension.finalize_grammar) == "function",
9559 [[User-defined syntax extension "]] .. pathname
9560 .. [[" specifies field "finalize_grammar" of type "]]
9561 .. type(user_extension.finalize_grammar)
9562 .. [[" but "function" was expected]])
9563 local extension = {
9564 name = [[user-defined "]] .. pathname .. [[" syntax extension]],
9565 extend_reader = user_extension.finalize_grammar,
9566 extend_writer = function() end,
9567 }
9568 return extension
9569 end)(user_extension_filename)
9570 table.insert(extensions, user_extension)
9571 end
9572 local writer = M.writer.new(options)
9573 local reader = M.reader.new(writer, options)
9574 local convert = reader.finalize_grammar(extensions)
9575 collectgarbage("collect")
9576 if options.singletonCache then
9577 local singletonCacheOptions = {}
9578 for k, v in pairs(options) do
9579 singletonCacheOptions[k] = v
9580 end
9581 setmetatable(singletonCacheOptions,
9582 { __index = function (_, key)
9583 return defaultOptions[key] end })
9584 singletonCache.options = singletonCacheOptions
9585 singletonCache.convert = convert
9586 end
9587 return convert
9588 end
9589 return M