-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparsing_engine.py
357 lines (326 loc) · 13.4 KB
/
parsing_engine.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
from __future__ import unicode_literals, division, absolute_import, print_function
from collections import OrderedDict
import regex as re
SVG_ATTR = ['attributeName', 'attributeType', 'baseFrequency', 'baseProfile', 'calcMode', 'clipPathUnits', 'contentScriptType', 'contentStyleType',
'diffuseConstant', 'edgeMode', 'externalResourcesRequired', 'filterRes', 'filterUnits', 'glyphRef', 'gradientTransform', 'gradientUnits',
'kernelMatrix', 'kernelUnitLength', 'keyPoints', 'keySplines', 'keyTimes', 'lengthAdjust', 'limitingConeAngle', 'markerHeight', 'markerUnits',
'markerWidth', 'maskContentUnits', 'maskUnits', 'numOctaves', 'pathLength', 'patternContentUnits', 'patternTransform', 'patternUnits',
'pointsAtX', 'pointsAtY', 'pointsAtZ', 'preserveAlpha', 'preserveAspectRatio', 'primitiveUnits', 'refX', 'refY', 'repeatCount', 'repeatDur',
'requiredExtensions', 'requiredFeatures', 'specularConstant', 'specularExponent', 'spreadMethod', 'spreadMethod', 'startOffset', 'stdDeviation',
'stitchTiles', 'surfaceScale', 'systemLanguage', 'tableValues', 'targetX','targetY', 'textLength', 'viewBox', 'viewTarget', 'xChannelSelector',
'yChannelSelector', 'zoomAndPan']
def attrMatch(attr_str, method, srch_str):
if method == 'normal':
return (attr_str == srch_str)
elif method == 'regex':
if re.match(r"""%s""" % srch_str, attr_str, re.U) is not None:
return True
else:
return False
class MarkupParser(object):
''' The criteria parameter dictionary specs
criteria['html'] Param 1 - the contents of the (x)html file: unicode text.
criteria['action'] Param 2 - action to take: unicode text ('modify' or 'delete')
criteria['tag'] Param 3 - tag to alter/delete: unicode text
criteria['attrib'] Param 4 - attribute to use in match: unicode text or None
criteria['srch_str'] Param 5 - value of the attribute to use in match: unicode text (literal or regexp) or None
criteria['srch_method'] Param 6 - is the value given literal or a regexp: boolean
criteria['new_tag'] Param 7 - tag to change to: unicode text, or None
criteria['new_str'] Param 8 - new attributes to be written: unicode text
criteria['copy'] Param 9 - copy the existing attributes verbatim?: boolean
'''
def __init__(self, criteria):
self.wipml = criteria['html']
self.action = criteria['action']
self.tag = criteria['tag']
self.attrib = criteria['attrib']
self.srch_str = criteria['srch_str']
self.srch_method = criteria['srch_method']
self.new_tag = criteria['new_tag']
self.new_str = criteria['new_str']
self.copy_attr = criteria['copy']
self.pos = 0
self.path = []
self.occurrences = 0
def parse_new_tattr(self, s, p=0):
tattr = {}
while s.find('=',p) != -1:
while s[p:p+1] == ' ':
p += 1
b = p
while s[p:p+1] != '=':
p += 1
aname = s[b:p]
if aname not in SVG_ATTR:
aname = aname.lower()
aname = aname.rstrip(' ')
p += 1
while s[p:p+1] == ' ':
p += 1
if s[p:p+1] == '"':
p = p + 1
b = p
while s[p:p+1] != '"':
p += 1
val = s[b:p]
p += 1
else:
b = p
while s[p:p+1] not in ('>', '/', ' '):
p += 1
val = s[b:p]
tattr[aname] = val
return tattr
# parse leading text of xhtml and tag
def parseml(self):
p = self.pos
if p >= len(self.wipml):
return None
if self.wipml[p] != '<':
res = self.wipml.find('<',p)
if res == -1:
res = len(self.wipml)
self.pos = res
return self.wipml[p:res], None
# handle comment as a special case
# deal with even multipline comments properly
# don't parse code in in between
if self.wipml[p:p+4] == '<!--':
te = self.wipml.find('-->', p+1)
if te != -1:
te = te+2
else :
# tb = p
te = self.wipml.find('>',p+1)
ntb = self.wipml.find('<',p+1)
if ntb != -1 and ntb < te:
self.pos = ntb
return self.wipml[p:ntb], None
self.pos = te + 1
return None, self.wipml[p:te+1]
# parses string version of tag to identify its name,
# its type 'begin', 'end' or ('single'|'single_ext'),
# plus build a hashtable of its atributes
# code is written to handle the possiblity of very poor formating
def parsetag(self, s):
p = 1
# get the tag name
tname = None
ttype = None
tattr = None
while s[p:p+1] == ' ':
p += 1
if s[p:p+1] == '/':
ttype = 'end'
p += 1
while s[p:p+1] == ' ':
p += 1
b = p
# handle comment special case as there may be no spaces to
# delimit name begin or end
if s[b:].startswith('!--'):
p = b+3
tname = '!--'
ttype = 'passthru'
info = s[p:-3].strip()
tattr = {}
tattr['info'] = info
return ttype, tname, tattr
while s[p:p+1] not in ('>', '/', ' ', '"', "'", "\r", "\n"):
p += 1
tname=s[b:p].lower()
# some special cases
if tname == "!--":
ttype = 'passthru'
info = s[p:-3]
tattr = {}
tattr['info'] = info
if tname == "!doctype":
tname = "!DOCTYPE"
ttype = 'passthru'
info = s[p:-1]
tattr = {}
tattr['info'] = info
if tname == "![cdata[*":
tname = "![CDATA[*"
ttype = 'passthru'
info = s[p:-1]
tattr = {}
tattr['info'] = info
if tname.startswith("?"):
ttype = 'passthru'
info = s[p:-2]
tattr = {}
tattr['info'] = info
if ttype is None:
# parse any attributes
# tattr = {}
# tattr = self.parse_attr(s,p)
tattr = OrderedDict()
while s.find('=',p) != -1:
while s[p:p+1] == ' ':
p += 1
b = p
while s[p:p+1] != '=':
p += 1
aname = s[b:p]
if aname not in SVG_ATTR:
aname = aname.lower()
aname = aname.rstrip(' ')
p += 1
while s[p:p+1] == ' ':
p += 1
if s[p:p+1] == '"':
p = p + 1
b = p
while s[p:p+1] != '"':
p += 1
val = s[b:p]
p += 1
else:
b = p
while s[p:p+1] not in ('>', '/', ' '):
p += 1
val = s[b:p]
tattr[aname] = val
if tattr and len(tattr)== 0:
tattr = None
# label beginning and single tags
if not ttype:
ttype = 'begin'
if s.find(' /',p) >= 0:
ttype = 'single_ext'
elif s.find('/',p) >= 0:
ttype = 'single'
return ttype, tname, tattr
# main routine to process the xhtml markup language
def processml(self):
htmlstr = ''
skip = False
# now parse the cleaned up ml into standard xhtml
while True:
r = self.parseml()
if not r:
break
text, tag = r
if text:
if not skip:
htmlstr += text
if tag:
ttype, tname, tattr = self.parsetag(tag)
# mark any tags to remove/modify
if self.attrib is not None: # Tags with attributes
if tname == self.tag and ttype in ('begin', 'single', 'single_ext') and \
self.attrib in tattr.keys() and attrMatch(tattr[self.attrib], self.srch_method, self.srch_str):
if self.action == 'delete':
tname = 'removeme:{0}'.format(tname)
tattr = None
elif self.action == 'modify':
if self.new_tag is None:
tname = 'changeme:{0}'.format(tname)
else:
tname = 'changeme:{0}'.format(self.new_tag)
if not self.copy_attr:
if not len(self.new_str):
tattr = None
else:
tattr = self.parse_new_tattr(self.new_str)
else: # Tags without any attributes
if tname == self.tag and ttype in ('begin', 'single', 'single_ext') and not len(tattr):
if self.action == 'delete':
tname = 'removeme:{0}'.format(tname)
tattr = None
elif self.action == 'modify':
if self.new_tag is None:
tname = 'changeme:{0}'.format(tname)
else:
tname = 'changeme:{0}'.format(self.new_tag)
if not len(self.new_str):
tattr = None
else:
tattr = self.parse_new_tattr(self.new_str)
if tname == self.tag and ttype == 'end':
if self.action == 'delete':
if self.path[-1] == 'removeme:{0}'.format(tname):
tname = 'removeme:{0}'.format(tname)
tattr = None
elif self.action == 'modify':
if self.new_tag is None:
if self.path[-1] == 'changeme:{0}'.format(tname):
tname = 'changeme:{0}'.format(tname)
else:
if self.path[-1] == 'changeme:{0}'.format(self.new_tag):
tname = 'changeme:{0}'.format(self.new_tag)
tattr = None
# keep track of nesting path
# special case xml doctype
# if ttype == 'begin' and tname != '?xml' and tname != '!DOCTYPE':
# should not need the previous if ttype cannot possibly equal both 'begin' and 'passthrough'.
if ttype == 'begin':
self.path.append(tname)
elif ttype == 'end':
if tname != self.path[-1]:
print('improper nesting: ', self.path, tname, type)
self.path.pop()
if tname == 'removeme:{0}'.format(tname):
if ttype in ('begin', 'single', 'single_ext'):
skip = True
else:
skip = False
else:
taginfo = (ttype, tname, tattr)
htmlstr += self.processtag(taginfo)
return htmlstr, self.occurrences/2
# flatten possibly modified tag back to string
def taginfo_tostring(self, taginfo):
(ttype, tname, tattr) = taginfo
res = '<'
if ttype == 'end':
res += '/' + tname + '>'
return res
res += tname
if tattr:
for key in tattr.keys():
res += ' '
res += key + '="'
res += tattr[key] + '"'
res == ' '
if ttype == 'single':
res += '/>'
elif ttype == 'single_ext':
res += ' />'
else :
res += '>'
return res
# routines to allow preprocessing and conversion of tags
def processtag(self, taginfo):
# current tag to work on
(ttype, tname, tattr) = taginfo
# put any code here to manipulate or process tags
if tname is None or tname.startswith('removeme'):
self.occurrences +=1
return ''
if tname.startswith('changeme'):
self.occurrences +=1
tname = tname[9:]
if tattr is None:
tattr = {}
# handle passthru special cases
if ttype == 'passthru':
if tname == '!--':
return '<!--{0}-->'.format(tattr.get('info',' '))
if tname == '!DOCTYPE':
return '<!DOCTYPE{}>'.format(tattr.get('info',''))
if tname == '![CDATA[*':
return '<![CDATA[*{}>'.format(tattr.get('info',''))
if tname.startswith('?'):
return "<{0}{1}?>".format(tname, tattr.get('info',''))
# add your own processing of tags here
# convert updated tag back to string representation
if len(tattr) == 0:
tattr = None
taginfo = (ttype, tname, tattr)
return self.taginfo_tostring(taginfo)