Skip to content

Commit 41f829c

Browse files
authored
Markdown reader: allow grid tables to be indented. (#11671)
Like the other table syntaxes (pipe, simple, and multiline tables) and block-level constructs generally, a grid table may now be indented by up to three spaces and still be recognized as a table. Previously the grid-table parser required the table to begin at the left margin, so an indented grid table was parsed as a paragraph. The leading indentation is stripped uniformly from each line before the table is parsed, so an indented grid table produces the same AST as its non-indented equivalent. Adds a command test.
1 parent 1c7cfb2 commit 41f829c

2 files changed

Lines changed: 79 additions & 1 deletion

File tree

src/Text/Pandoc/Readers/Markdown.hs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1403,7 +1403,20 @@ multilineTableHeader headless = try $ do
14031403
-- ending with a footer (dashed line followed by blank line).
14041404
gridTable :: PandocMonad m
14051405
=> MarkdownParser m (F TableComponents)
1406-
gridTable = gridTableWith' NormalizeHeader parseBlocks
1406+
gridTable = try $ do
1407+
-- Like other block-level constructs, a grid table may be indented by
1408+
-- up to three spaces. The underlying grid-table parser expects the
1409+
-- table to begin at the left margin, so strip a uniform indentation
1410+
-- from every line before handing it off.
1411+
indent <- T.length <$> lookAhead nonindentSpaces
1412+
if indent == 0
1413+
then gridTableWith' NormalizeHeader parseBlocks
1414+
else do
1415+
let gridLine = try $ count indent (char ' ')
1416+
*> lookAhead (oneOf "+|")
1417+
*> anyLineNewline
1418+
rawTable <- T.concat <$> many1 gridLine
1419+
parseFromString' (gridTableWith' NormalizeHeader parseBlocks) rawTable
14071420

14081421
pipeBreak :: PandocMonad m => MarkdownParser m ([Alignment], [Int])
14091422
pipeBreak = try $ do
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
Like other block-level constructs, grid tables may be indented by up to
2+
three spaces and are still recognized as tables.
3+
4+
```
5+
% pandoc -f markdown -t html
6+
+---+---+
7+
| a | b |
8+
+===+===+
9+
| 1 | 2 |
10+
+---+---+
11+
^D
12+
<table style="width:11%;">
13+
<colgroup>
14+
<col style="width: 5%" />
15+
<col style="width: 5%" />
16+
</colgroup>
17+
<thead>
18+
<tr>
19+
<th>a</th>
20+
<th>b</th>
21+
</tr>
22+
</thead>
23+
<tbody>
24+
<tr>
25+
<td>1</td>
26+
<td>2</td>
27+
</tr>
28+
</tbody>
29+
</table>
30+
```
31+
32+
A headerless indented grid table is recognized too.
33+
34+
```
35+
% pandoc -f markdown -t html
36+
+------+------+
37+
| foo | bar |
38+
+------+------+
39+
^D
40+
<table style="width:19%;">
41+
<colgroup>
42+
<col style="width: 9%" />
43+
<col style="width: 9%" />
44+
</colgroup>
45+
<tbody>
46+
<tr>
47+
<td>foo</td>
48+
<td>bar</td>
49+
</tr>
50+
</tbody>
51+
</table>
52+
```
53+
54+
A grid table indented four spaces is a code block, not a table.
55+
56+
```
57+
% pandoc -f markdown -t html
58+
+---+---+
59+
| a | b |
60+
+---+---+
61+
^D
62+
<pre><code>+---+---+
63+
| a | b |
64+
+---+---+</code></pre>
65+
```

0 commit comments

Comments
 (0)