Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(docreader): keep the footer rows of an HTML table in that table (#4441)

Lukasdc881cb48bb0
diff --git a/docreader/parser/mhtml_parser.py b/docreader/parser/mhtml_parser.py
index 4fdd143fd..0ad1bf8ad 100644
--- a/docreader/parser/mhtml_parser.py
+++ b/docreader/parser/mhtml_parser.py
@@ -329,6 +329,7 @@ class MHTMLParser(BaseParser):
self._unwrap_whitespace_only_inline(soup)
text_fallback = soup.get_text(separator="\n", strip=True)
self._escape_table_cell_pipes(soup)
+ self._move_table_footer_rows_after_body(soup)
markdown_text = md(str(soup), heading_style="ATX")
result = self._normalize_markdown(markdown_text)
if not result and text_fallback:
@@ -366,6 +367,32 @@ class MHTMLParser(BaseParser):
if tag.find(True) is None and not tag.get_text().strip():
tag.unwrap()
+ @staticmethod
+ def _move_table_footer_rows_after_body(soup: BeautifulSoup) -> None:
+ """Read the rows of a ``<tfoot>`` as the last rows of their table.
+
+ markdownify takes the first row of a ``<tfoot>`` for the start of a
+ table without a header and writes an empty header row and a
+ separator line in front of it, in the middle of the table. A browser
+ draws the footer after the body wherever it sits in the source, so
+ its rows go after the table's last body row.
+ """
+ for tfoot in soup.find_all("tfoot"):
+ table = tfoot.find_parent("table")
+ if table is None:
+ continue
+ own_rows = [
+ row for row in table.find_all("tr") if row.find_parent("table") is table
+ ]
+ footer_rows = [row for row in own_rows if row.find_parent("tfoot") is tfoot]
+ body_rows = [row for row in own_rows if row.find_parent("tfoot") is None]
+ if not body_rows:
+ continue
+ last = body_rows[-1]
+ for row in reversed(footer_rows):
+ last.insert_after(row.extract())
+ tfoot.decompose()
+
@staticmethod
def _escape_table_cell_pipes(soup: BeautifulSoup) -> None:
"""Escape the ``|`` in table cell text, which markdownify writes as is.
diff --git a/docreader/tests/test_html_table_footer.py b/docreader/tests/test_html_table_footer.py
new file mode 100644
index 000000000..2686c2d68
--- /dev/null
+++ b/docreader/tests/test_html_table_footer.py
@@ -0,0 +1,56 @@
+"""A table's ``<tfoot>`` rows stay rows of that table, after its body rows."""
+
+import unittest
+
+from docreader.parser.html_parser import HTMLParser
+
+
+def _markdown(html: str) -> str:
+ return HTMLParser(file_name="footer.html", file_type="html").parse_into_text(
+ html.encode()
+ ).content
+
+
+class HTMLTableFooterTest(unittest.TestCase):
+ def test_footer_rows_follow_the_body_rows(self):
+ # markdownify wrote an empty header row and a second separator line
+ # before the footer, in the middle of the table
+ markdown = _markdown(
+ "<table><thead><tr><th>Item</th><th>Qty</th></tr></thead>"
+ "<tbody><tr><td>pen</td><td>2</td></tr></tbody>"
+ "<tfoot><tr><td>Total</td><td>2</td></tr></tfoot></table>"
+ )
+
+ self.assertEqual(
+ markdown, "| Item | Qty |\n| --- | --- |\n| pen | 2 |\n| Total | 2 |"
+ )
+
+ def test_a_footer_written_before_the_body_still_comes_last(self):
+ markdown = _markdown(
+ "<table><thead><tr><th>Item</th><th>Qty</th></tr></thead>"
+ "<tfoot><tr><td>Total</td><td>3</td></tr></tfoot>"
+ "<tbody><tr><td>pen</td><td>2</td></tr><tr><td>ink</td><td>1</td></tr>"
+ "</tbody></table>"
+ )
+
+ self.assertEqual(
+ markdown,
+ "| Item | Qty |\n| --- | --- |\n| pen | 2 |\n| ink | 1 |\n| Total | 3 |",
+ )
+
+ def test_a_nested_table_footer_stays_in_the_nested_table(self):
+ markdown = _markdown(
+ "<table><tr><th>Outer</th></tr><tr><td>"
+ "<table><tr><th>Inner</th></tr><tr><td>a</td></tr>"
+ "<tfoot><tr><td>inner total</td></tr></tfoot></table>"
+ "</td></tr><tfoot><tr><td>outer total</td></tr></tfoot></table>"
+ )
+
+ # markdownify flattens the inner table into one cell; its footer row
+ # has to stay inside it rather than move after the outer body
+ self.assertLess(markdown.index("inner total"), markdown.index("outer total"))
+ self.assertTrue(markdown.rstrip().endswith("| outer total |"))
+
+
+if __name__ == "__main__":
+ unittest.main()