From 05ea8dc58ab35e1d3e8bae5b84df77bd4e3dc14d Mon Sep 17 00:00:00 2001 From: Igor Dvorkin Date: Sun, 13 Dec 2020 17:39:08 +0000 Subject: [PATCH] Add many tests and support image tag --- markdownify/__init__.py | 3 +++ tests/test_conversions.py | 38 ++++++++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/markdownify/__init__.py b/markdownify/__init__.py index cb12d43..2cd8fc8 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -229,6 +229,9 @@ class MarkdownConverter(object): src = el.attrs.get('src', None) or '' title = el.attrs.get('title', None) or '' title_part = ' "%s"' % title.replace('"', r'\"') if title else '' + if convert_as_inline: + return alt + return '![%s](%s%s)' % (alt, src, title_part) diff --git a/tests/test_conversions.py b/tests/test_conversions.py index ab1ce05..f5fc1c2 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -107,12 +107,42 @@ def test_hn(): assert md('
Hello
') == '###### Hello\n\n' -def test_hn_nested_tag(): - assert md('

A Bold C

') == '### A **Bold** C\n\n' - assert md('

A

P

C

') == '### A P C\n\n' +def test_hn_nested_tag_heading_style(): assert md('

A

P

C

', heading_style=ATX_CLOSED) == '# A P C #\n\n' assert md('

A

P

C

', heading_style=ATX) == '# A P C\n\n' - assert md('

A
BQ
C

') == '### A BQ C\n\n' + + +def test_hn_nested_simple_tag(): + tag_to_markdown = [ + ("strong", "**strong**"), + ("b", "**b**"), + ("em", "*em*"), + ("i", "*i*"), + ("p", "p"), + ("a", "a"), + ("div", "div"), + ("blockquote", "blockquote"), + ] + + for tag, markdown in tag_to_markdown: + assert md('

A <' + tag + '>' + tag + ' B

') == '### A ' + markdown + ' B\n\n' + + assert md('

A
B

', heading_style=ATX) == '### A B\n\n' + + # Nested lists not supported + # assert md('

A

', heading_style=ATX) == '### A li1 li2 B\n\n' + + +def test_hn_nested_img(): + assert md('Alt text') == '![Alt text](/path/to/img.jpg "Optional title")' + assert md('Alt text') == '![Alt text](/path/to/img.jpg)' + image_attributes_to_markdown = [ + ("", ""), + ("alt='Alt Text'", "Alt Text"), + ("alt='Alt Text' title='Optional title'", "Alt Text"), + ] + for image_attributes, markdown in image_attributes_to_markdown: + assert md('

A B

') == '### A ' + markdown + ' B\n\n' def test_hr():