From 04e68443040c7abad84d66477e98f93bed701760 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Mon, 4 Feb 2019 13:09:03 +0100 Subject: Initial import --- tools/highlight.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 tools/highlight.py (limited to 'tools/highlight.py') diff --git a/tools/highlight.py b/tools/highlight.py new file mode 100755 index 0000000..ecdb49e --- /dev/null +++ b/tools/highlight.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 +# vim: set fileencoding=utf8 : + +""" +Highlight elan source file +""" + +if __name__ == '__main__': + import sys, os, shutil + from pygments import highlight + from pygments.lexers import get_lexer_by_name + from pygments.formatters import HtmlFormatter + from jinja2 import Template + + tpl = Template(""" + + + + + + {{ path }} + + + +

{{ path }}

+

Raw file
Back to index

+ {{ code|safe }} + """) + + destdir = '_build' + f = sys.argv[1] + print (f) + + basedir = os.path.dirname (f) + basedestdir = os.path.join (destdir, basedir) + os.makedirs (basedestdir, exist_ok=True) + shutil.copy (f, os.path.join (destdir, f)) + destf = os.path.join (destdir, f + '.html') + + try: + with open (f, 'r') as srcfd: + code = srcfd.read () + except UnicodeDecodeError: + # that’s fine + sys.exit (0) + + lexer = get_lexer_by_name("elan", stripall=True) + formatter = HtmlFormatter (linenos=True, lineanchors='line', anchorlinenos=True) + with open (destf, 'w') as destfd: + tpl.stream(code=highlight(code, lexer, formatter), path=f, rawfile=os.path.basename (f), index=os.path.relpath ('.', os.path.dirname (f))).dump (destfd) + -- cgit v1.2.3