Repository navigation
Fix HTML blocks nested in indented blocks - #1666
inchang-ing wants to merge 7 commits into
Conversation
|
Please edit your initial comment and check the appropriate boxes (add a |
|
I would like to see this tested with |
Raw HTML blocks which start on a line indented by more than three spaces were not extracted by the HTML block preprocessor, as it could not distinguish such a start tag nested within a containing block (a list item, admonition, etc.) from an indented code block. The block parser then treated the HTML as inline content and split it into separate paragraphs at any blank lines within the raw block. The preprocessor now extracts such indented raw blocks, removes the indentation of the start tag from the stashed content, and preserves that indentation on the line which holds the placeholder. The block parser can then make the decision based on the resulting block structure: within a containing block the placeholder is parsed as nested content, whereas an indented code block keeps the placeholder in its content. In the latter case, the RawHtmlPostprocessor escapes the stashed HTML, so indented code blocks are rendered exactly as before. Fixes Python-Markdown#1096
6b8d1e8 to
d433147
Compare
Add TestIndentedHtmlBlockswMdInHTML mirroring the two new regression tests with the md_in_html extension enabled, per maintainer feedback on Python-Markdown#1666. Co-Authored-By: Claude <noreply@anthropic.com>
|
@facelessuser Good point — I've added
Full suite: 1138 tests OK (111 skipped). Also checked the boxes in the PR template per @waylan's request. |
|
You checked |
|
I see you force pushed a change. As the Contributing Guide states:
There have been multiple failures to follow our contributing guidelines. It is disrespectful for the reviewer's time. Please review the guidelines carefully and follow them fully going forward or this PR may be closed. |
An indented raw HTML block which the block parser placed in an indented code block was restored by RawHtmlPostprocessor after serialization, so the codehilite extension never saw the content and its non-Pygments escaping was bypassed (testFormatterLangStrGuessLang failed because quotes were not escaped). Restore such placeholders in a new RawHtmlInCodeTreeprocessor which runs before the hilite treeprocessor (priority 35 vs 30), setting the code text as escaped AtomicString content. The codehilite extension then unescapes and re-escapes the content itself, matching its established output, while documents without codehilite keep the code_escape semantics of ordinary code blocks. Co-Authored-By: Claude <noreply@anthropic.com>
|
Sorry about that — both fair points. The AI disclosure is now filled in: Claude was used to draft the code, tests, docs, changelog entry, commit messages and the description; I reviewed and verified everything before submitting. On the force push: understood, it won't happen again. The two fixes pushed since are regular incremental commits, and everything further will be as well. |
|
I ran tests in Pymdown Extensions using these changes. I noticed two failures. I need time to investigate why this breaks those tests. I'm not sure this necessarily signals that this could be a breaking change for other extensions, but it's at least something I need time to understand. |
…s/changelog conflicts
55f6299 to
0969347
Compare
…ed-indent # Conflicts: # docs/changelog.md
|
Since you mentioned you needed time to work out the two failures, here is what I have from my side — it may save you part of it. I ran the pymdown-extensions suite against two checkouts of Python-Markdown, everything else identical (same pymdown tree, same Python):
(The 13 shared failures are identical on both, so they look like version drift between your suite and 3.11.2 rather than anything this branch does — I left them out of the analysis.) In both failing tests the only delta is four spaces of indentation surviving on the line that follows the stashed block: -this is <span>raw</span> **content**</div>
+ this is <span>raw</span> **content**</div>Repro, no other extensions involved: markdown.Markdown(extensions=['pymdownx.blocks.html']).convert('''/// html | div
markdown: html
<div>
**content**
</div>
this is <span>raw</span> **content**
///''')What I have established so far: the lines handed to the Markdown core are byte-identical in both versions, so the divergence is inside What I have not settled is the right fix, and I would rather implement your call than guess:
Given this is exactly the 3rd-party surface you were worried about, tell me which of the two you would consider acceptable and I will push it with a test. |
|
Interesting. I'm not sure which is the best path yet, as I haven't had a chance to dig deeper, but this confirms what I was noticing. It is worrying if the blocks are altered such that 3rd party extensions are misinterpreting them in the block stage, so I agree a change needs to be made. Hopefully, I'll have a chance soon to look more closely. |
|
I've finally cleared my AI PR backlog on a number of other projects. I should be able to devote some time to Python Markdown soon. |
|
@inchang-ing I did a deep analysis because this is an area that has big implications. Full disclosure, I used AI to dig into various areas and create a list of tests that should pass, but didn't. I didn't trust that I would catch everything. I also tested things against Pymdown Extensions as well to try and ensure things remained backwards compatible and behaved sanely. The current implementation is definitely not ready to go as there were numerous areas where things were broken. Extensive testing is a must for something like this. What I found is that the earlier suggested "container" approach seemed to make the most sense and dramatically simplified the code. And it seems reasonably compatible with Pymdown Extensions (and hopefully others). I have a branch here: https://github.com/facelessuser/markdown/tree/html-indent. I think this may be a good starting point. If we are going to do this, we have to be careful and do it correctly. I think the approach in the linked branch, which is based on your suggestion, is likely the better approach. I make no assertions that, because I've provided it, it is ready to go. It is a prototype. You could certainly apply it here, or maybe we start a new PR based on the new approach, I'm not sure. |
|
I guess elements under blockquotes weren't handled properly, and there was a comment issue; those should be fixed on the branch. Again, this is just a prototype, and I'm sure more testing to gain confidence would be needed. It is certainly possible we could find some kind of deal-breaker, as I've only spent one evening cobbling this together. |
|
Going with a new PR for the container approach, as you suggested. I did not want to rewrite this branch on top of it: the two commits you pushed are the implementation, and it seemed cleaner to keep them intact with your authorship than to move the diff into a commit of mine under this discussion. So: I ran the suite on the result: 1310 tests pass, 111 skipped, and New PR: #1688 If you would rather have it open from your fork, say so and I will close mine instead of pushing to it further. |
|
It is fine that you run with it. |
Raw HTML blocks which start on a line indented by more than three spaces were not extracted by the HTML block preprocessor, as it could not distinguish such a start tag nested within a containing block (a list item, admonition, etc.) from an indented code block. The block parser then treated the HTML as inline content and split it into separate paragraphs at any blank lines within the raw block.
The preprocessor now extracts such indented raw blocks, removes the indentation of the start tag from the stashed content, and preserves that indentation on the line which holds the placeholder. The block parser can then make the decision based on the resulting block structure: within a containing block the placeholder is parsed as nested content, whereas an indented code block keeps the placeholder in its content. In the latter case, a
RawHtmlInCodeTreeprocessor(running before thecodehilitetreeprocessor) restores the stashed HTML as escaped text, so indented code blocks are rendered exactly as before, whether or notcodehiliteis enabled.Fixes #1096
Description
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:
Specifically, Claude (Anthropic's LLM, via the WorkBuddy agent) was used to draft the code, tests, documentation, changelog entry, commit messages and this description. All output was reviewed, tested and verified by a human before submission.
Checklist