Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support an escaped pipe char in a table cell #150

Merged
merged 1 commit into from Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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