document2md.tables

Turns the raw HTML tables mineru emits into Markdown tables.

Turn the raw HTML tables mineru emits into Markdown tables.

mineru renders simple tables as Markdown but falls back to raw HTML (<table>…</table> with rowspan/colspan) for anything complex. This module rewrites those HTML tables into GitHub Markdown tables so document2md’s output is Markdown all the way through — no leftover HTML.

Implemented with the standard library’s html.parser only (no BeautifulSoup) to keep document2md’s dependencies to just requests and mineru.

html_tables_to_markdown(text: str) → str[source]

Replace every <table>…</table> block in text with a GitHub Markdown table, leaving everything else untouched. Blank lines are ensured around each converted table so it renders as a table.

class _TableParser[source]

Collect a single HTML table’s cells as rows of (text, attrs).

__init__()[source]

Initialize and reset this instance.

If convert_charrefs is true (the default), all character references are automatically converted to the corresponding Unicode characters.

If scripting is false (the default), the content of the noscript element is parsed normally; if it’s true, it’s returned as is without being parsed.

handle_starttag(tag, attrs)[source]

Open a new row on <tr>, a new cell (recording its attrs, for rowspan/colspan) on <td>/<th>, and turn <br> inside a cell into a space so wrapped lines don’t run together.

handle_endtag(tag)[source]

Close the current cell into self._row on </td>/</th>, and the current row into self.rows on </tr>.

handle_data(data)[source]

Append text content to the currently-open cell, if any.

_parse_rows(fragment: str) → list[list[tuple[str, dict]]][source]

Parse a single <table>…</table> fragment into _TableParser’s row structure.

_span(attrs: dict, name: str) → int[source]

Read attrs[name] (rowspan/colspan) as a positive int, defaulting to 1 when it is missing or not a valid integer.

_clean_cell(text: str) → str[source]

Collapse a cell’s whitespace to single spaces and escape | so it can’t be mistaken for a Markdown table column separator.

_grid(rows: list[list[tuple[str, dict]]]) → list[list[str]][source]

Flatten rows into a rectangular grid honouring rowspan/colspan. A spanned cell’s text goes in its top-left slot; the covered slots are filled with empty strings, since Markdown tables can’t merge cells.

_render(rows: list[list[tuple[str, dict]]]) → str[source]

Render _grid(rows) as a GitHub Markdown table (first row as the header), or “” if the table has no rows/columns.