Skip to content

Speed up HtmlFormatter token escaping with quote=False - #3185

Merged
Anteru merged 2 commits into
pygments:masterfrom
jvoisin:esc
Jul 7, 2026
Merged

Anteru merged 2 commits into
pygments:masterfrom
jvoisin:esc

Conversation

@jvoisin

@jvoisin jvoisin commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

Token values are emitted as element text content (inside <span>/<pre>), where the " and ' characters do not need HTML-escaping. Escape them with html.escape(value, quote=False) directly instead of going through pygments.util.html_escape (which forces quote=True and adds a str() + extra call layer). This skips two of the five .replace() passes on the per-token hot path.

Interleaved, core-pinned benchmarking on pre-tokenized C/C++/Java/Python shows the full HtmlFormatter pipeline get ~7% faster, with every new sample beating every baseline sample.

The init option escaping (cssclass, cssstyles, filename, lineseparator, lineanchors, linespans) keeps quote=True since those may end up inside HTML attributes.

Note: this is a visible output change: " and ' inside highlighted code now render as literal characters rather than " / '. The result is equivalent, valid HTML (verified byte-for-byte across the example-file corpus: only quote characters differ, tag structure is unchanged). test_correct_output is updated to compare against quote=False escaping.

Token values are emitted as element text content (inside <span>/<pre>),
where the " and ' characters do not need HTML-escaping. Escape them with
html.escape(value, quote=False) directly instead of going through
pygments.util.html_escape (which forces quote=True and adds a str() +
extra call layer). This skips two of the five .replace() passes on the
per-token hot path.

Interleaved, core-pinned benchmarking on pre-tokenized C/C++/Java/Python
shows the full HtmlFormatter pipeline get ~7% faster, with every new sample
beating every baseline sample.

The __init__ option escaping (cssclass, cssstyles, filename,
lineseparator, lineanchors, linespans) keeps quote=True since those may
end up inside HTML attributes.

Note: this is a visible output change: " and ' inside highlighted code
now render as literal characters rather than &quot; / &#x27;. The result
is equivalent, valid HTML (verified byte-for-byte across the example-file
corpus: only quote characters differ, tag structure is unchanged).
test_correct_output is updated to compare against quote=False escaping.
@birkenfeld

Copy link
Copy Markdown
Member

7%? That's pretty crazy :)

@Anteru any concerns about compatibility? I'm kinda happy to emit raw " and ' since we nowhere guarantee those to be escaped.

@Anteru

Anteru commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

I don't think escaping those quotes was ever intentional. I wouldn't mind having a small unit test for this though, just to document the new behavior.

@jvoisin

jvoisin commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Sure :)

@Anteru
Anteru merged commit c97c0b5 into pygments:master Jul 7, 2026
17 checks passed
@jvoisin
jvoisin deleted the esc branch July 15, 2026 23:01
tony added a commit to tmux-python/libtmux-mcp that referenced this pull request Aug 22, 2026
why: pygments 2.21.0 makes HtmlFormatter render `"` and `'` literally
instead of `&quot;` and `&#39;` (pygments#3185), so the recorded
fragment for the uvx MCP config stopped matching. This lands as its own
commit because the dev-package refresh that raised pygments does not
mention it: the failure surfaced after that commit was already pushed.

Comparing the two revisions line by line, every difference is exactly
that one substitution - same tokens, same span classes, same
whitespace, four lines. Rendered output is unchanged, since inside
element text content `"` and `&quot;` are equivalent; the escaping was
only ever required inside attribute values.

Verified by pinning pygments back to 2.20.0, where the old snapshot
passes, and forward to 2.21.0, where only this substitution differs.

what:
- Re-record highlight_json-mcp-config-uvx against pygments 2.21.0

See also: pygments/pygments#3185
tony added a commit to tmux-python/libtmux-mcp that referenced this pull request Aug 27, 2026
why: pygments 2.21.0 makes HtmlFormatter render `"` and `'` literally
instead of `&quot;` and `&#39;` (pygments#3185), so the recorded
fragment for the uvx MCP config stopped matching. This lands as its own
commit because the dev-package refresh that raised pygments does not
mention it: the failure surfaced after that commit was already pushed.

Comparing the two revisions line by line, every difference is exactly
that one substitution - same tokens, same span classes, same
whitespace, four lines. Rendered output is unchanged, since inside
element text content `"` and `&quot;` are equivalent; the escaping was
only ever required inside attribute values.

Verified by pinning pygments back to 2.20.0, where the old snapshot
passes, and forward to 2.21.0, where only this substitution differs.

what:
- Re-record highlight_json-mcp-config-uvx against pygments 2.21.0

See also: pygments/pygments#3185
saschazepter pushed a commit to saschazepter/hg that referenced this pull request Sep 1, 2026
see pygments/pygments#3185

--HG--
branch : stable
extra : source : ba132b4d7deb3a9c272c2af7bae28737df28a2c8
extra : topic : pygment-compat
clayote added a commit to clayote/sphinx that referenced this pull request Sep 10, 2026
As of version 2.21.0, pygments doesn't escape quotation marks
when they appear between span tags like
`<span>"Hello sphinx world"</span>`.
This commit updates the test data to match the new behavior.

pygments/pygments#3185
clayote added a commit to clayote/sphinx that referenced this pull request Sep 10, 2026
As of version 2.21.0, pygments doesn't escape quotation marks
when they appear between span tags like
`<span>"Hello sphinx world"</span>`.
This commit updates the test data to match the new behavior.

pygments/pygments#3185
clayote added a commit to clayote/sphinx that referenced this pull request Sep 10, 2026
As of version 2.21.0, pygments doesn't escape quotation marks
when they appear between span tags like
`<span>"Hello sphinx world"</span>`.
This commit updates the test data to match the new behavior.

pygments/pygments#3185
clayote added a commit to clayote/sphinx that referenced this pull request Sep 10, 2026
As of version 2.21.0, pygments doesn't escape quotation marks
when they appear between span tags like
`<span>"Hello sphinx world"</span>`.
This commit updates the test data to match the new behavior.

pygments/pygments#3185
felixonmars added a commit to felixonmars/archriscv-packages that referenced this pull request Nov 15, 2026
Adapt HTML snapshot expectations to the intentional quote-escaping change
in Pygments 2.21 and argument-emphasis changes in Markdown2 2.5.5,
which cause the 29 snapshot failures on ninetales. Preserve exact output
comparisons and the existing test selection. No pdoc upstream fix was found.

pygments/pygments#3185
trentm/python-markdown2#665
https://archriscv.felixc.at/.status/logs/pdoc/pdoc-16.0.0-2.log
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.

3 participants