simplify 'docs/options.md' generation

use the plain text output from '--help'
instead of trying to generate 'fancy' HTML with Markdown restrictions
This commit is contained in:
Mike Fährmann
2023-01-06 11:21:47 +01:00
parent 9695c4e88d
commit 3c03928d75
2 changed files with 123 additions and 495 deletions

View File

@@ -7,11 +7,11 @@
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
"""Generate a Markdown document listing gallery-dl's command-line arguments"""
"""Generate a document listing gallery-dl's command-line arguments"""
import os
import re
from argparse import SUPPRESS
import sys
import util
from gallery_dl import option
@@ -21,71 +21,22 @@ TEMPLATE = """# Command-Line Options
<!-- auto-generated by {} -->
{}
"""
TABLE = """
## {}
<table>
<tbody valign="top">
{}
</tbody>
</table>
""".format
ROW = """<tr>
<td>{}</td>
<td>{}</td>
<td>{}</td>
<td>{}</td>
</tr>""".format
{}"""
tables = []
sub = re.compile(r"'([^']+)'").sub
nb_hyphen = "&#8209;" # non-breaking hyphen
parser = option.build_parser()
parser.usage = ""
for group in option.build_parser()._action_groups[2:]:
tbody = []
for action in group._group_actions:
help = action.help
if help == SUPPRESS:
continue
meta = action.metavar or ""
try:
short, long = action.option_strings
except ValueError:
try:
long = action.option_strings[0]
except IndexError:
continue
short = ""
if short:
short = "<code>" + short.replace("-", nb_hyphen) + "</code>"
if long:
long = "<code>" + long.replace("-", nb_hyphen) + "</code>"
if meta:
meta = "<code>" + meta.partition("[")[0] + "</code>"
if help:
help = help.replace("<", "&lt;").replace(">", "&gt;")
if "Example: " in help:
help, sep, example = help.partition("Example: ")
help = sub("<code>\\1</code>", help) + "<br />" + sep + example
else:
help = sub("<code>\\1</code>", help)
tbody.append(ROW(short, long, meta, help))
tables.append(TABLE(group.title, "\n".join(tbody)))
opts = parser.format_help()
opts = opts[8:] # strip 'usage'
opts = re.sub(r"(?m)^(\w+.*)", "## \\1", opts) # group names to headings
opts = opts.replace("\n ", "\n ") # indent by 4
with open(util.path("docs", "options.md"), "w", encoding="utf-8") as fp:
outfile = sys.argv[1] if len(sys.argv) > 1 else util.path("docs", "options.md")
with open(outfile, "w", encoding="utf-8") as fp:
fp.write(TEMPLATE.format(
"/".join(os.path.normpath(__file__).split(os.sep)[-2:]),
"\n".join(tables),
opts,
))