[docs/options] add Table of Contents

This commit is contained in:
Mike Fährmann
2026-01-27 18:33:04 +01:00
parent ef8f2869e7
commit 6c9dff1e29
2 changed files with 32 additions and 5 deletions

View File

@@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2023 Mike Fährmann
# Copyright 2023-2026 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
@@ -29,23 +29,36 @@ class Formatter(option.Formatter):
pass
def repl(match):
cat = match[1].rstrip(":")
toc.append(f"* [{cat}](#{cat.lower().replace(' ', '-')})")
return f"## {cat}:"
parser = option.build_parser()
parser.formatter_class = Formatter
parser.format_usage = lambda: ""
toc = []
opts = parser.format_help()
opts = re.sub(r"(?m)^(\w+.*)", "## \\1", opts) # group names to headings
opts = re.sub(r"(?m)^(\w+.*)", repl, opts) # group names to headings
opts = opts.replace("\n ", "\n ") # indent by 4
toc = "\n".join(toc)
SELF = "/".join(os.path.normpath(__file__).split(os.sep)[-2:])
PATH = (sys.argv[1] if len(sys.argv) > 1 else
util.path("docs", "options.md"))
with util.lazy(PATH) as fp:
fp.write(f"""# Command-Line Options
fp.write(f"""\
# Command-Line Options
<!-- auto-generated by {SELF} -->
{opts}""")
## Table of Contents
{toc}
{opts}\
""")