|
| 1 | +#!/usr/bin/env .venv/bin/python3 |
| 2 | + |
| 3 | +# This script is meant to be run from the top-level directory of the |
| 4 | +# repository, as 'scripts/h2h.py'. The virtualenv must already exist |
| 5 | +# (use "inv venv" to create it). |
| 6 | + |
| 7 | +import argparse |
| 8 | +import os.path |
| 9 | +import pathlib |
| 10 | +import sys |
| 11 | + |
| 12 | +import flask |
| 13 | + |
| 14 | +root_path = pathlib.Path(__file__).parent.parent |
| 15 | + |
| 16 | +sys.path.append(str(root_path)) |
| 17 | + |
| 18 | +from vimhelp.vimh2h import VimH2H # noqa: E402 |
| 19 | + |
| 20 | + |
| 21 | +def main(): |
| 22 | + parser = argparse.ArgumentParser(description="Convert Vim help files to HTML") |
| 23 | + parser.add_argument( |
| 24 | + "--in-dir", |
| 25 | + "-i", |
| 26 | + required=True, |
| 27 | + type=pathlib.Path, |
| 28 | + help="Directory of Vim doc files", |
| 29 | + ) |
| 30 | + parser.add_argument( |
| 31 | + "--out-dir", |
| 32 | + "-o", |
| 33 | + type=pathlib.Path, |
| 34 | + help="Output directory (omit for no output)", |
| 35 | + ) |
| 36 | + parser.add_argument( |
| 37 | + "--project", |
| 38 | + "-p", |
| 39 | + choices=("vim", "neovim"), |
| 40 | + default="vim", |
| 41 | + help="Vim flavour (default: vim)", |
| 42 | + ) |
| 43 | + parser.add_argument( |
| 44 | + "--web-version", |
| 45 | + "-w", |
| 46 | + action="store_true", |
| 47 | + help="Generate the web version of the files (default: offline version)", |
| 48 | + ) |
| 49 | + parser.add_argument( |
| 50 | + "--theme", |
| 51 | + "-t", |
| 52 | + choices=("light", "dark"), |
| 53 | + help="Color theme (default: OS-native)", |
| 54 | + ) |
| 55 | + parser.add_argument( |
| 56 | + "--no-tags", |
| 57 | + "-T", |
| 58 | + action="store_true", |
| 59 | + help="Ignore any tags file, always recreate tags from scratch", |
| 60 | + ) |
| 61 | + parser.add_argument( |
| 62 | + "--profile", "-P", action="store_true", help="Profile performance" |
| 63 | + ) |
| 64 | + parser.add_argument( |
| 65 | + "basenames", nargs="*", help="List of files to process (default: all)" |
| 66 | + ) |
| 67 | + args = parser.parse_args() |
| 68 | + |
| 69 | + app = flask.Flask( |
| 70 | + __name__, |
| 71 | + root_path=pathlib.Path(__file__).resolve().parent, |
| 72 | + static_url_path="", |
| 73 | + static_folder="../static", |
| 74 | + template_folder="../templates", |
| 75 | + ) |
| 76 | + app.jinja_options["trim_blocks"] = True |
| 77 | + app.jinja_options["lstrip_blocks"] = True |
| 78 | + |
| 79 | + with app.app_context(): |
| 80 | + if args.profile: |
| 81 | + import cProfile |
| 82 | + import pstats |
| 83 | + |
| 84 | + with cProfile.Profile() as pr: |
| 85 | + run(args) |
| 86 | + stats = pstats.Stats(pr).sort_stats("cumulative") |
| 87 | + stats.print_stats() |
| 88 | + else: |
| 89 | + run(args) |
| 90 | + |
| 91 | + |
| 92 | +def run(args): |
| 93 | + if not args.in_dir.is_dir(): |
| 94 | + raise RuntimeError(f"{args.in_dir} is not a directory") |
| 95 | + |
| 96 | + prelude = VimH2H.prelude(theme=args.theme) |
| 97 | + |
| 98 | + mode = "hybrid" if args.web_version else "offline" |
| 99 | + |
| 100 | + if not args.no_tags and (tags_file := args.in_dir / "tags").is_file(): |
| 101 | + print("Processing tags file...") |
| 102 | + h2h = VimH2H(mode=mode, project=args.project, tags=tags_file.read_text()) |
| 103 | + faq = args.in_dir / "vim_faq.txt" |
| 104 | + if faq.is_file(): |
| 105 | + print("Processing FAQ tags...") |
| 106 | + h2h.add_tags(faq.name, faq.read_text()) |
| 107 | + else: |
| 108 | + print("Initializing tags...") |
| 109 | + h2h = VimH2H(mode=mode, project=args.project) |
| 110 | + for infile in args.in_dir.iterdir(): |
| 111 | + if infile.suffix == ".txt": |
| 112 | + h2h.add_tags(infile.name, infile.read_text()) |
| 113 | + |
| 114 | + if args.out_dir is not None: |
| 115 | + args.out_dir.mkdir(exist_ok=True) |
| 116 | + |
| 117 | + for infile in args.in_dir.iterdir(): |
| 118 | + if len(args.basenames) != 0 and infile.name not in args.basenames: |
| 119 | + continue |
| 120 | + if infile.suffix != ".txt" and infile.name != "tags": |
| 121 | + print(f"Ignoring {infile}") |
| 122 | + continue |
| 123 | + content = infile.read_text() |
| 124 | + print(f"Processing {infile}...") |
| 125 | + html = h2h.to_html(infile.name, content) |
| 126 | + if args.out_dir is not None: |
| 127 | + with (args.out_dir / f"{infile.name}.html").open("w") as f: |
| 128 | + f.write(prelude) |
| 129 | + f.write(html) |
| 130 | + |
| 131 | + if args.out_dir is not None: |
| 132 | + print("Symlinking static files...") |
| 133 | + static_dir_rel = os.path.relpath(root_path / "static", args.out_dir) |
| 134 | + for target in (root_path / "static").iterdir(): |
| 135 | + target_name = target.name |
| 136 | + if target_name == f"favicon-{args.project}.ico": |
| 137 | + src_name = "favicon.ico" |
| 138 | + elif target_name.startswith("favicon-"): |
| 139 | + continue |
| 140 | + else: |
| 141 | + src_name = target_name |
| 142 | + src = pathlib.Path(args.out_dir / src_name) |
| 143 | + src.unlink(missing_ok=True) |
| 144 | + src.symlink_to(f"{static_dir_rel}/{target_name}") |
| 145 | + |
| 146 | + print("Done.") |
| 147 | + |
| 148 | + |
| 149 | +main() |
0 commit comments