Login

Markdown and Syntax Highlighting in Django

Author:
blinks
Posted:
March 19, 2007
Language:
Python
Version:
Pre .96
Tags:
pygments markdown syntax-highlighting template-tag
Score:
8 (after 8 ratings)

This is a Django template tag that renders an arbitrary block of text with Markdown and Pygments.

Use Markdown as usual, and when you have a code block to insert, put it inside code tags, with the language as the class:

<code class='python'>print "Hello, World"</code>

To use it in a template, first {% load ... %} the tag library, then {{ content|render }} your content.

The tag takes one optional argument, to enable safe rendering in markdown. To use it, call {{ content|render:"safe" }}.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from django import template
register = template.Library()

# Pygments: http://pygments.org -- a generic syntax highlighter.
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import get_lexer_by_name, guess_lexer

# Python Markdown (dropped in my project directory)
from codebase.markdown import markdown

# BeautifulSoup: http://www.crummy.com/software/BeautifulSoup/
from codebase.BeautifulSoup import BeautifulSoup

@register.filter
def render(content, safe="unsafe"):
    """Render this content for display."""

    # First, pull out all the <code></code> blocks, to keep them away
    # from Markdown (and preserve whitespace).
    soup = BeautifulSoup(str(content))
    code_blocks = soup.findAll('code')
    for block in code_blocks:
        block.replaceWith('<code class="removed"></code>')

    # Run the post through markdown.
    if safe == "unsafe":
        safe_mode = False
    else:
        safe_mode = True
    markeddown = markdown(str(soup), safe_mode=safe_mode)

    # Replace the pulled code blocks with syntax-highlighted versions.
    soup = BeautifulSoup(markeddown)
    empty_code_blocks, index = soup.findAll('code', 'removed'), 0
    formatter = HtmlFormatter(cssclass='source')
    for block in code_blocks:
        if block.has_key('class'):
            # <code class='python'>python code</code>
            language = block['class']
        else:
            # <code>plain text, whitespace-preserved</code>
            language = 'text'
        try:
            lexer = get_lexer_by_name(language, stripnl=True, encoding='UTF-8')
        except ValueError, e:
            try:
                # Guess a lexer by the contents of the block.
                lexer = guess_lexer(block.renderContents())
            except ValueError, e:
                # Just make it plain text.
                lexer = get_lexer_by_name('text', stripnl=True, encoding='UTF-8')
        empty_code_blocks[index].replaceWith(
                highlight(block.renderContents(), lexer, formatter))
        index = index + 1

    return str(soup)

More like this

  1. typygmentdown by ubernostrum 8 years ago
  2. Pygments Rendering Template Filter by ericflo 8 years, 6 months ago
  3. Code syntax highlighting templatetag by badrunner 7 years, 11 months ago
  4. template tag for highlighting currently active page by adunar 6 years, 10 months ago
  5. Parsing and Highlighting &lt;code&gt; Blocks by joshua 8 years, 6 months ago

Comments

bartTC (on March 20, 2007):

Thank you, this is just working great! As most Users will use UTF-8 as standard-encoding you should change Line 41 to:

lexer = get_lexer_by_name(language, stripnl=True, encoding='UTF-8')

That will fix problems in Pygments with Umlauts and other non-ascii charactern.

#

louis (on June 14, 2008):

Thank you,

[HTML_REMOVED] {{ content|render|safe }} [HTML_REMOVED]

worked out great

#

Please login first before commenting.