tools/listdir.py

Raw file
Back to index

#!/usr/bin/env python
# vim: set fileencoding=utf8 :

"""
Create HTML directory index
"""

if __name__ == '__main__':
    import sys, os
    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>Index of {{ name }}</title>
    </head>
    <body>
    <h1>Index of {{ name }}</h1>
    <p><a href="{{ root }}">Back</a>.</p>
    <ul>
    {% for f in files %}
        <li><a href="{{ f }}">{{ f.rsplit('.',1)[0] }}</a></li>
    {% endfor %}
    </ul>
    </body></html>""")

    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)