Skip to content

Commit 4aa6cf2

Browse files
committed
rewrote text processing to not escape _ in code
fixes matthewwithanm#47
1 parent 828e116 commit 4aa6cf2

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

markdownify/__init__.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,26 @@ def is_nested_node(el):
142142
return text
143143

144144
def process_text(self, el):
145-
text = six.text_type(el)
145+
text = six.text_type(el) or ''
146146

147147
# dont remove any whitespace when handling pre or code in pre
148-
if (el.parent.name == 'pre'
149-
or (el.parent.name == 'code' and el.parent.parent.name == 'pre')):
150-
return escape(text or '')
148+
if not (el.parent.name == 'pre'
149+
or (el.parent.name == 'code'
150+
and el.parent.parent.name == 'pre')):
151+
text = whitespace_re.sub(' ', text)
151152

152-
cleaned_text = escape(whitespace_re.sub(' ', text or ''))
153+
if el.parent.name != 'code':
154+
text = escape(text)
153155

154156
# remove trailing whitespaces if any of the following condition is true:
155157
# - current text node is the last node in li
156158
# - current text node is followed by an embedded list
157-
if el.parent.name == 'li' and (not el.next_sibling or el.next_sibling.name in ['ul', 'ol']):
158-
return cleaned_text.rstrip()
159+
if (el.parent.name == 'li'
160+
and (not el.next_sibling
161+
or el.next_sibling.name in ['ul', 'ol'])):
162+
text = text.rstrip()
159163

160-
return cleaned_text
164+
return text
161165

162166
def __getattr__(self, attr):
163167
# Handle headings

tests/test_conversions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def test_br():
7474

7575
def test_code():
7676
inline_tests('code', '`')
77+
assert md('<code>this_should_not_escape</code>') == '`this_should_not_escape`'
7778

7879

7980
def test_del():

0 commit comments

Comments
 (0)