Skip to content

fix(htmlprocessing): turn line-numbered code tables into code blocks - #1221

Open
fpuga wants to merge 1 commit into
adbar:masterfrom
fpuga:fix/line-numbered-code-tables
Open

fpuga wants to merge 1 commit into
adbar:masterfrom
fpuga:fix/line-numbered-code-tables

Conversation

@fpuga

@fpuga fpuga commented Dec 6, 2026

Copy link
Copy Markdown

Fixes #1187

Syntax highlighters that show line numbers (Pygments linenos="table" in Sphinx, MkDocs and Hugo, Crayon / Urvanov on WordPress) lay out code as a one-row table: line numbers in the first cell, code in the second. Trafilatura kept the table, so the code ended up in a table cell, on one line in markdown and txt output.

In convert_tags(), a table with one row and two cells is replaced by a <code> element when the first cell only holds consecutive line numbers and their count matches the lines of code in the second cell (the lines of its <pre>, else its innermost block elements). The check does not rely on class names; data and layout tables do not match it.

Tests: test_line_numbered_code_tables in tests/unit_tests.py, through extract(), covers Pygments, Crayon, a single line and two data tables. The three code cases fail without the change.

Checks, rebased on master (15c105a): pytest, mypy, ruff check, ruff format --check and tests/eval_gate.py pass, gate scores unchanged.

Processing time, python tests/evaluate.py --small, two runs each (time difference relative to the baseline extractor):

master branch
trafilatura 3.252 / 3.261 3.270 / 3.266
trafilatura fast 2.608 / 2.624 2.620 / 2.615

Scores are identical, the difference is within run-to-run noise.

AI disclosure: the fix and tests were written with Claude Code (Opus 5.5); I reviewed and tested them.

Syntax highlighters such as Pygments (linenos="table") and the
Crayon/Urvanov WordPress plugin lay out code as a one-row table with
line numbers in the first cell and the code in the second. Replace such
tables by a code element when the first cell only holds consecutive line
numbers matching the number of code lines.

Fixes adbar#1187
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.

Code blocks laid out as line-number tables come out as tables

1 participant