Package: python-markdown / 3.0.1-3

fix_double_escaping.diff Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
From: Isaac Muse <faceless.shop@gmail.com>
Date: Sun, 7 Oct 2018 17:23:02 -0600
Subject: Fix double escaping of block code

Origin: https://github.com/Python-Markdown/markdown/pull/727
---
 markdown/extensions/codehilite.py |  9 +++-
 tests/test_extensions.py          | 92 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 100 insertions(+), 1 deletion(-)

diff --git a/markdown/extensions/codehilite.py b/markdown/extensions/codehilite.py
index d204ebf..712618f 100644
--- a/markdown/extensions/codehilite.py
+++ b/markdown/extensions/codehilite.py
@@ -200,13 +200,20 @@ class CodeHilite(object):
 class HiliteTreeprocessor(Treeprocessor):
     """ Hilight source code in code blocks. """
 
+    def code_unescape(self, text):
+        """Unescape code."""
+        text = text.replace("&amp;", "&")
+        text = text.replace("&lt;", "<")
+        text = text.replace("&gt;", ">")
+        return text
+
     def run(self, root):
         """ Find code blocks and store in htmlStash. """
         blocks = root.iter('pre')
         for block in blocks:
             if len(block) == 1 and block[0].tag == 'code':
                 code = CodeHilite(
-                    block[0].text,
+                    self.code_unescape(block[0].text),
                     linenums=self.config['linenums'],
                     guess_lang=self.config['guess_lang'],
                     css_class=self.config['css_class'],
diff --git a/tests/test_extensions.py b/tests/test_extensions.py
index 5489887..86e4f05 100644
--- a/tests/test_extensions.py
+++ b/tests/test_extensions.py
@@ -250,6 +250,49 @@ class TestCodeHilite(TestCaseWithAssertStartsWith):
             '</code></pre>'
         )
 
+    def testDoubleEscape(self):
+        """ Test entity escape logic in indented code blocks. """
+
+        text = '\t:::html\n\t<span>This&amp;That</span>'
+        md = markdown.Markdown(
+            extensions=[markdown.extensions.codehilite.CodeHiliteExtension()]
+        )
+        if self.has_pygments:
+            self.assertEqual(
+                md.convert(text),
+                '<div class="codehilite"><pre>'
+                '<span></span>'
+                '<span class="p">&lt;</span><span class="nt">span</span><span class="p">&gt;</span>'
+                'This<span class="ni">&amp;amp;</span>That'
+                '<span class="p">&lt;/</span><span class="nt">span</span><span class="p">&gt;</span>'
+                '\n</pre></div>'
+            )
+        else:
+            self.assertEqual(
+                md.convert(text),
+                '<pre class="codehilite"><code class="language-html">'
+                '&lt;span&gt;This&amp;amp;That&lt;/span&gt;'
+                '</code></pre>'
+            )
+
+    def testHighlightAmps(self):
+        """ Test amp conversion logic. """
+
+        text = '\t:::text\n\t&\n\t&amp;\n\t&amp;amp;'
+        md = markdown.Markdown(
+            extensions=[markdown.extensions.codehilite.CodeHiliteExtension()]
+        )
+        if self.has_pygments:
+            self.assertEqual(
+                md.convert(text),
+                '<div class="codehilite"><pre><span></span>&amp;\n&amp;amp;\n&amp;amp;amp;\n</pre></div>'
+            )
+        else:
+            self.assertEqual(
+                md.convert(text),
+                '<pre class="codehilite"><code class="language-text">&amp;\n&amp;amp;\n&amp;amp;amp;</code></pre>'
+            )
+
 
 class TestFencedCode(TestCaseWithAssertStartsWith):
     """ Test fenced_code extension. """
@@ -406,6 +449,55 @@ line 3
         )
         self.assertTrue('<code class="language-python">' in md.convert(text))
 
+    def testFencedLanguageDoubleEscape(self):
+        """ Test entity escape logic in fences. """
+
+        text = '```html\n<span>This&amp;That</span>\n```'
+        md = markdown.Markdown(
+            extensions=[
+                markdown.extensions.codehilite.CodeHiliteExtension(),
+                'fenced_code'
+            ]
+        )
+        if self.has_pygments:
+            self.assertEqual(
+                md.convert(text),
+                '<div class="codehilite"><pre>'
+                '<span></span>'
+                '<span class="p">&lt;</span><span class="nt">span</span><span class="p">&gt;</span>'
+                'This<span class="ni">&amp;amp;</span>That'
+                '<span class="p">&lt;/</span><span class="nt">span</span><span class="p">&gt;</span>'
+                '\n</pre></div>'
+            )
+        else:
+            self.assertEqual(
+                md.convert(text),
+                '<pre class="codehilite"><code class="language-html">'
+                '&lt;span&gt;This&amp;amp;That&lt;/span&gt;'
+                '</code></pre>'
+            )
+
+    def testFencedAmps(self):
+        """ Test amp conversion. """
+
+        text = '```text\n&\n&amp;\n&amp;amp;\n```'
+        md = markdown.Markdown(
+            extensions=[
+                markdown.extensions.codehilite.CodeHiliteExtension(),
+                'fenced_code'
+            ]
+        )
+        if self.has_pygments:
+            self.assertEqual(
+                md.convert(text),
+                '<div class="codehilite"><pre><span></span>&amp;\n&amp;amp;\n&amp;amp;amp;\n</pre></div>'
+            )
+        else:
+            self.assertEqual(
+                md.convert(text),
+                '<pre class="codehilite"><code class="language-text">&amp;\n&amp;amp;\n&amp;amp;amp;</code></pre>'
+            )
+
 
 class TestMetaData(unittest.TestCase):
     """ Test MetaData extension. """