diff --git a/html2text/utils.py b/html2text/utils.py index 8f77668..8ecd2ce 100644 --- a/html2text/utils.py +++ b/html2text/utils.py @@ -256,6 +256,8 @@ def reformat_table(lines: List[str], right_margin: int) -> List[str]: Given the lines of a table padds the cells and returns the new lines """ + if not lines: + return lines # find the maximum width of the columns max_width = [len(x.rstrip()) + right_margin for x in lines[0].split("|")] max_cols = len(max_width) diff --git a/test/test_html2text.py b/test/test_html2text.py index 1f16b4f..8949a17 100644 --- a/test/test_html2text.py +++ b/test/test_html2text.py @@ -254,3 +254,14 @@ def test_strong_emptied() -> None: h.strong_mark = "" string = "A B C." assert h.handle(string) == "A B _C_.\n\n" + + +def test_pad_tables_empty_table() -> None: + """An empty table block should not crash table padding.""" + from html2text import config + from html2text.utils import pad_tables_in_text + + # Two consecutive markers leave the table buffer empty, which used to + # raise IndexError on lines[0]. + marker = config.TABLE_MARKER_FOR_PAD + pad_tables_in_text(f"{marker}\n{marker}\n")