md2star.preprocessing.tables module

HTML <table> → Pandoc pipe-table conversion.

Pandoc’s DOCX writer silently drops raw HTML in the document body, so any <table> left in the Markdown after preprocessing would simply vanish. This module parses HTML tables with a minimal HTMLParser subclass and emits an equivalent pipe-table that Pandoc renders natively.

Inline formatting tags inside cells (<code>, <strong>, <em>, …) are translated to their Markdown equivalents so the formatting survives. Local images embedded in cells are routed through resize_image_for_cell() to prevent overflow.

Author

[Warith HARCHAOUI](https://linkedin.com/in/warith-harchaoui/)

md2star.preprocessing.tables.convert_html_tables(content, base_dir='.')[source]

Replace every <table> block in content with a pipe-table equivalent.

Parameters:
  • content (str)

  • base_dir (str)

Return type:

str

md2star.preprocessing.tables.html_table_to_markdown(table_html, base_dir='.')[source]

Convert a single <table>...</table> block to a pipe-table string.

Returns the original table_html on parse failure or empty rows.

Parameters:
  • table_html (str)

  • base_dir (str)

Return type:

str

md2star.preprocessing.tables.normalize_grid_tables(content)[source]

Re-align every grid table so Pandoc still parses it after path/width rewrites.

Runs late in the pipeline (after image paths were absolutized and width hints added, both of which change cell text length). Grid tables — unlike pipe tables — are the only Markdown table form whose cells can hold images that Pandoc keeps in DOCX/PDF, so keeping them intact matters for image galleries. Idempotent; fenced code blocks and non-table +/| lines are left untouched.

Parameters:

content (str)

Return type:

str

md2star.preprocessing.tables.normalize_pipe_tables(content, min_total=90, min_per_col=3, min_weight_per_col=15, single_word_slack=1.7)[source]

Rewrite pipe-table separators with proportional dashes; ensure trailing blank line.

Columns whose cells are all single-word (no internal whitespace) are given a single_word_slack multiplicative bump on their weight, since such cells cannot wrap and would otherwise get squeezed character-by- character against multi-word columns that can break across lines.

The transformation is idempotent: running it twice yields the same output. Tables inside fenced code blocks are left untouched.

Parameters:
  • content (str)

  • min_total (int)

  • min_per_col (int)

  • min_weight_per_col (int)

  • single_word_slack (float)

Return type:

str