diff --git a/directory-listing/create-index.py b/directory-listing/create-index.py index 011a8c25bd30b3d7f02f0913aad09cd9cea13393..71bfd13611299926dda7f55c75daf8f683837c23 100755 --- a/directory-listing/create-index.py +++ b/directory-listing/create-index.py @@ -1,87 +1,21 @@ #!/usr/bin/env python3 +import pathlib + import jinja2 import os import sys from datetime import datetime +import argparse def timestamp_to_datetime(timestamp): return datetime.fromtimestamp(timestamp) -def main(inputfolder, outputfolder, ignore_files): - jinjaenv = jinja2.Environment(autoescape=jinja2.select_autoescape()) +def main(inputfolder, outputfolder, template, ignore_files): + jinjaenv = jinja2.Environment(autoescape=jinja2.select_autoescape(), loader=jinja2.FileSystemLoader("templates")) jinjaenv.filters["timestamp_to_datetime"] = timestamp_to_datetime - template = jinjaenv.from_string("""<!DOCTYPE html> - <html> - <head> - <meta http-equiv="content-type" content="text/html; charset=utf-8"> - <meta name="viewport" content="width=device-width"> - <style type="text/css"> - body,html { - background: #fff; - font-family: "Bitstream Vera Sans","Lucida Grande","Lucida Sans Unicode",Lucidux,Verdana,Lucida,sans-serif; - } - tr:nth-child(even) { - background:#f4f4f4; - } - th,td { - padding:0.1em 0.5em; - } - th { - text-align:left; - font-weight:bold; - background:#eee; - border-bottom:1px solid #aaa; - } - #list { - border:1px solid #aaa; - width:100%; - } - a { - color:#a33; - } - a:hover { - color:#e33; - } - </style> - </head> - <body> - <h1>Index of /{{ path }}</h1> - <table id="list"> - <thead> - <tr> - <th style="width:55%"> - File Name - </th> - <th style="width:20%"> - File Size</th> - <th style="width:25%"> - Date - </th> - </tr> - </thead> - <tbody> - <tr> - <td class="link"> - <a href="../">Parent directory/</a> - </td> - <td class="size">-</td> - <td class="date">-</td> - </tr> - {% for entry, stat, is_dir in entries if not entry.startswith('.') %} - <tr> - <td class="link"> - <a href="{{ entry + ('/' if is_dir else '') }}" title="{{ entry }}">{{ entry + ('/' if is_dir else '') }}</a> - </td> - <td class="size">{{ '-' if is_dir else stat.st_size|filesizeformat }}</td> - <td class="date">{{ (stat.st_mtime|timestamp_to_datetime).isoformat(timespec='seconds') }}</td> - </tr> - {% endfor %} - </tbody> - </table> - <p><a href="https://legal.cccv.de">Impressum - Datenschutzerklärung</a></p> - </body> - </html> - """) + + template = jinjaenv.get_template(template) + for path, dirs, files in os.walk(inputfolder): dirs.sort() files.sort() @@ -96,7 +30,11 @@ def main(inputfolder, outputfolder, ignore_files): outputfile.close() if __name__ == '__main__': - if len(sys.argv) != 3: - print(f'Usage: {sys.argv[0]} <directory tree to create index from> <output path for index tree>') - sys.exit(1) - main(inputfolder=sys.argv[1], outputfolder=sys.argv[2], ignore_files=['index.html']) + parser = argparse.ArgumentParser(description='Create html directory index for given path.') + parser.add_argument('path', type=pathlib.Path, help='Path to create index from') + parser.add_argument('output_directory', type=pathlib.Path, help='Directory for generated html files') + parser.add_argument('--template', help='Template to use', default='default.html.j2') + + args = parser.parse_args() + + main(inputfolder=args.path, outputfolder=args.output_directory, template=args.template, ignore_files=['index.html']) diff --git a/directory-listing/templates/default.html.j2 b/directory-listing/templates/default.html.j2 new file mode 100644 index 0000000000000000000000000000000000000000..0063f98f5d2c311c1c6336fe07383e261eeb497c --- /dev/null +++ b/directory-listing/templates/default.html.j2 @@ -0,0 +1,71 @@ +<!DOCTYPE html> +<html> + <head> + <meta http-equiv="content-type" content="text/html; charset=utf-8"> + <meta name="viewport" content="width=device-width"> + <style type="text/css"> + body,html { + background: #fff; + font-family: "Bitstream Vera Sans","Lucida Grande","Lucida Sans Unicode",Lucidux,Verdana,Lucida,sans-serif; + } + tr:nth-child(even) { + background:#f4f4f4; + } + th,td { + padding:0.1em 0.5em; + } + th { + text-align:left; + font-weight:bold; + background:#eee; + border-bottom:1px solid #aaa; + } + #list { + border:1px solid #aaa; + width:100%; + } + a { + color:#a33; + } + a:hover { + color:#e33; + } + </style> + </head> + <body> + <h1>Index of /{{ path }}</h1> + <table id="list"> + <thead> + <tr> + <th style="width:55%"> + File Name + </th> + <th style="width:20%"> + File Size</th> + <th style="width:25%"> + Date + </th> + </tr> + </thead> + <tbody> + <tr> + <td class="link"> + <a href="../">Parent directory/</a> + </td> + <td class="size">-</td> + <td class="date">-</td> + </tr> + {% for entry, stat, is_dir in entries if not entry.startswith('.') %} + <tr> + <td class="link"> + <a href="{{ entry + ('/' if is_dir else '') }}" title="{{ entry }}">{{ entry + ('/' if is_dir else '') }}</a> + </td> + <td class="size">{{ '-' if is_dir else stat.st_size|filesizeformat }}</td> + <td class="date">{{ (stat.st_mtime|timestamp_to_datetime).isoformat(timespec='seconds') }}</td> + </tr> + {% endfor %} + </tbody> + </table> + <p><a href="https://legal.cccv.de">Impressum - Datenschutzerklärung</a></p> + </body> +</html> \ No newline at end of file diff --git a/directory-listing/templates/simple.html.j2 b/directory-listing/templates/simple.html.j2 new file mode 100644 index 0000000000000000000000000000000000000000..ccf8cd1e1d7b06d7f8fd2d93a66879a202b6b78d --- /dev/null +++ b/directory-listing/templates/simple.html.j2 @@ -0,0 +1,66 @@ +<!DOCTYPE html> +<html> + <head> + <meta http-equiv="content-type" content="text/html; charset=utf-8"> + <meta name="viewport" content="width=device-width"> + <style type="text/css"> + body,html { + background: #fff; + font-family: "Bitstream Vera Sans","Lucida Grande","Lucida Sans Unicode",Lucidux,Verdana,Lucida,sans-serif; + } + tr:nth-child(even) { + background:#f4f4f4; + } + th,td { + padding:0.1em 0.5em; + } + th { + text-align:left; + font-weight:bold; + background:#eee; + border-bottom:1px solid #aaa; + } + #list { + border:1px solid #aaa; + width:100%; + } + a { + color:#a33; + } + a:hover { + color:#e33; + } + </style> + </head> + <body> + <h1>Index of /{{ path }}</h1> + <table id="list"> + <thead> + <tr> + <th style="width:55%"> + File Name + </th> + <th style="width:20%"> + File Size</th> + </tr> + </thead> + <tbody> + <tr> + <td class="link"> + <a href="../">Parent directory/</a> + </td> + <td class="size">-</td> + </tr> + {% for entry, stat, is_dir in entries if not entry.startswith('.') %} + <tr> + <td class="link"> + <a href="{{ entry + ('/' if is_dir else '') }}" title="{{ entry }}">{{ entry + ('/' if is_dir else '') }}</a> + </td> + <td class="size">{{ '-' if is_dir else stat.st_size|filesizeformat }}</td> + </tr> + {% endfor %} + </tbody> + </table> + <p><a href="https://legal.cccv.de">Impressum - Datenschutzerklärung</a></p> + </body> +</html> \ No newline at end of file