From 71e05375622cdfd285103715256444bdfe56f1c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Novotn=C3=BD?= Date: Mon, 27 Jun 2022 15:15:11 +0200 Subject: [PATCH 01/12] Move `parsers` upvalue and `larsers` local to `reader->parsers` --- markdown.dtx | 389 +++++++++++++++++++++++++++------------------------ 1 file changed, 203 insertions(+), 186 deletions(-) diff --git a/markdown.dtx b/markdown.dtx index 721ec0252..5fbf0368f 100644 --- a/markdown.dtx +++ b/markdown.dtx @@ -20008,7 +20008,34 @@ end M.reader = {} function M.reader.new(writer, options) local self = {} - options = options or {} +% \end{macrocode} +% \par +% \begin{markdown} +% +% Create a \luamdef{reader->parsers} hash table that stores \acro{peg} patterns +% that depend on the received `options`. Make \luamdef{reader->parsers} inherit +% from the global \luamref{parsers} table. +% +% \end{markdown} +% \begin{macrocode} + self.parsers = {} + (function(parsers) + setmetatable(self.parsers, { + __index = function (_, key) + return parsers[key] + end + }) + end)(parsers) +% \end{macrocode} +% \begin{markdown} +% +% Make \luamref{reader->parsers} available as a local `parsers` variable that +% will shadow the global \luamref{parsers} table and will make +% \luamref{reader->parsers} easier to type in the rest of the reader code. +% +% \end{markdown} +% \begin{macrocode} + local parsers = self.parsers % \end{macrocode} % \par % \begin{markdown} @@ -20017,6 +20044,7 @@ function M.reader.new(writer, options) % % \end{markdown} % \begin{macrocode} + options = options or {} setmetatable(options, { __index = function (_, key) return defaultOptions[key] end }) % \end{macrocode} @@ -20074,17 +20102,6 @@ function M.reader.new(writer, options) % \par % \begin{markdown} % -% The \luamdef{larsers} (as in ``local \luamref{parsers}'') hash table stores -% \acro{peg} patterns that depend on the received `options`, which impedes -% their reuse between different \luamref{reader} objects. -% -% \end{markdown} -% \begin{macrocode} - local larsers = {} -% \end{macrocode} -% \par -% \begin{markdown} -% %#### Top-Level Parser Functions % % \end{markdown} @@ -20146,43 +20163,43 @@ function M.reader.new(writer, options) local parse_blocks = create_parser("parse_blocks", function() - return larsers.blocks + return parsers.blocks end, true) local parse_blocks_nested = create_parser("parse_blocks_nested", function() - return larsers.blocks_nested + return parsers.blocks_nested end, false) local parse_inlines = create_parser("parse_inlines", function() - return larsers.inlines + return parsers.inlines end, false) local parse_inlines_no_link = create_parser("parse_inlines_no_link", function() - return larsers.inlines_no_link + return parsers.inlines_no_link end, false) local parse_inlines_no_inline_note = create_parser("parse_inlines_no_inline_note", function() - return larsers.inlines_no_inline_note + return parsers.inlines_no_inline_note end, false) local parse_inlines_no_html = create_parser("parse_inlines_no_html", function() - return larsers.inlines_no_html + return parsers.inlines_no_html end, false) local parse_inlines_nbsp = create_parser("parse_inlines_nbsp", function() - return larsers.inlines_nbsp + return parsers.inlines_nbsp end, false) % \end{macrocode} % \par @@ -20193,22 +20210,22 @@ function M.reader.new(writer, options) % \end{markdown} % \begin{macrocode} if options.hashEnumerators then - larsers.dig = parsers.digit + parsers.hash + parsers.dig = parsers.digit + parsers.hash else - larsers.dig = parsers.digit + parsers.dig = parsers.digit end - larsers.enumerator = C(larsers.dig^3 * parsers.period) * #parsers.spacing - + C(larsers.dig^2 * parsers.period) * #parsers.spacing + parsers.enumerator = C(parsers.dig^3 * parsers.period) * #parsers.spacing + + C(parsers.dig^2 * parsers.period) * #parsers.spacing * (parsers.tab + parsers.space^1) - + C(larsers.dig * parsers.period) * #parsers.spacing + + C(parsers.dig * parsers.period) * #parsers.spacing * (parsers.tab + parsers.space^-2) - + parsers.space * C(larsers.dig^2 * parsers.period) + + parsers.space * C(parsers.dig^2 * parsers.period) * #parsers.spacing - + parsers.space * C(larsers.dig * parsers.period) + + parsers.space * C(parsers.dig * parsers.period) * #parsers.spacing * (parsers.tab + parsers.space^-1) - + parsers.space * parsers.space * C(larsers.dig^1 + + parsers.space * parsers.space * C(parsers.dig^1 * parsers.period) * #parsers.spacing % \end{macrocode} % \par @@ -20219,14 +20236,14 @@ function M.reader.new(writer, options) % \end{markdown} % \begin{macrocode} -- strip off leading > and indents, and run through blocks - larsers.blockquote_body = ((parsers.leader * parsers.more * parsers.space^-1)/"" + parsers.blockquote_body = ((parsers.leader * parsers.more * parsers.space^-1)/"" * parsers.linechar^0 * parsers.newline)^1 * (-(parsers.leader * parsers.more + parsers.blankline) * parsers.linechar^1 * parsers.newline)^0 if not options.breakableBlockquotes then - larsers.blockquote_body = larsers.blockquote_body + parsers.blockquote_body = parsers.blockquote_body * (parsers.blankline^0 / "") end % \end{macrocode} @@ -20237,7 +20254,7 @@ function M.reader.new(writer, options) % % \end{markdown} % \begin{macrocode} - larsers.citations = function(text_cites, raw_cites) + parsers.citations = function(text_cites, raw_cites) local function normalize(str) if str == "" then str = nil @@ -20286,14 +20303,14 @@ function M.reader.new(writer, options) return "" end - larsers.NoteRef = parsers.RawNoteRef / lookup_note + parsers.NoteRef = parsers.RawNoteRef / lookup_note - larsers.NoteBlock = parsers.leader * parsers.RawNoteRef * parsers.colon + parsers.NoteBlock = parsers.leader * parsers.RawNoteRef * parsers.colon * parsers.spnl * parsers.indented_blocks(parsers.chunk) / register_note - larsers.InlineNote = parsers.circumflex + parsers.InlineNote = parsers.circumflex * (parsers.tag / parse_inlines_no_inline_note) -- no notes inside notes / writer.note % \end{macrocode} @@ -20304,7 +20321,7 @@ function M.reader.new(writer, options) % % \end{markdown} % \begin{macrocode} -larsers.table_row = pipe_table_row(true +parsers.table_row = pipe_table_row(true , (C((parsers.linechar - parsers.pipe)^1) / parse_inlines) , parsers.pipe @@ -20312,19 +20329,19 @@ larsers.table_row = pipe_table_row(true / parse_inlines)) if options.tableCaptions then - larsers.table_caption = #parsers.table_caption_beginning + parsers.table_caption = #parsers.table_caption_beginning * parsers.table_caption_beginning * Ct(parsers.IndentedInline^1) * parsers.newline else - larsers.table_caption = parsers.fail + parsers.table_caption = parsers.fail end -larsers.PipeTable = Ct(larsers.table_row * parsers.newline +parsers.PipeTable = Ct(parsers.table_row * parsers.newline * parsers.table_hline - * (parsers.newline * larsers.table_row)^0) + * (parsers.newline * parsers.table_row)^0) / make_pipe_table_rectangular - * larsers.table_caption^-1 + * parsers.table_caption^-1 / writer.table % \end{macrocode} % \par @@ -20400,56 +20417,56 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline % % \end{markdown} % \begin{macrocode} - larsers.Str = (parsers.normalchar * (parsers.normalchar + parsers.at)^0) + parsers.Str = (parsers.normalchar * (parsers.normalchar + parsers.at)^0) / writer.string - larsers.Symbol = (parsers.specialchar - parsers.tightblocksep) + parsers.Symbol = (parsers.specialchar - parsers.tightblocksep) / writer.string - larsers.Ellipsis = P("...") / writer.ellipsis + parsers.Ellipsis = P("...") / writer.ellipsis - larsers.Smart = larsers.Ellipsis + parsers.Smart = parsers.Ellipsis - larsers.Code = parsers.inticks / writer.code + parsers.Code = parsers.inticks / writer.code if options.blankBeforeBlockquote then - larsers.bqstart = parsers.fail + parsers.bqstart = parsers.fail else - larsers.bqstart = parsers.more + parsers.bqstart = parsers.more end if options.blankBeforeHeading then - larsers.headerstart = parsers.fail + parsers.headerstart = parsers.fail else - larsers.headerstart = parsers.hash + parsers.headerstart = parsers.hash + (parsers.line * (parsers.equal^1 + parsers.dash^1) * parsers.optionalspace * parsers.newline) end if not options.fencedCode or options.blankBeforeCodeFence then - larsers.fencestart = parsers.fail + parsers.fencestart = parsers.fail else - larsers.fencestart = parsers.fencehead(parsers.backtick) + parsers.fencestart = parsers.fencehead(parsers.backtick) + parsers.fencehead(parsers.tilde) end - larsers.Endline = parsers.newline * -( -- newline, but not before... + parsers.Endline = parsers.newline * -( -- newline, but not before... parsers.blankline -- paragraph break + parsers.tightblocksep -- nested list + parsers.eof -- end of document - + larsers.bqstart - + larsers.headerstart - + larsers.fencestart + + parsers.bqstart + + parsers.headerstart + + parsers.fencestart ) * parsers.spacechar^0 / (options.hardLineBreaks and writer.linebreak or writer.space) - larsers.OptionalIndent + parsers.OptionalIndent = parsers.spacechar^1 / writer.space - larsers.Space = parsers.spacechar^2 * larsers.Endline / writer.linebreak - + parsers.spacechar^1 * larsers.Endline^-1 * parsers.eof / "" - + parsers.spacechar^1 * larsers.Endline + parsers.Space = parsers.spacechar^2 * parsers.Endline / writer.linebreak + + parsers.spacechar^1 * parsers.Endline^-1 * parsers.eof / "" + + parsers.spacechar^1 * parsers.Endline * parsers.optionalspace / (options.hardLineBreaks and writer.linebreak @@ -20457,22 +20474,22 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline + parsers.spacechar^1 * parsers.optionalspace / writer.space - larsers.NonbreakingEndline + parsers.NonbreakingEndline = parsers.newline * -( -- newline, but not before... parsers.blankline -- paragraph break + parsers.tightblocksep -- nested list + parsers.eof -- end of document - + larsers.bqstart - + larsers.headerstart - + larsers.fencestart + + parsers.bqstart + + parsers.headerstart + + parsers.fencestart ) * parsers.spacechar^0 / (options.hardLineBreaks and writer.linebreak or writer.nbsp) - larsers.NonbreakingSpace - = parsers.spacechar^2 * larsers.Endline / writer.linebreak - + parsers.spacechar^1 * larsers.Endline^-1 * parsers.eof / "" - + parsers.spacechar^1 * larsers.Endline + parsers.NonbreakingSpace + = parsers.spacechar^2 * parsers.Endline / writer.linebreak + + parsers.spacechar^1 * parsers.Endline^-1 * parsers.eof / "" + + parsers.spacechar^1 * parsers.Endline * parsers.optionalspace / (options.hardLineBreaks and writer.linebreak @@ -20481,35 +20498,35 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline / writer.nbsp if options.underscores then - larsers.Strong = ( parsers.between(parsers.Inline, parsers.doubleasterisks, + parsers.Strong = ( parsers.between(parsers.Inline, parsers.doubleasterisks, parsers.doubleasterisks) + parsers.between(parsers.Inline, parsers.doubleunderscores, parsers.doubleunderscores) ) / writer.strong - larsers.Emph = ( parsers.between(parsers.Inline, parsers.asterisk, + parsers.Emph = ( parsers.between(parsers.Inline, parsers.asterisk, parsers.asterisk) + parsers.between(parsers.Inline, parsers.underscore, parsers.underscore) ) / writer.emphasis else - larsers.Strong = ( parsers.between(parsers.Inline, parsers.doubleasterisks, + parsers.Strong = ( parsers.between(parsers.Inline, parsers.doubleasterisks, parsers.doubleasterisks) ) / writer.strong - larsers.Emph = ( parsers.between(parsers.Inline, parsers.asterisk, + parsers.Emph = ( parsers.between(parsers.Inline, parsers.asterisk, parsers.asterisk) ) / writer.emphasis end - larsers.AutoLinkUrl = parsers.less + parsers.AutoLinkUrl = parsers.less * C(parsers.alphanumeric^1 * P("://") * parsers.urlchar^1) * parsers.more / function(url) return writer.link(writer.escape(url), url) end - larsers.AutoLinkEmail = parsers.less + parsers.AutoLinkEmail = parsers.less * C((parsers.alphanumeric + S("-._+"))^1 * P("@") * parsers.urlchar^1) * parsers.more @@ -20518,7 +20535,7 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline "mailto:"..email) end - larsers.AutoLinkRelativeReference + parsers.AutoLinkRelativeReference = parsers.less * C(parsers.urlchar^1) * parsers.more @@ -20526,7 +20543,7 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline return writer.link(writer.escape(url), url) end - larsers.DirectLink = (parsers.tag / parse_inlines_no_link) -- no links inside links + parsers.DirectLink = (parsers.tag / parse_inlines_no_link) -- no links inside links * parsers.spnl * parsers.lparent * (parsers.url + Cc("")) -- link can be empty [foo]() @@ -20534,13 +20551,13 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline * parsers.rparent / writer.link - larsers.IndirectLink = parsers.tag * (C(parsers.spnl) * parsers.tag)^-1 + parsers.IndirectLink = parsers.tag * (C(parsers.spnl) * parsers.tag)^-1 / indirect_link -- parse a link or image (direct or indirect) - larsers.Link = larsers.DirectLink + larsers.IndirectLink + parsers.Link = parsers.DirectLink + parsers.IndirectLink - larsers.DirectImage = parsers.exclamation + parsers.DirectImage = parsers.exclamation * (parsers.tag / parse_inlines) * parsers.spnl * parsers.lparent @@ -20549,12 +20566,12 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline * parsers.rparent / writer.image - larsers.IndirectImage = parsers.exclamation * parsers.tag + parsers.IndirectImage = parsers.exclamation * parsers.tag * (C(parsers.spnl) * parsers.tag)^-1 / indirect_image - larsers.Image = larsers.DirectImage + larsers.IndirectImage + parsers.Image = parsers.DirectImage + parsers.IndirectImage - larsers.TextCitations = Ct((parsers.spnl + parsers.TextCitations = Ct((parsers.spnl * Cc("") * parsers.citation_name * ((parsers.spnl @@ -20562,34 +20579,34 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline * parsers.citation_headless_body * parsers.rbracket) + Cc("")))^1) / function(raw_cites) - return larsers.citations(true, raw_cites) + return parsers.citations(true, raw_cites) end - larsers.ParenthesizedCitations + parsers.ParenthesizedCitations = Ct((parsers.spnl * parsers.lbracket * parsers.citation_body * parsers.rbracket)^1) / function(raw_cites) - return larsers.citations(false, raw_cites) + return parsers.citations(false, raw_cites) end - larsers.Citations = larsers.TextCitations + larsers.ParenthesizedCitations + parsers.Citations = parsers.TextCitations + parsers.ParenthesizedCitations -- avoid parsing long strings of * or _ as emph/strong - larsers.UlOrStarLine = parsers.asterisk^4 + parsers.underscore^4 + parsers.UlOrStarLine = parsers.asterisk^4 + parsers.underscore^4 / writer.string - larsers.EscapedChar = parsers.backslash * C(parsers.escapable) / writer.string + parsers.EscapedChar = parsers.backslash * C(parsers.escapable) / writer.string - larsers.InlineHtml = parsers.emptyelt_any / writer.inline_html_tag + parsers.InlineHtml = parsers.emptyelt_any / writer.inline_html_tag + (parsers.htmlcomment / parse_inlines_no_html) / writer.inline_html_comment + parsers.htmlinstruction + parsers.openelt_any / writer.inline_html_tag + parsers.closeelt_any / writer.inline_html_tag - larsers.HtmlEntity = parsers.hexentity / entities.hex_entity / writer.string + parsers.HtmlEntity = parsers.hexentity / entities.hex_entity / writer.string + parsers.decentity / entities.dec_entity / writer.string + parsers.tagentity / entities.char_entity / writer.string % \end{macrocode} @@ -20600,30 +20617,30 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline % % \end{markdown} % \begin{macrocode} - larsers.ContentBlock = parsers.leader + parsers.ContentBlock = parsers.leader * (parsers.localfilepath + parsers.onlineimageurl) * parsers.contentblock_tail / writer.contentblock - larsers.DisplayHtml = (parsers.htmlcomment / parse_blocks_nested) + parsers.DisplayHtml = (parsers.htmlcomment / parse_blocks_nested) / writer.block_html_comment + parsers.emptyelt_block / writer.block_html_element + parsers.openelt_exact("hr") / writer.block_html_element + parsers.in_matched_block_tags / writer.block_html_element + parsers.htmlinstruction - larsers.Verbatim = Cs( (parsers.blanklines + parsers.Verbatim = Cs( (parsers.blanklines * ((parsers.indentedline - parsers.blankline))^1)^1 ) / expandtabs / writer.verbatim - larsers.FencedCode = (parsers.TildeFencedCode + parsers.FencedCode = (parsers.TildeFencedCode + parsers.BacktickFencedCode) / function(infostring, code) return writer.fencedCode(writer.string(infostring), expandtabs(code)) end - larsers.JekyllData = Cmt( C((parsers.line - P("---") - P("..."))^0) + parsers.JekyllData = Cmt( C((parsers.line - P("---") - P("..."))^0) , function(s, i, text) local data local ran_ok, error = pcall(function() @@ -20640,32 +20657,32 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline end ) - larsers.UnexpectedJekyllData + parsers.UnexpectedJekyllData = P("---") * parsers.blankline / 0 * #(-parsers.blankline) -- if followed by blank, it's an hrule - * larsers.JekyllData + * parsers.JekyllData * (P("---") + P("...")) - larsers.ExpectedJekyllData + parsers.ExpectedJekyllData = ( P("---") * parsers.blankline / 0 * #(-parsers.blankline) -- if followed by blank, it's an hrule )^-1 - * larsers.JekyllData + * parsers.JekyllData * (P("---") + P("..."))^-1 - larsers.Blockquote = Cs(larsers.blockquote_body^1) + parsers.Blockquote = Cs(parsers.blockquote_body^1) / parse_blocks_nested / writer.blockquote - larsers.HorizontalRule = ( parsers.lineof(parsers.asterisk) + parsers.HorizontalRule = ( parsers.lineof(parsers.asterisk) + parsers.lineof(parsers.dash) + parsers.lineof(parsers.underscore) ) / writer.hrule - larsers.Reference = parsers.define_reference_parser / register_link + parsers.Reference = parsers.define_reference_parser / register_link - larsers.Paragraph = parsers.nonindentspace * Ct(parsers.Inline^1) + parsers.Paragraph = parsers.nonindentspace * Ct(parsers.Inline^1) * ( parsers.newline * ( parsers.blankline^1 + #parsers.hash @@ -20675,7 +20692,7 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline + parsers.eof ) / writer.paragraph - larsers.Plain = parsers.nonindentspace * Ct(parsers.Inline^1) + parsers.Plain = parsers.nonindentspace * Ct(parsers.Inline^1) / writer.plain % \end{macrocode} % \par @@ -20685,50 +20702,50 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline % % \end{markdown} % \begin{macrocode} - larsers.starter = parsers.bullet + larsers.enumerator + parsers.starter = parsers.bullet + parsers.enumerator if options.taskLists then - larsers.tickbox = ( parsers.ticked_box + parsers.tickbox = ( parsers.ticked_box + parsers.halfticked_box + parsers.unticked_box ) / writer.tickbox else - larsers.tickbox = parsers.fail + parsers.tickbox = parsers.fail end -- we use \001 as a separator between a tight list item and a -- nested list under it. - larsers.NestedList = Cs((parsers.optionallyindentedline - - larsers.starter)^1) + parsers.NestedList = Cs((parsers.optionallyindentedline + - parsers.starter)^1) / function(a) return "\001"..a end - larsers.ListBlockLine = parsers.optionallyindentedline + parsers.ListBlockLine = parsers.optionallyindentedline - parsers.blankline - (parsers.indent^-1 - * larsers.starter) + * parsers.starter) - larsers.ListBlock = parsers.line * larsers.ListBlockLine^0 + parsers.ListBlock = parsers.line * parsers.ListBlockLine^0 - larsers.ListContinuationBlock = parsers.blanklines * (parsers.indent / "") - * larsers.ListBlock + parsers.ListContinuationBlock = parsers.blanklines * (parsers.indent / "") + * parsers.ListBlock - larsers.TightListItem = function(starter) - return -larsers.HorizontalRule - * (Cs(starter / "" * larsers.tickbox^-1 * larsers.ListBlock * larsers.NestedList^-1) + parsers.TightListItem = function(starter) + return -parsers.HorizontalRule + * (Cs(starter / "" * parsers.tickbox^-1 * parsers.ListBlock * parsers.NestedList^-1) / parse_blocks_nested) * -(parsers.blanklines * parsers.indent) end - larsers.LooseListItem = function(starter) - return -larsers.HorizontalRule - * Cs( starter / "" * larsers.tickbox^-1 * larsers.ListBlock * Cc("\n") - * (larsers.NestedList + larsers.ListContinuationBlock^0) + parsers.LooseListItem = function(starter) + return -parsers.HorizontalRule + * Cs( starter / "" * parsers.tickbox^-1 * parsers.ListBlock * Cc("\n") + * (parsers.NestedList + parsers.ListContinuationBlock^0) * (parsers.blanklines / "\n\n") ) / parse_blocks_nested end - larsers.BulletList = ( Ct(larsers.TightListItem(parsers.bullet)^1) * Cc(true) + parsers.BulletList = ( Ct(parsers.TightListItem(parsers.bullet)^1) * Cc(true) * parsers.skipblanklines * -parsers.bullet - + Ct(larsers.LooseListItem(parsers.bullet)^1) * Cc(false) + + Ct(parsers.LooseListItem(parsers.bullet)^1) * Cc(false) * parsers.skipblanklines ) / writer.bulletlist @@ -20744,12 +20761,12 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline return writer.orderedlist(items,tight,startNumber) end - larsers.OrderedList = Cg(larsers.enumerator, "listtype") * - ( Ct(larsers.TightListItem(Cb("listtype")) - * larsers.TightListItem(larsers.enumerator)^0) - * Cc(true) * parsers.skipblanklines * -larsers.enumerator - + Ct(larsers.LooseListItem(Cb("listtype")) - * larsers.LooseListItem(larsers.enumerator)^0) + parsers.OrderedList = Cg(parsers.enumerator, "listtype") * + ( Ct(parsers.TightListItem(Cb("listtype")) + * parsers.TightListItem(parsers.enumerator)^0) + * Cc(true) * parsers.skipblanklines * -parsers.enumerator + + Ct(parsers.LooseListItem(Cb("listtype")) + * parsers.LooseListItem(parsers.enumerator)^0) * Cc(false) * parsers.skipblanklines ) * Cb("listtype") / ordered_list @@ -20757,21 +20774,21 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline return { term = parse_inlines(term), definitions = defs } end - larsers.DefinitionListItemLoose = C(parsers.line) * parsers.skipblanklines + parsers.DefinitionListItemLoose = C(parsers.line) * parsers.skipblanklines * Ct((parsers.defstart * parsers.indented_blocks(parsers.dlchunk) / parse_blocks_nested)^1) * Cc(false) / definition_list_item - larsers.DefinitionListItemTight = C(parsers.line) + parsers.DefinitionListItemTight = C(parsers.line) * Ct((parsers.defstart * parsers.dlchunk / parse_blocks_nested)^1) * Cc(true) / definition_list_item - larsers.DefinitionList = ( Ct(larsers.DefinitionListItemLoose^1) * Cc(false) - + Ct(larsers.DefinitionListItemTight^1) + parsers.DefinitionList = ( Ct(parsers.DefinitionListItemLoose^1) * Cc(false) + + Ct(parsers.DefinitionListItemTight^1) * (parsers.skipblanklines - * -larsers.DefinitionListItemLoose * Cc(true)) + * -parsers.DefinitionListItemLoose * Cc(true)) ) / writer.definitionlist % \end{macrocode} % \par @@ -20781,9 +20798,9 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline % % \end{markdown} % \begin{macrocode} - larsers.Blank = parsers.blankline / "" - + larsers.NoteBlock - + larsers.Reference + parsers.Blank = parsers.blankline / "" + + parsers.NoteBlock + + parsers.Reference + (parsers.tightblocksep / "\n") % \end{macrocode} % \par @@ -20795,7 +20812,7 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline % \begin{macrocode} -- parse atx header if options.headerAttributes then - larsers.AtxHeading = Cg(parsers.HeadingStart,"level") + parsers.AtxHeading = Cg(parsers.HeadingStart,"level") * parsers.optionalspace * (C(((parsers.linechar - ((parsers.hash^1 @@ -20819,7 +20836,7 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline * Cb("attributes") / writer.heading - larsers.SetextHeading = #(parsers.line * S("=-")) + parsers.SetextHeading = #(parsers.line * S("=-")) * (C(((parsers.linechar - (parsers.HeadingAttributes * parsers.optionalspace @@ -20837,13 +20854,13 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline * parsers.newline / writer.heading else - larsers.AtxHeading = Cg(parsers.HeadingStart,"level") + parsers.AtxHeading = Cg(parsers.HeadingStart,"level") * parsers.optionalspace * (C(parsers.line) / strip_atx_end / parse_inlines) * Cb("level") / writer.heading - larsers.SetextHeading = #(parsers.line * S("=-")) + parsers.SetextHeading = #(parsers.line * S("=-")) * Ct(parsers.linechar^1 / parse_inlines) * parsers.newline * parsers.HeadingLevel @@ -20852,7 +20869,7 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline / writer.heading end - larsers.Heading = larsers.AtxHeading + larsers.SetextHeading + parsers.Heading = parsers.AtxHeading + parsers.SetextHeading % \end{macrocode} % \par % \begin{markdown} @@ -20873,10 +20890,10 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline * V("Block"))^0 * V("Blank")^0 * parsers.eof, - Blank = larsers.Blank, + Blank = parsers.Blank, - UnexpectedJekyllData = larsers.UnexpectedJekyllData, - ExpectedJekyllData = larsers.ExpectedJekyllData, + UnexpectedJekyllData = parsers.UnexpectedJekyllData, + ExpectedJekyllData = parsers.ExpectedJekyllData, Block = V("ContentBlock") + V("UnexpectedJekyllData") @@ -20893,19 +20910,19 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline + V("Paragraph") + V("Plain"), - ContentBlock = larsers.ContentBlock, - Blockquote = larsers.Blockquote, - Verbatim = larsers.Verbatim, - FencedCode = larsers.FencedCode, - HorizontalRule = larsers.HorizontalRule, - BulletList = larsers.BulletList, - OrderedList = larsers.OrderedList, - Heading = larsers.Heading, - DefinitionList = larsers.DefinitionList, - DisplayHtml = larsers.DisplayHtml, - Paragraph = larsers.Paragraph, - PipeTable = larsers.PipeTable, - Plain = larsers.Plain, + ContentBlock = parsers.ContentBlock, + Blockquote = parsers.Blockquote, + Verbatim = parsers.Verbatim, + FencedCode = parsers.FencedCode, + HorizontalRule = parsers.HorizontalRule, + BulletList = parsers.BulletList, + OrderedList = parsers.OrderedList, + Heading = parsers.Heading, + DefinitionList = parsers.DefinitionList, + DisplayHtml = parsers.DisplayHtml, + Paragraph = parsers.Paragraph, + PipeTable = parsers.PipeTable, + Plain = parsers.Plain, Inline = V("Str") + V("Space") @@ -20949,28 +20966,28 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline + V("Smart") + V("Symbol"), - Str = larsers.Str, - Space = larsers.Space, - OptionalIndent = larsers.OptionalIndent, - Endline = larsers.Endline, - UlOrStarLine = larsers.UlOrStarLine, - Strong = larsers.Strong, - Emph = larsers.Emph, - InlineNote = larsers.InlineNote, - NoteRef = larsers.NoteRef, - Citations = larsers.Citations, - Link = larsers.Link, - Image = larsers.Image, - Code = larsers.Code, - AutoLinkUrl = larsers.AutoLinkUrl, - AutoLinkEmail = larsers.AutoLinkEmail, + Str = parsers.Str, + Space = parsers.Space, + OptionalIndent = parsers.OptionalIndent, + Endline = parsers.Endline, + UlOrStarLine = parsers.UlOrStarLine, + Strong = parsers.Strong, + Emph = parsers.Emph, + InlineNote = parsers.InlineNote, + NoteRef = parsers.NoteRef, + Citations = parsers.Citations, + Link = parsers.Link, + Image = parsers.Image, + Code = parsers.Code, + AutoLinkUrl = parsers.AutoLinkUrl, + AutoLinkEmail = parsers.AutoLinkEmail, AutoLinkRelativeReference - = larsers.AutoLinkRelativeReference, - InlineHtml = larsers.InlineHtml, - HtmlEntity = larsers.HtmlEntity, - EscapedChar = larsers.EscapedChar, - Smart = larsers.Smart, - Symbol = larsers.Symbol, + = parsers.AutoLinkRelativeReference, + InlineHtml = parsers.InlineHtml, + HtmlEntity = parsers.HtmlEntity, + EscapedChar = parsers.EscapedChar, + Smart = parsers.Smart, + Symbol = parsers.Symbol, } if not options.citations then @@ -21033,33 +21050,33 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline local blocks_nested_t = util.table_copy(syntax) blocks_nested_t.ExpectedJekyllData = parsers.fail - larsers.blocks_nested = Ct(blocks_nested_t) + parsers.blocks_nested = Ct(blocks_nested_t) - larsers.blocks = Ct(syntax) + parsers.blocks = Ct(syntax) local inlines_t = util.table_copy(syntax) inlines_t[1] = "Inlines" inlines_t.Inlines = parsers.Inline^0 * (parsers.spacing^0 * parsers.eof / "") - larsers.inlines = Ct(inlines_t) + parsers.inlines = Ct(inlines_t) local inlines_no_link_t = util.table_copy(inlines_t) inlines_no_link_t.Link = parsers.fail - larsers.inlines_no_link = Ct(inlines_no_link_t) + parsers.inlines_no_link = Ct(inlines_no_link_t) local inlines_no_inline_note_t = util.table_copy(inlines_t) inlines_no_inline_note_t.InlineNote = parsers.fail - larsers.inlines_no_inline_note = Ct(inlines_no_inline_note_t) + parsers.inlines_no_inline_note = Ct(inlines_no_inline_note_t) local inlines_no_html_t = util.table_copy(inlines_t) inlines_no_html_t.DisplayHtml = parsers.fail inlines_no_html_t.InlineHtml = parsers.fail inlines_no_html_t.HtmlEntity = parsers.fail - larsers.inlines_no_html = Ct(inlines_no_html_t) + parsers.inlines_no_html = Ct(inlines_no_html_t) local inlines_nbsp_t = util.table_copy(inlines_t) - inlines_nbsp_t.Endline = larsers.NonbreakingEndline - inlines_nbsp_t.Space = larsers.NonbreakingSpace - larsers.inlines_nbsp = Ct(inlines_nbsp_t) + inlines_nbsp_t.Endline = parsers.NonbreakingEndline + inlines_nbsp_t.Space = parsers.NonbreakingSpace + parsers.inlines_nbsp = Ct(inlines_nbsp_t) % \end{macrocode} % \par % \begin{markdown} From bab4cf534471a0a10e29e92328aeaa8b383b9fd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Novotn=C3=BD?= Date: Mon, 27 Jun 2022 15:32:05 +0200 Subject: [PATCH 02/12] Move `syntax` local to `reader->syntax` --- markdown.dtx | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/markdown.dtx b/markdown.dtx index 5fbf0368f..79e63e9be 100644 --- a/markdown.dtx +++ b/markdown.dtx @@ -20876,9 +20876,12 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline % %#### Syntax Specification % +% Create a \luamdef{reader->syntax} hash table that stores the \acro{peg} +% grammar. +% % \end{markdown} % \begin{macrocode} - local syntax = + self.syntax = { "Blocks", Blocks = ( V("ExpectedJekyllData") @@ -20991,45 +20994,45 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline } if not options.citations then - syntax.Citations = parsers.fail + self.syntax.Citations = parsers.fail end if not options.contentBlocks then - syntax.ContentBlock = parsers.fail + self.syntax.ContentBlock = parsers.fail end if not options.codeSpans then - syntax.Code = parsers.fail + self.syntax.Code = parsers.fail end if not options.definitionLists then - syntax.DefinitionList = parsers.fail + self.syntax.DefinitionList = parsers.fail end if not options.fencedCode then - syntax.FencedCode = parsers.fail + self.syntax.FencedCode = parsers.fail end if not options.footnotes then - syntax.NoteRef = parsers.fail + self.syntax.NoteRef = parsers.fail end if not options.html then - syntax.DisplayHtml = parsers.fail - syntax.InlineHtml = parsers.fail - syntax.HtmlEntity = parsers.fail + self.syntax.DisplayHtml = parsers.fail + self.syntax.InlineHtml = parsers.fail + self.syntax.HtmlEntity = parsers.fail end if not options.inlineFootnotes then - syntax.InlineNote = parsers.fail + self.syntax.InlineNote = parsers.fail end if not options.jekyllData then - syntax.UnexpectedJekyllData = parsers.fail + self.syntax.UnexpectedJekyllData = parsers.fail end if not options.jekyllData or not options.expectJekyllData then - syntax.ExpectedJekyllData = parsers.fail + self.syntax.ExpectedJekyllData = parsers.fail end if options.preserveTabs then @@ -21037,24 +21040,24 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline end if not options.pipeTables then - syntax.PipeTable = parsers.fail + self.syntax.PipeTable = parsers.fail end if not options.smartEllipses then - syntax.Smart = parsers.fail + self.syntax.Smart = parsers.fail end if not options.relativeReferences then - syntax.AutoLinkRelativeReference = parsers.fail + self.syntax.AutoLinkRelativeReference = parsers.fail end - local blocks_nested_t = util.table_copy(syntax) + local blocks_nested_t = util.table_copy(self.syntax) blocks_nested_t.ExpectedJekyllData = parsers.fail parsers.blocks_nested = Ct(blocks_nested_t) - parsers.blocks = Ct(syntax) + parsers.blocks = Ct(self.syntax) - local inlines_t = util.table_copy(syntax) + local inlines_t = util.table_copy(self.syntax) inlines_t[1] = "Inlines" inlines_t.Inlines = parsers.Inline^0 * (parsers.spacing^0 * parsers.eof / "") parsers.inlines = Ct(inlines_t) From 201cd7bf85ff9d64c80c500476d8e07f4bd9b9cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Novotn=C3=BD?= Date: Mon, 27 Jun 2022 15:39:01 +0200 Subject: [PATCH 03/12] Move `writer` local to `reader->writer` --- markdown.dtx | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/markdown.dtx b/markdown.dtx index 79e63e9be..0990fbbba 100644 --- a/markdown.dtx +++ b/markdown.dtx @@ -20012,6 +20012,16 @@ function M.reader.new(writer, options) % \par % \begin{markdown} % +% Make the `writer` parameter available as \luamdef{reader->writer}, so that it +% is accessible from extensions. +% +% \end{markdown} +% \begin{macrocode} + self.writer = writer +% \end{macrocode} +% \par +% \begin{markdown} +% % Create a \luamdef{reader->parsers} hash table that stores \acro{peg} patterns % that depend on the received `options`. Make \luamdef{reader->parsers} inherit % from the global \luamref{parsers} table. From 2b1bdbd85189c2e6fd382093fc4ec545ea0de2ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Novotn=C3=BD?= Date: Mon, 27 Jun 2022 16:11:17 +0200 Subject: [PATCH 04/12] Move `parse_*()` locals to `reader->parser_functions.parse_*()` --- markdown.dtx | 151 ++++++++++++++++++++++++++++----------------------- 1 file changed, 83 insertions(+), 68 deletions(-) diff --git a/markdown.dtx b/markdown.dtx index 0990fbbba..3927fa45a 100644 --- a/markdown.dtx +++ b/markdown.dtx @@ -20112,20 +20112,28 @@ function M.reader.new(writer, options) % \par % \begin{markdown} % -%#### Top-Level Parser Functions +%#### High-Level Parser Functions +% +% Create a \luamdef{reader->parser_functions} hash table that stores high-level +% parser functions. Define \luamdef{reader->create_parser} as a function that +% will create a high-level parser function \luamdef{reader->parser_functions.name}, +% that matches input using grammar `grammar`. If `toplevel` is true, the input +% is expected to come straight from the user, not from a recursive call, and +% will be preprocessed. % % \end{markdown} % \begin{macrocode} - local function create_parser(name, grammar, toplevel) - return function(str) + self.parser_functions = {} + self.create_parser = function(name, grammar, toplevel) + self.parser_functions[name] = function(str) % \end{macrocode} % \par % \begin{markdown} % -% If the parser is top-level and the \Opt{stripIndent} Lua option is enabled, -% we will first expand tabs in the input string `str` into spaces and then we -% will count the minimum indent across all lines, skipping blank lines. Next, -% we will remove the minimum indent from all lines. +% If the parser function is top-level and the \Opt{stripIndent} Lua option is +% enabled, we will first expand tabs in the input string `str` into spaces +% and then we will count the minimum indent across all lines, skipping +% blank lines. Next, we will remove the minimum indent from all lines. % % \end{markdown} % \begin{macrocode} @@ -20170,47 +20178,40 @@ function M.reader.new(writer, options) end end - local parse_blocks - = create_parser("parse_blocks", - function() - return parsers.blocks - end, true) + self.create_parser("parse_blocks", + function() + return parsers.blocks + end, true) - local parse_blocks_nested - = create_parser("parse_blocks_nested", - function() - return parsers.blocks_nested - end, false) + self.create_parser("parse_blocks_nested", + function() + return parsers.blocks_nested + end, false) - local parse_inlines - = create_parser("parse_inlines", - function() - return parsers.inlines - end, false) + self.create_parser("parse_inlines", + function() + return parsers.inlines + end, false) - local parse_inlines_no_link - = create_parser("parse_inlines_no_link", - function() - return parsers.inlines_no_link - end, false) + self.create_parser("parse_inlines_no_link", + function() + return parsers.inlines_no_link + end, false) - local parse_inlines_no_inline_note - = create_parser("parse_inlines_no_inline_note", - function() - return parsers.inlines_no_inline_note - end, false) + self.create_parser("parse_inlines_no_inline_note", + function() + return parsers.inlines_no_inline_note + end, false) - local parse_inlines_no_html - = create_parser("parse_inlines_no_html", - function() - return parsers.inlines_no_html - end, false) + self.create_parser("parse_inlines_no_html", + function() + return parsers.inlines_no_html + end, false) - local parse_inlines_nbsp - = create_parser("parse_inlines_nbsp", - function() - return parsers.inlines_nbsp - end, false) + self.create_parser("parse_inlines_nbsp", + function() + return parsers.inlines_nbsp + end, false) % \end{macrocode} % \par % \begin{markdown} @@ -20269,8 +20270,9 @@ function M.reader.new(writer, options) if str == "" then str = nil else - str = (options.citationNbsps and parse_inlines_nbsp or - parse_inlines)(str) + str = (options.citationNbsps and + self.parser_functions.parse_inlines_nbsp or + self.parser_functions.parse_inlines)(str) end return str end @@ -20301,9 +20303,11 @@ function M.reader.new(writer, options) return writer.defer_call(function() local found = rawnotes[normalize_tag(ref)] if found then - return writer.note(parse_blocks_nested(found)) + return writer.note( + self.parser_functions.parse_blocks_nested(found)) else - return {"[", parse_inlines("^" .. ref), "]"} + return {"[", + self.parser_functions.parse_inlines("^" .. ref), "]"} end end) end @@ -20321,7 +20325,7 @@ function M.reader.new(writer, options) / register_note parsers.InlineNote = parsers.circumflex - * (parsers.tag / parse_inlines_no_inline_note) -- no notes inside notes + * (parsers.tag / self.parser_functions.parse_inlines_no_inline_note) / writer.note % \end{macrocode} % \par @@ -20333,10 +20337,10 @@ function M.reader.new(writer, options) % \begin{macrocode} parsers.table_row = pipe_table_row(true , (C((parsers.linechar - parsers.pipe)^1) - / parse_inlines) + / self.parser_functions.parse_inlines) , parsers.pipe , (C((parsers.linechar - parsers.pipe)^0) - / parse_inlines)) + / self.parser_functions.parse_inlines)) if options.tableCaptions then parsers.table_caption = #parsers.table_caption_beginning @@ -20381,7 +20385,9 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline tag = label tagpart = "[]" else - tagpart = {"[", parse_inlines(tag), "]"} + tagpart = {"[", + self.parser_functions.parse_inlines(tag), + "]"} end if sps then tagpart = {sps, tagpart} @@ -20390,7 +20396,9 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline if r then return r else - return nil, {"[", parse_inlines(label), "]", tagpart} + return nil, {"[", + self.parser_functions.parse_inlines(label), + "]", tagpart} end end @@ -20400,7 +20408,9 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline return writer.defer_call(function() local r,fallback = lookup_reference(label,sps,tag) if r then - return writer.link(parse_inlines_no_link(label), r.url, r.title) + return writer.link( + self.parser_functions.parse_inlines_no_link(label), + r.url, r.title) else return fallback end @@ -20553,7 +20563,7 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline return writer.link(writer.escape(url), url) end - parsers.DirectLink = (parsers.tag / parse_inlines_no_link) -- no links inside links + parsers.DirectLink = (parsers.tag / self.parser_functions.parse_inlines_no_link) * parsers.spnl * parsers.lparent * (parsers.url + Cc("")) -- link can be empty [foo]() @@ -20568,7 +20578,7 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline parsers.Link = parsers.DirectLink + parsers.IndirectLink parsers.DirectImage = parsers.exclamation - * (parsers.tag / parse_inlines) + * (parsers.tag / self.parser_functions.parse_inlines) * parsers.spnl * parsers.lparent * (parsers.url + Cc("")) -- link can be empty [foo]() @@ -20610,7 +20620,7 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline parsers.EscapedChar = parsers.backslash * C(parsers.escapable) / writer.string parsers.InlineHtml = parsers.emptyelt_any / writer.inline_html_tag - + (parsers.htmlcomment / parse_inlines_no_html) + + (parsers.htmlcomment / self.parser_functions.parse_inlines_no_html) / writer.inline_html_comment + parsers.htmlinstruction + parsers.openelt_any / writer.inline_html_tag @@ -20632,7 +20642,7 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline * parsers.contentblock_tail / writer.contentblock - parsers.DisplayHtml = (parsers.htmlcomment / parse_blocks_nested) + parsers.DisplayHtml = (parsers.htmlcomment / self.parser_functions.parse_blocks_nested) / writer.block_html_comment + parsers.emptyelt_block / writer.block_html_element + parsers.openelt_exact("hr") / writer.block_html_element @@ -20659,7 +20669,7 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline end) if ran_ok and data ~= nil then return true, writer.jekyllData(data, function(s) - return parse_blocks_nested(s) + return self.parser_functions.parse_blocks_nested(s) end, nil) else return false @@ -20683,7 +20693,8 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline * (P("---") + P("..."))^-1 parsers.Blockquote = Cs(parsers.blockquote_body^1) - / parse_blocks_nested / writer.blockquote + / self.parser_functions.parse_blocks_nested + / writer.blockquote parsers.HorizontalRule = ( parsers.lineof(parsers.asterisk) + parsers.lineof(parsers.dash) @@ -20741,7 +20752,7 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline parsers.TightListItem = function(starter) return -parsers.HorizontalRule * (Cs(starter / "" * parsers.tickbox^-1 * parsers.ListBlock * parsers.NestedList^-1) - / parse_blocks_nested) + / self.parser_functions.parse_blocks_nested) * -(parsers.blanklines * parsers.indent) end @@ -20750,7 +20761,7 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline * Cs( starter / "" * parsers.tickbox^-1 * parsers.ListBlock * Cc("\n") * (parsers.NestedList + parsers.ListContinuationBlock^0) * (parsers.blanklines / "\n\n") - ) / parse_blocks_nested + ) / self.parser_functions.parse_blocks_nested end parsers.BulletList = ( Ct(parsers.TightListItem(parsers.bullet)^1) * Cc(true) @@ -20781,18 +20792,19 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline ) * Cb("listtype") / ordered_list local function definition_list_item(term, defs, tight) - return { term = parse_inlines(term), definitions = defs } + return { term = self.parser_functions.parse_inlines(term), + definitions = defs } end parsers.DefinitionListItemLoose = C(parsers.line) * parsers.skipblanklines * Ct((parsers.defstart * parsers.indented_blocks(parsers.dlchunk) - / parse_blocks_nested)^1) + / self.parser_functions.parse_blocks_nested)^1) * Cc(false) / definition_list_item parsers.DefinitionListItemTight = C(parsers.line) * Ct((parsers.defstart * parsers.dlchunk - / parse_blocks_nested)^1) + / self.parser_functions.parse_blocks_nested)^1) * Cc(true) / definition_list_item parsers.DefinitionList = ( Ct(parsers.DefinitionListItemLoose^1) * Cc(false) @@ -20834,7 +20846,7 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline * (parsers.linechar - parsers.hash - parsers.lbrace)^0)^1) - / parse_inlines) + / self.parser_functions.parse_inlines) * Cg(Ct(parsers.newline + (parsers.hash^1 * parsers.optionalspace @@ -20853,7 +20865,7 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline * parsers.newline)) * (parsers.linechar - parsers.lbrace)^0)^1) - / parse_inlines) + / self.parser_functions.parse_inlines) * Cg(Ct(parsers.newline + (parsers.HeadingAttributes * parsers.optionalspace @@ -20866,12 +20878,15 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline else parsers.AtxHeading = Cg(parsers.HeadingStart,"level") * parsers.optionalspace - * (C(parsers.line) / strip_atx_end / parse_inlines) + * (C(parsers.line) + / strip_atx_end + / self.parser_functions.parse_inlines) * Cb("level") / writer.heading parsers.SetextHeading = #(parsers.line * S("=-")) - * Ct(parsers.linechar^1 / parse_inlines) + * Ct(parsers.linechar^1 + / self.parser_functions.parse_inlines) * parsers.newline * parsers.HeadingLevel * parsers.optionalspace @@ -21128,7 +21143,7 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline % \end{markdown} % \begin{macrocode} local function convert(input) - local document = parse_blocks(input) + local document = self.parser_functions.parse_blocks(input) return util.rope_to_string(writer.document(document)) end if options.eagerCache or options.finalizeCache then From 29f11216272a43f3d4283c32dcea4f258614c357 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Novotn=C3=BD?= Date: Mon, 27 Jun 2022 17:20:41 +0200 Subject: [PATCH 05/12] Create a `extensions.table(captions)` function --- markdown.dtx | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/markdown.dtx b/markdown.dtx index 3927fa45a..35d7450ff 100644 --- a/markdown.dtx +++ b/markdown.dtx @@ -15648,7 +15648,9 @@ texexec --passon=--shell-escape document.tex %-------------------- % % The Lua implementation implements \luamdef{writer} and \luamdef{reader} -% objects that provide the conversion from markdown to plain \TeX{}. +% objects, which provide the conversion from markdown to plain \TeX, and +% \luamdef{extension} objects, which provide syntax extensions for the +% \luamref{writer} and \luamref{reader} objects. % % The Lunamark Lua module implements writers for the conversion to various % other formats, such as DocBook, Groff, or \acro{HTML}. These were stripped @@ -18107,7 +18109,7 @@ function M.writer.new(options) % \par % \begin{markdown} % -% Parse the \Opt{slice} option and define \luamdef{writer->slice\_begin} +% Parse the \Opt{slice} option and define \luamdef{writer->slice\_begin}, % \luamdef{writer->slice\_end}, and \luamdef{writer->is\_writing}. The % \luamref{writer->is\_writing} member variable is mutable. % @@ -21184,10 +21186,24 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline return self end % \end{macrocode} -% \par +% \begin{markdown} +% +%### Syntax Extensions for Markdown +% +% Create \luamdef{extensions} hash table that contains syntax extensions. +% Syntax extensions are objects with two methods: `extend_writer` and +% `extend_reader`. The `extend_writer` object takes a \luamref{writer} object +% as the only parameter and mutates it. Similarly, `extend_reader` takes a +% \luamref{reader} object as the only parameter and mutates it. +% +% \end{markdown} +% \begin{macrocode} +M.extensions = {} +% \end{macrocode} % \begin{markdown} % %### Conversion from Markdown to Plain \TeX{} +% % The \luamref{new} method returns the \luamref{reader->convert} function of a reader % object associated with the Lua interface options (see Section % <#sec:luaoptions>) `options` and with a writer object associated with From 267d5d9fdabdf675ac94e63a57d9d1451ea93622 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Novotn=C3=BD?= Date: Mon, 27 Jun 2022 18:00:43 +0200 Subject: [PATCH 06/12] Add extension mechanism and try it on `pipeTables` and `tableCaptions` --- markdown.dtx | 359 ++++++++++++++++++++++++++++----------------------- 1 file changed, 199 insertions(+), 160 deletions(-) diff --git a/markdown.dtx b/markdown.dtx index 35d7450ff..258e16c0c 100644 --- a/markdown.dtx +++ b/markdown.dtx @@ -6103,7 +6103,7 @@ defaultOptions.jekyllData = false % : true - : Enable the \acro{PHP} Markdown table syntax extension: + : Enable the \acro{PHP} Markdown pipe table syntax extension: ``` md | Right | Left | Default | Center | @@ -6115,7 +6115,7 @@ defaultOptions.jekyllData = false : false - : Disable the \acro{PHP} Markdown table syntax extension. + : Disable the \acro{PHP} Markdown pipe table syntax extension. % \end{markdown} % \iffalse @@ -18384,38 +18384,6 @@ function M.writer.new(options) % \par % \begin{markdown} % -% Define \luamdef{writer->table} as a function that will transform an input -% table to the output format, where `rows` is a sequence of columns and a -% column is a sequence of cell texts. -% -% \end{markdown} -% \begin{macrocode} - function self.table(rows, caption) - if not self.is_writing then return "" end - local buffer = {"\\markdownRendererTable{", - caption or "", "}{", #rows - 1, "}{", #rows[1], "}"} - local temp = rows[2] -- put alignments on the first row - rows[2] = rows[1] - rows[1] = temp - for i, row in ipairs(rows) do - table.insert(buffer, "{") - for _, column in ipairs(row) do - if i > 1 then -- do not use braces for alignments - table.insert(buffer, "{") - end - table.insert(buffer, column) - if i > 1 then - table.insert(buffer, "}") - end - end - table.insert(buffer, "}") - end - return buffer - end -% \end{macrocode} -% \par -% \begin{markdown} -% % Define \luamdef{writer->image} as a function that will transform an input % image to the output format, where `lab` corresponds to the label, `src` % to the \acro{url}, and `tit` to the title of the image. @@ -19658,91 +19626,6 @@ parsers.RawNoteRef = #(parsers.lbracket * parsers.circumflex) % \par % \begin{markdown} % -%#### Parsers Used for Tables -% -% \end{markdown} -% \begin{macrocode} -local function make_pipe_table_rectangular(rows) - local num_columns = #rows[2] - local rectangular_rows = {} - for i = 1, #rows do - local row = rows[i] - local rectangular_row = {} - for j = 1, num_columns do - rectangular_row[j] = row[j] or "" - end - table.insert(rectangular_rows, rectangular_row) - end - return rectangular_rows -end - -local function pipe_table_row(allow_empty_first_column - , nonempty_column - , column_separator - , column) - local row_beginning - if allow_empty_first_column then - row_beginning = -- empty first column - #(parsers.spacechar^4 - * column_separator) - * parsers.optionalspace - * column - * parsers.optionalspace - -- non-empty first column - + parsers.nonindentspace - * nonempty_column^-1 - * parsers.optionalspace - else - row_beginning = parsers.nonindentspace - * nonempty_column^-1 - * parsers.optionalspace - end - - return Ct(row_beginning - * (-- single column with no leading pipes - #(column_separator - * parsers.optionalspace - * parsers.newline) - * column_separator - * parsers.optionalspace - -- single column with leading pipes or - -- more than a single column - + (column_separator - * parsers.optionalspace - * column - * parsers.optionalspace)^1 - * (column_separator - * parsers.optionalspace)^-1)) -end - -parsers.table_hline_separator = parsers.pipe + parsers.plus -parsers.table_hline_column = (parsers.dash - - #(parsers.dash - * (parsers.spacechar - + parsers.table_hline_separator - + parsers.newline)))^1 - * (parsers.colon * Cc("r") - + parsers.dash * Cc("d")) - + parsers.colon - * (parsers.dash - - #(parsers.dash - * (parsers.spacechar - + parsers.table_hline_separator - + parsers.newline)))^1 - * (parsers.colon * Cc("c") - + parsers.dash * Cc("l")) -parsers.table_hline = pipe_table_row(false - , parsers.table_hline_column - , parsers.table_hline_separator - , parsers.table_hline_column) -parsers.table_caption_beginning = parsers.skipblanklines - * parsers.nonindentspace - * (P("Table")^-1 * parsers.colon) - * parsers.optionalspace -% \end{macrocode} -% \par -% \begin{markdown} -% %#### Parsers Used for HTML % % \end{markdown} @@ -20008,7 +19891,7 @@ end % \end{markdown} % \begin{macrocode} M.reader = {} -function M.reader.new(writer, options) +function M.reader.new(writer, options, extensions) local self = {} % \end{macrocode} % \par @@ -20333,36 +20216,6 @@ function M.reader.new(writer, options) % \par % \begin{markdown} % -%#### Parsers Used for Tables (local) -% -% \end{markdown} -% \begin{macrocode} -parsers.table_row = pipe_table_row(true - , (C((parsers.linechar - parsers.pipe)^1) - / self.parser_functions.parse_inlines) - , parsers.pipe - , (C((parsers.linechar - parsers.pipe)^0) - / self.parser_functions.parse_inlines)) - -if options.tableCaptions then - parsers.table_caption = #parsers.table_caption_beginning - * parsers.table_caption_beginning - * Ct(parsers.IndentedInline^1) - * parsers.newline -else - parsers.table_caption = parsers.fail -end - -parsers.PipeTable = Ct(parsers.table_row * parsers.newline - * parsers.table_hline - * (parsers.newline * parsers.table_row)^0) - / make_pipe_table_rectangular - * parsers.table_caption^-1 - / writer.table -% \end{macrocode} -% \par -% \begin{markdown} -% %#### Helpers for Links and References (local) % % \end{markdown} @@ -20951,7 +20804,7 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline DefinitionList = parsers.DefinitionList, DisplayHtml = parsers.DisplayHtml, Paragraph = parsers.Paragraph, - PipeTable = parsers.PipeTable, + PipeTable = parsers.fail, Plain = parsers.Plain, Inline = V("Str") @@ -21020,6 +20873,19 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline Symbol = parsers.Symbol, } +% \end{macrocode} +% \par +% \begin{markdown} +% +% Apply syntax extensions. +% +% \end{markdown} +% \begin{macrocode} + for _, extension in ipairs(extensions) do + extension.extend_writer(writer) + extension.extend_reader(self) + end + if not options.citations then self.syntax.Citations = parsers.fail end @@ -21066,10 +20932,6 @@ parsers.PipeTable = Ct(parsers.table_row * parsers.newline options.stripIndent = false end - if not options.pipeTables then - self.syntax.PipeTable = parsers.fail - end - if not options.smartEllipses then self.syntax.Smart = parsers.fail end @@ -21191,10 +21053,11 @@ end %### Syntax Extensions for Markdown % % Create \luamdef{extensions} hash table that contains syntax extensions. -% Syntax extensions are objects with two methods: `extend_writer` and -% `extend_reader`. The `extend_writer` object takes a \luamref{writer} object -% as the only parameter and mutates it. Similarly, `extend_reader` takes a -% \luamref{reader} object as the only parameter and mutates it. +% Syntax extensions are functions that produce objects with two methods: +% `extend_writer` and `extend_reader`. The `extend_writer` object takes a +% \luamref{writer} object as the only parameter and mutates it. Similarly, +% `extend_reader` takes a \luamref{reader} object as the only parameter and +% mutates it. % % \end{markdown} % \begin{macrocode} @@ -21202,6 +21065,168 @@ M.extensions = {} % \end{macrocode} % \begin{markdown} % +%#### Pipe Tables +% +% The \luamdef{extensions.pipe_table} function implements the \acro{PHP} +% Markdown table syntax extension (affectionately known as pipe tables). When +% the parameter `table_captions` is `true`, the function also implements the +% Pandoc `table_captions` syntax extension for table captions. +% +% \end{markdown} +% \begin{macrocode} +M.extensions.pipe_tables = function(table_captions) + + local function make_pipe_table_rectangular(rows) + local num_columns = #rows[2] + local rectangular_rows = {} + for i = 1, #rows do + local row = rows[i] + local rectangular_row = {} + for j = 1, num_columns do + rectangular_row[j] = row[j] or "" + end + table.insert(rectangular_rows, rectangular_row) + end + return rectangular_rows + end + + local function pipe_table_row(allow_empty_first_column + , nonempty_column + , column_separator + , column) + local row_beginning + if allow_empty_first_column then + row_beginning = -- empty first column + #(parsers.spacechar^4 + * column_separator) + * parsers.optionalspace + * column + * parsers.optionalspace + -- non-empty first column + + parsers.nonindentspace + * nonempty_column^-1 + * parsers.optionalspace + else + row_beginning = parsers.nonindentspace + * nonempty_column^-1 + * parsers.optionalspace + end + + return Ct(row_beginning + * (-- single column with no leading pipes + #(column_separator + * parsers.optionalspace + * parsers.newline) + * column_separator + * parsers.optionalspace + -- single column with leading pipes or + -- more than a single column + + (column_separator + * parsers.optionalspace + * column + * parsers.optionalspace)^1 + * (column_separator + * parsers.optionalspace)^-1)) + end + + return { + extend_writer = function(self) +% \end{macrocode} +% \par +% \begin{markdown} +% +% Define \luamdef{writer->table} as a function that will transform an input +% table to the output format, where `rows` is a sequence of columns and a +% column is a sequence of cell texts. +% +% \end{markdown} +% \begin{macrocode} + function self.table(rows, caption) + if not self.is_writing then return "" end + local buffer = {"\\markdownRendererTable{", + caption or "", "}{", #rows - 1, "}{", #rows[1], "}"} + local temp = rows[2] -- put alignments on the first row + rows[2] = rows[1] + rows[1] = temp + for i, row in ipairs(rows) do + table.insert(buffer, "{") + for _, column in ipairs(row) do + if i > 1 then -- do not use braces for alignments + table.insert(buffer, "{") + end + table.insert(buffer, column) + if i > 1 then + table.insert(buffer, "}") + end + end + table.insert(buffer, "}") + end + return buffer + end + end, extend_reader = function(self) + local parsers = self.parsers + local syntax = self.syntax + local writer = self.writer + + local table_hline_separator = parsers.pipe + parsers.plus + + local table_hline_column = (parsers.dash + - #(parsers.dash + * (parsers.spacechar + + table_hline_separator + + parsers.newline)))^1 + * (parsers.colon * Cc("r") + + parsers.dash * Cc("d")) + + parsers.colon + * (parsers.dash + - #(parsers.dash + * (parsers.spacechar + + table_hline_separator + + parsers.newline)))^1 + * (parsers.colon * Cc("c") + + parsers.dash * Cc("l")) + + local table_hline = pipe_table_row(false + , table_hline_column + , table_hline_separator + , table_hline_column) + + local table_caption_beginning = parsers.skipblanklines + * parsers.nonindentspace + * (P("Table")^-1 * parsers.colon) + * parsers.optionalspace + + local table_row = pipe_table_row(true + , (C((parsers.linechar - parsers.pipe)^1) + / self.parser_functions.parse_inlines) + , parsers.pipe + , (C((parsers.linechar - parsers.pipe)^0) + / self.parser_functions.parse_inlines)) + + local table_caption + if table_captions then + table_caption = #table_caption_beginning + * table_caption_beginning + * Ct(parsers.IndentedInline^1) + * parsers.newline + else + table_caption = parsers.fail + end + + local PipeTable = Ct(table_row * parsers.newline + * table_hline + * (parsers.newline * table_row)^0) + / make_pipe_table_rectangular + * table_caption^-1 + / writer.table + + syntax.PipeTable = PipeTable + end + } +end +% \end{macrocode} +% \begin{markdown} +% %### Conversion from Markdown to Plain \TeX{} % % The \luamref{new} method returns the \luamref{reader->convert} function of a reader @@ -21212,8 +21237,22 @@ M.extensions = {} % \end{markdown} % \begin{macrocode} function M.new(options) +% \par +% \begin{markdown} +% +% Apply syntax extensions based on `options`. +% +% \end{markdown} +% \begin{macrocode} + extensions = {} + if options.pipeTables then + pipe_tables_extension = M.extensions.pipe_tables(options.tableCaptions) + table.insert(extensions, pipe_tables_extension) + end + local writer = M.writer.new(options) - local reader = M.reader.new(writer, options) + local reader = M.reader.new(writer, options, extensions) + return reader.convert end From eb1dc8080ad15ebb0c1f48c492f0d7296f17c3aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Novotn=C3=BD?= Date: Mon, 27 Jun 2022 19:59:50 +0200 Subject: [PATCH 07/12] Options `fencedCode` and `blankBeforeCodeFence` --- markdown.dtx | 223 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 151 insertions(+), 72 deletions(-) diff --git a/markdown.dtx b/markdown.dtx index 258e16c0c..80c70b342 100644 --- a/markdown.dtx +++ b/markdown.dtx @@ -18109,6 +18109,16 @@ function M.writer.new(options) % \par % \begin{markdown} % +% Make the `cache` parameter available as \luamdef{writer->cacheDir}, so that +% it is accessible from extensions. +% +% \end{markdown} +% \begin{macrocode} + self.cacheDir = options.cacheDir +% \end{macrocode} +% \par +% \begin{markdown} +% % Parse the \Opt{slice} option and define \luamdef{writer->slice\_begin}, % \luamdef{writer->slice\_end}, and \luamdef{writer->is\_writing}. The % \luamref{writer->is\_writing} member variable is mutable. @@ -18605,7 +18615,7 @@ function M.writer.new(options) % \begin{macrocode} function self.block_html_element(s) if not self.is_writing then return "" end - local name = util.cache(options.cacheDir, s, nil, nil, ".verbatim") + local name = util.cache(self.cacheDir, s, nil, nil, ".verbatim") return {"\\markdownRendererInputBlockHtmlElement{",name,"}"} end % \end{macrocode} @@ -18712,29 +18722,13 @@ function M.writer.new(options) function self.verbatim(s) if not self.is_writing then return "" end s = string.gsub(s, '[\r\n%s]*$', '') - local name = util.cache(options.cacheDir, s, nil, nil, ".verbatim") + local name = util.cache(self.cacheDir, s, nil, nil, ".verbatim") return {"\\markdownRendererInputVerbatim{",name,"}"} end % \end{macrocode} % \par % \begin{markdown} % -% Define \luamdef{writer->codeFence} as a function that will transform an -% input fenced code block `s` with the infostring `i` to the output -% format. -% -% \end{markdown} -% \begin{macrocode} - function self.fencedCode(i, s) - if not self.is_writing then return "" end - s = string.gsub(s, '[\r\n%s]*$', '') - local name = util.cache(options.cacheDir, s, nil, nil, ".verbatim") - return {"\\markdownRendererInputFencedCode{",name,"}{",i,"}"} - end -% \end{macrocode} -% \par -% \begin{markdown} -% % Define \luamdef{writer->document} as a function that will transform a % document `d` to the output format. % @@ -19790,16 +19784,6 @@ parsers.LocalFilePath * parsers.localfilepath * parsers.optionaltitle -parsers.TildeFencedCode - = parsers.fencehead(parsers.tilde) - * Cs(parsers.fencedline(parsers.tilde)^0) - * parsers.fencetail(parsers.tilde) - -parsers.BacktickFencedCode - = parsers.fencehead(parsers.backtick) - * Cs(parsers.fencedline(parsers.backtick)^0) - * parsers.fencetail(parsers.backtick) - parsers.JekyllFencedCode = parsers.fencehead(parsers.dash) * Cs(parsers.fencedline(parsers.dash)^0) @@ -19981,17 +19965,16 @@ function M.reader.new(writer, options, extensions) % % \end{markdown} % \begin{macrocode} - local expandtabs if options.preserveTabs then - expandtabs = function(s) return s end + self.expandtabs = function(s) return s end else - expandtabs = function(s) - if s:find("\t") then - return iterlines(s, util.expand_tabs_in_line) - else - return s - end - end + self.expandtabs = function(s) + if s:find("\t") then + return iterlines(s, util.expand_tabs_in_line) + else + return s + end + end end % \end{macrocode} % \par @@ -20318,21 +20301,16 @@ function M.reader.new(writer, options, extensions) * parsers.optionalspace * parsers.newline) end - if not options.fencedCode or options.blankBeforeCodeFence then - parsers.fencestart = parsers.fail - else - parsers.fencestart = parsers.fencehead(parsers.backtick) - + parsers.fencehead(parsers.tilde) - end + parsers.EndlineExceptions + = parsers.blankline -- paragraph break + + parsers.tightblocksep -- nested list + + parsers.eof -- end of document + + parsers.bqstart + + parsers.headerstart - parsers.Endline = parsers.newline * -( -- newline, but not before... - parsers.blankline -- paragraph break - + parsers.tightblocksep -- nested list - + parsers.eof -- end of document - + parsers.bqstart - + parsers.headerstart - + parsers.fencestart - ) * parsers.spacechar^0 + parsers.Endline = parsers.newline + * -V("EndlineExceptions") + * parsers.spacechar^0 / (options.hardLineBreaks and writer.linebreak or writer.space) @@ -20350,14 +20328,9 @@ function M.reader.new(writer, options, extensions) / writer.space parsers.NonbreakingEndline - = parsers.newline * -( -- newline, but not before... - parsers.blankline -- paragraph break - + parsers.tightblocksep -- nested list - + parsers.eof -- end of document - + parsers.bqstart - + parsers.headerstart - + parsers.fencestart - ) * parsers.spacechar^0 + = parsers.newline + * -V("EndlineExceptions") + * parsers.spacechar^0 / (options.hardLineBreaks and writer.linebreak or writer.nbsp) @@ -20506,14 +20479,7 @@ function M.reader.new(writer, options, extensions) parsers.Verbatim = Cs( (parsers.blanklines * ((parsers.indentedline - parsers.blankline))^1)^1 - ) / expandtabs / writer.verbatim - - parsers.FencedCode = (parsers.TildeFencedCode - + parsers.BacktickFencedCode) - / function(infostring, code) - return writer.fencedCode(writer.string(infostring), - expandtabs(code)) - end + ) / self.expandtabs / writer.verbatim parsers.JekyllData = Cmt( C((parsers.line - P("---") - P("..."))^0) , function(s, i, text) @@ -20796,7 +20762,7 @@ function M.reader.new(writer, options, extensions) ContentBlock = parsers.ContentBlock, Blockquote = parsers.Blockquote, Verbatim = parsers.Verbatim, - FencedCode = parsers.FencedCode, + FencedCode = parsers.fail, HorizontalRule = parsers.HorizontalRule, BulletList = parsers.BulletList, OrderedList = parsers.OrderedList, @@ -20806,6 +20772,7 @@ function M.reader.new(writer, options, extensions) Paragraph = parsers.Paragraph, PipeTable = parsers.fail, Plain = parsers.Plain, + EndlineExceptions = parsers.EndlineExceptions, Inline = V("Str") + V("Space") @@ -20902,10 +20869,6 @@ function M.reader.new(writer, options, extensions) self.syntax.DefinitionList = parsers.fail end - if not options.fencedCode then - self.syntax.FencedCode = parsers.fail - end - if not options.footnotes then self.syntax.NoteRef = parsers.fail end @@ -21065,6 +21028,116 @@ M.extensions = {} % \end{macrocode} % \begin{markdown} % +%#### Fenced Code +% +% The \luamdef{extensions.fenced_code} function implements the commonmark +% fenced code block extension. When the `blank_before_code_fence` parameter is +% `true`, the syntax extension requires between a paragraph and the following +% fenced code block. +% +% \end{markdown} +% \begin{macrocode} +M.extensions.fenced_code = function(blank_before_code_fence) + return { + extend_writer = function(self) +% \end{macrocode} +% \par +% \begin{markdown} +% +% Define \luamdef{writer->codeFence} as a function that will transform an +% input fenced code block `s` with the infostring `i` to the output +% format. +% +% \end{markdown} +% \begin{macrocode} + function self.fencedCode(i, s) + if not self.is_writing then return "" end + s = string.gsub(s, '[\r\n%s]*$', '') + local name = util.cache(self.cacheDir, s, nil, nil, ".verbatim") + return {"\\markdownRendererInputFencedCode{",name,"}{",i,"}"} + end + end, extend_reader = function(self) + local parsers = self.parsers + local syntax = self.syntax + local writer = self.writer + + local function captures_geq_length(s,i,a,b) + return #a >= #b and i + end + + local infostring = (parsers.linechar - (parsers.backtick + + parsers.space^1 * (parsers.newline + parsers.eof)))^0 + + local fenceindent + local fencehead = function(char) + return C(parsers.nonindentspace) / function(s) fenceindent = #s end + * Cg(char^3, "fencelength") + * parsers.optionalspace * C(infostring) + * parsers.optionalspace * (parsers.newline + parsers.eof) + end + + local fencetail = function(char) + return parsers.nonindentspace + * Cmt(C(char^3) * Cb("fencelength"), captures_geq_length) + * parsers.optionalspace * (parsers.newline + parsers.eof) + + parsers.eof + end + + local fencedline = function(char) + return C(parsers.line - fencetail(char)) + / function(s) + i = 1 + remaining = fenceindent + while true do + c = s:sub(i, i) + if c == " " and remaining > 0 then + remaining = remaining - 1 + i = i + 1 + elseif c == "\t" and remaining > 3 then + remaining = remaining - 4 + i = i + 1 + else + break + end + end + return s:sub(i) + end + end + + local TildeFencedCode + = fencehead(parsers.tilde) + * Cs(fencedline(parsers.tilde)^0) + * fencetail(parsers.tilde) + + local BacktickFencedCode + = fencehead(parsers.backtick) + * Cs(fencedline(parsers.backtick)^0) + * fencetail(parsers.backtick) + + local FencedCode = (TildeFencedCode + + BacktickFencedCode) + / function(infostring, code) + return writer.fencedCode(writer.string(infostring), + self.expandtabs(code)) + end + + syntax.FencedCode = FencedCode + + if blank_before_code_fence then + fencestart = parsers.fail + else + fencestart = fencehead(parsers.backtick) + + fencehead(parsers.tilde) + end + + parsers.EndlineExceptions = parsers.EndlineExceptions + fencestart + syntax.EndlineExceptions = parsers.EndlineExceptions + end + } +end +% \end{macrocode} +% \begin{markdown} +% %#### Pipe Tables % % The \luamdef{extensions.pipe_table} function implements the \acro{PHP} @@ -21245,6 +21318,12 @@ function M.new(options) % \end{markdown} % \begin{macrocode} extensions = {} + + if options.fencedCode then + fenced_code_extension = M.extensions.fenced_code(options.blankBeforeCodeFence) + table.insert(extensions, fenced_code_extension) + end + if options.pipeTables then pipe_tables_extension = M.extensions.pipe_tables(options.tableCaptions) table.insert(extensions, pipe_tables_extension) From 4fb4dbe7521ac590a1ccc9e80ffc633ddb1bb7d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Novotn=C3=BD?= Date: Mon, 27 Jun 2022 20:54:56 +0200 Subject: [PATCH 08/12] Options `citations` and `citationNbsps` --- markdown.dtx | 455 ++++++++++++++++++++++++++++----------------------- 1 file changed, 251 insertions(+), 204 deletions(-) diff --git a/markdown.dtx b/markdown.dtx index 80c70b342..bc56d839f 100644 --- a/markdown.dtx +++ b/markdown.dtx @@ -3506,7 +3506,7 @@ defaultOptions.citationNbsps = true % : true - : Enable the pandoc citation syntax extension: + : Enable the Pandoc citation syntax extension: ``` md Here is a simple parenthetical citation [@doe99] and here @@ -3526,7 +3526,7 @@ defaultOptions.citationNbsps = true : false - : Disable the pandoc citation syntax extension. + : Disable the Pandoc citation syntax extension. % \end{markdown} % \iffalse @@ -18109,8 +18109,8 @@ function M.writer.new(options) % \par % \begin{markdown} % -% Make the `cache` parameter available as \luamdef{writer->cacheDir}, so that -% it is accessible from extensions. +% Make `options.cacheDir` available as \luamdef{writer->cacheDir}, so that it +% is accessible from extensions. % % \end{markdown} % \begin{macrocode} @@ -18119,6 +18119,16 @@ function M.writer.new(options) % \par % \begin{markdown} % +% Make `options.hybrid` available as \luamdef{writer->hybrid}, so that it is +% accessible from extensions. +% +% \end{markdown} +% \begin{macrocode} + self.hybrid = options.hybrid +% \end{macrocode} +% \par +% \begin{markdown} +% % Parse the \Opt{slice} option and define \luamdef{writer->slice\_begin}, % \luamdef{writer->slice\_end}, and \luamdef{writer->is\_writing}. The % \luamref{writer->is\_writing} member variable is mutable. @@ -18263,26 +18273,20 @@ function M.writer.new(options) % \par % \begin{markdown} % -% Define tables \luamdef{escaped_uri_chars}, \luamdef{escaped_citation_chars}, -% and \luamdef{escaped_minimal_strings} containing the mapping from special -% plain characters and character strings that always need to be escaped. +% Define tables \luamdef{writer->escaped_uri_chars} and +% \luamdef{writer->escaped_minimal_strings} containing the mapping from +% special plain characters and character strings that always need to be +% escaped. % % \end{markdown} % \begin{macrocode} - local escaped_uri_chars = { - ["{"] = "\\markdownRendererLeftBrace{}", - ["}"] = "\\markdownRendererRightBrace{}", - ["%"] = "\\markdownRendererPercentSign{}", - ["\\"] = "\\markdownRendererBackslash{}", - } - local escaped_citation_chars = { + self.escaped_uri_chars = { ["{"] = "\\markdownRendererLeftBrace{}", ["}"] = "\\markdownRendererRightBrace{}", ["%"] = "\\markdownRendererPercentSign{}", ["\\"] = "\\markdownRendererBackslash{}", - ["#"] = "\\markdownRendererHash{}", } - local escaped_minimal_strings = { + self.escaped_minimal_strings = { ["^^"] = "\\markdownRendererCircumflex\\markdownRendererCircumflex ", ["☒"] = "\\markdownRendererTickedBox{}", ["⌛"] = "\\markdownRendererHalfTickedBox{}", @@ -18292,13 +18296,13 @@ function M.writer.new(options) % \par % \begin{markdown} % -% Define a table \luamdef{escaped_chars} containing the mapping from special -% plain \TeX{} characters (including the active pipe character (`|`) of -% \Hologo{ConTeXt}) that need to be escaped for typeset content. +% Define a table \luamdef{writer->escaped_chars} containing the mapping from +% special plain \TeX{} characters (including the active pipe character (`|`) +% of \Hologo{ConTeXt}) that need to be escaped for typeset content. % % \end{markdown} % \begin{macrocode} - local escaped_chars = { + self.escaped_chars = { ["{"] = "\\markdownRendererLeftBrace{}", ["}"] = "\\markdownRendererRightBrace{}", ["%"] = "\\markdownRendererPercentSign{}", @@ -18315,57 +18319,40 @@ function M.writer.new(options) % \par % \begin{markdown} % -% Use the \luamref{escaped_chars}, \luamref{escaped_uri_chars}, -% \luamref{escaped_citation_chars}, and \luamref{escaped_minimal_strings} tables -% to create the \luamdef{escape}, \luamdef{escape_citation}, -% \luamdef{escape_uri}, and \luamdef{escape_minimal} escaper functions. +% Use the \luamref{writer->escaped_chars}, \luamref{writer->escaped_uri_chars}, +% and \luamref{writer->escaped_minimal_strings} tables to create the +% \luamdef{writer->escape}, \luamdef{writer->escape_uri}, and +% \luamdef{writer->escape_minimal} escaper functions. % % \end{markdown} % \begin{macrocode} - local escape = util.escaper(escaped_chars, escaped_minimal_strings) - local escape_citation = util.escaper(escaped_citation_chars, - escaped_minimal_strings) - local escape_uri = util.escaper(escaped_uri_chars, escaped_minimal_strings) - local escape_minimal = util.escaper({}, escaped_minimal_strings) + self.escape = util.escaper(self.escaped_chars, self.escaped_minimal_strings) + self.escape_uri = util.escaper(self.escaped_uri_chars, self.escaped_minimal_strings) + self.escape_minimal = util.escaper({}, self.escaped_minimal_strings) % \end{macrocode} % \par % \begin{markdown} % % Define \luamdef{writer->string} as a function that will transform an input -% plain text span `s` to the output format, \luamdef{writer->citation} as a -% function that will transform an input citation name `c` to the output format, -% and \luamdef{writer->uri} as a function that will transform an input -% \acro{uri} `u` to the output format. If the \Opt{hybrid} option is enabled, -% use the \luamref{escape_minimal}. Otherwise, use the \luamref{escape}, -% \luamref{escape_citation}, and \luamref{escape_uri} functions. +% plain text span `s` to the output format and \luamdef{writer->uri} as a +% function that will transform an input \acro{uri} `u` to the output format. +% If the \Opt{hybrid} option is enabled, use the +% \luamref{writer->escape_minimal}. Otherwise, use the +% \luamref{writer->escape}, and \luamref{writer->escape_uri} functions. % % \end{markdown} % \begin{macrocode} if options.hybrid then - self.string = escape_minimal - self.citation = escape_minimal - self.uri = escape_minimal + self.string = self.escape_minimal + self.uri = self.escape_minimal else - self.string = escape - self.citation = escape_citation - self.uri = escape_uri + self.string = self.escape + self.uri = self.escape_uri end % \end{macrocode} % \par % \begin{markdown} % -% Define \luamdef{writer->escape} as a function that will transform an input -% plain text span to the output format. Unlike the \luamref{writer->string} -% function, \luamref{writer->escape} always uses the \luamref{escape} function, -% even when the \Opt{hybrid} option is enabled. -% -% \end{markdown} -% \begin{macrocode} - self.escape = escape -% \end{macrocode} -% \par -% \begin{markdown} -% % Define \luamdef{writer->code} as a function that will transform an input % inlined code span `s` to the output format. % @@ -19002,35 +18989,6 @@ function M.writer.new(options) % \par % \begin{markdown} % -% Define \luamdef{writer->citations} as a function that will transform an -% input array of citations `cites` to the output format. If `text_cites` -% is enabled, the citations should be rendered in-text, when applicable. -% The `cites` array contains tables with the following keys and values: -% \begin{itemize} -% \item`suppress_author` -- If the value of the key is true, then the -% author of the work should be omitted in the citation, when applicable. -% \item`prenote` -- The value of the key is either `nil` or a rope -% that should be inserted before the citation. -% \item`postnote` -- The value of the key is either `nil` or a rope -% that should be inserted after the citation. -% \item`name` -- The value of this key is the citation name. -% \end{itemize} -% -% \end{markdown} -% \begin{macrocode} - function self.citations(text_cites, cites) - local buffer = {"\\markdownRenderer", text_cites and "TextCite" or "Cite", - "{", #cites, "}"} - for _,cite in ipairs(cites) do - buffer[#buffer+1] = {cite.suppress_author and "-" or "+", "{", - cite.prenote or "", "}{", cite.postnote or "", "}{", cite.name, "}"} - end - return buffer - end -% \end{macrocode} -% \par -% \begin{markdown} -% % Define \luamdef{writer->get_state} as a function that returns the current % state of the writer, where the state of a writer are its mutable member % variables. @@ -19141,8 +19099,6 @@ parsers.letter = R("AZ","az") parsers.alphanumeric = R("AZ","az","09") parsers.keyword = parsers.letter * parsers.alphanumeric^0 -parsers.citation_chars = parsers.alphanumeric - + S("#$%&-+<>~/_") parsers.internal_punctuation = S(":;,.?") parsers.doubleasterisks = P("**") @@ -19547,65 +19503,6 @@ parsers.localfilepath % \par % \begin{markdown} % -%#### Parsers Used for Citations -% -% \end{markdown} -% \begin{macrocode} -parsers.citation_name = Cs(parsers.dash^-1) * parsers.at - * Cs(parsers.citation_chars - * (((parsers.citation_chars + parsers.internal_punctuation - - parsers.comma - parsers.semicolon) - * -#((parsers.internal_punctuation - parsers.comma - - parsers.semicolon)^0 - * -(parsers.citation_chars + parsers.internal_punctuation - - parsers.comma - parsers.semicolon)))^0 - * parsers.citation_chars)^-1) - -parsers.citation_body_prenote - = Cs((parsers.alphanumeric^1 - + parsers.bracketed - + parsers.inticks - + (parsers.anyescaped - - (parsers.rbracket + parsers.blankline^2)) - - (parsers.spnl * parsers.dash^-1 * parsers.at))^0) - -parsers.citation_body_postnote - = Cs((parsers.alphanumeric^1 - + parsers.bracketed - + parsers.inticks - + (parsers.anyescaped - - (parsers.rbracket + parsers.semicolon - + parsers.blankline^2)) - - (parsers.spnl * parsers.rbracket))^0) - -parsers.citation_body_chunk - = parsers.citation_body_prenote - * parsers.spnl * parsers.citation_name - * (parsers.internal_punctuation - parsers.semicolon)^-1 - * parsers.spnl * parsers.citation_body_postnote - -parsers.citation_body - = parsers.citation_body_chunk - * (parsers.semicolon * parsers.spnl - * parsers.citation_body_chunk)^0 - -parsers.citation_headless_body_postnote - = Cs((parsers.alphanumeric^1 - + parsers.bracketed - + parsers.inticks - + (parsers.anyescaped - - (parsers.rbracket + parsers.at - + parsers.semicolon + parsers.blankline^2)) - - (parsers.spnl * parsers.rbracket))^0) - -parsers.citation_headless_body - = parsers.citation_headless_body_postnote - * (parsers.sp * parsers.semicolon * parsers.spnl - * parsers.citation_body_chunk)^0 -% \end{macrocode} -% \par -% \begin{markdown} -% %#### Parsers Used for Footnotes % % \end{markdown} @@ -20129,37 +20026,6 @@ function M.reader.new(writer, options, extensions) % \par % \begin{markdown} % -%#### Parsers Used for Citations (local) -% -% \end{markdown} -% \begin{macrocode} - parsers.citations = function(text_cites, raw_cites) - local function normalize(str) - if str == "" then - str = nil - else - str = (options.citationNbsps and - self.parser_functions.parse_inlines_nbsp or - self.parser_functions.parse_inlines)(str) - end - return str - end - - local cites = {} - for i = 1,#raw_cites,4 do - cites[#cites+1] = { - prenote = normalize(raw_cites[i]), - suppress_author = raw_cites[i+1] == "-", - name = writer.citation(raw_cites[i+2]), - postnote = normalize(raw_cites[i+3]), - } - end - return writer.citations(text_cites, cites) - end -% \end{macrocode} -% \par -% \begin{markdown} -% %#### Parsers Used for Footnotes (local) % % \end{markdown} @@ -20419,28 +20285,6 @@ function M.reader.new(writer, options, extensions) parsers.Image = parsers.DirectImage + parsers.IndirectImage - parsers.TextCitations = Ct((parsers.spnl - * Cc("") - * parsers.citation_name - * ((parsers.spnl - * parsers.lbracket - * parsers.citation_headless_body - * parsers.rbracket) + Cc("")))^1) - / function(raw_cites) - return parsers.citations(true, raw_cites) - end - - parsers.ParenthesizedCitations - = Ct((parsers.spnl - * parsers.lbracket - * parsers.citation_body - * parsers.rbracket)^1) - / function(raw_cites) - return parsers.citations(false, raw_cites) - end - - parsers.Citations = parsers.TextCitations + parsers.ParenthesizedCitations - -- avoid parsing long strings of * or _ as emph/strong parsers.UlOrStarLine = parsers.asterisk^4 + parsers.underscore^4 / writer.string @@ -20825,7 +20669,7 @@ function M.reader.new(writer, options, extensions) Emph = parsers.Emph, InlineNote = parsers.InlineNote, NoteRef = parsers.NoteRef, - Citations = parsers.Citations, + Citations = parsers.fail, Link = parsers.Link, Image = parsers.Image, Code = parsers.Code, @@ -20853,10 +20697,6 @@ function M.reader.new(writer, options, extensions) extension.extend_reader(self) end - if not options.citations then - self.syntax.Citations = parsers.fail - end - if not options.contentBlocks then self.syntax.ContentBlock = parsers.fail end @@ -21028,12 +20868,214 @@ M.extensions = {} % \end{macrocode} % \begin{markdown} % +%#### Citations +% +% The \luamdef{extensions.citations} function implements the Pandoc citation +% syntax extension. When the `citation_nbsps` parameter is `true`, the syntax +% extension will replace regular spaces with non-breaking spaces inside the +% prenotes and postnotes of citations. +% +% \end{markdown} +% \begin{macrocode} +M.extensions.citations = function(citation_nbsps) +% \end{macrocode} +% \par +% \begin{markdown} +% +% Define table \luamdef{escaped_citation_chars} containing the characters to +% escape in citations. +% +% \end{markdown} +% \begin{macrocode} + local escaped_citation_chars = { + ["{"] = "\\markdownRendererLeftBrace{}", + ["}"] = "\\markdownRendererRightBrace{}", + ["%"] = "\\markdownRendererPercentSign{}", + ["\\"] = "\\markdownRendererBackslash{}", + ["#"] = "\\markdownRendererHash{}", + } + return { + extend_writer = function(self) +% \end{macrocode} +% \par +% \begin{markdown} +% +% Use the \luamref{escaped_citation_chars} to create the +% \luamdef{escape_citation} escaper functions. +% +% \end{markdown} +% \begin{macrocode} + local escape_citation = util.escaper( + escaped_citation_chars, + self.escaped_minimal_strings) +% \end{macrocode} +% \par +% \begin{markdown} +% +% Define \luamdef{writer->citation} as a function that will transform an input +% citation name `c` to the output format. If \luamref{writer->hybrid} is `true`, +% use the \luamref{writer->escape_minimal} function. Otherwise, use the +% \luamref{escape_citation} function. +% +% \end{markdown} +% \begin{macrocode} + if self.hybrid then + self.citation = self.escape_minimal + else + self.citation = escape_citation + end +% \end{macrocode} +% \par +% \begin{markdown} +% +% Define \luamdef{writer->citations} as a function that will transform an +% input array of citations `cites` to the output format. If `text_cites` +% is enabled, the citations should be rendered in-text, when applicable. +% The `cites` array contains tables with the following keys and values: +% \begin{itemize} +% \item`suppress_author` -- If the value of the key is true, then the +% author of the work should be omitted in the citation, when applicable. +% \item`prenote` -- The value of the key is either `nil` or a rope +% that should be inserted before the citation. +% \item`postnote` -- The value of the key is either `nil` or a rope +% that should be inserted after the citation. +% \item`name` -- The value of this key is the citation name. +% \end{itemize} +% +% \end{markdown} +% \begin{macrocode} + function self.citations(text_cites, cites) + local buffer = {"\\markdownRenderer", text_cites and "TextCite" or "Cite", + "{", #cites, "}"} + for _,cite in ipairs(cites) do + buffer[#buffer+1] = {cite.suppress_author and "-" or "+", "{", + cite.prenote or "", "}{", cite.postnote or "", "}{", cite.name, "}"} + end + return buffer + end + end, extend_reader = function(self) + local parsers = self.parsers + local syntax = self.syntax + local writer = self.writer + + local citation_chars + = parsers.alphanumeric + + S("#$%&-+<>~/_") + + local citation_name + = Cs(parsers.dash^-1) * parsers.at + * Cs(citation_chars + * (((citation_chars + parsers.internal_punctuation + - parsers.comma - parsers.semicolon) + * -#((parsers.internal_punctuation - parsers.comma + - parsers.semicolon)^0 + * -(citation_chars + parsers.internal_punctuation + - parsers.comma - parsers.semicolon)))^0 + * citation_chars)^-1) + + local citation_body_prenote + = Cs((parsers.alphanumeric^1 + + parsers.bracketed + + parsers.inticks + + (parsers.anyescaped + - (parsers.rbracket + parsers.blankline^2)) + - (parsers.spnl * parsers.dash^-1 * parsers.at))^0) + + local citation_body_postnote + = Cs((parsers.alphanumeric^1 + + parsers.bracketed + + parsers.inticks + + (parsers.anyescaped + - (parsers.rbracket + parsers.semicolon + + parsers.blankline^2)) + - (parsers.spnl * parsers.rbracket))^0) + + local citation_body_chunk + = citation_body_prenote + * parsers.spnl * citation_name + * (parsers.internal_punctuation - parsers.semicolon)^-1 + * parsers.spnl * citation_body_postnote + + local citation_body + = citation_body_chunk + * (parsers.semicolon * parsers.spnl + * citation_body_chunk)^0 + + local citation_headless_body_postnote + = Cs((parsers.alphanumeric^1 + + parsers.bracketed + + parsers.inticks + + (parsers.anyescaped + - (parsers.rbracket + parsers.at + + parsers.semicolon + parsers.blankline^2)) + - (parsers.spnl * parsers.rbracket))^0) + + local citation_headless_body + = citation_headless_body_postnote + * (parsers.sp * parsers.semicolon * parsers.spnl + * citation_body_chunk)^0 + + local citations + = function(text_cites, raw_cites) + local function normalize(str) + if str == "" then + str = nil + else + str = (citation_nbsps and + self.parser_functions.parse_inlines_nbsp or + self.parser_functions.parse_inlines)(str) + end + return str + end + + local cites = {} + for i = 1,#raw_cites,4 do + cites[#cites+1] = { + prenote = normalize(raw_cites[i]), + suppress_author = raw_cites[i+1] == "-", + name = writer.citation(raw_cites[i+2]), + postnote = normalize(raw_cites[i+3]), + } + end + return writer.citations(text_cites, cites) + end + + local TextCitations + = Ct((parsers.spnl + * Cc("") + * citation_name + * ((parsers.spnl + * parsers.lbracket + * citation_headless_body + * parsers.rbracket) + Cc("")))^1) + / function(raw_cites) + return citations(true, raw_cites) + end + + local ParenthesizedCitations + = Ct((parsers.spnl + * parsers.lbracket + * citation_body + * parsers.rbracket)^1) + / function(raw_cites) + return citations(false, raw_cites) + end + + local Citations = TextCitations + ParenthesizedCitations + + syntax.Citations = Citations + end + } +end +% \end{macrocode} +% \begin{markdown} +% %#### Fenced Code % % The \luamdef{extensions.fenced_code} function implements the commonmark -% fenced code block extension. When the `blank_before_code_fence` parameter is -% `true`, the syntax extension requires between a paragraph and the following -% fenced code block. +% fenced code block syntax extension. When the `blank_before_code_fence` +% parameter is `true`, the syntax extension requires between a paragraph and +% the following fenced code block. % % \end{markdown} % \begin{macrocode} @@ -21319,6 +21361,11 @@ function M.new(options) % \begin{macrocode} extensions = {} + if options.citations then + citations_extension = M.extensions.citations(options.citationNbsps) + table.insert(extensions, citations_extension) + end + if options.fencedCode then fenced_code_extension = M.extensions.fenced_code(options.blankBeforeCodeFence) table.insert(extensions, fenced_code_extension) From d820526518c7b6f28d81e66e024daf3fb571ef31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Novotn=C3=BD?= Date: Mon, 27 Jun 2022 21:02:56 +0200 Subject: [PATCH 09/12] Options `contentBlocks` and `contentBlocksLanguageMap` --- markdown.dtx | 392 ++++++++++++++++++++++++++------------------------- 1 file changed, 200 insertions(+), 192 deletions(-) diff --git a/markdown.dtx b/markdown.dtx index bc56d839f..b64046af7 100644 --- a/markdown.dtx +++ b/markdown.dtx @@ -3997,7 +3997,7 @@ defaultOptions.contentBlocks = false % % \Valitem[markdown-languages.json]{contentBlocksLanguageMap}{filename} % -: The filename of the JSON file that maps filename extensions to +: The filename of the \acro{JSON} file that maps filename extensions to programming language names in the iA\,Writer content blocks. % See Section <#sec:texcontentblockrenderers> for more information. @@ -18094,17 +18094,6 @@ M.writer = {} % \begin{macrocode} function M.writer.new(options) local self = {} - options = options or {} -% \end{macrocode} -% \par -% \begin{markdown} -% -% Make the `options` table inherit from the \luamref{defaultOptions} table. -% -% \end{markdown} -% \begin{macrocode} - setmetatable(options, { __index = function (_, key) - return defaultOptions[key] end }) % \end{macrocode} % \par % \begin{markdown} @@ -18397,90 +18386,6 @@ function M.writer.new(options) % \par % \begin{markdown} % -% The \luamdef{languages_json} table maps programming language filename -% extensions to fence infostrings. All `options.`\luamref{contentBlocksLanguageMap} -% files located by the KPathSea library are loaded into a chain of tables. -% \luamref{languages_json} corresponds to the first table and is chained with -% the rest via Lua metatables. -% -% \end{markdown} -% \begin{macrocode} - local languages_json = (function() - local ran_ok, kpse = pcall(require, "kpse") - if ran_ok then - kpse.set_program_name("luatex") -% \end{macrocode} -% \begin{markdown} -% -% If the KPathSea library is unavailable, perhaps because we are using -% LuaMeta\TeX, we will only locate the `options.`\luamref{contentBlocksLanguageMap} -% in the current working directory: -% -% \end{markdown} -% \begin{macrocode} - else - kpse = {lookup=function(filename, options) return filename end} - end - local base, prev, curr - for _, filename in ipairs{kpse.lookup(options.contentBlocksLanguageMap, - { all=true })} do - local file = io.open(filename, "r") - if not file then goto continue end - json = file:read("*all"):gsub('("[^\n]-"):','[%1]=') - curr = (function() - local _ENV={ json=json, load=load } -- run in sandbox - return load("return "..json)() - end)() - if type(curr) == "table" then - if base == nil then - base = curr - else - setmetatable(prev, { __index = curr }) - end - prev = curr - end - ::continue:: - end - return base or {} - end)() -% \end{macrocode} -% \par -% \begin{markdown} -% -% Define \luamdef{writer->contentblock} as a function that will transform an -% input iA\,Writer content block to the output format, where `src` -% corresponds to the \acro{uri} prefix, `suf` to the \acro{uri} extension, -% `type` to the type of the content block (`localfile` or `onlineimage`), -% and `tit` to the title of the content block. -% -% \end{markdown} -% \begin{macrocode} - function self.contentblock(src,suf,type,tit) - if not self.is_writing then return "" end - src = src.."."..suf - suf = suf:lower() - if type == "onlineimage" then - return {"\\markdownRendererContentBlockOnlineImage{",suf,"}", - "{",self.string(src),"}", - "{",self.uri(src),"}", - "{",self.string(tit or ""),"}"} - elseif languages_json[suf] then - return {"\\markdownRendererContentBlockCode{",suf,"}", - "{",self.string(languages_json[suf]),"}", - "{",self.string(src),"}", - "{",self.uri(src),"}", - "{",self.string(tit or ""),"}"} - else - return {"\\markdownRendererContentBlock{",suf,"}", - "{",self.string(src),"}", - "{",self.uri(src),"}", - "{",self.string(tit or ""),"}"} - end - end -% \end{macrocode} -% \par -% \begin{markdown} -% % Define \luamdef{writer->bulletlist} as a function that will transform an input % bulleted list to the output format, where `items` is an array of the list % items and `tight` specifies, whether the list is tight or not. @@ -19435,70 +19340,6 @@ parsers.optionaltitle % % \end{markdown} % \begin{macrocode} -parsers.contentblock_tail - = parsers.optionaltitle - * (parsers.newline + parsers.eof) - --- case insensitive online image suffix: -parsers.onlineimagesuffix - = (function(...) - local parser = nil - for _,suffix in ipairs({...}) do - local pattern=nil - for i=1,#suffix do - local char=suffix:sub(i,i) - char = S(char:lower()..char:upper()) - if pattern == nil then - pattern = char - else - pattern = pattern * char - end - end - if parser == nil then - parser = pattern - else - parser = parser + pattern - end - end - return parser - end)("png", "jpg", "jpeg", "gif", "tif", "tiff") - --- online image url for iA Writer content blocks with mandatory suffix, --- allowing nested brackets: -parsers.onlineimageurl - = (parsers.less - * Cs((parsers.anyescaped - - parsers.more - - #(parsers.period - * parsers.onlineimagesuffix - * parsers.more - * parsers.contentblock_tail))^0) - * parsers.period - * Cs(parsers.onlineimagesuffix) - * parsers.more - + (Cs((parsers.inparens - + (parsers.anyescaped - - parsers.spacing - - parsers.rparent - - #(parsers.period - * parsers.onlineimagesuffix - * parsers.contentblock_tail)))^0) - * parsers.period - * Cs(parsers.onlineimagesuffix)) - ) * Cc("onlineimage") - --- filename for iA Writer content blocks with mandatory suffix: -parsers.localfilepath - = parsers.slash - * Cs((parsers.anyescaped - - parsers.tab - - parsers.newline - - #(parsers.period - * parsers.alphanumeric^1 - * parsers.contentblock_tail))^1) - * parsers.period - * Cs(parsers.alphanumeric^1) - * Cc("localfile") % \end{macrocode} % \par % \begin{markdown} @@ -19671,16 +19512,6 @@ parsers.urlchar = parsers.anyescaped - parsers.newline - parsers.more % % \end{markdown} % \begin{macrocode} -parsers.OnlineImageURL - = parsers.leader - * parsers.onlineimageurl - * parsers.optionaltitle - -parsers.LocalFilePath - = parsers.leader - * parsers.localfilepath - * parsers.optionaltitle - parsers.JekyllFencedCode = parsers.fencehead(parsers.dash) * Cs(parsers.fencedline(parsers.dash)^0) @@ -19816,17 +19647,6 @@ function M.reader.new(writer, options, extensions) % \par % \begin{markdown} % -% Make the `options` table inherit from the \luamref{defaultOptions} table. -% -% \end{markdown} -% \begin{macrocode} - options = options or {} - setmetatable(options, { __index = function (_, key) - return defaultOptions[key] end }) -% \end{macrocode} -% \par -% \begin{markdown} -% %#### Top-Level Helper Functions % Define \luamdef{normalize_tag} as a function that normalizes a markdown % reference tag by lowercasing it, and by collapsing any adjacent whitespace @@ -20309,11 +20129,6 @@ function M.reader.new(writer, options, extensions) % % \end{markdown} % \begin{macrocode} - parsers.ContentBlock = parsers.leader - * (parsers.localfilepath + parsers.onlineimageurl) - * parsers.contentblock_tail - / writer.contentblock - parsers.DisplayHtml = (parsers.htmlcomment / self.parser_functions.parse_blocks_nested) / writer.block_html_comment + parsers.emptyelt_block / writer.block_html_element @@ -20603,7 +20418,7 @@ function M.reader.new(writer, options, extensions) + V("Paragraph") + V("Plain"), - ContentBlock = parsers.ContentBlock, + ContentBlock = parsers.fail, Blockquote = parsers.Blockquote, Verbatim = parsers.Verbatim, FencedCode = parsers.fail, @@ -20697,10 +20512,6 @@ function M.reader.new(writer, options, extensions) extension.extend_reader(self) end - if not options.contentBlocks then - self.syntax.ContentBlock = parsers.fail - end - if not options.codeSpans then self.syntax.Code = parsers.fail end @@ -21070,6 +20881,185 @@ end % \end{macrocode} % \begin{markdown} % +%#### iA\,Writer Content Blocks +% +% The \luamdef{extensions.content_blocks} function implements the iA\,Writer +% content blocks syntax extension. The `language_map` parameter specifies +% the filename of the \acro{JSON} file that maps filename extensions to +% programming language names. +% +% \end{markdown} +% \begin{macrocode} +M.extensions.content_blocks = function(language_map) +% \end{macrocode} +% \par +% \begin{markdown} +% +% The \luamdef{languages_json} table maps programming language filename +% extensions to fence infostrings. All `language_map` files located by the +% KPathSea library are loaded into a chain of tables. \luamref{languages_json} +% corresponds to the first table and is chained with the rest via Lua +% metatables. +% +% \end{markdown} +% \begin{macrocode} + local languages_json = (function() + local ran_ok, kpse = pcall(require, "kpse") + if ran_ok then + kpse.set_program_name("luatex") +% \end{macrocode} +% \begin{markdown} +% +% If the KPathSea library is unavailable, perhaps because we are using +% LuaMeta\TeX, we will only locate the `options.`\luamref{contentBlocksLanguageMap} +% in the current working directory: +% +% \end{markdown} +% \begin{macrocode} + else + kpse = {lookup=function(filename, options) return filename end} + end + local base, prev, curr + for _, filename in ipairs{kpse.lookup(language_map, { all=true })} do + local file = io.open(filename, "r") + if not file then goto continue end + json = file:read("*all"):gsub('("[^\n]-"):','[%1]=') + curr = (function() + local _ENV={ json=json, load=load } -- run in sandbox + return load("return "..json)() + end)() + if type(curr) == "table" then + if base == nil then + base = curr + else + setmetatable(prev, { __index = curr }) + end + prev = curr + end + ::continue:: + end + return base or {} + end)() + + return { + extend_writer = function(self) +% \end{macrocode} +% \par +% \begin{markdown} +% +% Define \luamdef{writer->contentblock} as a function that will transform an +% input iA\,Writer content block to the output format, where `src` +% corresponds to the \acro{uri} prefix, `suf` to the \acro{uri} extension, +% `type` to the type of the content block (`localfile` or `onlineimage`), +% and `tit` to the title of the content block. +% +% \end{markdown} +% \begin{macrocode} + function self.contentblock(src,suf,type,tit) + if not self.is_writing then return "" end + src = src.."."..suf + suf = suf:lower() + if type == "onlineimage" then + return {"\\markdownRendererContentBlockOnlineImage{",suf,"}", + "{",self.string(src),"}", + "{",self.uri(src),"}", + "{",self.string(tit or ""),"}"} + elseif languages_json[suf] then + return {"\\markdownRendererContentBlockCode{",suf,"}", + "{",self.string(languages_json[suf]),"}", + "{",self.string(src),"}", + "{",self.uri(src),"}", + "{",self.string(tit or ""),"}"} + else + return {"\\markdownRendererContentBlock{",suf,"}", + "{",self.string(src),"}", + "{",self.uri(src),"}", + "{",self.string(tit or ""),"}"} + end + end + end, extend_reader = function(self) + local parsers = self.parsers + local syntax = self.syntax + local writer = self.writer + + local contentblock_tail + = parsers.optionaltitle + * (parsers.newline + parsers.eof) + + -- case insensitive online image suffix: + local onlineimagesuffix + = (function(...) + local parser = nil + for _, suffix in ipairs({...}) do + local pattern=nil + for i=1,#suffix do + local char=suffix:sub(i,i) + char = S(char:lower()..char:upper()) + if pattern == nil then + pattern = char + else + pattern = pattern * char + end + end + if parser == nil then + parser = pattern + else + parser = parser + pattern + end + end + return parser + end)("png", "jpg", "jpeg", "gif", "tif", "tiff") + + -- online image url for iA Writer content blocks with mandatory suffix, + -- allowing nested brackets: + local onlineimageurl + = (parsers.less + * Cs((parsers.anyescaped + - parsers.more + - #(parsers.period + * onlineimagesuffix + * parsers.more + * contentblock_tail))^0) + * parsers.period + * Cs(onlineimagesuffix) + * parsers.more + + (Cs((parsers.inparens + + (parsers.anyescaped + - parsers.spacing + - parsers.rparent + - #(parsers.period + * onlineimagesuffix + * contentblock_tail)))^0) + * parsers.period + * Cs(onlineimagesuffix)) + ) * Cc("onlineimage") + + -- filename for iA Writer content blocks with mandatory suffix: + local localfilepath + = parsers.slash + * Cs((parsers.anyescaped + - parsers.tab + - parsers.newline + - #(parsers.period + * parsers.alphanumeric^1 + * contentblock_tail))^1) + * parsers.period + * Cs(parsers.alphanumeric^1) + * Cc("localfile") + + local ContentBlock + = parsers.leader + * (localfilepath + onlineimageurl) + * contentblock_tail + / writer.contentblock + + syntax.ContentBlock = ContentBlock + end + } +end +% \end{macrocode} +% \begin{markdown} +% %#### Fenced Code % % The \luamdef{extensions.fenced_code} function implements the commonmark @@ -21352,6 +21342,17 @@ end % \end{markdown} % \begin{macrocode} function M.new(options) +% \end{macrocode} +% \par +% \begin{markdown} +% +% Make the `options` table inherit from the \luamref{defaultOptions} table. +% +% \end{markdown} +% \begin{macrocode} + options = options or {} + setmetatable(options, { __index = function (_, key) + return defaultOptions[key] end }) % \par % \begin{markdown} % @@ -21366,8 +21367,15 @@ function M.new(options) table.insert(extensions, citations_extension) end + if options.contentBlocks then + content_blocks_extension = M.extensions.content_blocks( + options.contentBlocksLanguageMap) + table.insert(extensions, content_blocks_extension) + end + if options.fencedCode then - fenced_code_extension = M.extensions.fenced_code(options.blankBeforeCodeFence) + fenced_code_extension = M.extensions.fenced_code( + options.blankBeforeCodeFence) table.insert(extensions, fenced_code_extension) end From 2709e63f54d01aae2894985d78cffddb78dafcd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Novotn=C3=BD?= Date: Mon, 27 Jun 2022 22:24:19 +0200 Subject: [PATCH 10/12] Option `definitionLists` --- markdown.dtx | 189 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 110 insertions(+), 79 deletions(-) diff --git a/markdown.dtx b/markdown.dtx index b64046af7..a2886866c 100644 --- a/markdown.dtx +++ b/markdown.dtx @@ -7511,7 +7511,7 @@ defaultOptions.texComments = false % : true - : Unordered and ordered Lists whose items do not consist of multiple + : Unordered and ordered lists whose items do not consist of multiple paragraphs will be considered *tight*. Tight lists will produce tight renderers that may produce different output than lists that are not tight: @@ -18514,42 +18514,6 @@ function M.writer.new(options) % \par % \begin{markdown} % -% Define \luamdef{writer->definitionlist} as a function that will transform an -% input definition list to the output format, where `items` is an array of -% tables, each of the form `{ term = t, definitions = defs }`, where `t` -% is a term and `defs` is an array of definitions. `tight` specifies, -% whether the list is tight or not. -% -% \end{markdown} -% \begin{macrocode} - local function dlitem(term, defs) - local retVal = {"\\markdownRendererDlItem{",term,"}"} - for _, def in ipairs(defs) do - retVal[#retVal+1] = {"\\markdownRendererDlDefinitionBegin ",def, - "\\markdownRendererDlDefinitionEnd "} - end - retVal[#retVal+1] = "\\markdownRendererDlItemEnd " - return retVal - end - - function self.definitionlist(items,tight) - if not self.is_writing then return "" end - local buffer = {} - for _,item in ipairs(items) do - buffer[#buffer + 1] = dlitem(item.term, item.definitions) - end - if tight and options.tightLists then - return {"\\markdownRendererDlBeginTight\n", buffer, - "\n\\markdownRendererDlEndTight"} - else - return {"\\markdownRendererDlBegin\n", buffer, - "\n\\markdownRendererDlEnd"} - end - end -% \end{macrocode} -% \par -% \begin{markdown} -% % Define \luamdef{writer->emphasis} as a function that will transform an % emphasized span `s` of input text to the output format. % @@ -19530,19 +19494,6 @@ end % % \end{markdown} % \begin{macrocode} -parsers.defstartchar = S("~:") -parsers.defstart = ( parsers.defstartchar * #parsers.spacing - * (parsers.tab + parsers.space^-3) - + parsers.space * parsers.defstartchar * #parsers.spacing - * (parsers.tab + parsers.space^-2) - + parsers.space * parsers.space * parsers.defstartchar - * #parsers.spacing - * (parsers.tab + parsers.space^-1) - + parsers.space * parsers.space * parsers.space - * parsers.defstartchar * #parsers.spacing - ) - -parsers.dlchunk = Cs(parsers.line * (parsers.indentedline - parsers.blankline)^0) % \end{macrocode} % \par % \begin{markdown} @@ -20270,28 +20221,6 @@ function M.reader.new(writer, options, extensions) * parsers.LooseListItem(parsers.enumerator)^0) * Cc(false) * parsers.skipblanklines ) * Cb("listtype") / ordered_list - - local function definition_list_item(term, defs, tight) - return { term = self.parser_functions.parse_inlines(term), - definitions = defs } - end - - parsers.DefinitionListItemLoose = C(parsers.line) * parsers.skipblanklines - * Ct((parsers.defstart - * parsers.indented_blocks(parsers.dlchunk) - / self.parser_functions.parse_blocks_nested)^1) - * Cc(false) / definition_list_item - - parsers.DefinitionListItemTight = C(parsers.line) - * Ct((parsers.defstart * parsers.dlchunk - / self.parser_functions.parse_blocks_nested)^1) - * Cc(true) / definition_list_item - - parsers.DefinitionList = ( Ct(parsers.DefinitionListItemLoose^1) * Cc(false) - + Ct(parsers.DefinitionListItemTight^1) - * (parsers.skipblanklines - * -parsers.DefinitionListItemLoose * Cc(true)) - ) / writer.definitionlist % \end{macrocode} % \par % \begin{markdown} @@ -20426,7 +20355,7 @@ function M.reader.new(writer, options, extensions) BulletList = parsers.BulletList, OrderedList = parsers.OrderedList, Heading = parsers.Heading, - DefinitionList = parsers.DefinitionList, + DefinitionList = parsers.fail, DisplayHtml = parsers.DisplayHtml, Paragraph = parsers.Paragraph, PipeTable = parsers.fail, @@ -20516,10 +20445,6 @@ function M.reader.new(writer, options, extensions) self.syntax.Code = parsers.fail end - if not options.definitionLists then - self.syntax.DefinitionList = parsers.fail - end - if not options.footnotes then self.syntax.NoteRef = parsers.fail end @@ -20881,7 +20806,7 @@ end % \end{macrocode} % \begin{markdown} % -%#### iA\,Writer Content Blocks +%#### Content Blocks % % The \luamdef{extensions.content_blocks} function implements the iA\,Writer % content blocks syntax extension. The `language_map` parameter specifies @@ -21060,6 +20985,105 @@ end % \end{macrocode} % \begin{markdown} % +%#### Definition Lists +% +% The \luamdef{extensions.definition_lists} function implements the definition +% list syntax extension. If the `tight_lists` parameter is `true`, tight lists +% will produce special right item renderers. +% +% \end{markdown} +% \begin{macrocode} +M.extensions.definition_lists = function(tight_lists) + return { + extend_writer = function(self) +% \end{macrocode} +% \par +% \begin{markdown} +% +% Define \luamdef{writer->definitionlist} as a function that will transform an +% input definition list to the output format, where `items` is an array of +% tables, each of the form `{ term = t, definitions = defs }`, where `t` +% is a term and `defs` is an array of definitions. `tight` specifies, +% whether the list is tight or not. +% +% \end{markdown} +% \begin{macrocode} + local function dlitem(term, defs) + local retVal = {"\\markdownRendererDlItem{",term,"}"} + for _, def in ipairs(defs) do + retVal[#retVal+1] = {"\\markdownRendererDlDefinitionBegin ",def, + "\\markdownRendererDlDefinitionEnd "} + end + retVal[#retVal+1] = "\\markdownRendererDlItemEnd " + return retVal + end + + function self.definitionlist(items,tight) + if not self.is_writing then return "" end + local buffer = {} + for _,item in ipairs(items) do + buffer[#buffer + 1] = dlitem(item.term, item.definitions) + end + if tight and tight_lists then + return {"\\markdownRendererDlBeginTight\n", buffer, + "\n\\markdownRendererDlEndTight"} + else + return {"\\markdownRendererDlBegin\n", buffer, + "\n\\markdownRendererDlEnd"} + end + end + end, extend_reader = function(self) + local parsers = self.parsers + local syntax = self.syntax + local writer = self.writer + + local defstartchar = S("~:") + + local defstart = ( defstartchar * #parsers.spacing + * (parsers.tab + parsers.space^-3) + + parsers.space * defstartchar * #parsers.spacing + * (parsers.tab + parsers.space^-2) + + parsers.space * parsers.space * defstartchar + * #parsers.spacing + * (parsers.tab + parsers.space^-1) + + parsers.space * parsers.space * parsers.space + * defstartchar * #parsers.spacing + ) + + local dlchunk = Cs(parsers.line * (parsers.indentedline - parsers.blankline)^0) + + local function definition_list_item(term, defs, tight) + return { term = self.parser_functions.parse_inlines(term), + definitions = defs } + end + + local DefinitionListItemLoose + = C(parsers.line) * parsers.skipblanklines + * Ct((defstart + * parsers.indented_blocks(dlchunk) + / self.parser_functions.parse_blocks_nested)^1) + * Cc(false) / definition_list_item + + local DefinitionListItemTight + = C(parsers.line) + * Ct((defstart * dlchunk + / self.parser_functions.parse_blocks_nested)^1) + * Cc(true) / definition_list_item + + local DefinitionList + = ( Ct(DefinitionListItemLoose^1) * Cc(false) + + Ct(DefinitionListItemTight^1) + * (parsers.skipblanklines + * -DefinitionListItemLoose * Cc(true)) + ) / writer.definitionlist + + syntax.DefinitionList = DefinitionList + end + } +end +% \end{macrocode} +% \begin{markdown} +% %#### Fenced Code % % The \luamdef{extensions.fenced_code} function implements the commonmark @@ -21373,6 +21397,12 @@ function M.new(options) table.insert(extensions, content_blocks_extension) end + if options.definitionLists then + definition_lists_extension = M.extensions.definition_lists( + options.tightLists) + table.insert(extensions, definition_lists_extension) + end + if options.fencedCode then fenced_code_extension = M.extensions.fenced_code( options.blankBeforeCodeFence) @@ -21380,7 +21410,8 @@ function M.new(options) end if options.pipeTables then - pipe_tables_extension = M.extensions.pipe_tables(options.tableCaptions) + pipe_tables_extension = M.extensions.pipe_tables( + options.tableCaptions) table.insert(extensions, pipe_tables_extension) end From 5464c5e48825210ff41d202aa85c93677759aad8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Novotn=C3=BD?= Date: Mon, 27 Jun 2022 22:50:47 +0200 Subject: [PATCH 11/12] Options `jekyllData` and `expectJekyllData` --- markdown.dtx | 306 +++++++++++++++++++++++++++------------------------ 1 file changed, 164 insertions(+), 142 deletions(-) diff --git a/markdown.dtx b/markdown.dtx index a2886866c..885a6022b 100644 --- a/markdown.dtx +++ b/markdown.dtx @@ -18613,101 +18613,6 @@ function M.writer.new(options) % \par % \begin{markdown} % -% Define \luamdef{writer->jekyllData} as a function that will transform an -% input \acro{yaml} table `d` to the output format. The table is the value for -% the key `p` in the parent table; if `p` is nil, then the table has no parent. -% All scalar keys and values encountered in the table will be cast to a string -% following \acro{yaml} serialization rules. String values will also be -% transformed using the function `t`. -% -% \end{markdown} -% \begin{macrocode} - function self.jekyllData(d, t, p) - if not self.is_writing then return "" end - - local buf = {} - - local keys = {} - for k, _ in pairs(d) do - table.insert(keys, k) - end - table.sort(keys) - - if not p then - table.insert(buf, "\\markdownRendererJekyllDataBegin") - end - - if #d > 0 then - table.insert(buf, "\\markdownRendererJekyllDataSequenceBegin{") - table.insert(buf, self.uri(p or "null")) - table.insert(buf, "}{") - table.insert(buf, #keys) - table.insert(buf, "}") - else - table.insert(buf, "\\markdownRendererJekyllDataMappingBegin{") - table.insert(buf, self.uri(p or "null")) - table.insert(buf, "}{") - table.insert(buf, #keys) - table.insert(buf, "}") - end - - for _, k in ipairs(keys) do - local v = d[k] - local typ = type(v) - k = tostring(k or "null") - if typ == "table" and next(v) ~= nil then - table.insert( - buf, - self.jekyllData(v, t, k) - ) - else - k = self.uri(k) - v = tostring(v) - if typ == "boolean" then - table.insert(buf, "\\markdownRendererJekyllDataBoolean{") - table.insert(buf, k) - table.insert(buf, "}{") - table.insert(buf, v) - table.insert(buf, "}") - elseif typ == "number" then - table.insert(buf, "\\markdownRendererJekyllDataNumber{") - table.insert(buf, k) - table.insert(buf, "}{") - table.insert(buf, v) - table.insert(buf, "}") - elseif typ == "string" then - table.insert(buf, "\\markdownRendererJekyllDataString{") - table.insert(buf, k) - table.insert(buf, "}{") - table.insert(buf, t(v)) - table.insert(buf, "}") - elseif typ == "table" then - table.insert(buf, "\\markdownRendererJekyllDataEmpty{") - table.insert(buf, k) - table.insert(buf, "}") - else - error(format("Unexpected type %s for value of " .. - "YAML key %s", typ, k)) - end - end - end - - if #d > 0 then - table.insert(buf, "\\markdownRendererJekyllDataSequenceEnd") - else - table.insert(buf, "\\markdownRendererJekyllDataMappingEnd") - end - - if not p then - table.insert(buf, "\\markdownRendererJekyllDataEnd") - end - - return buf - end -% \end{macrocode} -% \par -% \begin{markdown} -% % Define \luamdef{writer->active\_attributes} as a stack of attributes % of the headings that are currently active. The % \luamref{writer->active\_headings} member variable is mutable. @@ -19476,11 +19381,6 @@ parsers.urlchar = parsers.anyescaped - parsers.newline - parsers.more % % \end{markdown} % \begin{macrocode} -parsers.JekyllFencedCode - = parsers.fencehead(parsers.dash) - * Cs(parsers.fencedline(parsers.dash)^0) - * parsers.fencetail(parsers.dash) - parsers.lineof = function(c) return (parsers.leader * (P(c) * parsers.optionalspace)^3 * (parsers.newline * parsers.blankline^1 @@ -20091,38 +19991,6 @@ function M.reader.new(writer, options, extensions) * ((parsers.indentedline - parsers.blankline))^1)^1 ) / self.expandtabs / writer.verbatim - parsers.JekyllData = Cmt( C((parsers.line - P("---") - P("..."))^0) - , function(s, i, text) - local data - local ran_ok, error = pcall(function() - local tinyyaml = require("markdown-tinyyaml") - data = tinyyaml.parse(text, {timestamps=false}) - end) - if ran_ok and data ~= nil then - return true, writer.jekyllData(data, function(s) - return self.parser_functions.parse_blocks_nested(s) - end, nil) - else - return false - end - end - ) - - parsers.UnexpectedJekyllData - = P("---") - * parsers.blankline / 0 - * #(-parsers.blankline) -- if followed by blank, it's an hrule - * parsers.JekyllData - * (P("---") + P("...")) - - parsers.ExpectedJekyllData - = ( P("---") - * parsers.blankline / 0 - * #(-parsers.blankline) -- if followed by blank, it's an hrule - )^-1 - * parsers.JekyllData - * (P("---") + P("..."))^-1 - parsers.Blockquote = Cs(parsers.blockquote_body^1) / self.parser_functions.parse_blocks_nested / writer.blockquote @@ -20329,8 +20197,8 @@ function M.reader.new(writer, options, extensions) Blank = parsers.Blank, - UnexpectedJekyllData = parsers.UnexpectedJekyllData, - ExpectedJekyllData = parsers.ExpectedJekyllData, + UnexpectedJekyllData = parsers.fail, + ExpectedJekyllData = parsers.fail, Block = V("ContentBlock") + V("UnexpectedJekyllData") @@ -20459,14 +20327,6 @@ function M.reader.new(writer, options, extensions) self.syntax.InlineNote = parsers.fail end - if not options.jekyllData then - self.syntax.UnexpectedJekyllData = parsers.fail - end - - if not options.jekyllData or not options.expectJekyllData then - self.syntax.ExpectedJekyllData = parsers.fail - end - if options.preserveTabs then options.stripIndent = false end @@ -21194,6 +21054,162 @@ end % \end{macrocode} % \begin{markdown} % +%#### YAML Metadata +% +% The \luamdef{extensions.jekyll_data} function implements the Pandoc +% `yaml_metadata_block` syntax extension for entering metadata in \acro{yaml}. +% When the `expect_jekyll_data` is `true`, then a markdown document may +% begin directly with \acro{yaml} metadata and may contain nothing but +% \acro{yaml} metadata +% +% \end{markdown} +% \begin{macrocode} +M.extensions.jekyll_data = function(expect_jekyll_data) + return { + extend_writer = function(self) +% \end{macrocode} +% \par +% \begin{markdown} +% +% Define \luamdef{writer->jekyllData} as a function that will transform an +% input \acro{yaml} table `d` to the output format. The table is the value for +% the key `p` in the parent table; if `p` is nil, then the table has no parent. +% All scalar keys and values encountered in the table will be cast to a string +% following \acro{yaml} serialization rules. String values will also be +% transformed using the function `t`. +% +% \end{markdown} +% \begin{macrocode} + function self.jekyllData(d, t, p) + if not self.is_writing then return "" end + + local buf = {} + + local keys = {} + for k, _ in pairs(d) do + table.insert(keys, k) + end + table.sort(keys) + + if not p then + table.insert(buf, "\\markdownRendererJekyllDataBegin") + end + + if #d > 0 then + table.insert(buf, "\\markdownRendererJekyllDataSequenceBegin{") + table.insert(buf, self.uri(p or "null")) + table.insert(buf, "}{") + table.insert(buf, #keys) + table.insert(buf, "}") + else + table.insert(buf, "\\markdownRendererJekyllDataMappingBegin{") + table.insert(buf, self.uri(p or "null")) + table.insert(buf, "}{") + table.insert(buf, #keys) + table.insert(buf, "}") + end + + for _, k in ipairs(keys) do + local v = d[k] + local typ = type(v) + k = tostring(k or "null") + if typ == "table" and next(v) ~= nil then + table.insert( + buf, + self.jekyllData(v, t, k) + ) + else + k = self.uri(k) + v = tostring(v) + if typ == "boolean" then + table.insert(buf, "\\markdownRendererJekyllDataBoolean{") + table.insert(buf, k) + table.insert(buf, "}{") + table.insert(buf, v) + table.insert(buf, "}") + elseif typ == "number" then + table.insert(buf, "\\markdownRendererJekyllDataNumber{") + table.insert(buf, k) + table.insert(buf, "}{") + table.insert(buf, v) + table.insert(buf, "}") + elseif typ == "string" then + table.insert(buf, "\\markdownRendererJekyllDataString{") + table.insert(buf, k) + table.insert(buf, "}{") + table.insert(buf, t(v)) + table.insert(buf, "}") + elseif typ == "table" then + table.insert(buf, "\\markdownRendererJekyllDataEmpty{") + table.insert(buf, k) + table.insert(buf, "}") + else + error(format("Unexpected type %s for value of " .. + "YAML key %s", typ, k)) + end + end + end + + if #d > 0 then + table.insert(buf, "\\markdownRendererJekyllDataSequenceEnd") + else + table.insert(buf, "\\markdownRendererJekyllDataMappingEnd") + end + + if not p then + table.insert(buf, "\\markdownRendererJekyllDataEnd") + end + + return buf + end + end, extend_reader = function(self) + local parsers = self.parsers + local syntax = self.syntax + local writer = self.writer + + local JekyllData + = Cmt( C((parsers.line - P("---") - P("..."))^0) + , function(s, i, text) + local data + local ran_ok, error = pcall(function() + local tinyyaml = require("markdown-tinyyaml") + data = tinyyaml.parse(text, {timestamps=false}) + end) + if ran_ok and data ~= nil then + return true, writer.jekyllData(data, function(s) + return self.parser_functions.parse_blocks_nested(s) + end, nil) + else + return false + end + end + ) + + local UnexpectedJekyllData + = P("---") + * parsers.blankline / 0 + * #(-parsers.blankline) -- if followed by blank, it's an hrule + * JekyllData + * (P("---") + P("...")) + + local ExpectedJekyllData + = ( P("---") + * parsers.blankline / 0 + * #(-parsers.blankline) -- if followed by blank, it's an hrule + )^-1 + * JekyllData + * (P("---") + P("..."))^-1 + + syntax.UnexpectedJekyllData = UnexpectedJekyllData + if expect_jekyll_data then + syntax.ExpectedJekyllData = ExpectedJekyllData + end + end + } +end +% \end{macrocode} +% \begin{markdown} +% %#### Pipe Tables % % The \luamdef{extensions.pipe_table} function implements the \acro{PHP} @@ -21409,6 +21425,12 @@ function M.new(options) table.insert(extensions, fenced_code_extension) end + if options.jekyllData then + jekyll_data_extension = M.extensions.jekyll_data( + options.expectJekyllData) + table.insert(extensions, jekyll_data_extension) + end + if options.pipeTables then pipe_tables_extension = M.extensions.pipe_tables( options.tableCaptions) From 794d6ba6ac519138f1494b8619100789c118cbb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Novotn=C3=BD?= Date: Mon, 27 Jun 2022 23:20:23 +0200 Subject: [PATCH 12/12] Options `footnotes` and `inlineFootnotes` --- markdown.dtx | 190 ++++++++++++++++++++++++++++----------------------- 1 file changed, 104 insertions(+), 86 deletions(-) diff --git a/markdown.dtx b/markdown.dtx index 885a6022b..bc9b08205 100644 --- a/markdown.dtx +++ b/markdown.dtx @@ -4859,7 +4859,7 @@ defaultOptions.finalizeCache = false % : true - : Enable the pandoc footnote syntax extension: + : Enable the Pandoc footnote syntax extension: ``` md Here is a footnote reference,[^1] and another.[^longnote] @@ -4883,7 +4883,7 @@ defaultOptions.finalizeCache = false : false - : Disable the pandoc footnote syntax extension. + : Disable the Pandoc footnote syntax extension. % \end{markdown} % \iffalse @@ -5836,7 +5836,7 @@ defaultOptions.hybrid = false % : true - : Enable the pandoc inline footnote syntax extension: + : Enable the Pandoc inline footnote syntax extension: ``` md Here is an inline note.^[Inlines notes are easier to @@ -5846,7 +5846,7 @@ defaultOptions.hybrid = false : false - : Disable the pandoc inline footnote syntax extension. + : Disable the Pandoc inline footnote syntax extension. % \end{markdown} % \iffalse @@ -18751,18 +18751,6 @@ function M.writer.new(options) % \par % \begin{markdown} % -% Define \luamdef{writer->note} as a function that will transform an -% input footnote `s` to the output format. -% -% \end{markdown} -% \begin{macrocode} - function self.note(s) - return {"\\markdownRendererFootnote{",s,"}"} - end -% \end{macrocode} -% \par -% \begin{markdown} -% % Define \luamdef{writer->get_state} as a function that returns the current % state of the writer, where the state of a writer are its mutable member % variables. @@ -19205,28 +19193,6 @@ parsers.optionaltitle % \par % \begin{markdown} % -%#### Parsers Used for iA\,Writer Content Blocks -% -% \end{markdown} -% \begin{macrocode} -% \end{macrocode} -% \par -% \begin{markdown} -% -%#### Parsers Used for Footnotes -% -% \end{markdown} -% \begin{macrocode} -local function strip_first_char(s) - return s:sub(2) -end - -parsers.RawNoteRef = #(parsers.lbracket * parsers.circumflex) - * parsers.tag / strip_first_char -% \end{macrocode} -% \par -% \begin{markdown} -% %#### Parsers Used for HTML % % \end{markdown} @@ -19499,13 +19465,13 @@ function M.reader.new(writer, options, extensions) % \begin{markdown} % %#### Top-Level Helper Functions -% Define \luamdef{normalize_tag} as a function that normalizes a markdown -% reference tag by lowercasing it, and by collapsing any adjacent whitespace -% characters. +% Define \luamdef{reader->normalize_tag} as a function that normalizes a +% markdown reference tag by lowercasing it, and by collapsing any adjacent +% whitespace characters. % % \end{markdown} % \begin{macrocode} - local function normalize_tag(tag) + function self.normalize_tag(tag) return string.lower( gsub(util.rope_to_string(tag), "[ \n\r\t]+", " ")) end @@ -19701,37 +19667,6 @@ function M.reader.new(writer, options, extensions) % % \end{markdown} % \begin{macrocode} - local rawnotes = {} - - -- like indirect_link - local function lookup_note(ref) - return writer.defer_call(function() - local found = rawnotes[normalize_tag(ref)] - if found then - return writer.note( - self.parser_functions.parse_blocks_nested(found)) - else - return {"[", - self.parser_functions.parse_inlines("^" .. ref), "]"} - end - end) - end - - local function register_note(ref,rawnote) - rawnotes[normalize_tag(ref)] = rawnote - return "" - end - - parsers.NoteRef = parsers.RawNoteRef / lookup_note - - - parsers.NoteBlock = parsers.leader * parsers.RawNoteRef * parsers.colon - * parsers.spnl * parsers.indented_blocks(parsers.chunk) - / register_note - - parsers.InlineNote = parsers.circumflex - * (parsers.tag / self.parser_functions.parse_inlines_no_inline_note) - / writer.note % \end{macrocode} % \par % \begin{markdown} @@ -19745,7 +19680,7 @@ function M.reader.new(writer, options, extensions) -- add a reference to the list local function register_link(tag,url,title) - references[normalize_tag(tag)] = { url = url, title = title } + references[self.normalize_tag(tag)] = { url = url, title = title } return "" end @@ -19767,7 +19702,7 @@ function M.reader.new(writer, options, extensions) if sps then tagpart = {sps, tagpart} end - local r = references[normalize_tag(tag)] + local r = references[self.normalize_tag(tag)] if r then return r else @@ -20098,7 +20033,6 @@ function M.reader.new(writer, options, extensions) % \end{markdown} % \begin{macrocode} parsers.Blank = parsers.blankline / "" - + parsers.NoteBlock + parsers.Reference + (parsers.tightblocksep / "\n") % \end{macrocode} @@ -20279,8 +20213,8 @@ function M.reader.new(writer, options, extensions) UlOrStarLine = parsers.UlOrStarLine, Strong = parsers.Strong, Emph = parsers.Emph, - InlineNote = parsers.InlineNote, - NoteRef = parsers.NoteRef, + InlineNote = parsers.fail, + NoteRef = parsers.fail, Citations = parsers.fail, Link = parsers.Link, Image = parsers.Image, @@ -20313,20 +20247,12 @@ function M.reader.new(writer, options, extensions) self.syntax.Code = parsers.fail end - if not options.footnotes then - self.syntax.NoteRef = parsers.fail - end - if not options.html then self.syntax.DisplayHtml = parsers.fail self.syntax.InlineHtml = parsers.fail self.syntax.HtmlEntity = parsers.fail end - if not options.inlineFootnotes then - self.syntax.InlineNote = parsers.fail - end - if options.preserveTabs then options.stripIndent = false end @@ -21054,6 +20980,92 @@ end % \end{macrocode} % \begin{markdown} % +%#### Footnotes +% +% The \luamdef{extensions.footnotes} function implements the Pandoc footnote +% and inline footnote syntax extensions. When the `footnote` parameter is +% `true`, the Pandoc footnote syntax extension will be enabled. When the +% `inline_footnotes` parameter is `true`, the Pandoc inline footnote syntax +% extension will be enabled. +% +% \end{markdown} +% \begin{macrocode} +M.extensions.footnotes = function(footnotes, inline_footnotes) + assert(footnotes or inline_footnotes) + return { + extend_writer = function(self) +% \end{macrocode} +% \par +% \begin{markdown} +% +% Define \luamdef{writer->note} as a function that will transform an +% input footnote `s` to the output format. +% +% \end{markdown} +% \begin{macrocode} + function self.note(s) + return {"\\markdownRendererFootnote{",s,"}"} + end + end, extend_reader = function(self) + local parsers = self.parsers + local syntax = self.syntax + local writer = self.writer + + if footnotes then + local function strip_first_char(s) + return s:sub(2) + end + + local RawNoteRef + = #(parsers.lbracket * parsers.circumflex) + * parsers.tag / strip_first_char + + local rawnotes = {} + + -- like indirect_link + local function lookup_note(ref) + return writer.defer_call(function() + local found = rawnotes[self.normalize_tag(ref)] + if found then + return writer.note( + self.parser_functions.parse_blocks_nested(found)) + else + return {"[", + self.parser_functions.parse_inlines("^" .. ref), "]"} + end + end) + end + + local function register_note(ref,rawnote) + rawnotes[self.normalize_tag(ref)] = rawnote + return "" + end + + local NoteRef = RawNoteRef / lookup_note + + local NoteBlock + = parsers.leader * RawNoteRef * parsers.colon + * parsers.spnl * parsers.indented_blocks(parsers.chunk) + / register_note + + parsers.Blank = NoteBlock + parsers.Blank + syntax.Blank = parsers.Blank + + syntax.NoteRef = NoteRef + end + if inline_footnotes then + local InlineNote + = parsers.circumflex + * (parsers.tag / self.parser_functions.parse_inlines_no_inline_note) + / writer.note + syntax.InlineNote = InlineNote + end + end + } +end +% \end{macrocode} +% \begin{markdown} +% %#### YAML Metadata % % The \luamdef{extensions.jekyll_data} function implements the Pandoc @@ -21425,6 +21437,12 @@ function M.new(options) table.insert(extensions, fenced_code_extension) end + if options.footnotes or options.inlineFootnotes then + footnotes_extension = M.extensions.footnotes( + options.footnotes, options.inlineFootnotes) + table.insert(extensions, footnotes_extension) + end + if options.jekyllData then jekyll_data_extension = M.extensions.jekyll_data( options.expectJekyllData)