2022-02-23 20:54:56 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
"""
|
|
|
|
Auto-generates documentation from command defs in console.py.
|
|
|
|
"""
|
|
|
|
|
2024-03-26 09:04:32 +00:00
|
|
|
import click
|
2022-02-23 20:54:56 +00:00
|
|
|
import html
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import shutil
|
|
|
|
import textwrap
|
|
|
|
|
2024-03-26 09:04:32 +00:00
|
|
|
from click import Command
|
|
|
|
|
|
|
|
from twitchdl.cli import cli
|
2022-02-23 20:54:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
START_MARKER = "<!-- ------------------- generated docs start ------------------- -->"
|
|
|
|
END_MARKER = "<!-- ------------------- generated docs end ------------------- -->"
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
update_changelog()
|
2024-03-26 09:04:32 +00:00
|
|
|
|
|
|
|
parent_ctx = click.Context(cli, info_name="twitch-dl")
|
|
|
|
for name, command in cli.commands.items():
|
|
|
|
ctx = click.Context(cli, info_name=name, parent=parent_ctx)
|
|
|
|
update_docs(command, ctx)
|
2022-02-23 20:54:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def update_changelog():
|
|
|
|
print("Updating: docs/changelog.md")
|
|
|
|
root = os.path.realpath(os.path.dirname(os.path.dirname(__file__)))
|
|
|
|
source = os.path.join(root, "CHANGELOG.md")
|
|
|
|
target = os.path.join(root, "docs/changelog.md")
|
|
|
|
shutil.copy(source, target)
|
|
|
|
|
|
|
|
|
2024-03-26 09:04:32 +00:00
|
|
|
def update_docs(command: Command, ctx: click.Context):
|
2022-02-23 20:54:56 +00:00
|
|
|
path = os.path.join("docs", "commands", f"{command.name}.md")
|
2024-03-26 09:04:32 +00:00
|
|
|
content = render_command(command, ctx)
|
2022-02-23 20:54:56 +00:00
|
|
|
|
|
|
|
if not os.path.exists(path):
|
|
|
|
print(f"Creating: {path}")
|
|
|
|
write(path, content)
|
|
|
|
else:
|
|
|
|
print(f"Updating: {path}")
|
|
|
|
[_, handwritten] = read(path).split(END_MARKER)
|
|
|
|
content = f"{content.strip()}\n\n{END_MARKER}\n\n{handwritten.strip()}"
|
|
|
|
write(path, content)
|
|
|
|
|
|
|
|
|
2024-03-26 09:04:32 +00:00
|
|
|
def render_command(command: Command, ctx: click.Context):
|
2022-02-23 20:54:56 +00:00
|
|
|
content = START_MARKER
|
|
|
|
content += f"\n# twitch-dl {command.name}\n\n"
|
|
|
|
|
2024-03-26 09:04:32 +00:00
|
|
|
if command.help:
|
|
|
|
content += command.help + "\n\n"
|
|
|
|
|
|
|
|
content += render_usage(ctx, command)
|
|
|
|
content += render_options(ctx, command)
|
|
|
|
return content
|
2022-02-23 20:54:56 +00:00
|
|
|
|
|
|
|
|
2024-03-26 09:04:32 +00:00
|
|
|
def render_usage(ctx: click.Context, command: Command):
|
2022-02-23 20:54:56 +00:00
|
|
|
content = "### USAGE\n\n"
|
|
|
|
content += "```\n"
|
2024-03-26 09:04:32 +00:00
|
|
|
content += command.get_usage(ctx).replace("Usage: ", "")
|
2022-02-23 20:54:56 +00:00
|
|
|
|
|
|
|
content += "\n```\n\n"
|
|
|
|
return content
|
|
|
|
|
|
|
|
|
2024-03-26 09:04:32 +00:00
|
|
|
def render_options(ctx, command: Command):
|
|
|
|
options = list(get_options(command))
|
2022-02-23 20:54:56 +00:00
|
|
|
|
|
|
|
if not options:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
content = "### OPTIONS\n\n"
|
|
|
|
|
|
|
|
content += "<table>\n"
|
|
|
|
content += "<tbody>"
|
2024-03-26 09:04:32 +00:00
|
|
|
for opts, help in options:
|
2022-02-23 20:54:56 +00:00
|
|
|
content += textwrap.dedent(f"""
|
|
|
|
<tr>
|
2024-03-26 09:04:32 +00:00
|
|
|
<td class="code">{escape(opts)}</td>
|
|
|
|
<td>{escape(help)}</td>
|
2022-02-23 20:54:56 +00:00
|
|
|
</tr>
|
|
|
|
""")
|
|
|
|
content += "</tbody>\n"
|
|
|
|
content += "</table>\n\n"
|
|
|
|
|
|
|
|
return content
|
|
|
|
|
|
|
|
|
2024-03-26 09:04:32 +00:00
|
|
|
def get_options(command: Command):
|
|
|
|
for option in command.params:
|
|
|
|
if isinstance(option, click.Option):
|
|
|
|
opts = ", ".join(option.opts)
|
|
|
|
opts += option_type(option)
|
2022-02-23 20:54:56 +00:00
|
|
|
|
2024-03-26 09:04:32 +00:00
|
|
|
help = option.help or ""
|
|
|
|
help = re.sub(r"\s+", " ", help)
|
|
|
|
help += choices(option)
|
|
|
|
if option.default:
|
|
|
|
help += f" [default: `{option.default}`]"
|
2022-02-23 20:54:56 +00:00
|
|
|
|
2024-03-26 09:04:32 +00:00
|
|
|
yield opts, help
|
2022-02-23 20:54:56 +00:00
|
|
|
|
|
|
|
|
2024-03-26 09:04:32 +00:00
|
|
|
def option_type(option: click.Option):
|
|
|
|
match option.type:
|
|
|
|
case click.types.StringParamType():
|
|
|
|
return " TEXT"
|
|
|
|
case click.types.Choice():
|
|
|
|
return " TEXT"
|
|
|
|
case click.types.IntParamType():
|
|
|
|
return " INTEGER"
|
|
|
|
case _:
|
|
|
|
return ""
|
2022-02-23 20:54:56 +00:00
|
|
|
|
2024-03-26 09:04:32 +00:00
|
|
|
def choices(option: click.Option):
|
|
|
|
if isinstance(option.type, click.Choice):
|
|
|
|
choices = ", ".join(f"`{c}`" for c in option.type.choices)
|
|
|
|
return f" Possible values: {choices}."
|
|
|
|
return ""
|
2022-02-23 20:54:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def read(path):
|
|
|
|
with open(path, "r") as f:
|
|
|
|
return f.read()
|
|
|
|
|
|
|
|
|
|
|
|
def write(path, content):
|
|
|
|
with open(path, "w") as f:
|
|
|
|
return f.write(content)
|
|
|
|
|
|
|
|
|
|
|
|
def escape(text: str):
|
|
|
|
text = html.escape(text)
|
|
|
|
text = re.sub(r"`([\S]+)`", "<code>\\1</code>", text)
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|