aboutsummaryrefslogtreecommitdiff
path: root/tools/pandocfilter-pygments.py
blob: 2b4161f064dda70bcff3a786a98be91c84ada867 (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
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python3
"""
From: https://github.com/DoomHammer/pandocfilter-pygments

Requires:

    - pygments
    - pandocfilters 
"""

from pandocfilters import toJSONFilter, RawBlock
from pygments import highlight
from pygments.lexers import get_lexer_by_name, guess_lexer, TextLexer
from pygments.formatters import get_formatter_by_name


def pygmentize(key, value, format, meta):
    if key == "CodeBlock":
        [[ident, classes, keyvals], code] = value
        lexer = None
        for klass in classes:
            try:
                lexer = get_lexer_by_name(klass)
                break
            except:
                pass
        if lexer is None:
            try:
                lexer = guess_lexer(code)
            except:
                lexer = TextLexer()
        if format == "html5":
            format = "html"
        return [RawBlock(format, highlight(code, lexer, get_formatter_by_name(format)))]


if __name__ == "__main__":
    toJSONFilter(pygmentize)