|
| 1 | +from typing import TYPE_CHECKING |
| 2 | + |
| 3 | +from markdownify import MarkdownConverter |
| 4 | + |
| 5 | +if TYPE_CHECKING: |
| 6 | + from bs4 import Tag |
| 7 | + |
| 8 | + |
| 9 | +class _TableComplexityException(Exception): |
| 10 | + pass |
| 11 | + |
| 12 | + |
| 13 | +class _GFMTableConverter(MarkdownConverter): |
| 14 | + """ |
| 15 | + Custom converter that detects complex table features. |
| 16 | +
|
| 17 | + The base markdownify library will silently convert complex HTML tables to |
| 18 | + GFM pipe tables, but this loses information (e.g., merged cells become |
| 19 | + empty cells). This custom converter detects such cases and raises an |
| 20 | + exception to allow fallback to HTML. |
| 21 | +
|
| 22 | + Background: |
| 23 | + - GFM markdown tables don't support colspan/rowspan |
| 24 | + - markdownify doesn't provide rowspan support (issue #121): |
| 25 | + https://github.com/matthewwithanm/python-markdownify/issues/121 |
| 26 | + - Instead of silent data loss, we detect complexity and preserve HTML |
| 27 | +
|
| 28 | + Raises TableComplexityException when encountering: |
| 29 | + - colspan > 1 |
| 30 | + - rowspan > 1 |
| 31 | + - Multiple tbody sections |
| 32 | + """ |
| 33 | + |
| 34 | + def __init__(self, **options): |
| 35 | + super().__init__(**options) |
| 36 | + self._tbody_count = 0 |
| 37 | + |
| 38 | + def convert_td(self, el: "Tag", text: str, parent_tags: set[str]) -> str: |
| 39 | + self._check_cell_complexity(el) |
| 40 | + return super().convert_td(el, text, parent_tags) # type: ignore[attr-defined] |
| 41 | + |
| 42 | + def convert_th(self, el: "Tag", text: str, parent_tags: set[str]) -> str: |
| 43 | + self._check_cell_complexity(el) |
| 44 | + return super().convert_th(el, text, parent_tags) # type: ignore[attr-defined] |
| 45 | + |
| 46 | + def convert_table(self, el: "Tag", text: str, parent_tags: set[str]) -> str: |
| 47 | + self._tbody_count = len(el.find_all("tbody", recursive=False)) |
| 48 | + if self._tbody_count > 1: |
| 49 | + raise _TableComplexityException( |
| 50 | + f"Table has {self._tbody_count} tbody sections (GFM only supports 1)" |
| 51 | + ) |
| 52 | + return super().convert_table(el, text, parent_tags) # type: ignore[attr-defined] |
| 53 | + |
| 54 | + def _check_cell_complexity(self, el: "Tag") -> None: |
| 55 | + colspan = el.get("colspan", "1") |
| 56 | + rowspan = el.get("rowspan", "1") |
| 57 | + |
| 58 | + try: |
| 59 | + colspan_str = str(colspan) if colspan else "1" |
| 60 | + rowspan_str = str(rowspan) if rowspan else "1" |
| 61 | + |
| 62 | + if int(colspan_str) > 1: |
| 63 | + raise _TableComplexityException( |
| 64 | + f"Table has colspan={colspan_str} (GFM doesn't support colspan)" |
| 65 | + ) |
| 66 | + if int(rowspan_str) > 1: |
| 67 | + raise _TableComplexityException( |
| 68 | + f"Table has rowspan={rowspan_str} (GFM doesn't support rowspan)" |
| 69 | + ) |
| 70 | + except ValueError as error: |
| 71 | + raise _TableComplexityException( |
| 72 | + "Table has invalid colspan/rowspan values" |
| 73 | + ) from error |
| 74 | + |
| 75 | + |
| 76 | +def render_table_content(html_string: str) -> str: |
| 77 | + try: |
| 78 | + converter = _GFMTableConverter(heading_style="ATX") |
| 79 | + gfm_table = converter.convert(html_string).strip() |
| 80 | + return gfm_table |
| 81 | + except _TableComplexityException: |
| 82 | + return html_string |
0 commit comments