diff options
author | Mustafa Haddara <mhaddara@venasolutions.com> | 2015-10-22 10:33:54 -0400 |
---|---|---|
committer | Mustafa Haddara <mustafaayhaddara@yahoo.com> | 2015-10-24 23:40:30 -0400 |
commit | fb65ed196df567ab71d63029e7ab36002b12112e (patch) | |
tree | 22e7486cdf9a1744bd3ef97db907bcb031fc015a /markdown | |
parent | f3a68faffae9fd728c689b4732c1d3397d1d3faa (diff) | |
download | markdown-fb65ed196df567ab71d63029e7ab36002b12112e.tar.gz markdown-fb65ed196df567ab71d63029e7ab36002b12112e.tar.bz2 markdown-fb65ed196df567ab71d63029e7ab36002b12112e.zip |
Fixed handling of table cell split
Diffstat (limited to 'markdown')
-rw-r--r-- | markdown/extensions/tables.py | 51 |
1 files changed, 49 insertions, 2 deletions
diff --git a/markdown/extensions/tables.py b/markdown/extensions/tables.py index 368321d..494aaeb 100644 --- a/markdown/extensions/tables.py +++ b/markdown/extensions/tables.py @@ -19,6 +19,7 @@ from __future__ import absolute_import from __future__ import unicode_literals from . import Extension from ..blockprocessors import BlockProcessor +from ..inlinepatterns import BacktickPattern, BACKTICK_RE from ..util import etree @@ -72,7 +73,11 @@ class TableProcessor(BlockProcessor): 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: @@ -85,7 +90,49 @@ class TableProcessor(BlockProcessor): 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) + # 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): |