generate-page.py (1635B)
1 #!/usr/bin/env python3 2 3 from argparse import ArgumentParser 4 from os import path 5 from pathlib import Path 6 7 from git import Repo 8 9 from helpers import generate_crumbs, PandocRunner 10 11 12 def parse_arguments(): 13 parser = ArgumentParser() 14 parser.add_argument( 15 '--template', help='Pandoc template for HTML output.' 16 ) 17 parser.add_argument( 18 '--site-title', help='Prefix to add to <title>.' 19 ) 20 parser.add_argument( 21 '--lua-filter', dest='filters', action='append', 22 help='Lua filter to run the page through.' 23 ) 24 parser.add_argument( 25 '--stylesheet', dest='css', action='append', 26 help='CSS stylesheet to link to.' 27 ) 28 parser.add_argument( 29 '--title', help='Page title.' 30 ) 31 parser.add_argument( 32 'page', help='Page to convert to HTML.' 33 ) 34 parser.add_argument( 35 'output', help='Path to the output file.' 36 ) 37 return parser.parse_args() 38 39 40 def main(arguments): 41 repo_top = Repo(search_parent_directories=True).working_dir 42 path_to_top = path.relpath(repo_top, path.dirname(arguments.page)) 43 stylesheets = (path.join(path_to_top, s) for s in arguments.css) 44 45 page_path = Path(arguments.page).resolve().relative_to(repo_top) 46 47 pandoc = PandocRunner( 48 arguments.output, 49 arguments.template, 50 arguments.filters, 51 stylesheets, 52 variables={'crumbs': generate_crumbs(page_path)}, 53 ) 54 55 pandoc.run( 56 arguments.page, 57 metadata={'pagetitle': arguments.title, 58 'sitetitle': arguments.site_title} 59 ) 60 61 62 if __name__ == '__main__': 63 main(parse_arguments())