Skip to content

Commit

Permalink
Add basic support for table captions
Browse files Browse the repository at this point in the history
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 jgm#32
  • Loading branch information
Witiko committed Apr 17, 2019
1 parent 0c7f87b commit ac61383
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
32 changes: 21 additions & 11 deletions markdown.dtx
Original file line number Diff line number Diff line change
Expand Up @@ -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}
%
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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, "{")
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion tests/support/latex-setup.tex
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,5 @@
textCite = {%
\TEXTCITATIONS{#1}},
table = {%
\TABLE{#1}{#2}}
\TABLE{#1}{#2}{#3}}
}
4 changes: 2 additions & 2 deletions tests/support/plain-setup.tex
Original file line number Diff line number Diff line change
Expand Up @@ -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}}%
9 changes: 5 additions & 4 deletions tests/support/setup.tex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/testfiles/lunamark-markdown/pipe-tables.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,4 +32,3 @@ BEGIN table (4 rows, 4 columns)
- row 4, column 3: 1
- row 4, column 4: 1
END table
interblockSeparator

0 comments on commit ac61383

Please sign in to comment.