How to wrap the original output in between special output? #1739
-
Hi there. I read the question here but it didn't solve my problem. The link given by @facelessuser is 404 now and I don't understand what @oprypin did there… I'm lost 😿 Here is my sample code: import markdown
from pymdownx.superfences import fence_code_format
def convert_to_html(md):
extras_markdown = ["nl2br", "pymdownx.superfences", "pymdownx.highlight"]
html = markdown.markdown(
md,
extensions=extras_markdown,
extension_configs={
"pymdownx.superfences": {
"custom_fences": [
{
"name": "python",
"class": "",
"format": _code_custom_formatter,
},
]
}
},
)
return html
def _code_custom_formatter(source, language, *args, **kwargs):
code_block = fence_code_format(source, language, *args, **kwargs)
return f'<div class="{language}">{code_block}</div>'
md = """
# Title
```python
print("Hello World")
```
```php
echo "Hello World";
```
"""
print(convert_to_html(md)) I would expect this: <h1>Title</h1>
<div class="python"><div class="highlight"><pre><span></span><code><span class="nb">print</span><span class="p">(</span><span class="s2">"Hello World"</span><span class="p">)</span>
</code></pre></div></div>
<div class="highlight"><pre><span></span><code><span class="x">echo "Hello World";</span>
</code></pre></div> But instead I get this: <h1>Title</h1>
<div class="python"><pre><code>print("Hello World")</code></pre></div>
<div class="highlight"><pre><span></span><code><span class="x">echo "Hello World";</span>
</code></pre></div> I would need to keep the original behavior (with hilighting) but to wrap the output by something custom. It's been days and I can't make it work. Thank you very much ❤️ |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Can you explain exactly why you expect what you are suggesting? You are wrapping Am I correct to assume you want to run the Python code block through the default Pygments highlighter and then wrap it in your custom |
Beta Was this translation helpful? Give feedback.
Can you explain exactly why you expect what you are suggesting? You are wrapping
fenced_code_format
with your own custom formatter, and you get exactly what you are supposed to. Check out whatfenced_code_format
does here.Am I correct to assume you want to run the Python code block through the default Pygments highlighter and then wrap it in your custom
<div>
? I'm not currently sure what you are trying to accomplish.