Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add language to HTML output in pygments code blocks (#379) #596

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- [pull #605] Add support for Python 3.13, drop EOL 3.8
- [pull #607] Fix `middle-word-em` extra preventing strongs from being recognized (#606)
- [pull #596] Add language to HTML output in pygments code blocks (#379)


## python-markdown2 2.5.1
Expand Down
5 changes: 4 additions & 1 deletion lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2060,7 +2060,9 @@ def _wrap_code(self, inner):
"""A function for use in a Pygments Formatter which
wraps in <code> tags.
"""
yield 0, "<code>"
# class format following what we do for highlight-js
# eg: class="python language-python"
yield 0, f'<code class="{lang_class} language-{lang_class}">'
yield from inner
yield 0, "</code>"

Expand All @@ -2079,6 +2081,7 @@ def wrap(self, source, outfile=None):
# pygments < 2.12
return self._wrap_div(self._add_newline(self._wrap_pre(self._wrap_code(source))))

lang_class = lexer.name.lower().replace(' ', '-')
formatter_opts.setdefault("cssclass", "codehilite")
formatter = HtmlCodeFormatter(**formatter_opts)
return pygments.highlight(codeblock, lexer, formatter)
Expand Down
10 changes: 5 additions & 5 deletions test/tm-cases/admonitions_with_fenced_code_blocks.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@
<strong>note</strong>
<p>Admonitions are able to contain fenced code blocks</p>
<div class="codehilite">
<pre><span></span><code><span class="nb">print</span><span class="p">(</span><span class="s1">&#39;like so&#39;</span><span class="p">)</span>
<pre><span></span><code class="python language-python"><span class="nb">print</span><span class="p">(</span><span class="s1">&#39;like so&#39;</span><span class="p">)</span>
</code></pre>
</div>
</aside>

<aside class="admonition warning">
<strong>warning</strong>
<div class="codehilite">
<pre><span></span><code><span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Consecutive blocks should also be fine&#39;</span><span class="p">)</span>
<pre><span></span><code class="python language-python"><span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Consecutive blocks should also be fine&#39;</span><span class="p">)</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Even though fenced code blocks wrap themselves in newlines&#39;</span><span class="p">)</span>
<pre><span></span><code class="python language-python"><span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Even though fenced code blocks wrap themselves in newlines&#39;</span><span class="p">)</span>
</code></pre>
</div>
<aside class="admonition hint">
<strong>hint</strong>
<em>It should also work nested</em>
<div class="codehilite">
<pre><span></span><code><span class="nb">print</span><span class="p">(</span><span class="s1">&#39;ok&#39;</span><span class="p">)</span>
<pre><span></span><code class="python language-python"><span class="nb">print</span><span class="p">(</span><span class="s1">&#39;ok&#39;</span><span class="p">)</span>
</code></pre>
</div>
</aside>
</aside>

<div class="codehilite">
<pre><span></span><code><span class="c1"># admonitions WITHIN fenced code blocks should NOT be rendered</span>
<pre><span></span><code class="python language-python"><span class="c1"># admonitions WITHIN fenced code blocks should NOT be rendered</span>
<span class="o">..</span> <span class="n">attention</span><span class="p">::</span> <span class="n">title</span>
<span class="n">body</span>
</code></pre>
Expand Down
4 changes: 2 additions & 2 deletions test/tm-cases/empty_fenced_code_blocks.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="codehilite">
<pre><span></span><code>
<pre><span></span><code class="bash language-bash">
</code></pre>
</div>

Expand All @@ -9,7 +9,7 @@
<p>Pygments removes the empty line whitespace from the next code block</p>

<div class="codehilite">
<pre><span></span><code>
<pre><span></span><code class="bash language-bash">
</code></pre>
</div>

Expand Down
2 changes: 1 addition & 1 deletion test/tm-cases/fenced_code_blocks_issue355.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="codehilite">
<pre><span></span><code><span class="n">some</span> <span class="n">code</span> <span class="n">block</span>
<pre><span></span><code class="python language-python"><span class="n">some</span> <span class="n">code</span> <span class="n">block</span>
</code></pre>
</div>

Expand Down
4 changes: 2 additions & 2 deletions test/tm-cases/fenced_code_blocks_issue426.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h2>URL PARAMETERS IN THE TEMPLATE</h2>
<li><p><code>ContextMixin</code> defines the method <code>get_context_data</code>:</p>

<div class="codehilite">
<pre><span></span><code><span class="k">def</span> <span class="nf">get_context_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<pre><span></span><code class="python language-python"><span class="k">def</span> <span class="nf">get_context_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">kwargs</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s1">&#39;view&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">extra_context</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">kwargs</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">extra_context</span><span class="p">)</span>
Expand All @@ -26,7 +26,7 @@ <h2>URL PARAMETERS IN THE TEMPLATE</h2>
<p>So when overriding one must be careful to extends <code>super</code>'s <code>kwargs</code>:</p>

<div class="codehilite">
<pre><span></span><code><span class="k">def</span> <span class="nf">get_context_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<pre><span></span><code class="python language-python"><span class="k">def</span> <span class="nf">get_context_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">kwargs</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_context_data</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;page_title&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;Documentation&quot;</span>
<span class="k">return</span> <span class="n">kwargs</span>
Expand Down
2 changes: 1 addition & 1 deletion test/tm-cases/fenced_code_blocks_issue462.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h1>Section 1</h1>

<div class="codehilite">
<pre><span></span><code><span class="n">x</span> <span class="o">=</span> <span class="mi">1</span>
<pre><span></span><code class="python language-python"><span class="n">x</span> <span class="o">=</span> <span class="mi">1</span>
</code></pre>
</div>

Expand Down
2 changes: 1 addition & 1 deletion test/tm-cases/fenced_code_blocks_leading_lang_space.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="codehilite">
<pre><span></span><code><span class="k">if</span> <span class="kc">True</span><span class="p">:</span>
<pre><span></span><code class="python language-python"><span class="k">if</span> <span class="kc">True</span><span class="p">:</span>
<span class="nb">print</span> <span class="s2">&quot;hi&quot;</span>
</code></pre>
</div>
4 changes: 2 additions & 2 deletions test/tm-cases/fenced_code_blocks_safe_highlight.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="codehilite">
<pre><span></span><code><span class="k">if</span> <span class="kc">True</span><span class="p">:</span>
<pre><span></span><code class="python language-python"><span class="k">if</span> <span class="kc">True</span><span class="p">:</span>
<span class="nb">print</span> <span class="s2">&quot;hi&quot;</span>
</code></pre>
</div>
Expand All @@ -9,7 +9,7 @@
<a href="http://github.github.com/github-flavored-markdown/">http://github.github.com/github-flavored-markdown/</a>.</p>

<div class="codehilite">
<pre><span></span><code><span class="k">def</span><span class="w"> </span><span class="nf">foo</span>
<pre><span></span><code class="ruby language-ruby"><span class="k">def</span><span class="w"> </span><span class="nf">foo</span>
<span class="w"> </span><span class="nb">puts</span><span class="w"> </span><span class="s2">&quot;hi&quot;</span>
<span class="k">end</span>
</code></pre>
Expand Down
4 changes: 2 additions & 2 deletions test/tm-cases/fenced_code_blocks_syntax_highlighting.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="codehilite">
<pre><span></span><code><span class="k">if</span> <span class="kc">True</span><span class="p">:</span>
<pre><span></span><code class="python language-python"><span class="k">if</span> <span class="kc">True</span><span class="p">:</span>
<span class="nb">print</span> <span class="s2">&quot;hi&quot;</span>
</code></pre>
</div>
Expand All @@ -9,7 +9,7 @@
<a href="http://github.github.com/github-flavored-markdown/">http://github.github.com/github-flavored-markdown/</a>.</p>

<div class="codehilite">
<pre><span></span><code><span class="k">def</span><span class="w"> </span><span class="nf">foo</span>
<pre><span></span><code class="ruby language-ruby"><span class="k">def</span><span class="w"> </span><span class="nf">foo</span>
<span class="w"> </span><span class="nb">puts</span><span class="w"> </span><span class="s2">&quot;hi&quot;</span>
<span class="k">end</span>
</code></pre>
Expand Down
2 changes: 1 addition & 1 deletion test/tm-cases/fenced_code_blocks_syntax_indentation.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="codehilite">
<pre><span></span><code><span class="k">def</span> <span class="nf">foo</span><span class="p">():</span>
<pre><span></span><code class="python language-python"><span class="k">def</span> <span class="nf">foo</span><span class="p">():</span>
<span class="nb">print</span> <span class="s2">&quot;foo&quot;</span>

<span class="nb">print</span> <span class="s2">&quot;bar&quot;</span>
Expand Down
2 changes: 1 addition & 1 deletion test/tm-cases/hash_nested_html_blocks.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="enclosing">
<div class="codehilite">
<pre><span></span><code><span class="n">x</span> <span class="o">=</span> <span class="mi">1</span>
<pre><span></span><code class="python language-python"><span class="n">x</span> <span class="o">=</span> <span class="mi">1</span>
</code></pre>
</div>

Expand Down
2 changes: 1 addition & 1 deletion test/tm-cases/issue276_fenced_code_blocks_in_lists.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<p>empty codeblock just for sh*ts and giggles</p>

<div class="codehilite">
<pre><span></span><code><span class="n">test</span> <span class="k">with</span> <span class="n">language</span> <span class="nb">set</span>
<pre><span></span><code class="python language-python"><span class="n">test</span> <span class="k">with</span> <span class="n">language</span> <span class="nb">set</span>
</code></pre>
</div>

Expand Down
8 changes: 4 additions & 4 deletions test/tm-cases/pyshell_and_fenced_code_blocks.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ <h1>From Recipe 302035</h1>
<p>Some examples:</p>

<div class="codehilite">
<pre><span></span><code><span class="gp">&gt;&gt;&gt; </span><span class="n">nprint</span><span class="p">(</span><span class="mi">9876543210</span><span class="p">)</span>
<pre><span></span><code class="python-console-session language-python-console-session"><span class="gp">&gt;&gt;&gt; </span><span class="n">nprint</span><span class="p">(</span><span class="mi">9876543210</span><span class="p">)</span>
<span class="go">&#39;9 876 543 210&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nprint</span><span class="p">(</span><span class="mi">987654321</span><span class="p">,</span> <span class="n">period</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">delimiter</span><span class="o">=</span><span class="s2">&quot;,&quot;</span><span class="p">)</span>
<span class="go">&#39;9,8,7,6,5,4,3,2,1,0&#39;</span>
Expand All @@ -13,7 +13,7 @@ <h1>From Recipe 302035</h1>
<p>Indented a bit:</p>

<div class="codehilite">
<pre><span></span><code><span class="gp">&gt;&gt;&gt; </span><span class="mi">1</span> <span class="o">+</span> <span class="mi">1</span>
<pre><span></span><code class="python-console-session language-python-console-session"><span class="gp">&gt;&gt;&gt; </span><span class="mi">1</span> <span class="o">+</span> <span class="mi">1</span>
<span class="go">2</span>
</code></pre>
</div>
Expand All @@ -22,7 +22,7 @@ <h1>From Recipe 302035</h1>

<blockquote>
<div class="codehilite">
<pre><span></span><code><span class="gp">&gt;&gt;&gt; </span><span class="mi">1</span> <span class="o">+</span> <span class="mi">1</span>
<pre><span></span><code class="python-console-session language-python-console-session"><span class="gp">&gt;&gt;&gt; </span><span class="mi">1</span> <span class="o">+</span> <span class="mi">1</span>
<span class="go">2</span>
</code></pre>
</div>
Expand All @@ -31,7 +31,7 @@ <h1>From Recipe 302035</h1>
<p>Cuddled to previous para (and at end of document):</p>

<div class="codehilite">
<pre><span></span><code><span class="gp">&gt;&gt;&gt; </span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span>
<pre><span></span><code class="python-console-session language-python-console-session"><span class="gp">&gt;&gt;&gt; </span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span>
<span class="go">4</span>
</code></pre>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ <h3>Example:</h3>

<blockquote>
<div class="codehilite">
<pre><span></span><code><span class="k">if</span> <span class="kc">True</span><span class="p">:</span>
<pre><span></span><code class="python language-python"><span class="k">if</span> <span class="kc">True</span><span class="p">:</span>
<span class="nb">print</span><span class="p">()</span>

<span class="nb">print</span><span class="p">()</span>
Expand Down