Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More selective escaping of -#.) (alternative approach) #149

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,20 @@ def escape(self, text):
if not text:
return ''
if self.options['escape_misc']:
text = re.sub(r'([\\&<`[>~#=+|-])', r'\\\1', text)
text = re.sub(r'([0-9])([.)])', r'\1\\\2', text)
text = re.sub(r'([\\&<`[>~=+|])', r'\\\1', text)
# A sequence of one or more consecutive '-', preceded and
# followed by whitespace or start/end of fragment, might
# be confused with an underline of a header, or with a
# list marker.
text = re.sub(r'(\s|^)(-+(?:\s|$))', r'\1\\\2', text)
# A sequence of up to six consecutive '#', preceded and
# followed by whitespace or start/end of fragment, might
# be confused with an ATX heading.
text = re.sub(r'(\s|^)(#{1,6}(?:\s|$))', r'\1\\\2', text)
# '.' or ')' preceded by up to nine digits might be
# confused with a list item.
text = re.sub(r'((?:\s|^)[0-9]{1,9})([.)](?:\s|$))', r'\1\\\2',
text)
if self.options['escape_asterisks']:
text = text.replace('*', r'\*')
if self.options['escape_underscores']:
Expand Down
31 changes: 27 additions & 4 deletions tests/test_escaping.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,43 @@ def test_single_escaping_entities():
assert md('&amp;amp;') == r'\&amp;'


def text_misc():
def test_misc():
assert md('\\*') == r'\\\*'
assert md('<foo>') == r'\<foo\>'
assert md('&lt;foo>') == r'\<foo\>'
assert md('# foo') == r'\# foo'
assert md('#5') == r'#5'
assert md('5#') == '5#'
assert md('####### foo') == r'####### foo'
assert md('> foo') == r'\> foo'
assert md('~~foo~~') == r'\~\~foo\~\~'
assert md('foo\n===\n') == 'foo\n\\=\\=\\=\n'
assert md('---\n') == '\\-\\-\\-\n'
assert md('---\n') == '\\---\n'
assert md('- test') == r'\- test'
assert md('x - y') == r'x \- y'
assert md('test-case') == 'test-case'
assert md('x-') == 'x-'
assert md('-y') == '-y'
assert md('+ x\n+ y\n') == '\\+ x\n\\+ y\n'
assert md('`x`') == r'\`x\`'
assert md('[text](link)') == r'\[text](link)'
assert md('1. x') == r'1\. x'
# assert md('1<span>.</span> x') == r'1\. x'
assert md('<span>1.</span> x') == r'1\. x'
assert md(' 1. x') == r' 1\. x'
assert md('123456789. x') == r'123456789\. x'
assert md('1234567890. x') == r'1234567890. x'
assert md('A1. x') == r'A1. x'
assert md('1.2') == r'1.2'
assert md('not a number. x') == r'not a number. x'
assert md('1) x') == r'1\) x'
# assert md('1<span>)</span> x') == r'1\) x'
assert md('<span>1)</span> x') == r'1\) x'
assert md(' 1) x') == r' 1\) x'
assert md('123456789) x') == r'123456789\) x'
assert md('1234567890) x') == r'1234567890) x'
assert md('(1) x') == r'(1) x'
assert md('A1) x') == r'A1) x'
assert md('1)x') == r'1)x'
assert md('not a number) x') == r'not a number) x'
assert md('|not table|') == r'\|not table\|'
assert md(r'\ <foo> &amp;amp; | ` `', escape_misc=False) == r'\ <foo> &amp; | ` `'
assert md(r'\ &lt;foo> &amp;amp; | ` `', escape_misc=False) == r'\ <foo> &amp; | ` `'
Loading