From d5fb0fbb859f7c9881d04894d75129d4e17fe83f Mon Sep 17 00:00:00 2001 From: Chris Papademetrious Date: Tue, 26 Mar 2024 16:41:56 -0400 Subject: [PATCH] make sure there are blank lines around table/figure captions (#114) Signed-off-by: chrispy Co-authored-by: AlexVonB --- markdownify/__init__.py | 6 ++++++ tests/test_conversions.py | 5 +++++ tests/test_tables.py | 9 +++++++++ 3 files changed, 20 insertions(+) diff --git a/markdownify/__init__.py b/markdownify/__init__.py index b67c32b..cab4f36 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -369,6 +369,12 @@ def convert_style(self, el, text, convert_as_inline): def convert_table(self, el, text, convert_as_inline): return '\n\n' + text + '\n' + def convert_caption(self, el, text, convert_as_inline): + return text + '\n' + + def convert_figcaption(self, el, text, convert_as_inline): + return '\n\n' + text + '\n\n' + def convert_td(self, el, text, convert_as_inline): return ' ' + text.strip().replace("\n", " ") + ' |' diff --git a/tests/test_conversions.py b/tests/test_conversions.py index ae56837..1e685f3 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -74,6 +74,11 @@ def test_br(): assert md('a
b
c', newline_style=BACKSLASH) == 'a\\\nb\\\nc' +def test_caption(): + assert md('TEXT
Caption
SPAN
') == 'TEXT\n\nCaption\n\nSPAN' + assert md('
SPAN
Caption
TEXT') == 'SPAN\n\nCaption\n\nTEXT' + + def test_code(): inline_tests('code', '`') assert md('*this_should_not_escape*') == '`*this_should_not_escape*`' diff --git a/tests/test_tables.py b/tests/test_tables.py index ebbb146..9be876d 100644 --- a/tests/test_tables.py +++ b/tests/test_tables.py @@ -201,6 +201,14 @@ """ +table_with_caption = """TEXT + + + + + +
Caption
FirstnameLastnameAge
""" + def test_table(): assert md(table) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n' @@ -213,3 +221,4 @@ def test_table(): assert md(table_missing_text) == '\n\n| | Lastname | Age |\n| --- | --- | --- |\n| Jill | | 50 |\n| Eve | Jackson | 94 |\n\n' assert md(table_missing_head) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n' assert md(table_body) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n' + assert md(table_with_caption) == 'TEXT\n\nCaption\n| Firstname | Lastname | Age |\n\n'