-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_subcommand_rsts
executable file
·190 lines (138 loc) · 4.51 KB
/
generate_subcommand_rsts
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/env python3
import os
import ctc.cli
#
# # paths
#
# html organization as output by
html_dir = './subcommand_html'
root_html = os.path.join(html_dir, 'root__help.html')
category_html = os.path.join(html_dir, 'categories/{category}__help.html')
subcommand_html = os.path.join(
html_dir,
'subcommands/{category}/{subcommand}__help.html',
)
# follow strict hierarchy because it becomes the url structure
root_rst = './source/cli/subcommands.rst'
category_rst = './source/cli/subcommands/{category}.rst'
subcommand_rst = './source/cli/subcommands/{category}/{subcommand}.rst'
#
# # rst templates
#
root_template = """
Subcommands
===========
.. note::
Click on a subcommand to view its documentation page.
.. raw:: html
:file: {cli_html_root}
.. toctree::
:maxdepth: 2
:hidden:
{toc_entries}
"""
category_template = """
{category}
{category_underline}
.. note::
Click on a subcommand to view its documentation page.
.. raw:: html
:file: {category_html_path}
.. toctree::
:maxdepth: 2
:hidden:
{toc_entries}
"""
subcommand_template = """
{header}
{header_underline}
.. raw:: html
:file: {subcommand_html_path}
"""
#
# # rst creation
#
def get_subcommands_by_category():
subcommands_by_category = {}
root_dir = './source/cli/subcommand_html/subcommands'
for category in os.listdir(root_dir):
subcommands_by_category[category] = []
for subcommand_filename in os.listdir(os.path.join(root_dir, category)):
subcommand_str = subcommand_filename.split('__help.html')[0]
subcommand = tuple(subcommand_str.split('_'))
subcommands_by_category[category].append(subcommand)
subcommands_by_category[category] = sorted(subcommands_by_category[category])
return subcommands_by_category
def create_rst_root(subcommands_by_category):
toc_entries = ''
for category in get_category_order():
toc_entries += ' ./subcommands/' + category + '\n'
content = root_template.format(
cli_html_root=root_html,
toc_entries=toc_entries,
)
with open(root_rst, 'w') as f:
f.write(content)
def get_category_order():
category_order = list(ctc.cli.command_index_by_category.keys())
category_order.append('other')
return category_order
def create_rst_categories(subcommands_by_category):
category_order = get_category_order()
for category in category_order:
subcommands = subcommands_by_category[category]
# collect category data
header = category.title() + ' Subcommands'
category_underline = '=' * len(header)
category_html_path = (
'../subcommand_html/categories/' + category + '__help.html'
)
toc_entries = ''
for subcommand in subcommands:
toc_entries += (
' ./' + category + '/' + '_'.join(subcommand) + '\n'
)
# merge into content
content = category_template.format(
category=header,
category_underline=category_underline,
category_html_path=category_html_path,
toc_entries=toc_entries,
)
# write content
path = category_rst.format(category=category)
with open(path, 'w') as f:
f.write(content)
def create_rst_subcommands(subcommands_by_category):
for category, subcommands in subcommands_by_category.items():
for subcommand in subcommands:
# collect subcommand data
header = ' '.join(subcommand)
header_underline = '=' * len(header)
subcommand_html_path = (
'../../subcommand_html/subcommands/'
+ category
+ '/'
+ '_'.join(subcommand)
+ '__help.html'
)
content = subcommand_template.format(
header=header,
header_underline=header_underline,
subcommand_html_path=subcommand_html_path,
)
path = subcommand_rst.format(
category=category,
subcommand='_'.join(subcommand),
)
parent = os.path.dirname(path)
os.makedirs(parent, exist_ok=True)
with open(path, 'w') as f:
f.write(content)
def create_rsts():
subcommands_by_category = get_subcommands_by_category()
create_rst_subcommands(subcommands_by_category)
create_rst_categories(subcommands_by_category)
create_rst_root(subcommands_by_category)
if __name__ == '__main__':
create_rsts()