From 18261c201b6ed3342d30271f8647be257e843acc Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Fri, 10 Feb 2017 19:53:49 +0100 Subject: Add syntax highlighting tools Patched pygments is required --- tools/highlight.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/listdir.py | 34 +++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100755 tools/highlight.py create mode 100755 tools/listdir.py (limited to 'tools') diff --git a/tools/highlight.py b/tools/highlight.py new file mode 100755 index 0000000..a5da820 --- /dev/null +++ b/tools/highlight.py @@ -0,0 +1,56 @@ +#!/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) + if not os.path.isdir (basedestdir): + os.makedirs (basedestdir) + 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) + diff --git a/tools/listdir.py b/tools/listdir.py new file mode 100755 index 0000000..58e0ced --- /dev/null +++ b/tools/listdir.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# vim: set fileencoding=utf8 : + +""" +Create HTML directory index +""" + +if __name__ == '__main__': + import sys, os + from jinja2 import Template + + tpl = Template(""" + + + + + + Index of {{ name }} + + +

Index of {{ name }}

+

Back.

+ + """) + + files = map (str.strip, sys.stdin.readlines ()) + name = sys.argv[1] + + tpl.stream(files=files, name=name, root=os.path.relpath('.', name)).dump (sys.stdout) + -- cgit v1.2.3