diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 6c64c60..4e442bd 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -205,6 +205,18 @@ class MarkdownConverter(object): return '' return '%s%s%s%s%s' % (prefix, em_tag, text, em_tag, suffix) + def convert_code(self, el, text, convert_as_inline): + prefix, suffix, text = chomp(text) + if not text: + return '' + return '%s`%s`%s' % (prefix, text, suffix) + + def convert_samp(self, el, text, convert_as_inline): + return self.convert_code(el, text, convert_as_inline) + + def convert_kbd(self, el, text, convert_as_inline): + return self.convert_code(el, text, convert_as_inline) + def convert_hn(self, n, el, text, convert_as_inline): if convert_as_inline: return text diff --git a/tests/test_advanced.py b/tests/test_advanced.py index 7ee61d2..2435cac 100644 --- a/tests/test_advanced.py +++ b/tests/test_advanced.py @@ -14,3 +14,10 @@ def test_ignore_comments(): def test_ignore_comments_with_other_tags(): text = md("example link") assert text == "[example link](http://example.com/)" + + +def test_code_with_tricky_content(): + assert md('>') == "`>`" + assert md('/home/username') == "`/home/`**username**" + assert md('First line blah blah
blah blah
second line') \ + == "First line `blah blah \nblah blah` second line" diff --git a/tests/test_conversions.py b/tests/test_conversions.py index 31fe7f2..b3b4233 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -240,6 +240,27 @@ def test_em_spaces(): assert md('foo bar') == 'foo bar' +def code_samp_kbd_tests(tag): + # Basically re-use test_em() and test_em_spaces(), + assert md(f'<{tag}>Hello') == '`Hello`' + assert md(f'foo <{tag}>Hello bar') == 'foo `Hello` bar' + assert md(f'foo<{tag}> Hello bar') == 'foo `Hello` bar' + assert md(f'foo <{tag}>Hello bar') == 'foo `Hello` bar' + assert md(f'foo <{tag}> bar') in ['foo bar', 'foo bar'] # Either is OK + + +def test_code(): + code_samp_kbd_tests('code') + + +def test_samp(): + code_samp_kbd_tests('samp') + + +def test_kbd(): + code_samp_kbd_tests('kbd') + + def test_h1(): assert md('

Hello

') == 'Hello\n=====\n\n'