From 1b7eebb3563dab36584247bcab1d8e3b4e78833b Mon Sep 17 00:00:00 2001 From: Kévin Le Gouguec Date: Wed, 25 Mar 2020 18:48:12 +0100 Subject: Split index generation and HTML conversion So that I can re-use generate-index.py for READMEs. --- repo/www/generate-deps.py | 91 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100755 repo/www/generate-deps.py (limited to 'repo/www/generate-deps.py') diff --git a/repo/www/generate-deps.py b/repo/www/generate-deps.py new file mode 100755 index 0000000..d88d333 --- /dev/null +++ b/repo/www/generate-deps.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python3 + +"""Write dependencies for all website pages in makefile syntax. + +We want to compute: + +- a list of leaf pages, +- a list of auto-generated indices, +- dependencies for leaf pages: + OUTPUT/foo/bar.html: foo/bar.txt | OUTPUT/foo + - special case for READMEs: + OUTPUT/foo/index.html: foo/README.txt foo | OUTPUT/foo +""" + +from os import path +from sys import argv, exit + +from git import Repo + +from helpers import compute_directories + + +def parse_arguments(args): + if len(args) != 3: + exit(f'Usage: {argv[0]} EXTENSIONS OUTPUT-DIR') + + return argv[1].split(), argv[2] + + +def pjoin(directory, item): + return ( + path.join(directory, item) + if item # Avoid trailing slash for top-level files. + else directory + ) + + +def write_dependencies(deps_file, directories, top_dir, out_dir): + pages = list() + indices = list() + autoindices = list() + + for dpath, d in directories.items(): + autoindex = True + + src_dir = pjoin(top_dir, dpath) + + for f in d.files: + src_path = path.join(src_dir, f) + + name, _ = path.splitext(f) + deps = [src_path] + target = pages + + if name == 'README': + name = 'index' + deps.append(src_dir) + target = indices + autoindex = False + + html_dir = pjoin(out_dir, dpath) + html_path = path.join(html_dir, name+'.html') + + print(f'{html_path}: {" ".join(deps)} | {html_dir}', file=deps_file) + target.append(html_path) + + if autoindex: + autoindices.append( + path.join(out_dir, dpath, 'index.html') + ) + + print(file=deps_file) + print(f'pages = {" ".join(pages)}', file=deps_file) + print(f'indices = {" ".join(indices)}', file=deps_file) + print(f'autoindices = {" ".join(autoindices)}', file=deps_file) + + +def main(arguments): + extensions, out_dir = parse_arguments(arguments) + + repository = Repo(search_parent_directories=True) + top_dir = path.relpath(repository.working_dir, path.curdir) + + directories = compute_directories(extensions, repository) + + with open('deps.mk', 'w') as deps: + write_dependencies(deps, directories, top_dir, out_dir) + + +if __name__ == '__main__': + main(argv) -- cgit v1.2.3