From bd6b5811223dc7f9c3a3f9e5afa1005ce9a005aa Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Tue, 18 Jan 2022 08:51:44 +0100 Subject: [PATCH 1/2] add option to not escape underscores closes #59 --- README.rst | 4 ++++ markdownify/__init__.py | 11 +++++++---- tests/test_escaping.py | 1 + 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 670ae6c..9a06912 100644 --- a/README.rst +++ b/README.rst @@ -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. diff --git a/markdownify/__init__.py b/markdownify/__init__.py index a9572c8..098c784 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -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): @@ -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 @@ -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 diff --git a/tests/test_escaping.py b/tests/test_escaping.py index 23a828c..3388b7e 100644 --- a/tests/test_escaping.py +++ b/tests/test_escaping.py @@ -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(): From d3eff116177cf245cae8ce8d6a79e853cf9b5a43 Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Tue, 18 Jan 2022 08:53:33 +0100 Subject: [PATCH 2/2] bump to v0.10.2 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index bc567ac..895ed62 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ pkgmeta = { '__title__': 'markdownify', '__author__': 'Matthew Tretter', - '__version__': '0.10.1', + '__version__': '0.10.2', }