Add py-render custom code block formater - #1637
Conversation
|
Are the backticks intended in |
|
Good question. I had originally tried |
|
I'm not sure I'm following. What is the issue? |
|
Raw HTML in the title of a code block is escaped and Markdown rendering does not happen. Therefore, there is no way to include any markup in a title. For a simple example consider this plain superfence code block. ``` text { title='Content of <code>somefile.txt</code>' }
Text goes here...
```It occurs to me that the above might not work as the raw HTML might trip up the parser. However, when using a custom formatter, I want to assemble a title which includes markup and can't because it is escaped. |
|
Yeah, I think that was because when I originally put SuperFences together, the focus was getting fenced code to render in lists and other indented objects. Everything is run in a preprocessor and the stashed. It essentially becomes invisible to the parser except for the placeholder and unpacked at the end. We waited until the end as we don't have full context of whether the fenced code we parsed was under raw HTML or not. I haven't looked into it, but I guess it could be possible to unpack them all in the treeprocessor, as we should be past the HTML processor at that point. We should have context of Right now, we store fence HTML as strings, so we'd likely need to have them store content as etree Elements, or at least optionally for those who are okay with stuff in them getting processed. So yeah, it was intentional, with the idea that code is handled and not seen until the end, unless you specifically go in and unpack them, but it is possible we could allow fenced code to be stored as etree Elements and unpack them before the inline treeprocessor, assuming no adverse effects. |
|
It's probably fine to create a feature request, but I'll have to dig in and see how complicated it would be to pull off allowing that to get processed normally. I imagine people want access to all the normal inline stuff, so we'd need to ensure it gets inserted back into the tree at the proper time. I make no promises until I have a chance to dig into this as I haven't refactored this area in quite some time. |
|
Turns out the issue is with Pygments, not Superfences. Supfences passes the >>> from pygments import highlight
>>> from pygments.lexers import PythonLexer
>>> from pygments.formatters import HtmlFormatter
>>> print(highlight('# Code goes here', PythonLexer(), HtmlFormatter(filename='Content of <code>file.py</code>')))
<div class="highlight"><span class="filename">Content of <code>file.py</code></span><pre><span></span><span class="c1"># Code goes here</span>
</pre></div>The only way to work around that would be to use a placeholder which could be swapped out later. |
|
Yeah, there is some complexity in this area, so it's not something I'm committed to providing until I have time to investigate and determine all the potential issues. It's likely possible to get something working; I'm just not 100% sure it's possible without side effects, but a thorough investigation will help make that more clear. |
|
Turns out it was a simple fix for me to address in my custom formatter (see #1640). I just passed the title to |
|
Apparently, I misunderstood what we were talking about. I thought it was desired that the inline code with |

Description
This is similar to the previous markdown renderer (#1635) except that it runs Python code blocks in an isolated environment and renders the output as a result block. The Contributing Guide has been updated with an explanation of the feature.
AI Assistance Disclosure
Checklist