-
Notifications
You must be signed in to change notification settings - Fork 0
/
program.py
201 lines (148 loc) · 6.53 KB
/
program.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
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
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python3
import os
import shutil
import sys
from xml.dom import minidom
from yattag import Doc, indent
def get_xml_attribute(source, attribute_name):
if source.hasAttribute(attribute_name):
return source.attributes[attribute_name].firstChild.data
else:
return None
def get_xml_elements(source, tag_name):
return source.getElementsByTagName(tag_name)
def get_xml_element(source, tag_name):
el = source.getElementsByTagName(tag_name)
return el[0] if el else None
def get_xml_element_value(source, tag_name):
return source.getElementsByTagName(tag_name)[0].firstChild.data
def get_xml_elements_values(source, tag_name):
els = source.getElementsByTagName(tag_name)
texts = []
for el in els:
texts.append(el.firstChild.data)
return texts
if len(sys.argv) == 1 or len(sys.argv) > 3:
print('Wrong number of parameters! For usage help, use \'./program.py -h\'')
sys.exit()
if len(sys.argv) == 2:
if sys.argv[1] == '-h':
print('The proper usage is: \'./program.py <source> <target>\'')
print()
print('For example: \'./program.py ./examples/example_documentation ./site\'')
else:
print('Not enough parameters! For usage help, use \'./program.py -h\'')
sys.exit()
source_dir = sys.argv[1]
if source_dir[-1] == '/':
source_dir = source_dir[:-1]
target_dir = sys.argv[2]
if target_dir[-1] == '/':
target_dir = target_dir[:-1]
xml = minidom.parse(source_dir + '/main.xml')
content = get_xml_element(xml, 'content')
doc, tag, text = Doc().tagtext()
language = get_xml_element_value(content, 'language')
site_name = get_xml_element_value(content, 'site_name')
target_htmldata_dir = target_dir + '/.htmldata'
stylesheet_name = get_xml_element_value(content, 'stylesheet_name')
source_stylesheet_path = 'stylesheets/' + stylesheet_name + '.css'
target_stylesheet_path = target_htmldata_dir + '/style.css'
source_content_dir = source_dir + '/content'
target_content_dir = target_htmldata_dir + '/content'
if not os.path.exists(target_dir):
os.makedirs(target_dir, exist_ok = True)
if not os.path.exists(target_htmldata_dir):
os.makedirs(target_htmldata_dir, exist_ok = True)
shutil.copy(source_stylesheet_path, target_stylesheet_path)
doc.asis('<!DOCTYPE html>')
with tag('html', lang = language):
with tag('head'):
with tag('title'):
text(site_name)
doc.asis('<meta charset="UTF-8">')
doc.asis('<meta name="viewport" content="height=device-width, initial-scale=0.9">')
doc.asis('<link rel="stylesheet" href=".htmldata/style.css">')
with tag('body'):
with tag('header'):
with tag('h1'):
text(site_name)
introduction = get_xml_element(content, 'introduction')
if introduction:
with tag('section', id = 'introduction'):
with tag('h2'):
text('Introduction')
is_auto = get_xml_attribute(introduction, 'auto') == 'true'
if is_auto:
introduction_path = source_content_dir + '/introduction.html'
if os.path.exists(introduction_path):
file = open(introduction_path, 'r', encoding='utf-8')
data = file.read()
if data:
doc.asis(data)
else:
text('No introduction text found!')
else:
text('No introduction text found!')
else:
paras = get_xml_elements_values(introduction, 'p')
if paras:
for p in paras:
with tag('p'):
doc.asis(p)
else:
text('No introduction text found!')
with tag('section', id = 'legend'):
with tag('h2'):
text('Legend')
with tag('ul'):
legend = ['✔️ — Guide complete',
'🛠️ — Guide incomplete',
'⚠️ — Guide contains errors',
'🚫 — Guide not started yet']
for s in legend:
with tag('li'):
text(s)
with tag('main', id = 'guides'):
with tag('h2'):
text('Guides')
guides = get_xml_element(content, 'guides')
if guides:
categories = get_xml_elements(guides, 'category')
if guides and categories:
statuses = {'Complete': '✔️', 'Incomplete': '🛠️', 'With errors': '⚠️', 'Not started': '🚫'}
for c in categories:
name = get_xml_attribute(c, 'name')
category_id = get_xml_attribute(c, 'id')
with tag('div', id = category_id):
with tag('h3'):
text(name)
entries = get_xml_elements(c, 'entry')
if entries:
with tag('table'):
for e in entries:
name = get_xml_attribute(e, 'name')
entry_id = get_xml_attribute(e, 'id')
status = get_xml_attribute(e, 'status')
completeness = get_xml_attribute(e, 'completeness')
with tag('tr'):
with tag('td'):
text(statuses[status])
with tag('td'):
text(completeness)
with tag('td'):
if status == 'Not started' or completeness == '0%':
href = '#';
else:
href = '.htmldata/pages/' + category_id + '/' + entry_id + '.html'
with tag('a', href = href):
text(name)
else:
with tag('p'):
text('There are no guide entries!')
else:
with tag('p'):
text('There are no guide categories!')
result = indent(doc.getvalue())
file = open(target_dir + '/index.html', 'w', encoding='utf-8')
file.write(result)