summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Reports/reports/templatetags/syntax_coloring.py
blob: 083b83a73701675a61266596eb269224675c88e9 (plain)
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
from django import template

register = template.Library()

try:
    from pygments import highlight
    from pygments.lexers import get_lexer_by_name
    from pygments.formatters import HtmlFormatter
    colorize = True

except:
    colorize = False

def syntaxhilight(value, arg="diff"):
    '''Returns a syntax-hilighted version of Code; requires code/language arguments'''
    if colorize:
        try:
            lexer = get_lexer_by_name(arg)
            return highlight(value, lexer, HtmlFormatter())
        except:
            return value
    else:
        return value

register.filter('syntaxhilight', syntaxhilight)