Skip to content

Commit bd6b581

Browse files
committed
add option to not escape underscores
closes #59
1 parent c8f7cf6 commit bd6b581

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ code_language
102102
should be annotated with `````python`` or similar.
103103
Defaults to ``''`` (empty string) and can be any string.
104104

105+
escape_underscores
106+
If set to ``False``, do not escape ``_`` to ``\_`` in text.
107+
Defaults to ``True``.
108+
105109
Options may be specified as kwargs to the ``markdownify`` function, or as a
106110
nested ``Options`` class in ``MarkdownConverter`` subclasses.
107111

markdownify/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
UNDERSCORE = '_'
2626

2727

28-
def escape(text):
28+
def escape(text, escape_underscores):
2929
if not text:
3030
return ''
31-
return text.replace('_', r'\_')
31+
if escape_underscores:
32+
return text.replace('_', r'\_')
33+
return text
3234

3335

3436
def chomp(text):
@@ -68,15 +70,16 @@ class MarkdownConverter(object):
6870
class DefaultOptions:
6971
autolinks = True
7072
bullets = '*+-' # An iterable of bullet types.
73+
code_language = ''
7174
convert = None
7275
default_title = False
76+
escape_underscores = True
7377
heading_style = UNDERLINED
7478
newline_style = SPACES
7579
strip = None
7680
strong_em_symbol = ASTERISK
7781
sub_symbol = ''
7882
sup_symbol = ''
79-
code_language = ''
8083

8184
class Options(DefaultOptions):
8285
pass
@@ -155,7 +158,7 @@ def process_text(self, el):
155158
text = whitespace_re.sub(' ', text)
156159

157160
if el.parent.name != 'code':
158-
text = escape(text)
161+
text = escape(text, self.options['escape_underscores'])
159162

160163
# remove trailing whitespaces if any of the following condition is true:
161164
# - current text node is the last node in li

tests/test_escaping.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
def test_underscore():
55
assert md('_hey_dude_') == r'\_hey\_dude\_'
6+
assert md('_hey_dude_', escape_underscores=False) == r'_hey_dude_'
67

78

89
def test_xml_entities():

0 commit comments

Comments
 (0)