-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.py
166 lines (133 loc) · 4.69 KB
/
template.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
# Helper script to generate web pages for the site
from xml.dom import minidom
from xml.parsers.expat import ExpatError
import sys
def usage():
print(sys.argv[0] + " <filename.html>")
sys.exit(2)
def main():
nav = None
ul = None
doc_div = None
containers = list()
if len(sys.argv) != 2:
usage()
filename = sys.argv[-1]
try:
doc = minidom.parse(filename)
except (IOError, ExpatError), e:
print(e)
sys.exit(1)
# Add bootstrap meta tags to <head>
metas = doc.getElementsByTagName("meta")
for m in metas:
head = m.parentNode
head.removeChild(m)
meta1 = doc.createElement("meta")
meta1.setAttribute("charset", "utf-8")
meta2 = doc.createElement("meta")
meta2.setAttribute("content", "IE=edge")
meta2.setAttribute("http-equiv", "X-UA-Compatible")
meta3 = doc.createElement("meta")
meta3.setAttribute("content", "width=device-width, initial-scale=1")
meta3.setAttribute("name", "viewport")
head = doc.getElementsByTagName("head")[0]
head.insertBefore(meta3,head.firstChild)
head.insertBefore(meta2,meta3)
head.insertBefore(meta1,meta2)
# Add bootstrap class to all <h1>
h1_list = doc.getElementsByTagName("h1")
for h1 in h1_list:
h1.setAttribute("class", "page-header")
# Find <nav class="col-md-3"> so we can add TOC nav to it
nav_list = doc.getElementsByTagName("nav")
for n in nav_list:
if n.hasAttributes() and n.getAttribute("class") == "col-md-3":
nav = n
div_list = doc.getElementsByTagName("div")
for div in div_list:
# Find <div class="document">, for removal
if div.hasAttributes() and div.getAttribute("class") == "document":
doc_div = div
# Find <div class="col-md-3" so we can add TOC nav to it
# if div.hasAttributes() and div.getAttribute("class") == "col-md-3":
# nav = div
# Find TOC generated by rst2html
if div.hasAttribute("id") and div.getAttribute("id") == "contents":
contents_div = div
for node in div.childNodes:
if node.nodeType != node.TEXT_NODE:
if node.tagName == "ul":
ul = node
# Remove id from divs added by rst2html
# if div.hasAttribute("class") and div.getAttribute("class") == "section":
# if div.hasAttribute("id"):
# div.removeAttribute("id")
# Insert TOC as a sidebar nav
if ul is not None and nav is not None:
ul.setAttribute("class", "nav nav-underline nav-stacked hidden-xs hidden-sm")
ul.setAttribute("data-spy", "affix")
ul.setAttribute("data-offset-top", "20")
ul.setAttribute("data-offset-bottom", "200")
ul.setAttribute("id", "sidebar")
nav.appendChild(ul)
contents_div.parentNode.removeChild(contents_div)
# Now fix all the sub-nav links
for li in ul.childNodes:
if li.nodeType not in (li.TEXT_NODE, li.COMMENT_NODE) and li.tagName == "li":
for node in li.childNodes:
if node.nodeType not in (node.TEXT_NODE, node.COMMENT_NODE) and node.tagName == "ul":
node.setAttribute("class", "nav nav-underline sub-nav")
for node in doc_div.childNodes:
if node.nodeType not in (node.TEXT_NODE, node.COMMENT_NODE) and (node.tagName == "div" or node.tagName == "nav"):
containers.append(node.cloneNode(deep=True))
# if node.hasAttributes and node.getAttribute("class") == "container":
# containers.append(node.cloneNode(deep=True))
# Make all images responsive
img_list = doc.getElementsByTagName("img")
for img in img_list:
img.setAttribute("class", "img-responsive")
# clone = i.cloneNode(deep=True)
# panel = doc.createElement("div")
# panel.setAttribute("class", "panel")
# panelbody = doc.createElement("div")
# panelbody.setAttribute("class", "panel-body")
# # panelbody.appendChild(clone)
# panel.appendChild(panelbody)
# print panel.getAttribute("class")
# p = i.parentNode
# p.replaceChild(panel,i)
# print p.getAttribute("class")
# # Remove up all section header <a> tags
# a_tags = doc.getElementsByTagName("a")
# for a in a_tags:
# if a.hasAttribute("class") and a.getAttribute("class") == "toc-backref":
# text = a.firstChild
# p = a.parentNode
# # p.removeChild(a)
# # a.unlink()
# # print(a)
# p.replaceChild(text, a)
# # p.appendChild(text)
scripts = doc.getElementsByTagName("script")
footers = doc.getElementsByTagName("footer")
body = doc_div.parentNode
# scrollspy and affix attributes
body.setAttribute("data-spy", "scroll")
body.setAttribute("data-target", "#sidenav")
body.setAttribute("data-offset", "15")
# Remove extra <div class="document">
# containers is found earlier in the code
if containers:
for c in containers:
body.appendChild(c)
for f in footers:
body.appendChild(f)
for s in scripts:
body.appendChild(s)
body.removeChild(doc_div)
# Write file to disk
f = open(filename, "wb")
f.write(doc.toxml().encode('UTF-8'))
if __name__ == "__main__":
main()