diff options
| author | Kévin Le Gouguec <kevin.legouguec@gmail.com> | 2020-03-15 17:16:00 +0100 |
|---|---|---|
| committer | Kévin Le Gouguec <kevin.legouguec@gmail.com> | 2020-03-15 18:44:58 +0100 |
| commit | 63a9ef331f5761410255e5f0f2177111c719e3ac (patch) | |
| tree | 986050cee85f19b454e650293664e9611bbef294 /repo/www/make-index.py | |
| parent | 4914800729158cfc6de90b3964c222c4ab225e8e (diff) | |
| download | memory-leaks-63a9ef331f5761410255e5f0f2177111c719e3ac.tar.xz | |
Generate indices for folders without READMEs
Diffstat (limited to 'repo/www/make-index.py')
| -rwxr-xr-x | repo/www/make-index.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/repo/www/make-index.py b/repo/www/make-index.py new file mode 100755 index 0000000..07aa266 --- /dev/null +++ b/repo/www/make-index.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 + +from os import path +from subprocess import run +from sys import argv, exit + +from git import Repo + +from helpers import compute_directories + + +def parse_arguments(args): + if len(args) != 4: + exit(f'Usage: {argv[0]} EXTENSIONS FOLDER OUTPUT-DIR') + + return argv[1].split(), argv[2], argv[3] + + +def list_files(extensions, folder): + directories = compute_directories( + extensions, Repo(search_parent_directories=True) + ) + return directories[folder].subfolders, directories[folder].files + + +def generate_index_page(title, directories, files): + lines = (f'% {title}',) + tuple( + f'- [{d}/]({d}/index.html)' for d in directories + ) + tuple( + f'- [{f}]({f}.html)' for f in files + ) + mdown = '\n'.join(lines) + + return run( + ('pandoc', '-s'), text=True, check=True, capture_output=True, + input=mdown + ).stdout + + +def main(arguments): + extensions, folder, out_dir = parse_arguments(arguments) + + title = path.basename(folder) if folder else 'index' + + folders, files = list_files(extensions, folder) + + parsed_filenames = (path.splitext(f) for f in files) + names = tuple(name for name, _ in parsed_filenames) + + with open(path.join(out_dir, folder, 'index.html'), 'w') as index: + index.write(generate_index_page(title, folders, names)) + + +if __name__ == '__main__': + main(argv) |
