Repository navigation
Speed up HtmlFormatter token escaping with quote=False - #3185
Merged
Merged
Conversation
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.
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. |
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. |
Contributor
Author
|
Sure :) |
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 `"` and `'` (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 `"` 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
1 task
1 of 5 tasks
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 `"` and `'` (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 `"` 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.