aboutsummaryrefslogtreecommitdiff
path: root/tools/pandocfilter-pygments.py
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2020-12-30 19:07:31 +0000
committerJuan J. Martinez <jjm@usebox.net>2020-12-30 19:23:41 +0000
commit2682bc5d1d864341aaeb42a449db73c3ecd16d70 (patch)
tree9116764364b4ee0ce7f6037305077807b57776de /tools/pandocfilter-pygments.py
downloadubox-msx-lib-2682bc5d1d864341aaeb42a449db73c3ecd16d70.tar.gz
ubox-msx-lib-2682bc5d1d864341aaeb42a449db73c3ecd16d70.zip
Initial import1.0
Diffstat (limited to 'tools/pandocfilter-pygments.py')
-rwxr-xr-xtools/pandocfilter-pygments.py37
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)
+