#!/usr/bin/env python3 from os import path from subprocess import run from sys import argv, exit def parse_arguments(args): if len(args) != 3: exit(f'Usage: {argv[0]} TOP-DIR OUTPUT-DIR') return argv[1], argv[2] def find_sources(top_dir): p = run( ('find', top_dir, '-name', '*.md', '-o', '-name', '*.org'), capture_output=True, check=True, text=True ) return p.stdout.splitlines() def html_path(source_path, top_dir, out_dir): fname = path.basename(source_path) dname = path.dirname(source_path) name, _ = path.splitext(fname) if name == 'README': name = 'index' return path.join( out_dir, path.relpath(dname, top_dir), f'{name}.html' ) def write_dependencies(output, sources, top_dir, out_dir): pages = [] for src in sources: html = html_path(src, top_dir, out_dir) html_dir = path.dirname(html) print(f'{html}: {src} | {html_dir}', file=output) pages.append(html) print(file=output) print(f'pages = {" ".join(pages)}', file=output) def main(argv): top_dir, out_dir = parse_arguments(argv) source_files = find_sources(top_dir) with open('deps.mk', 'w') as deps: write_dependencies(deps, source_files, top_dir, out_dir) if __name__ == '__main__': main(argv)