diff options
author | Juan J. Martinez <jjm@usebox.net> | 2020-12-30 19:07:31 +0000 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2020-12-30 19:23:41 +0000 |
commit | 2682bc5d1d864341aaeb42a449db73c3ecd16d70 (patch) | |
tree | 9116764364b4ee0ce7f6037305077807b57776de /tools/pandocfilter-pygments.py | |
download | ubox-msx-lib-1.0.tar.gz ubox-msx-lib-1.0.zip |
Initial import1.0
Diffstat (limited to 'tools/pandocfilter-pygments.py')
-rwxr-xr-x | tools/pandocfilter-pygments.py | 37 |
1 files changed, 37 insertions, 0 deletions
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) + |