-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgenerate_docs.py
136 lines (109 loc) · 4.24 KB
/
generate_docs.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""
Script to generate documentation for TTP Templates by using <docs>
tag from templates.
This script scans all templates folders, reads all templates, loads
docs tags and creates markdown .md pages for the templates.
"""
import os
from ttp import ttp
import yaml
import time
# load and re-make mkdocs.yml nav section
misc = []
misc_dict = {}
platform = []
yang = []
templates_count = 0
page_template = """Reference path:
```
ttp://{path}
```
---
{doc}
---
<details><summary>Template Content</summary>
```
{template_content}
```
</details>"""
with open("mkdocs.yml", "r") as f:
mkdocs_yaml = yaml.safe_load(f.read())
for item in mkdocs_yaml["nav"]:
if "Templates" in item:
item["Templates"] = [
{"Misc": misc},
{"Platform": platform},
{"YANG": yang},
]
# load templates docs and form mkdocs.yml nav section
for dirpath, dirnames, filenames in os.walk(top="ttp_templates"):
for filename in filenames:
# process readme files
if filename == "readme.md":
# read readme.md content
filepath = os.path.join(dirpath, filename)
with open(filepath) as tf:
doc_string = tf.read()
splitted_path = dirpath.split(os.sep)
docs_filename = ".".join(splitted_path[1:]) + "." + filename
# save md file
with open(os.path.join("docs", "ttp_templates", docs_filename), "w") as f:
f.write(doc_string)
# form nav section of mkdocs.yaml
if splitted_path[1] == "misc":
misc_dict.setdefault(splitted_path[2], [])
misc_dict[splitted_path[2]].append({docs_filename.split(".")[2] + ".readme": "ttp_templates/" + docs_filename})
# process TTP templates files
elif filename.endswith(".txt"):
# load doc strings from template
doc_string = ""
filepath = os.path.join(dirpath, filename)
print(filepath)
parser = ttp(template=filepath)
for template in parser._templates:
doc_string += "\n" + template.__doc__
# list templates without docs
if doc_string.strip() == "":
print("Template has no docs: {}".format(filepath))
# open template file content
with open(filepath) as tf:
template_content = tf.read()
# form template doc page content using template
splitted_path = dirpath.split(os.sep)
doc_string = page_template.format(
path="/".join(splitted_path[1:]) + "/" + filename,
doc=doc_string if doc_string.strip() else "No `<doc>` tags found",
template_content=template_content.replace("`", "'")
)
docs_filename = ".".join(splitted_path[1:]) + "." + filename.replace(".txt", ".md")
# save md file
with open(os.path.join("docs", "ttp_templates", docs_filename), "w") as f:
f.write(doc_string)
# form nav section of mkdocs.yaml
if splitted_path[1] == "misc":
misc_dict.setdefault(splitted_path[2], [])
misc_dict[splitted_path[2]].append({".".join(docs_filename.split(".")[2:-1]): "ttp_templates/" + docs_filename})
elif splitted_path[1] == "platform":
platform.append({".".join(docs_filename.split(".")[1:-1]): "ttp_templates/" + docs_filename})
elif splitted_path[1] == "yang":
yang.append({".".join(docs_filename.split(".")[1:-1]): "ttp_templates/" + docs_filename})
else:
continue
templates_count += 1
# fill in misc nav
for misc_dir_name, pages in misc_dict.items():
misc.append({misc_dir_name: pages})
with open("mkdocs.yml", "w") as f:
f.write(yaml.dump(mkdocs_yaml, default_flow_style=False))
# copy README.md content to index.md
with open("README.md") as readme_file:
with open("docs/index.md", "w") as index_file:
index_file.write("""
---
**Templates count: {}**
---
{}""".format(
templates_count,
readme_file.read()
)
)