Skip to main content

Jinja2 extension to highlight source code using Pygments

Project description



A simple Jinja2 extension that uses Pygments to highlight code blocks.

.. image::





* Jinja2 >= 2.4
* Pygments >= 1.5

Highlight examples

{% highlight 'language' %}
{% endhighlight %}

Replace `language` with the appropriate Pygments lexer short name:

Line numbering can be turned on using `lineno='inline'` or `lineno='table'` depending on the style of line numbering you want (as per Pygment's documentation:


{% highlight 'language', lineno='inline' %}
{% endhighlight %}

This can also be used without a language setting


{% highlight lineno='table' %}
{% endhighlight %}

Optional settings

By default Pygments renders the code block inside a div with the class 'highlight', if you want to change the name you can set the environment variable `jinja2_highlight_cssclass` to the class name you would like.

In jinja this can be done after you've created your environment;

env = Environment(extensions=['jinja2_highlight.HighlightExtension'])
# Set the css class name to 'codehilite'
env.extend(jinja2_highlight_cssclass = 'codehilite')

In Flask this can be done with the following (after creating your app):

app.jinja_env.extend(jinja2_highlight_cssclass = 'codehilite')

Using with Flask

See the example at:

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jinja2-highlight-0.6.1.tar.gz (3.4 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page