From c232c25b3be16b860e6633435e4d5a4133312f0b Mon Sep 17 00:00:00 2001 From: facelessuser Date: Tue, 17 Jan 2017 21:09:09 -0700 Subject: [PATCH] Tables should add the escaping of pipes The table extension should have escaped pipes in order to adhere to the PHP Markdown extra implementation. In order to properly add escaped pipes, a bug relating to persisting appended escapes also had to be patched. Reference: #526 --- markdown/__init__.py | 6 +++--- markdown/extensions/tables.py | 2 ++ tests/extensions/extra/tables.html | 15 +++++++++++++++ tests/extensions/extra/tables.txt | 6 ++++++ tests/test_apis.py | 12 ++++++++++++ 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/markdown/__init__.py b/markdown/__init__.py index 78ea4cbe6..409f9cfd4 100644 --- a/markdown/__init__.py +++ b/markdown/__init__.py @@ -75,9 +75,6 @@ class Markdown(object): 'xhtml5': to_xhtml_string, } - ESCAPED_CHARS = ['\\', '`', '*', '_', '{', '}', '[', ']', - '(', ')', '>', '#', '+', '-', '.', '!'] - def __init__(self, *args, **kwargs): """ Creates a new Markdown instance. @@ -147,6 +144,9 @@ def __init__(self, *args, **kwargs): 'deprecated along with "safe_mode".', DeprecationWarning) + self.ESCAPED_CHARS = ['\\', '`', '*', '_', '{', '}', '[', ']', + '(', ')', '>', '#', '+', '-', '.', '!'] + self.registeredExtensions = [] self.docType = "" self.stripTopLevelTags = True diff --git a/markdown/extensions/tables.py b/markdown/extensions/tables.py index 4bb20767e..4f12bfd88 100644 --- a/markdown/extensions/tables.py +++ b/markdown/extensions/tables.py @@ -160,6 +160,8 @@ class TableExtension(Extension): def extendMarkdown(self, md, md_globals): """ Add an instance of TableProcessor to BlockParser. """ + if '|' not in md.ESCAPED_CHARS: + md.ESCAPED_CHARS.append('|') md.parser.blockprocessors.add('table', TableProcessor(md.parser), 'Table Tests \ + +

Test escaped pipes

+ + + + + + + + + + + + +
Column1Column 2
| |Pipes are okay in code and escaped. |
\ No newline at end of file diff --git a/tests/extensions/extra/tables.txt b/tests/extensions/extra/tables.txt index a9677ba8e..705fc14b3 100644 --- a/tests/extensions/extra/tables.txt +++ b/tests/extensions/extra/tables.txt @@ -90,3 +90,9 @@ Odd backticks | Even backticks Escapes | More Escapes ------- | ------ `` `\`` | `\` + +Test escaped pipes + +Column1 | Column 2 +------- | -------- +`|` \| | Pipes are okay in code and escaped. \| diff --git a/tests/test_apis.py b/tests/test_apis.py index e3de779dd..7b1214fba 100644 --- a/tests/test_apis.py +++ b/tests/test_apis.py @@ -758,3 +758,15 @@ def testExtensonConfigOptionBadFormat(self): """ self.create_config_file(config) self.assertRaises(yaml.YAMLError, parse_options, ['-c', self.tempfile]) + + +class TestEscapeAppend(unittest.TestCase): + """ Tests escape character append. """ + + def testAppend(self): + """ Test that appended escapes are only in the current instance. """ + md = markdown.Markdown() + md.ESCAPED_CHARS.append('|') + self.assertEqual('|' in md.ESCAPED_CHARS, True) + md2 = markdown.Markdown() + self.assertEqual('|' not in md2.ESCAPED_CHARS, True)