summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Reports/reports/templatetags/syntax_coloring.py
blob: 14827b3c6527a8302a432743cff7c5adbf539727 (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

try:
    from pygments import highlight
    from pygments.lexers import get_lexer_by_name
    from pygments.formatters import HtmlFormatter
    colorize = True
except:
    colorize = False
    
register = template.Library()

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


register.filter('syntaxhilight', syntaxhilight)