Skip to content

fix: escape pipes and header bangs in the mediawiki format - #500

Open
rahul05ranjan wants to merge 1 commit into
astanin:masterfrom
rahul05ranjan:fix/mediawiki-pipe-escaping
Open

rahul05ranjan wants to merge 1 commit into
astanin:masterfrom
rahul05ranjan:fix/mediawiki-pipe-escaping

Conversation

@rahul05ranjan

Copy link
Copy Markdown

What

tablefmt="mediawiki" writes cell and header values raw, so any literal | silently splits the cell into extra columns when the table is rendered on a MediaWiki wiki — || is the column separator in data rows, !! in header rows:

>>> print(tabulate.tabulate([["x | y"], ["a || b"]], headers=["h | 1"], tablefmt="mediawiki"))
{| class="wikitable" style="text-align: left;"
|+ <!-- caption -->
|-
! h | 1          <- header gains a spurious empty column
|-
| x | y          <- becomes two cells
|-
| a || b         <- becomes three cells
|}

The data is silently corrupted. This is the same class the github/pipe formats already handle for their separator (they render x | y as x \| y), and the same class prettytable fixed for its own MediaWiki emitter (prettytable #544, | → &#124;).

Fixes #499

Fix

_mediawiki_row_with_attrs now escapes cell values before joining them: | → &#124; everywhere, and ! → &#33; in header rows (where !! is the separator). Values without pipes/bangs are byte-identical to before. Newlines inside cells are deliberately untouched — multiline mediawiki support is already on the maintainer's TODO list and would use <br />.

Verification

Two new tests (test_mediawiki_escapes_pipes_in_cells_and_headers, test_mediawiki_escapes_bang_in_headers) fail on master and pass with this change; the existing test_mediawiki/test_mediawiki_headerless pins are unchanged. Full suite: python3 -m pytest test/ → 351 passed, 17 skipped, zero failures.

A literal | in a cell or header silently split it into extra columns
when the table was rendered on a MediaWiki wiki, the same way || and
!! act as column separators. Write literal pipes as &astanin#124; (and ! in
header rows as &astanin#33;), like the github/pipe formats already escape
their separator.

Fixes astanin#499

Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mediawiki format: literal pipes in cells/headers silently split columns

1 participant