|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | +import re |
| 4 | +import subprocess |
| 5 | + |
| 6 | + |
| 7 | +def get_filename(args): |
| 8 | + directory = Path("created").joinpath(*args[:-1]) |
| 9 | + filename = args[-1] + ".md" |
| 10 | + return directory / filename |
| 11 | + |
| 12 | + |
| 13 | +def sanitise_line(line): |
| 14 | + # Remove trailing whitespace otherwise the markdown parser can insert an extra \n |
| 15 | + line = line.rstrip() |
| 16 | + |
| 17 | + # Replace angular brackets with HTML equivalents. |
| 18 | + line = line.replace(r"&", r"&") |
| 19 | + line = line.replace(r"<", r"<") |
| 20 | + line = line.replace(r">", r">") |
| 21 | + |
| 22 | + # If there are whitespace characters at the start of the line, replace the first with an   |
| 23 | + # so that it is not discarded by the markdown parser used by the parsed-literal directive. |
| 24 | + line = re.sub(r"^\s", r" ", line) |
| 25 | + |
| 26 | + return line |
| 27 | + |
| 28 | + |
| 29 | +# Process a single subcommand, adding new subcommands found to to_process. |
| 30 | +def process(args, to_process): |
| 31 | + cmd = args + ["--help"] |
| 32 | + cmd_string = " ".join(cmd) |
| 33 | + filename = get_filename(args) |
| 34 | + filename.parent.mkdir(parents=True, exist_ok=True) |
| 35 | + |
| 36 | + print(f"Writing '{cmd_string}' to file {filename}") |
| 37 | + p = subprocess.run(cmd, capture_output=True, text=True, check=True) |
| 38 | + |
| 39 | + # Write output markdown file, identifying subcommands at the same time to provide |
| 40 | + # links to the subcommand markdown files. |
| 41 | + subcommands = [] |
| 42 | + with open(filename, "w") as f: |
| 43 | + f.write(f"({filename})=\n") # Target for links. |
| 44 | + f.write(f"# {' '.join(args)}\n") |
| 45 | + f.write("\n") |
| 46 | + f.write("```{parsed-literal}\n") |
| 47 | + |
| 48 | + in_subcommand_section = False |
| 49 | + for line in p.stdout.splitlines(): |
| 50 | + if in_subcommand_section: |
| 51 | + match = re.match(r"^( )([\w\-_]+)(\s+.*)$", line) |
| 52 | + if match: |
| 53 | + subcommand = match.group(2) |
| 54 | + subcommand_filename = get_filename(args + [subcommand]) |
| 55 | + line = match.group(1) + f"[{subcommand}]({subcommand_filename})" + match.group(3) |
| 56 | + subcommands.append(subcommand) |
| 57 | + elif line.startswith("SUBCOMMANDS:"): |
| 58 | + in_subcommand_section = True |
| 59 | + |
| 60 | + f.write(sanitise_line(line) + "\n") |
| 61 | + f.write("```\n") |
| 62 | + |
| 63 | + subcommands.sort() |
| 64 | + to_process.extend(args + [subcommand] for subcommand in subcommands) |
| 65 | + |
| 66 | + if len(subcommands) > 0: |
| 67 | + # Hidden table of contents for subcommands of this command/subcommand. |
| 68 | + f.write("\n") |
| 69 | + f.write("```{toctree}\n") |
| 70 | + f.write(":hidden:\n") |
| 71 | + for subcommand in subcommands: |
| 72 | + f.write(f"{args[-1]}/{subcommand}\n") |
| 73 | + f.write("```\n") |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + # Modify the PATH so that git2cpp is found by name, as using a full path will cause the help |
| 78 | + # pages to write that full path. |
| 79 | + git2cpp_dir = Path(__file__).parent.parent / 'build' |
| 80 | + os.environ["PATH"] = f'{git2cpp_dir}{os.pathsep}{os.environ["PATH"]}' |
| 81 | + |
| 82 | + to_process = [["git2cpp"]] |
| 83 | + while len(to_process) > 0: |
| 84 | + subcommand = to_process.pop(0) |
| 85 | + process(subcommand, to_process) |
0 commit comments