Add langclass option to HtmlFormatter - #3305
Open
BiswasNehaa wants to merge 1 commit into
Open
BiswasNehaa wants to merge 1 commit into
BiswasNehaa wants to merge 1 commit into
Conversation
Fixes pygments#1844. HtmlFormatter previously had no way to signal the source language in its HTML output. Since Formatter is never told which Lexer produced its token stream, this requires the caller to supply the language explicitly. Adds a langclass option: when set (e.g. langclass='python'), the wrapping <div> gets an additional CSS class of language-python, matching the language-xxx convention used by highlight.js, Prism, and CommonMark. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.
Fixes #1844
HtmlFormatterhad no way to indicate the source language in its output. The core reason:Formatteris never told whichLexerproduced its token stream (they're decoupled by design — you can already dohighlight(code, PythonLexer(), HtmlFormatter())and the formatter never seesPythonLexer), so this can't be auto-detected.This adds a
langclassoption: when set (e.g.HtmlFormatter(langclass='python')), the wrapping<div>gets an additional CSS class oflanguage-python, following thelanguage-xxxconvention used by highlight.js, Prism, and CommonMark. The caller supplies the language explicitly, same pattern as the existingcssclassoption — e.g.HtmlFormatter(langclass=lexer.aliases[0]).Changes
pygments/formatters/html.py: newlangclassoption, documented with a usage exampletests/test_html_formatter.py: new-value emission, interaction with an emptycssclass, default-empty behavior, and HTML-escaping/Nonehandling alongside the other string optionsCHANGES: entry under the unreleased 2.22.0 sectionTest plan
pytest tests/test_html_formatter.py— 26 passedpytest tests/(excluding the unrelatedcontrastmodule, which fails to collect onmastertoo due to a missing optional dependency) — 5340 passed, 8 skippedruff check— cleanhighlight(code, PythonLexer(), HtmlFormatter(langclass='python'))→<div class="highlight language-python">...🤖 Generated with Claude Code