pythondjangomarkdownsyntax-highlightingpython-markdown

How to Add "lang" Attribute to a <pre> Tag in python-markdown?


I'm using django-pygmentes in order to highlight my code-blocks. I'm writing the contents with markdown and convert it to HTML in the view part. Everything is ok. Now, I want to implement the highlighting side. The Pygmentes package needs something like this in order to apply the colors:

<pre lang="python">...</pre>

But, it's what I get when I write a code block in my post:

<pre><code clas="language-python">...</code></pre>

Here is my markdown:

```python
print('Hey')

So, Pygments could not find the element. Is there any way to override any method and apply the changes?

UPDATED: Now, I've installed the pygments and added this extension to the markdown extensions. Here is my markdown and what it generates:

```{lang="python"}
print('Hello World')

Output:

<pre><code lang="python">
print('Hello World')
</code></pre>

Which is great, but there is no highliting yet.. :( I also linked the classic styles.css file after running pygmentize -S default -f html -a .codehilite > styles.css and it linked properly.

Here is my custom markdown filter. The problem might be coming from this module:

from django import template
from django.template.defaultfilters import stringfilter

import markdown as md

register = template.Library()


@register.filter()
@stringfilter
def markdown(value):
    return md.markdown(value, extensions=['markdown.extensions.fenced_code', 'markdown.extensions.attr_list'])

Solution

  • I forgot to add the highlight extension in the custom filter module. Now, everything is working well. Thanks.