From 3354f143d81d5774f0250b7d5e4f50e3d3d8cd12 Mon Sep 17 00:00:00 2001 From: Andrew Richards Date: Mon, 23 Nov 2020 16:16:10 +0000 Subject: [PATCH 1/3] Add method for tag Add method and tests for inline tag . --- markdownify/__init__.py | 12 ++++++++++++ tests/test_advanced.py | 8 ++++++++ tests/test_conversions.py | 21 +++++++++++++++++++++ 3 files changed, 41 insertions(+) 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') == '`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' From 92a73c8dfe4f84094bf892988797a616ef9d83f0 Mon Sep 17 00:00:00 2001 From: Andrew Richards Date: Thu, 26 Nov 2020 22:20:29 +0000 Subject: [PATCH 2/3] Correct test_code_with_tricky_content() Result of previous test didn't check for the trailing ' ' that convert_br() adds: This is needed to ensure that the resulting markdown not only has \n for the
but also renders it as a newline. --- tests/test_advanced.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/test_advanced.py b/tests/test_advanced.py index 7694fa1..585e868 100644 --- a/tests/test_advanced.py +++ b/tests/test_advanced.py @@ -9,6 +9,5 @@ def test_nested(): 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`") + assert md('First line blah blah
blah blah
second line') \ + == "First line `blah blah \nblah blah` second line" From 7685738344a27deeaa6148cd1cd117d69cb35967 Mon Sep 17 00:00:00 2001 From: Andrew Richards Date: Fri, 27 Nov 2020 14:18:08 +0000 Subject: [PATCH 3/3] Formatting tweak Change indent of continuation line; squashes a flake8 warning. --- tests/test_advanced.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_advanced.py b/tests/test_advanced.py index 585e868..7b62836 100644 --- a/tests/test_advanced.py +++ b/tests/test_advanced.py @@ -10,4 +10,4 @@ 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" + == "First line `blah blah \nblah blah` second line"