Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVonB committed Jan 18, 2022
2 parents 9231704 + d3eff11 commit 28793ac
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ code_language
should be annotated with `````python`` or similar.
Defaults to ``''`` (empty string) and can be any string.

escape_underscores
If set to ``False``, do not escape ``_`` to ``\_`` in text.
Defaults to ``True``.

Options may be specified as kwargs to the ``markdownify`` function, or as a
nested ``Options`` class in ``MarkdownConverter`` subclasses.

Expand Down
11 changes: 7 additions & 4 deletions markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
UNDERSCORE = '_'


def escape(text):
def escape(text, escape_underscores):
if not text:
return ''
return text.replace('_', r'\_')
if escape_underscores:
return text.replace('_', r'\_')
return text


def chomp(text):
Expand Down Expand Up @@ -68,15 +70,16 @@ class MarkdownConverter(object):
class DefaultOptions:
autolinks = True
bullets = '*+-' # An iterable of bullet types.
code_language = ''
convert = None
default_title = False
escape_underscores = True
heading_style = UNDERLINED
newline_style = SPACES
strip = None
strong_em_symbol = ASTERISK
sub_symbol = ''
sup_symbol = ''
code_language = ''

class Options(DefaultOptions):
pass
Expand Down Expand Up @@ -155,7 +158,7 @@ def process_text(self, el):
text = whitespace_re.sub(' ', text)

if el.parent.name != 'code':
text = escape(text)
text = escape(text, self.options['escape_underscores'])

# remove trailing whitespaces if any of the following condition is true:
# - current text node is the last node in li
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
pkgmeta = {
'__title__': 'markdownify',
'__author__': 'Matthew Tretter',
'__version__': '0.10.1',
'__version__': '0.10.2',
}


Expand Down
1 change: 1 addition & 0 deletions tests/test_escaping.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

def test_underscore():
assert md('_hey_dude_') == r'\_hey\_dude\_'
assert md('_hey_dude_', escape_underscores=False) == r'_hey_dude_'


def test_xml_entities():
Expand Down

0 comments on commit 28793ac

Please sign in to comment.