Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions markdown/extensions/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from __future__ import unicode_literals
from . import Extension
from ..blockprocessors import BlockProcessor
from ..inlinepatterns import BacktickPattern, BACKTICK_RE
from ..util import etree


Expand Down Expand Up @@ -72,7 +73,11 @@ def _build_row(self, row, parent, align, border):
for i, a in enumerate(align):
c = etree.SubElement(tr, tag)
try:
c.text = cells[i].strip()
if isinstance(cells[i], str) or isinstance(cells[i], unicode):
c.text = cells[i].strip()
else:
# we've already inserted a code element
c.append(cells[i])
except IndexError: # pragma: no cover
c.text = ""
if a:
Expand All @@ -85,7 +90,49 @@ def _split_row(self, row, border):
row = row[1:]
if row.endswith('|'):
row = row[:-1]
return row.split('|')
return self._split(row, '|')

def _split(self, row, marker):
""" split a row of text with some code into a list of cells. """
if self._row_has_unpaired_backticks(row):
# fallback on old behaviour
return row.split(marker)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the coverage report, this is never tested. Might want to add a test for it.

# modify the backtick pattern to only match at the beginning of the search string
backtick_pattern = BacktickPattern('^' + BACKTICK_RE)
elements = []
current = ''
i = 0
while i < len(row):
letter = row[i]
if letter == marker:
if current != '' or len(elements) == 0:
# Don't append empty string unless it is the first element
# The border is already removed when we get the row, then the line is strip()'d
# If the first element is a marker, then we have an empty first cell
elements.append(current)
current = ''
else:
match = backtick_pattern.getCompiledRegExp().match(row[i:])
if not match:
current += letter
else:
groups = match.groups()
delim = groups[1] # the code block delimeter (ie 1 or more backticks)
row_contents = groups[2] # the text contained inside the code block
i += match.start(4) # jump pointer to the beginning of the rest of the text (group #4)
element = delim + row_contents + delim # reinstert backticks
current += element
i += 1
elements.append(current)
return elements

def _row_has_unpaired_backticks(self, row):
count_total_backtick = row.count('`')
count_escaped_backtick = row.count('\`')
count_backtick = count_total_backtick - count_escaped_backtick
# odd number of backticks,
# we won't be able to build correct code blocks
return count_backtick & 1


class TableExtension(Extension):
Expand Down
67 changes: 67 additions & 0 deletions tests/extensions/extra/tables.html
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,71 @@ <h2>Table Tests</h2>
</tr>
</thead>
<tbody></tbody>
</table>
<p>More inline code block tests</p>
<table>
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>word 1</td>
<td>word 2</td>
<td>word 3</td>
</tr>
<tr>
<td>word 1</td>
<td><code>word 2</code></td>
<td>word 3</td>
</tr>
<tr>
<td>word 1</td>
<td>`word 2</td>
<td>word 3</td>
</tr>
<tr>
<td>word 1</td>
<td>`word 2</td>
<td>word 3</td>
</tr>
<tr>
<td>word 1</td>
<td><code>word |2</code></td>
<td>word 3</td>
</tr>
<tr>
<td>words</td>
<td><code>some | code</code></td>
<td>more words</td>
</tr>
<tr>
<td>words</td>
<td><code>some | code</code></td>
<td>more words</td>
</tr>
<tr>
<td>words</td>
<td><code>some | code</code></td>
<td>more words</td>
</tr>
<tr>
<td>words</td>
<td><code>some ` | ` code</code></td>
<td>more words</td>
</tr>
<tr>
<td>words</td>
<td><code>some ` | ` code</code></td>
<td>more words</td>
</tr>
<tr>
<td>words</td>
<td><code>some ` | ` code</code></td>
<td>more words</td>
</tr>
</tbody>
</table>
18 changes: 17 additions & 1 deletion tests/extensions/extra/tables.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,20 @@ Four spaces is a code block:
Content Cell | Content Cell

| First Header | Second Header |
| ------------ | ------------- |
| ------------ | ------------- |

More inline code block tests

Column 1 | Column 2 | Column 3
---------|----------|---------
word 1 | word 2 | word 3
word 1 | `word 2` | word 3
word 1 | \`word 2 | word 3
word 1 | `word 2 | word 3
word 1 | `word |2` | word 3
words |`` some | code `` | more words
words |``` some | code ``` | more words
words |```` some | code ```` | more words
words |`` some ` | ` code `` | more words
words |``` some ` | ` code ``` | more words
words |```` some ` | ` code ```` | more words
2 changes: 1 addition & 1 deletion tests/extensions/test.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ smarty:
- markdown.extensions.smarty
extension_configs:
markdown.extensions.smarty:
smart_angled_quotes: True
smart_angled_quotes: True