diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 2d5daf1..44d729d 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -143,6 +143,18 @@ class MarkdownConverter(object): return '' return '%s*%s*%s' % (prefix, text, suffix) + def convert_code(self, el, text): + prefix, suffix, text = chomp(text) + if not text: + return '' + return '%s`%s`%s' % (prefix, text, suffix) + + def convert_samp(self, el, text): + return self.convert_code(el, text) + + def convert_kbd(self, el, text): + return self.convert_code(el, text) + def convert_hn(self, n, el, text): style = self.options['heading_style'] text = text.rstrip() diff --git a/tests/test_advanced.py b/tests/test_advanced.py index 4c480d7..7694fa1 100644 --- a/tests/test_advanced.py +++ b/tests/test_advanced.py @@ -4,3 +4,11 @@ from markdownify import markdownify as md def test_nested(): text = md('
This is an example link.
') assert text == 'This is an [example link](http://example.com/).\n\n' + + +def test_code_with_tricky_content(): + assert md('>') == "`>`"
+ assert md('/home/username') == "`/home/`**username**"
+ # convert_br() adds trailing spaces (why?); ignore them by using 2 tests,
+ assert md('Line1
Line2').startswith("`Line1")
+ assert md('Line1
Line2').endswith("\nLine2`")
diff --git a/tests/test_conversions.py b/tests/test_conversions.py
index 12ce36b..5b7db1f 100644
--- a/tests/test_conversions.py
+++ b/tests/test_conversions.py
@@ -94,6 +94,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{tag}>') == '`Hello`'
+ assert md(f'foo <{tag}>Hello{tag}> bar') == 'foo `Hello` bar'
+ assert md(f'foo<{tag}> Hello{tag}> bar') == 'foo `Hello` bar'
+ assert md(f'foo <{tag}>Hello {tag}>bar') == 'foo `Hello` bar'
+ assert md(f'foo <{tag}>{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('