From 2682bc5d1d864341aaeb42a449db73c3ecd16d70 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Wed, 30 Dec 2020 19:07:31 +0000 Subject: Initial import --- tools/pandocfilter-pygments.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100755 tools/pandocfilter-pygments.py (limited to 'tools/pandocfilter-pygments.py') diff --git a/tools/pandocfilter-pygments.py b/tools/pandocfilter-pygments.py new file mode 100755 index 0000000..ee678f8 --- /dev/null +++ b/tools/pandocfilter-pygments.py @@ -0,0 +1,37 @@ +#!/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) + -- cgit v1.2.3