diff --git a/CHANGES.md b/CHANGES.md index 4f1b4478..b32d7f4e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 diff --git a/lib/markdown2.py b/lib/markdown2.py index 2cac3624..247beccf 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -2060,7 +2060,9 @@ def _wrap_code(self, inner): """A function for use in a Pygments Formatter which wraps in tags. """ - yield 0, "" + # class format following what we do for highlight-js + # eg: class="python language-python" + yield 0, f'' yield from inner yield 0, "" @@ -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) diff --git a/test/tm-cases/admonitions_with_fenced_code_blocks.html b/test/tm-cases/admonitions_with_fenced_code_blocks.html index 7428c571..c8cf385d 100644 --- a/test/tm-cases/admonitions_with_fenced_code_blocks.html +++ b/test/tm-cases/admonitions_with_fenced_code_blocks.html @@ -2,7 +2,7 @@ note

Admonitions are able to contain fenced code blocks

-
print('like so')
+    
print('like so')
     
@@ -10,25 +10,25 @@
-
# admonitions WITHIN fenced code blocks should NOT be rendered
+
# admonitions WITHIN fenced code blocks should NOT be rendered
 .. attention:: title
    body
 
diff --git a/test/tm-cases/empty_fenced_code_blocks.html b/test/tm-cases/empty_fenced_code_blocks.html index 70cbf9ca..570e87bf 100644 --- a/test/tm-cases/empty_fenced_code_blocks.html +++ b/test/tm-cases/empty_fenced_code_blocks.html @@ -1,5 +1,5 @@
-

+

 
@@ -9,7 +9,7 @@

Pygments removes the empty line whitespace from the next code block

-

+

 
diff --git a/test/tm-cases/fenced_code_blocks_issue355.html b/test/tm-cases/fenced_code_blocks_issue355.html index 6696ede4..fc1c778c 100644 --- a/test/tm-cases/fenced_code_blocks_issue355.html +++ b/test/tm-cases/fenced_code_blocks_issue355.html @@ -1,5 +1,5 @@
-
some code block
+
some code block
 
diff --git a/test/tm-cases/fenced_code_blocks_issue426.html b/test/tm-cases/fenced_code_blocks_issue426.html index 66b1cb9c..5367ea97 100644 --- a/test/tm-cases/fenced_code_blocks_issue426.html +++ b/test/tm-cases/fenced_code_blocks_issue426.html @@ -15,7 +15,7 @@

URL PARAMETERS IN THE TEMPLATE

  • ContextMixin defines the method get_context_data:

    -
    def get_context_data(self, **kwargs):
    +
    def get_context_data(self, **kwargs):
         kwargs.setdefault('view', self)
         if self.extra_context is not None:
             kwargs.update(self.extra_context)
    @@ -26,7 +26,7 @@ 

    URL PARAMETERS IN THE TEMPLATE

    So when overriding one must be careful to extends super's kwargs:

    -
    def get_context_data(self, **kwargs):
    +
    def get_context_data(self, **kwargs):
         kwargs = super().get_context_data(**kwargs)
         kwargs['page_title'] = "Documentation"
         return kwargs
    diff --git a/test/tm-cases/fenced_code_blocks_issue462.html b/test/tm-cases/fenced_code_blocks_issue462.html
    index 6049aa67..f5f0f995 100644
    --- a/test/tm-cases/fenced_code_blocks_issue462.html
    +++ b/test/tm-cases/fenced_code_blocks_issue462.html
    @@ -1,7 +1,7 @@
     

    Section 1

    -
    x = 1
    +
    x = 1
     
    diff --git a/test/tm-cases/fenced_code_blocks_leading_lang_space.html b/test/tm-cases/fenced_code_blocks_leading_lang_space.html index f5815e5e..f3a7ee73 100644 --- a/test/tm-cases/fenced_code_blocks_leading_lang_space.html +++ b/test/tm-cases/fenced_code_blocks_leading_lang_space.html @@ -1,5 +1,5 @@
    -
    if True:
    +
    if True:
         print "hi"
     
    diff --git a/test/tm-cases/fenced_code_blocks_safe_highlight.html b/test/tm-cases/fenced_code_blocks_safe_highlight.html index b77f50b1..b6c79a88 100644 --- a/test/tm-cases/fenced_code_blocks_safe_highlight.html +++ b/test/tm-cases/fenced_code_blocks_safe_highlight.html @@ -1,5 +1,5 @@
    -
    if True:
    +
    if True:
         print "hi"
     
    @@ -9,7 +9,7 @@ http://github.github.com/github-flavored-markdown/.

    -
    def foo
    +
    def foo
         puts "hi"
     end
     
    diff --git a/test/tm-cases/fenced_code_blocks_syntax_highlighting.html b/test/tm-cases/fenced_code_blocks_syntax_highlighting.html index b77f50b1..b6c79a88 100644 --- a/test/tm-cases/fenced_code_blocks_syntax_highlighting.html +++ b/test/tm-cases/fenced_code_blocks_syntax_highlighting.html @@ -1,5 +1,5 @@
    -
    if True:
    +
    if True:
         print "hi"
     
    @@ -9,7 +9,7 @@ http://github.github.com/github-flavored-markdown/.

    -
    def foo
    +
    def foo
         puts "hi"
     end
     
    diff --git a/test/tm-cases/fenced_code_blocks_syntax_indentation.html b/test/tm-cases/fenced_code_blocks_syntax_indentation.html index f1a37817..060c66d3 100644 --- a/test/tm-cases/fenced_code_blocks_syntax_indentation.html +++ b/test/tm-cases/fenced_code_blocks_syntax_indentation.html @@ -1,5 +1,5 @@
    -
    def foo():
    +
    def foo():
         print "foo"
     
         print "bar"
    diff --git a/test/tm-cases/hash_nested_html_blocks.html b/test/tm-cases/hash_nested_html_blocks.html
    index 1e7bc4cb..39795ea0 100644
    --- a/test/tm-cases/hash_nested_html_blocks.html
    +++ b/test/tm-cases/hash_nested_html_blocks.html
    @@ -1,6 +1,6 @@
     
    -
    x = 1
    +
    x = 1
     
    diff --git a/test/tm-cases/issue276_fenced_code_blocks_in_lists.html b/test/tm-cases/issue276_fenced_code_blocks_in_lists.html index 7154404c..625be653 100644 --- a/test/tm-cases/issue276_fenced_code_blocks_in_lists.html +++ b/test/tm-cases/issue276_fenced_code_blocks_in_lists.html @@ -13,7 +13,7 @@

    empty codeblock just for sh*ts and giggles

    -
    test with language set
    +
    test with language set
     
    diff --git a/test/tm-cases/pyshell_and_fenced_code_blocks.html b/test/tm-cases/pyshell_and_fenced_code_blocks.html index 28b13705..56be151f 100644 --- a/test/tm-cases/pyshell_and_fenced_code_blocks.html +++ b/test/tm-cases/pyshell_and_fenced_code_blocks.html @@ -3,7 +3,7 @@

    From Recipe 302035

    Some examples:

    -
    >>> nprint(9876543210)
    +
    >>> nprint(9876543210)
     '9 876 543 210'
     >>> nprint(987654321, period=1, delimiter=",")
     '9,8,7,6,5,4,3,2,1,0'
    @@ -13,7 +13,7 @@ 

    From Recipe 302035

    Indented a bit:

    -
    >>> 1 + 1
    +
    >>> 1 + 1
     2
     
    @@ -22,7 +22,7 @@

    From Recipe 302035

    -
    >>> 1 + 1
    +
    >>> 1 + 1
     2
     
    @@ -31,7 +31,7 @@

    From Recipe 302035

    Cuddled to previous para (and at end of document):

    -
    >>> 2 + 2
    +
    >>> 2 + 2
     4
     
    diff --git a/test/tm-cases/quoted_fenced_code_blocks_whitespace_around_indented_lines.html b/test/tm-cases/quoted_fenced_code_blocks_whitespace_around_indented_lines.html index 58d5d439..5219b6ad 100644 --- a/test/tm-cases/quoted_fenced_code_blocks_whitespace_around_indented_lines.html +++ b/test/tm-cases/quoted_fenced_code_blocks_whitespace_around_indented_lines.html @@ -2,7 +2,7 @@

    Example:

    -
    if True:
    +
    if True:
         print()
     
         print()