Skip to content

Commit

Permalink
Support an escaped pipe char in a table cell
Browse files Browse the repository at this point in the history
  • Loading branch information
lazyfrosch committed Feb 1, 2018
1 parent 92b7f32 commit 1c74f10
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
16 changes: 12 additions & 4 deletions mistune.py
Expand Up @@ -373,9 +373,9 @@ def parse_table(self, m):
cells = cells.split('\n')
for i, v in enumerate(cells):
v = re.sub(r'^ *\| *| *\| *$', '', v)
cells[i] = re.split(r' *\| *', v)
cells[i] = re.split(r' *(?<!\\)\| *', v)

item['cells'] = cells
item['cells'] = self._process_cells(cells)
self.tokens.append(item)

def parse_nptable(self, m):
Expand All @@ -384,9 +384,9 @@ def parse_nptable(self, m):
cells = re.sub(r'\n$', '', m.group(3))
cells = cells.split('\n')
for i, v in enumerate(cells):
cells[i] = re.split(r' *\| *', v)
cells[i] = re.split(r' *(?<!\\)\| *', v)

item['cells'] = cells
item['cells'] = self._process_cells(cells)
self.tokens.append(item)

def _process_table(self, m):
Expand All @@ -412,6 +412,14 @@ def _process_table(self, m):
}
return item

def _process_cells(self, cells):
for i, line in enumerate(cells):
for c, cell in enumerate(line):
# de-escape any pipe inside the cell here
cells[i][c] = re.sub('\\\\\|', '|', cell)

return cells

def parse_block_html(self, m):
tag = m.group(1)
if not tag:
Expand Down
9 changes: 9 additions & 0 deletions tests/fixtures/extra/gfm_tables.html
Expand Up @@ -35,3 +35,12 @@
<tr><td style="text-align:left"><em>Cell 5</em></td><td style="text-align:center">Cell 6</td><td style="text-align:right">Cell 7</td><td>Cell 8</td></tr>
</tbody>
</table>
<table>
<thead>
<tr><th>Header 1</th><th>Header 2</th></tr>
</thead>
<tbody>
<tr><td>Cell 1</td><td>Cell 2 with a pipe in <code>|</code> inline code</td></tr>
<tr><td>Cell 3</td><td>Cell 4 with escaped | pipe for some content</td></tr>
</tbody>
</table>
5 changes: 5 additions & 0 deletions tests/fixtures/extra/gfm_tables.text
Expand Up @@ -19,3 +19,8 @@ Header 1|Header 2|Header 3|Header 4
:-------|:------:|-------:|--------
Cell 1 |Cell 2 |Cell 3 |Cell 4
*Cell 5*|Cell 6 |Cell 7 |Cell 8

Header 1 | Header 2
-------- | --------------------------------------------
Cell 1 | Cell 2 with a pipe in `\|` inline code
Cell 3 | Cell 4 with escaped \| pipe for some content

0 comments on commit 1c74f10

Please sign in to comment.