forked from revarbat/snappy-reprap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_assembly_index.py
executable file
·195 lines (169 loc) · 5.16 KB
/
gen_assembly_index.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
#!/usr/bin/env python
import re
import sys
class GenAssemblyIndex(object):
indexfile = "docs/assembly/index.html"
markdownfile = "wiki/Assembly.md"
sourcefile = "full_assembly.scad"
modules = []
modinfo = {}
def write_index(self):
with open(self.indexfile, "w") as f:
f.write("""\
<html>
<head>
<title>Snappy Assembly</title>
<style>
BODY {
margin-bottom: 200px;
}
TABLE TD {
vertical-align: middle;
}
H2 {
margin-bottom: 5px;
margin-top: 24px;
font-size: 20pt;
}
LI.section {
font-size: 20pt;
font-weight: bold;
}
H3 {
margin-left: -15px;
margin-bottom: 5px;
margin-top: 18px;
font-size: 16pt;
}
LI.step {
padding-left: 15px;
margin-left: 0;
font-size: 16pt;
font-weight: bold;
list-style-type: none;
}
DIV.desc {
margin-bottom: 15px;
font-size: 12pt;
font-weight: normal;
}
OL {
margin-left: 30px;
}
UL {
padding-left: 5px;
}
</style>
</head>
<body>
<h1>Snappy RepRap Assembly Instructions</h1>
<ol>
""")
for mod_eng in self.modules:
f.write('<li class="section">')
f.write('<h2>%s</h2>\n' % mod_eng)
stepcnt = len(self.modinfo[mod_eng])
if stepcnt > 1:
f.write('<ul>\n')
for stepinfo in self.modinfo[mod_eng]:
if stepcnt > 1:
f.write('<li class="step">')
f.write('<h3>Step {step}</h3>\n'.format(**stepinfo))
f.write(
'<div class="desc">{desc}</div>\n'
'<table>'
'<tr>'
'<td class="befor"><img src="{module}_before.png"></td>'
'<td class="arrow"><img src="arrow.png"></td>'
'<td class="after"><img src="{module}_after.png"></td>'
'</tr>'
'</table>\n'
.format(**stepinfo)
)
if stepcnt > 1:
f.write('</li>\n')
if stepcnt > 1:
f.write('</ul>\n')
f.write('</li>\n')
f.write('</ol>\n')
f.write('</body>\n')
f.write('</html>\n')
def write_markdown(self):
with open(self.markdownfile, "w") as f:
f.write("# Snappy RepRap Assembly Instructions\n\n")
for mod_eng in self.modules:
f.write('##%s\n\n' % mod_eng)
stepcnt = len(self.modinfo[mod_eng])
for stepinfo in self.modinfo[mod_eng]:
stepinfo['base'] = \
'https://raw.githubusercontent.com/' \
'revarbat/snappy-reprap/master/docs/assembly/'
if stepcnt > 1:
f.write('### Step {step}\n\n'.format(**stepinfo))
f.write(
'{desc}\n\n'
'Before | After\n'
'------ | -----\n'
'![{module} Step {step} Before]'
'({base}{module}_before.png) | '
'![{module} Step {step} After]'
'({base}{module}_after.png)\n\n'
.format(**stepinfo)
)
def process_module(self, module, desc):
print("module: %s" % module)
step = 1
mod_eng = module.replace('_', ' ') \
.title() \
.replace('Xy', 'XY') \
.replace('Yz', 'YZ')
mod_split = mod_eng.split(" ")
if mod_split[-1].isdigit():
step = int(mod_split[-1])
mod_eng = " ".join(mod_split[:-1])
if mod_eng not in self.modules:
self.modules.append(mod_eng)
self.modinfo[mod_eng] = [
{
'module': module,
'step': step,
'desc': desc
},
]
else:
self.modinfo[mod_eng].append(
{
'module': module,
'step': step,
'desc': desc
},
)
def generate_index(self):
mod_re = re.compile(
r'module *([a-z_][a-z0-9_]*_assembly(_[0-9]+)?) *\('
)
desc_re = re.compile(r'// *desc: *(.*)$')
module = ""
desc = ""
with open(self.sourcefile, "r") as f:
for line in f.readlines():
mod_res = mod_re.search(line)
if mod_res:
if module:
self.process_module(module, desc)
module = mod_res.group(1)
desc = ""
desc_res = desc_re.search(line)
if desc_res:
desc += desc_res.group(1)
if module:
self.process_module(module, desc)
self.write_index()
self.write_markdown()
def main():
genidx = GenAssemblyIndex()
genidx.generate_index()
sys.exit(0)
if __name__ == "__main__":
main()
# vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap