summaryrefslogtreecommitdiff
path: root/tools/highlight.py
blob: 224d2d93d1ad611f30b1490d778ad0beffbe1ecd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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("""<!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
      <link rel="stylesheet" href="/style.min.css" type="text/css" />
      <title>{{ path }}</title>
      <style>
      body {
            max-width: none;
      }
      </style>
    </head>
    <body>
    <h1>{{ path }}</h1>
    <p><a href="{{ rawfile }}">Raw file</a><br><a href="{{ index }}">Back to index</a></p>
    {{ code|safe }}
    </body></html>""")

    destdir = '_build'
    f = sys.argv[1]

    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 as e:
        # that’s expected for data files
        print ('skipping', f, e)
        sys.exit (0)

    print (f)
    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)