Skip to content

Ignore pygments_formatter in fenced_code attribute lists - #1683

Closed
naaz234 wants to merge 1 commit into
Python-Markdown:masterfrom
naaz234:fenced-code-pygments-formatter
Closed

naaz234 wants to merge 1 commit into
Python-Markdown:masterfrom
naaz234:fenced-code-pygments-formatter

Conversation

@naaz234

@naaz234 naaz234 commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Description

FencedBlockPreprocessor.run passes every key/value pair from a fenced block's attribute list on to CodeHilite, including pygments_formatter, so one block can replace the formatter set in the codehilite configuration. With Pygments installed and just fenced_code and codehilite enabled:

>>> import markdown
>>> markdown.markdown("``` { .text pygments_formatter='text' }\n<b>Some HTML</b>\n```", extensions=['fenced_code', 'codehilite'])
'<p><b>Some HTML</b>\n</p>'

Pygments' text formatter writes the code out as is, so the content of that block is not escaped the way it is in every other code block. Other formatters don't give HTML at all (pygments_formatter='raw' renders a b"..." bytes repr). The fenced code docs list HtmlFormatter and lexer options as what a block can set, and pygments_formatter is neither.

The key is now dropped from the attribute list's settings before they are merged with the codehilite configuration, which is similar to CodeHilite always forcing full off. This only affects the Pygments branch. Without Pygments, the key is still written as a <code> attribute (with attr_list) as before, and other per-block options such as linenums, css_class and use_pygments work as before.

The new test sits with the other per-block option tests. It fails on master with Pygments 2.7.1 and passes with this change. The full suite passes with Pygments 2.7.1 and without Pygments, and flake8 is clean.

AI Assistance Disclosure

  • No AI tools were used in preparing this PR.

  • AI tools were used and I have disclosed which ones, and have fully reviewed and verified their output.

    AI tools were use to assist with the following:

    • Code
    • Code Comments
    • Documentation
    • Changelog Entry
    • Commit Message
    • The Description Above
    • Other: finding the issue, the test, and the reproduction above

    Tool used: Claude Code (Claude Opus 5.5).

Checklist

`FencedBlockPreprocessor.run` merges every key/value pair from a fenced
block's attribute list into the `codehilite` settings, so a block could
set `pygments_formatter` and replace the formatter chosen in the
`codehilite` configuration. Pygments' `text` formatter writes the code
out as is, so `{ .text pygments_formatter='text' }` rendered the block's
content as markup instead of escaping it like any other code block.
Other formatters do not produce HTML at all.

The key is now dropped before the merge, in the Pygments branch only.
The fenced code docs only list `HtmlFormatter` and lexer options as
settable per block, and `CodeHilite` already forces the `full` option
off for a similar reason. The non-Pygments branch and the other
per-block options are unchanged.
@waylan

waylan commented Oct 29, 2026

Copy link
Copy Markdown
Member

I'm not sure why we care about this. This seems like a situation of garbage in, garbage out.

@waylan waylan added the needs-decision A decision needs to be made regarding request. label Oct 29, 2026
@naaz234

naaz234 commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Fair point. The reason I thought it was worth it is that, as far as I can tell, pygments_formatter is the only key a block can set that switches off the escaping every other code block gets, and the docs only list HtmlFormatter and lexer options as settable per block, so it looked like it had slipped through rather than being intended. That said, it does need the author to ask for that formatter explicitly, so I take the garbage in, garbage out point. Happy to close this if you don't think it's worth carrying.

@waylan waylan closed this Oct 30, 2026
@waylan waylan added rejected The pull request is rejected for the stated reasons. and removed needs-decision A decision needs to be made regarding request. labels Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

rejected The pull request is rejected for the stated reasons.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants