From 2ec33384de85d0906b4b40a59f1a3650846150cb Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Sun, 23 Jun 2024 13:17:20 +0200 Subject: [PATCH] handle un-parsable colspan values fixes #126 --- markdownify/__init__.py | 6 +++--- tests/test_tables.py | 14 +++++++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/markdownify/__init__.py b/markdownify/__init__.py index eaa6ded..6a983d9 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -383,13 +383,13 @@ class MarkdownConverter(object): def convert_td(self, el, text, convert_as_inline): colspan = 1 - if 'colspan' in el.attrs: + if 'colspan' in el.attrs and el['colspan'].isdigit(): colspan = int(el['colspan']) return ' ' + text.strip().replace("\n", " ") + ' |' * colspan def convert_th(self, el, text, convert_as_inline): colspan = 1 - if 'colspan' in el.attrs: + if 'colspan' in el.attrs and el['colspan'].isdigit(): colspan = int(el['colspan']) return ' ' + text.strip().replace("\n", " ") + ' |' * colspan @@ -406,7 +406,7 @@ class MarkdownConverter(object): # first row and is headline: print headline underline full_colspan = 0 for cell in cells: - if "colspan" in cell.attrs: + if 'colspan' in cell.attrs and cell['colspan'].isdigit(): full_colspan += int(cell["colspan"]) else: full_colspan += 1 diff --git a/tests/test_tables.py b/tests/test_tables.py index 9120c29..594e5bf 100644 --- a/tests/test_tables.py +++ b/tests/test_tables.py @@ -215,7 +215,7 @@ table_with_colspan = """ - + @@ -226,6 +226,17 @@ table_with_colspan = """
Age
JillJill Smith 50
""" +table_with_undefined_colspan = """ + + + + + + + + +
NameAge
JillSmith
""" + def test_table(): assert md(table) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n' @@ -240,3 +251,4 @@ def test_table(): 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\n' assert md(table_with_colspan) == '\n\n| Name | | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n' + assert md(table_with_undefined_colspan) == '\n\n| Name | Age |\n| --- | --- |\n| Jill | Smith |\n\n'