-
Notifications
You must be signed in to change notification settings - Fork 1
/
mkdocs.py
73 lines (60 loc) · 2.54 KB
/
mkdocs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from jinja2 import Environment, Template, FileSystemLoader
from subprocess import run
from pathlib import Path
import sys
from typing import Dict, List
import re
TOOL_NAME = "overlay_maintain_tools"
def get_typer_cli_docs() -> str:
"""Extracts the stdout from typer docs generator"""
typer_docs = run(
f"{sys.executable} -m typer_cli " # to make sure venv is used
f"{TOOL_NAME}/main.py utils docs --name {TOOL_NAME}".split(" "),
capture_output=True,
text=True,
)
assert typer_docs.returncode == 0
return str(typer_docs.stdout)
def process_chapters(chapters: List[str]) -> Dict[str, Dict[str, str]]:
"""Reprocesses chapters. Contains logic on changing the data inside the chapters"""
result = {}
_chapters = chapters.copy()
for chapter in _chapters:
if chapter.startswith("#"): # h1
chapter_title = "Description"
chapter = chapter.replace(f"`{TOOL_NAME}`", chapter_title, 1)
else:
chapter_title = re.findall("`[a-zA-Z-_ ]*`", chapter)[0]
chapter = "##" + chapter
intro, *_ = chapter.partition("**Usage**") # "Usage" always exists
result.update({chapter_title: {"intro": intro.rstrip()}})
the_rest = "".join(_)
sections = list(enumerate(["usage", "arguments", "options", "commands"]))
for number, section in sections:
index = the_rest.find(f"**{section.capitalize()}**")
if index == -1:
continue
else:
next_index = -1
for _, next_section in sections[number + 1 :]:
next_index = the_rest.find(f"**{next_section.capitalize()}**")
if next_index != -1:
break
if next_index == -1: # next section not found
value = the_rest
else:
value, the_rest = the_rest[:next_index], the_rest[next_index:]
result[chapter_title].update({section: value.rstrip()})
return result
def render_template() -> str:
env = Environment(
loader=FileSystemLoader(str(Path.cwd()) + "/docs/templates"), autoescape=False
)
env.lstrip_blocks = True
env.trim_blocks = True
template = env.get_template("skeleton.jinja2")
typer_cli_help: str = get_typer_cli_docs()
chapters = process_chapters(typer_cli_help.split("##"))
return template.render(typer_help_chapters=chapters, tool_name=TOOL_NAME)
if __name__ == "__main__":
Path("README.md").write_text(render_template())