Skip to content

Commit 2b22d23

Browse files
committed
avoid text normalization/escaping in any preformatted/code context
Signed-off-by: chrispy <chrispy@synopsys.com>
1 parent e6e23fd commit 2b22d23

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

markdownify/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,12 @@ def is_nested_node(el):
152152
def process_text(self, el):
153153
text = six.text_type(el) or ''
154154

155-
# dont remove any whitespace when handling pre or code in pre
156-
if not (el.parent.name == 'pre'
157-
or (el.parent.name == 'code'
158-
and el.parent.parent.name == 'pre')):
155+
# normalize whitespace if we're not inside a preformatted element
156+
if not el.find_parent('pre'):
159157
text = whitespace_re.sub(' ', text)
160158

161-
if el.parent.name != 'code' and el.parent.name != 'pre':
159+
# escape special characters if we're not inside a preformatted or code element
160+
if not el.find_parent(['pre', 'code', 'kbd', 'samp']):
162161
text = self.escape(text)
163162

164163
# remove trailing whitespaces if any of the following condition is true:

tests/test_conversions.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ def test_br():
7070

7171
def test_code():
7272
inline_tests('code', '`')
73-
assert md('<code>this_should_not_escape</code>') == '`this_should_not_escape`'
73+
assert md('<code>*this_should_not_escape*</code>') == '`*this_should_not_escape*`'
74+
assert md('<kbd>*this_should_not_escape*</kbd>') == '`*this_should_not_escape*`'
75+
assert md('<samp>*this_should_not_escape*</samp>') == '`*this_should_not_escape*`'
76+
assert md('<code><span>*this_should_not_escape*</span></code>') == '`*this_should_not_escape*`'
77+
assert md('<code>this should\t\tnormalize</code>') == '`this should normalize`'
78+
assert md('<code><span>this should\t\tnormalize</span></code>') == '`this should normalize`'
7479

7580

7681
def test_del():
@@ -187,7 +192,10 @@ def test_p():
187192
def test_pre():
188193
assert md('<pre>test\n foo\nbar</pre>') == '\n```\ntest\n foo\nbar\n```\n'
189194
assert md('<pre><code>test\n foo\nbar</code></pre>') == '\n```\ntest\n foo\nbar\n```\n'
190-
assert md('<pre>this_should_not_escape</pre>') == '\n```\nthis_should_not_escape\n```\n'
195+
assert md('<pre>*this_should_not_escape*</pre>') == '\n```\n*this_should_not_escape*\n```\n'
196+
assert md('<pre><span>*this_should_not_escape*</span></pre>') == '\n```\n*this_should_not_escape*\n```\n'
197+
assert md('<pre>\t\tthis should\t\tnot normalize</pre>') == '\n```\n\t\tthis should\t\tnot normalize\n```\n'
198+
assert md('<pre><span>\t\tthis should\t\tnot normalize</span></pre>') == '\n```\n\t\tthis should\t\tnot normalize\n```\n'
191199

192200

193201
def test_s():

0 commit comments

Comments
 (0)