1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | from django import template
register = template.Library()
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import get_lexer_by_name, guess_lexer
@register.filter
def render(content):
"""Render this content for display."""
formatter = HtmlFormatter(cssclass=u'source')
try:
# Guess a lexer by the contents of the block.
lexer = guess_lexer(content)
except ValueError, e:
# Just make it plain text.
lexer = get_lexer_by_name(u'text', stripnl=True, encoding=u'UTF-8')
result = highlight(content, lexer, formatter)
return unicode(result)
|
More like this
- Custom color field with Javascript color picker by seanl 4 years, 10 months ago
- Url filter middleware by limodou 6 years, 2 months ago
- Pygmentation by alcides 4 years, 11 months ago
- Another pygments for ReST by limodou 6 years, 2 months ago
- whitespaceoptimize block tag by peterbe 4 years, 8 months ago
Comments