forked from commoncriteria/transforms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost-process.py
237 lines (200 loc) · 7.02 KB
/
post-process.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env python3
"""
Module that fixes internal references and counters (which are hard to do
with XSLT).
"""
from io import StringIO
import re
import sys
import xml.etree.ElementTree as ET
from xml.sax.saxutils import escape
def warn(msg):
log(2, msg)
def err(msg):
sys.stderr.write(msg)
sys.exit(1)
def debug(msg):
log(5, msg)
def log(level, msg):
sys.stderr.write(msg)
sys.stderr.write("\n")
def get_appendix_prefix(num):
if num > 26:
err("Cannot handle more than 26 appendices")
ABC=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
return ABC[num]
class State:
def __init__(self, root):
self.root = root
self.parent_map = {c:p for p in self.root.iter() for c in p}
self.create_classmapping()
def create_classmapping(self):
self.classmap={}
for el in self.root.findall(".//*[@class]"):
classes = el.attrib["class"].split(",")
# Go through all the classes the elment is a part of
for clazz in classes:
# If we already have this class in the classmap
if clazz in self.classmap:
# Grab the old
clazzset = self.classmap[clazz]
# We're working with a list here, not a set
# Should really only not meet this if the
# input document has an element where a class is listed twice
if not el in clazzset:
clazzset.append(el)
else:
self.classmap[clazz]=[el]
def to_html(self):
return """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
""" + self.to_html_helper(self.root)
def to_html_helper(self, elem):
tagr = elem.tag.split('}')
noname=tagr[len(tagr)-1]
if noname=="br":
return "<br/>"
ret="<" + noname
for attrname in elem.attrib:
ret = ret + " " + attrname + "='"+ escape(elem.attrib[attrname])+"'"
ret=ret+">"
if elem.text:
ret += escape(elem.text)
for child in elem:
ret += self.to_html_helper(child)
if child.tail:
ret += child.tail
ret= ret + '</' + noname +'>'
return ret
def fix_counters(self):
# Bail if there are no ctrs
if not "ctr" in self.classmap:
return
countables = self.classmap['ctr']
occurs={}
# Go through all the counters
for countable in countables:
# Get the type of counter
typee = countable.attrib['data-counter-type']
# If we haven't seen it yet
if not typee in occurs:
# Make a list
occurs[typee]=0
# Increment by one
occurs[typee]+=1
countable.find("*[@class='counter']").text = str(occurs[typee])
refclass=countable.attrib["data-myid"]+"-ref"
if refclass in self.classmap:
refs=self.classmap[refclass]
for ref in refs:
ref.find("*[@class='counter']").text= str(occurs[typee])
def fix_tooltips(self):
if not "tooltiptext" in self.classmap:
return
for elem in self.classmap["tooltiptext"]:
attribs=self.parent_map[elem].attrib
if "class" in attribs:
attribs["class"]=attribs["class"]+",tooltipped"
else:
attribs["class"]="tooltipped"
def fix_index_refs(self):
# Bail if there are no dynamic references
if not "dynref" in self.classmap:
return
# Gather them
brokeRefs = self.classmap["dynref"]
for brokeRef in brokeRefs:
linkend=brokeRef.attrib["href"][1:]
target=root.find(".//*[@id='"+linkend+"']")
try:
brokeRef.text = target.text
except AttributeError:
warn("Failed to find an element with the id of '"+linkend+"'")
def fix_indices(self):
# Find the table of contents
toc=self.root.find(".//*[@id='toc']")
# Initialize the index number generator
inums=[0,0,0,0,0,0]
# Initialize the is_alpha switch
is_alpha=False
# Gather all elements with a data-level
eles=self.root.findall(".//*[@data-level]")
#
base=-1
# Go through the elemeents
for aa in range( len(eles)):
level=eles[aa].attrib["data-level"]
# If this is the first time we see an appendix
if level == 'A' and not is_alpha:
inums=[-1,0,0,0,0]
is_alpha=True
# Turn it into an index
if level == 'A':
level = 0
else:
level=int(level)
if base==-1:
base=level
level=level-base
# If we have to pad out
while level > len(inums):
inums.append(0)
# If we go up one set
if level+1 < len(inums):
inums[level+1]=0
inums[level]+=1
if is_alpha and level == 0:
prefix= "Appendix " + get_appendix_prefix(inums[0]) + " - "
elif is_alpha:
prefix = get_appendix_prefix(inums[0])
else:
prefix = str(inums[0])
spacer=""
for bb in range(1, level+1):
prefix = prefix + "." + str(inums[bb])
spacer=spacer+"&nbps;"
# Fix inline index number
spany = ET.Element("span")
spany.text = eles[aa].text
if eles[aa].text:
eles[aa].text = prefix + " " + eles[aa].text
else:
eles[aa].text = prefix
entry = ET.Element("a")
entry.attrib['href'] = '#'+escape(eles[aa].attrib['id'])
entry.attrib['style']= 'text-indent:'+str(level*10)+ 'px'
entry.text=prefix
entry.append(spany)
toc.append(entry)
def handle_element(self, elem):
pass
def getalltext(elem):
ret=""
if elem.text:
ret=elem.text
for child in elem:
ret=ret+getalltext(child)+child.tail
if __name__ == "__main__":
if len(sys.argv) < 2:
# 0 1
print("Usage: <protection-profile>[::<output-file>]")
sys.exit(0)
# Split on double colon
out=sys.argv[1].split("::")
infile=out[0]
outfile=""
if len(out) < 2:
outfile=infile.split('.')[0]+".html"
else:
outfile=out[1]
if infile=="-":
root=ET.fromstring(sys.stdin.read())
else:
root=ET.parse(infile).getroot()
state = State(root)
state.fix_indices()
state.fix_index_refs()
state.fix_counters()
state.fix_tooltips()
with open(outfile, "w+") as outstream:
outstream.write(state.to_html())