Skip to content
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
23 changes: 20 additions & 3 deletions markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
# confused with a list item
re_escape_misc_list_items = re.compile(r'((?:\s|^)[0-9]{1,9})([.)](?:\s|$))')

# Find consecutive backtick sequences in a string
re_backtick_runs = re.compile(r'`+')

# Heading styles
ATX = 'atx'
ATX_CLOSED = 'atx_closed'
Expand Down Expand Up @@ -480,10 +483,24 @@ def convert_br(self, el, text, parent_tags):
return ' \n'

def convert_code(self, el, text, parent_tags):
if 'pre' in parent_tags:
if '_noformat' in parent_tags:
return text
converter = abstract_inline_conversion(lambda self: '`')
return converter(self, el, text, parent_tags)

prefix, suffix, text = chomp(text)
if not text:
return ''

# Find the maximum number of consecutive backticks in the text, then
# delimit the code span with one more backtick than that
max_backticks = max((len(match) for match in re.findall(re_backtick_runs, text)), default=0)
markup_delimiter = '`' * (max_backticks + 1)

# If the maximum number of backticks is greater than zero, add a space
# to avoid interpretation of inside backticks as literals
if max_backticks > 0:
text = " " + text + " "

return '%s%s%s%s%s' % (prefix, markup_delimiter, text, markup_delimiter, suffix)

convert_del = abstract_inline_conversion(lambda self: '~~')

Expand Down
3 changes: 3 additions & 0 deletions tests/test_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ def test_code():
assert md('<code>foo<s> bar </s>baz</code>') == '`foo bar baz`'
assert md('<code>foo<sup>bar</sup>baz</code>', sup_symbol='^') == '`foobarbaz`'
assert md('<code>foo<sub>bar</sub>baz</code>', sub_symbol='^') == '`foobarbaz`'
assert md('foo<code>`bar`</code>baz') == 'foo`` `bar` ``baz'
assert md('foo<code>``bar``</code>baz') == 'foo``` ``bar`` ```baz'
assert md('foo<code> `bar` </code>baz') == 'foo `` `bar` `` baz'


def test_dl():
Expand Down