From ac61383cec981bde83c723ce8ef15112a5a18ed8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Novotn=C3=BD?= Date: Wed, 17 Apr 2019 23:18:56 +0200 Subject: [PATCH 1/3] Add basic support for table captions In Pandoc, table captions (enabled by the `table_caption` option) can be entire paragraphs. This is a little difficult to parse, because we need to detect lines that might start a block quote, a code fence, or a heading, since we support parsing these without a preceding blank line. In the case of a code fence, we need to actually read the whole code fence to see if it is valid. Therefore, this commit implements only single-line captions. Progresses #32 --- markdown.dtx | 32 ++++++++++++------- tests/support/latex-setup.tex | 2 +- tests/support/plain-setup.tex | 4 +-- tests/support/setup.tex | 9 +++--- .../lunamark-markdown/pipe-tables.test | 4 +-- 5 files changed, 31 insertions(+), 20 deletions(-) diff --git a/markdown.dtx b/markdown.dtx index 7b7d1c21f..2321c8ef4 100644 --- a/markdown.dtx +++ b/markdown.dtx @@ -9451,9 +9451,10 @@ following text: #### Table Renderer The \mdef{markdownRendererTable} macro represents a table. This macro will only be produced, when the \Opt{pipeTables} option is `true`. The macro receives the -parameters `{`\meta{number of rows}`}{`\meta{number of columns}`}` followed by -`{`\meta{row}`}` repeated \meta{number of rows} times, where \meta{row} is -`{`\meta{column}`}` repeated \meta{number of columns} times. +parameters `{`\meta{caption}`}``{`\meta{number of rows}`}{`\meta{number of +columns}`}` followed by `{`\meta{row}`}` repeated \meta{number of rows} times, +where \meta{row} is `{`\meta{column}`}` repeated \meta{number of columns} +times. % \end{markdown} % @@ -9666,7 +9667,7 @@ following text: \def\markdownRendererFootnotePrototype#1{}% \def\markdownRendererCitePrototype#1{}% \def\markdownRendererTextCitePrototype#1{}% -\def\markdownRendererTablePrototype#1#2{}% +\def\markdownRendererTablePrototype#1#2#3{}% % \end{macrocode} % \par % \begin{markdown} @@ -10166,7 +10167,7 @@ pdflatex --shell-escape document.tex \define@key{markdownRenderers}{textCite}{% \renewcommand\markdownRendererTextCite[1]{#1}}% \define@key{markdownRenderers}{table}{% - \renewcommand\markdownRendererTable[2]{#1}}% + \renewcommand\markdownRendererTable[3]{#1}}% % \end{macrocode} % \par % \begin{markdown} @@ -11387,8 +11388,9 @@ function M.writer.new(options) % % \end{markdown} % \begin{macrocode} - function self.table(rows) - local buffer = {"\\markdownRendererTable{", #rows - 1, "}{", #rows[1], "}"} + function self.table(rows, caption) + local buffer = {"\\markdownRendererTable{", + caption or "", "}{", #rows - 1, "}{", #rows[1], "}"} for i, row in ipairs(rows) do if i ~= 2 then -- the horizontal line table.insert(buffer, "{") @@ -12307,6 +12309,10 @@ parsers.table_hline = pipe_table_row(false , parsers.pipe + parsers.plus , C((parsers.dash + parsers.colon)^1) * parsers.optionalspace) +parsers.table_caption_beginning = parsers.skipblanklines + * parsers.nonindentspace + * (P("table") + parsers.colon) + * parsers.optionalspace % \end{macrocode} % \par % \begin{markdown} @@ -12822,13 +12828,17 @@ larsers.table_row = pipe_table_row(true , (C((parsers.linechar - parsers.pipe)^0) / parse_inlines) * parsers.optionalspace) +larsers.table_caption = #parsers.table_caption_beginning + * parsers.table_caption_beginning + * C(parsers.linechar^0) / parse_inlines + * parsers.newline larsers.PipeTable = Ct(larsers.table_row * parsers.newline * parsers.table_hline - * (parsers.newline * larsers.table_row)^0 - * (parsers.eof / "")^-1) - / make_pipe_table_rectangular - / writer.table + * (parsers.newline * larsers.table_row)^0) + / make_pipe_table_rectangular + * larsers.table_caption^-1 + / writer.table % \end{macrocode} % \par % \begin{markdown} diff --git a/tests/support/latex-setup.tex b/tests/support/latex-setup.tex index d5ca24f8d..be002d6ec 100644 --- a/tests/support/latex-setup.tex +++ b/tests/support/latex-setup.tex @@ -158,5 +158,5 @@ textCite = {% \TEXTCITATIONS{#1}}, table = {% - \TABLE{#1}{#2}} + \TABLE{#1}{#2}{#3}} } diff --git a/tests/support/plain-setup.tex b/tests/support/plain-setup.tex index 2b9da68e0..1c163781e 100644 --- a/tests/support/plain-setup.tex +++ b/tests/support/plain-setup.tex @@ -138,5 +138,5 @@ \CITATIONS{#1}}% \def\markdownRendererTextCite#1{% \TEXTCITATIONS{#1}}% -\def\markdownRendererTable#1#2{% - \TABLE{#1}{#2}}% +\def\markdownRendererTable#1#2#3{% + \TABLE{#1}{#2}{#3}}% diff --git a/tests/support/setup.tex b/tests/support/setup.tex index 12d296ebd..20562a33e 100644 --- a/tests/support/setup.tex +++ b/tests/support/setup.tex @@ -27,11 +27,12 @@ \ifnum\COLCOUNTER>\COLTOTAL\relax \expandafter\GOBBLE \fi\DOCOL}% -\def\TABLE#1#2{% +\def\TABLE#1#2#3{% \ROWCOUNTER=1% - \def\ROWTOTAL{#1}% - \def\COLTOTAL{#2}% - \TYPE{BEGIN table (#1 rows, #2 columns)}% + \def\ROWTOTAL{#2}% + \def\COLTOTAL{#3}% + \TYPE{BEGIN table (#2 rows, #3 columns)}% + \TYPE{- caption: #1}% \DOROW}% % Citations parsing diff --git a/tests/testfiles/lunamark-markdown/pipe-tables.test b/tests/testfiles/lunamark-markdown/pipe-tables.test index 24899fd57..1f2e39965 100644 --- a/tests/testfiles/lunamark-markdown/pipe-tables.test +++ b/tests/testfiles/lunamark-markdown/pipe-tables.test @@ -9,11 +9,12 @@ the plain TeX interface. | 123 | 123 | **123** | 123 | | 1 | 1 | 1 | 1 | - : Demonstration of pipe table syntax. + : Demonstration of *pipe table* syntax. >>> codeSpan: pipeTables interblockSeparator BEGIN table (4 rows, 4 columns) +- caption: Demonstration of (emphasis: pipe table) syntax. - row 1, column 1: Right - row 1, column 2: (emphasis: Left) - row 1, column 3: Default @@ -31,4 +32,3 @@ BEGIN table (4 rows, 4 columns) - row 4, column 3: 1 - row 4, column 4: 1 END table -interblockSeparator From 55fec88e8fa68133efd90b1da7075710c3dfff31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Novotn=C3=BD?= Date: Thu, 18 Apr 2019 00:18:15 +0200 Subject: [PATCH 2/3] Fix failing PHP Markdown table unit test --- tests/testfiles/PHP_Markdown/tables.test | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/testfiles/PHP_Markdown/tables.test b/tests/testfiles/PHP_Markdown/tables.test index 154157302..97dc0880c 100644 --- a/tests/testfiles/PHP_Markdown/tables.test +++ b/tests/testfiles/PHP_Markdown/tables.test @@ -112,6 +112,7 @@ Cell | Cell | Cell | Cell | Extra cell? | >>> BEGIN table (3 rows, 2 columns) +- caption: - row 1, column 1: Header 1 - row 1, column 2: Header 2 - row 2, column 1: Cell 1 @@ -122,6 +123,7 @@ END table interblockSeparator interblockSeparator BEGIN table (3 rows, 2 columns) +- caption: - row 1, column 1: Header 1 - row 1, column 2: Header 2 - row 2, column 1: Cell 1 @@ -132,6 +134,7 @@ END table interblockSeparator interblockSeparator BEGIN table (3 rows, 2 columns) +- caption: - row 1, column 1: Header 1 - row 1, column 2: Header 2 - row 2, column 1: Cell 1 @@ -142,6 +145,7 @@ END table interblockSeparator interblockSeparator BEGIN table (3 rows, 2 columns) +- caption: - row 1, column 1: Header 1 - row 1, column 2: Header 2 - row 2, column 1: Cell 1 @@ -156,18 +160,21 @@ headingOne: One-column one-row table interblockSeparator interblockSeparator BEGIN table (2 rows, 1 columns) +- caption: - row 1, column 1: Header - row 2, column 1: Cell END table interblockSeparator interblockSeparator BEGIN table (2 rows, 1 columns) +- caption: - row 1, column 1: Header - row 2, column 1: Cell END table interblockSeparator interblockSeparator BEGIN table (2 rows, 1 columns) +- caption: - row 1, column 1: Header - row 2, column 1: Cell END table @@ -176,6 +183,7 @@ horizontalRule interblockSeparator interblockSeparator BEGIN table (3 rows, 4 columns) +- caption: - row 1, column 1: Default - row 1, column 2: Right - row 1, column 3: Center @@ -192,6 +200,7 @@ END table interblockSeparator interblockSeparator BEGIN table (3 rows, 4 columns) +- caption: - row 1, column 1: Default - row 1, column 2: Right - row 1, column 3: Center @@ -211,6 +220,7 @@ interblockSeparator headingOne: Empty cells interblockSeparator BEGIN table (3 rows, 2 columns) +- caption: - row 1, column 1: Header 1 - row 1, column 2: Header 2 - row 2, column 1: A @@ -220,6 +230,7 @@ BEGIN table (3 rows, 2 columns) END table interblockSeparator BEGIN table (3 rows, 2 columns) +- caption: - row 1, column 1: Header 1 - row 1, column 2: Header 2 - row 2, column 1: A @@ -233,6 +244,7 @@ interblockSeparator headingOne: Missing tailing pipe interblockSeparator BEGIN table (3 rows, 2 columns) +- caption: - row 1, column 1: Header 1 - row 1, column 2: Header 2 - row 2, column 1: Cell @@ -242,6 +254,7 @@ BEGIN table (3 rows, 2 columns) END table interblockSeparator BEGIN table (3 rows, 2 columns) +- caption: - row 1, column 1: Header 1 - row 1, column 2: Header 2 - row 2, column 1: Cell @@ -251,6 +264,7 @@ BEGIN table (3 rows, 2 columns) END table interblockSeparator BEGIN table (3 rows, 2 columns) +- caption: - row 1, column 1: Header 1 - row 1, column 2: Header 2 - row 2, column 1: Cell @@ -260,6 +274,7 @@ BEGIN table (3 rows, 2 columns) END table interblockSeparator BEGIN table (3 rows, 2 columns) +- caption: - row 1, column 1: Header 1 - row 1, column 2: Header 2 - row 2, column 1: Cell @@ -273,6 +288,7 @@ interblockSeparator headingOne: Too many pipes in rows interblockSeparator BEGIN table (3 rows, 1 columns) +- caption: - row 1, column 1: Header 1 - row 2, column 1: Cell - row 3, column 1: Cell From 8d0fe1f2fb050be30cbb258908cbfcf806fb2080 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Novotn=C3=BD?= Date: Thu, 18 Apr 2019 19:53:31 +0200 Subject: [PATCH 3/3] Add full support for table captions Progresses #32 --- markdown.dtx | 97 +++++++++++++++++-- ...ipe-tables.test => no-table-captions.test} | 16 ++- .../lunamark-markdown/table-captions.test | 39 ++++++++ 3 files changed, 139 insertions(+), 13 deletions(-) rename tests/testfiles/lunamark-markdown/{pipe-tables.test => no-table-captions.test} (69%) create mode 100644 tests/testfiles/lunamark-markdown/table-captions.test diff --git a/markdown.dtx b/markdown.dtx index 2321c8ef4..a22820a93 100644 --- a/markdown.dtx +++ b/markdown.dtx @@ -4295,7 +4295,7 @@ defaultOptions.inlineFootnotes = false % %<*manual-options> -#### Option `pipeTables` +#### Option `pipeTables` {#pipe-tables} `pipeTables` (default value: `false`) @@ -4314,8 +4314,6 @@ defaultOptions.inlineFootnotes = false | 12 | 12 | 12 | 12 | | 123 | 123 | 123 | 123 | | 1 | 1 | 1 | 1 | - - : Demonstration of pipe table syntax. `````` : false @@ -5042,6 +5040,53 @@ defaultOptions.startNumber = true % %<*manual-options> +#### Option `tableCaptions` + +`tableCaptions` (default value: `false`) + +% \fi +% \begin{markdown} +% +% \Optitem[false]{tableCaptions}{\opt{true}, \opt{false}} +% +: true + + : Enable the Pandoc `table_captions` syntax extension for +% pipe tables (see the \Opt{pipeTables} option). + \iffalse + [pipe tables](#pipe-tables). +% \fi + + ``` md + | Right | Left | Default | Center | + |------:|:-----|---------|:------:| + | 12 | 12 | 12 | 12 | + | 123 | 123 | 123 | 123 | + | 1 | 1 | 1 | 1 | + + : Demonstration of pipe table syntax. + `````` + +: false + + : Enable the Pandoc `table_captions` syntax extension. + +% \end{markdown} +% \iffalse + + +% +%<*lua,lua-cli> +% \fi +% \begin{macrocode} +defaultOptions.tableCaptions = false +% \end{macrocode} +% \par +% \iffalse +% +%<*manual-options> + + #### Option `tightLists` `tightLists` (default value: `true`) @@ -5816,6 +5861,7 @@ bug](https://github.com/witiko/markdown/issues). \let\markdownOptionSlice\undefined \let\markdownOptionSmartEllipses\undefined \let\markdownOptionStartNumber\undefined +\let\markdownOptionTableCaptions\undefined \let\markdownOptionTightLists\undefined % \end{macrocode} % \par @@ -10019,6 +10065,8 @@ pdflatex --shell-escape document.tex \def\markdownOptionSlice{#1}}% \define@key{markdownOptions}{startNumber}[true]{% \def\markdownOptionStartNumber{#1}}% +\define@key{markdownOptions}{tableCaptions}[true]{% + \def\markdownOptionTableCaptions{#1}}% \define@key{markdownOptions}{tightLists}[true]{% \def\markdownOptionTightLists{#1}}% \define@key{markdownOptions}{underscores}[true]{% @@ -12460,7 +12508,8 @@ parsers.define_reference_parser = parsers.leader * parsers.tag * parsers.colon % % \end{markdown} % \begin{macrocode} -parsers.Inline = V("Inline") +parsers.Inline = V("Inline") +parsers.IndentedInline = V("IndentedInline") -- parse many p between starter and ender parsers.between = function(p, starter, ender) @@ -12828,10 +12877,15 @@ larsers.table_row = pipe_table_row(true , (C((parsers.linechar - parsers.pipe)^0) / parse_inlines) * parsers.optionalspace) -larsers.table_caption = #parsers.table_caption_beginning - * parsers.table_caption_beginning - * C(parsers.linechar^0) / parse_inlines - * parsers.newline + +if options.tableCaptions then + larsers.table_caption = #parsers.table_caption_beginning + * parsers.table_caption_beginning + * Ct(parsers.IndentedInline^1) + * parsers.newline +else + larsers.table_caption = parsers.fail +end larsers.PipeTable = Ct(larsers.table_row * parsers.newline * parsers.table_hline @@ -12954,6 +13008,9 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline + larsers.fencestart ) * parsers.spacechar^0 / writer.space + larsers.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^-1 @@ -13363,8 +13420,29 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline + V("Smart") + V("Symbol"), + IndentedInline = V("Str") + + V("OptionalIndent") + + V("Endline") + + V("UlOrStarLine") + + V("Strong") + + V("Emph") + + V("InlineNote") + + V("NoteRef") + + V("Citations") + + V("Link") + + V("Image") + + V("Code") + + V("AutoLinkUrl") + + V("AutoLinkEmail") + + V("InlineHtml") + + V("HtmlEntity") + + V("EscapedChar") + + V("Smart") + + V("Symbol"), + Str = larsers.Str, Space = larsers.Space, + OptionalIndent = larsers.OptionalIndent, Endline = larsers.Endline, UlOrStarLine = larsers.UlOrStarLine, Strong = larsers.Strong, @@ -13753,6 +13831,9 @@ end \ifx\markdownOptionStartNumber\undefined\else startNumber = \markdownOptionStartNumber, \fi +\ifx\markdownOptionTableCaptions\undefined\else + tableCaptions = \markdownOptionTableCaptions, +\fi \ifx\markdownOptionTightLists\undefined\else tightLists = \markdownOptionTightLists, \fi diff --git a/tests/testfiles/lunamark-markdown/pipe-tables.test b/tests/testfiles/lunamark-markdown/no-table-captions.test similarity index 69% rename from tests/testfiles/lunamark-markdown/pipe-tables.test rename to tests/testfiles/lunamark-markdown/no-table-captions.test index 1f2e39965..f4e66afe7 100644 --- a/tests/testfiles/lunamark-markdown/pipe-tables.test +++ b/tests/testfiles/lunamark-markdown/no-table-captions.test @@ -1,7 +1,7 @@ \def\markdownOptionPipeTables{true} <<< -This test ensures that the Lua `pipeTables` option correctly propagates through -the plain TeX interface. +This test ensures that the Lua `tableCaptions` option is disabled by +default. | Right | *Left* | Default | Center | |------:|:-------|-------------|:------:| @@ -9,12 +9,15 @@ the plain TeX interface. | 123 | 123 | **123** | 123 | | 1 | 1 | 1 | 1 | - : Demonstration of *pipe table* syntax. + : Demonstration of *pipe table* syntax with the caption spreading over + multiple lines. + +A caption may not span multiple paragraphs. >>> -codeSpan: pipeTables +codeSpan: tableCaptions interblockSeparator BEGIN table (4 rows, 4 columns) -- caption: Demonstration of (emphasis: pipe table) syntax. +- caption: - row 1, column 1: Right - row 1, column 2: (emphasis: Left) - row 1, column 3: Default @@ -32,3 +35,6 @@ BEGIN table (4 rows, 4 columns) - row 4, column 3: 1 - row 4, column 4: 1 END table +interblockSeparator +emphasis: pipe table +interblockSeparator diff --git a/tests/testfiles/lunamark-markdown/table-captions.test b/tests/testfiles/lunamark-markdown/table-captions.test new file mode 100644 index 000000000..0c809e939 --- /dev/null +++ b/tests/testfiles/lunamark-markdown/table-captions.test @@ -0,0 +1,39 @@ +\def\markdownOptionPipeTables{true} +\def\markdownOptionTableCaptions{true} +<<< +This test ensures that the Lua `tableCaptions` option correctly propagates +through the plain TeX interface. + +| Right | *Left* | Default | Center | +|------:|:-------|-------------|:------:| +| 12 | 12 | 12 | 12 | +| 123 | 123 | **123** | 123 | +| 1 | 1 | 1 | 1 | + + : Demonstration of *pipe table* syntax with the caption spreading over + multiple lines. + +A caption may not span multiple paragraphs. +>>> +codeSpan: tableCaptions +interblockSeparator +BEGIN table (4 rows, 4 columns) +- caption: Demonstration of (emphasis: pipe table) syntax with the caption spreading over multiple lines. +- row 1, column 1: Right +- row 1, column 2: (emphasis: Left) +- row 1, column 3: Default +- row 1, column 4: Center +- row 2, column 1: 12 +- row 2, column 2: 12 +- row 2, column 3: 12 +- row 2, column 4: 12 +- row 3, column 1: 123 +- row 3, column 2: 123 +- row 3, column 3: (strongEmphasis: 123) +- row 3, column 4: 123 +- row 4, column 1: 1 +- row 4, column 2: 1 +- row 4, column 3: 1 +- row 4, column 4: 1 +END table +interblockSeparator