Move backslash unescaping to treeprocessor · Python-Markdown/markdown@c0f6e5a (original) (raw)
`@@ -19,6 +19,7 @@
`
19
19
`License: BSD (see LICENSE.md for details).
`
20
20
`"""
`
21
21
``
``
22
`+
import re
`
22
23
`import xml.etree.ElementTree as etree
`
23
24
`from . import util
`
24
25
`from . import inlinepatterns
`
`@@ -29,6 +30,7 @@ def build_treeprocessors(md, **kwargs):
`
29
30
`treeprocessors = util.Registry()
`
30
31
`treeprocessors.register(InlineProcessor(md), 'inline', 20)
`
31
32
`treeprocessors.register(PrettifyTreeprocessor(md), 'prettify', 10)
`
``
33
`+
treeprocessors.register(UnescapeTreeprocessor(md), 'unescape', 0)
`
32
34
`return treeprocessors
`
33
35
``
34
36
``
`@@ -429,3 +431,28 @@ def run(self, root):
`
429
431
`# Only prettify code containing text only
`
430
432
`if not len(code) and code.text is not None:
`
431
433
`code.text = util.AtomicString(code.text.rstrip() + '\n')
`
``
434
+
``
435
+
``
436
`+
class UnescapeTreeprocessor(Treeprocessor):
`
``
437
`+
""" Restore escaped chars """
`
``
438
+
``
439
`+
RE = re.compile(r'{}(\d+){}'.format(util.STX, util.ETX))
`
``
440
+
``
441
`+
def _unescape(self, m):
`
``
442
`+
return chr(int(m.group(1)))
`
``
443
+
``
444
`+
def unescape(self, text):
`
``
445
`+
return self.RE.sub(self._unescape, text)
`
``
446
+
``
447
`+
def run(self, root):
`
``
448
`+
""" Loop over all elements and unescape all text. """
`
``
449
`+
for elem in root.iter():
`
``
450
`+
Unescape text content
`
``
451
`+
if elem.text and not elem.tag == 'code':
`
``
452
`+
elem.text = self.unescape(elem.text)
`
``
453
`+
Unescape tail content
`
``
454
`+
if elem.tail:
`
``
455
`+
elem.tail = self.unescape(elem.tail)
`
``
456
`+
Unescape attribute values
`
``
457
`+
for key, value in elem.items():
`
``
458
`+
elem.set(key, self.unescape(value))
`