-
Couldn't load subscription status.
- Fork 887
Move backslash unescaping to treeprocessor #1272
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
| License: BSD (see LICENSE.md for details). | ||
| """ | ||
|
|
||
| import re | ||
| import xml.etree.ElementTree as etree | ||
| from . import util | ||
| from . import inlinepatterns | ||
|
|
@@ -29,6 +30,7 @@ def build_treeprocessors(md, **kwargs): | |
| treeprocessors = util.Registry() | ||
| treeprocessors.register(InlineProcessor(md), 'inline', 20) | ||
| treeprocessors.register(PrettifyTreeprocessor(md), 'prettify', 10) | ||
| treeprocessors.register(UnescapeTreeprocessor(md), 'unescape', 0) | ||
| return treeprocessors | ||
|
|
||
|
|
||
|
|
@@ -429,3 +431,28 @@ def run(self, root): | |
| # Only prettify code containing text only | ||
| if not len(code) and code.text is not None: | ||
| code.text = util.AtomicString(code.text.rstrip() + '\n') | ||
|
|
||
|
|
||
| class UnescapeTreeprocessor(Treeprocessor): | ||
| """ Restore escaped chars """ | ||
|
|
||
| RE = re.compile(r'{}(\d+){}'.format(util.STX, util.ETX)) | ||
|
|
||
| def _unescape(self, m): | ||
| return chr(int(m.group(1))) | ||
|
|
||
| def unescape(self, text): | ||
| return self.RE.sub(self._unescape, text) | ||
|
|
||
| def run(self, root): | ||
| """ Loop over all elements and unescape all text. """ | ||
| for elem in root.iter(): | ||
| # Unescape text content | ||
| if elem.text and not elem.tag == 'code': | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure we actually need to skip |
||
| elem.text = self.unescape(elem.text) | ||
| # Unescape tail content | ||
| if elem.tail: | ||
| elem.tail = self.unescape(elem.tail) | ||
| # Unescape attribute values | ||
| for key, value in elem.items(): | ||
| elem.set(key, self.unescape(value)) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ | |
| <p>Right bracket: ]</p> | ||
| <p>Left paren: (</p> | ||
| <p>Right paren: )</p> | ||
| <p>Greater-than: ></p> | ||
| <p>Greater-than: ></p> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the one and only change in behavior in the existing tests. I'm okay with this, however, as technically this results in valid output. The reason for the change is that the angle bracket gets escaped during serialization. Previously, a placeholder was there during serialization, which was swapped out for the actual character later. The whole point of this change was to better ensure valid HTML output, so this is an acceptable change in behavior. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having unescaped |
||
| <p>Hash: #</p> | ||
| <p>Period: .</p> | ||
| <p>Bang: !</p> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """ | ||
| Python Markdown | ||
|
|
||
| A Python implementation of John Gruber's Markdown. | ||
|
|
||
| Documentation: https://python-markdown.github.io/ | ||
| GitHub: https://github.com/Python-Markdown/markdown/ | ||
| PyPI: https://pypi.org/project/Markdown/ | ||
|
|
||
| Started by Manfred Stienstra (http://www.dwerg.net/). | ||
| Maintained for a few years by Yuri Takhteyev (http://www.freewisdom.org). | ||
| Currently maintained by Waylan Limberg (https://github.com/waylan), | ||
| Dmitry Shachnev (https://github.com/mitya57) and Isaac Muse (https://github.com/facelessuser). | ||
|
|
||
| Copyright 2007-2022 The Python Markdown Project (v. 1.7 and later) | ||
| Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b) | ||
| Copyright 2004 Manfred Stienstra (the original version) | ||
|
|
||
| License: BSD (see LICENSE.md for details). | ||
| """ | ||
|
|
||
| from markdown.test_tools import TestCase | ||
|
|
||
|
|
||
| class TestSmarty(TestCase): | ||
|
|
||
| default_kwargs = {'extensions': ['smarty']} | ||
|
|
||
| def test_escaped_attr(self): | ||
| self.assertMarkdownRenders( | ||
| '', | ||
| '<p><img alt="x"x" src="x" /></p>' | ||
| ) | ||
|
|
||
| # TODO: Move rest of smarty tests here. |
Uh oh!
There was an error while loading. Please reload this page.