-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml.py
More file actions
216 lines (170 loc) · 6.08 KB
/
html.py
File metadata and controls
216 lines (170 loc) · 6.08 KB
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
#----------------------------------------------------#
# *Python browser
# html.py
#----------------
# Combines functionality of
# - html_tools.py
# - html_tokenizer.py
# - html_postprocessor.py
#----------------------------------------------------#
import html_tools
import html_tokenizer
#---------------------------------------------------------------#
# *HTMLTag
# This class handles html tags
class HTMLTag:
def __init__(self):
self.type = ""
self.content = []
self.modifiers = {}
self.id = 0
self.parent = 0
def setType(self, type):
self.type = type
def addMod(self, name, val=True):
self.modifiers[name] = val
def push(self, val):
self.content.append(val)
#def setRenderMod(self, name, val):
#self.render[ name ] = val
#---------------------------------------------------------------#
# *HTML
# This class handles html files
class HTML:
#---------------------------------------#
# *HTML.__init__()
# Object constructor
def __init__(self):
self.tags = []
self.head = 0
self.body = 0
self.doctype = 0
self.html = 0
self.classes = {}
self.ids = {}
self.types = {}
# ---------------------------------------#
# *HTML.debug( tag to push )
# Prints debug information
def debug(self):
print( "HTML object")
print("Tags: ")
for i in range( len( self.tags ) ):
val = self.tags[ i ]
print( i, ":", val.type, val.content, val.modifiers )
print( "<head> id: ",self.head )
print( "<body> id: ",self.body )
print( "<html> id: ",self.html )
print( "<!DOCTYPE> id: ",self.doctype )
#---------------------------------------#
# *HTML.pushTag( tag to push )
# Pushes tag to tag stack and returns it's id
def pushTag( self, tag ):
tag.id = len( self.tags )
self.tags.append( tag )
if "id" in tag.modifiers:
self.ids[ tag.modifiers[ "id" ] ] = tag.id
if "class" in tag.modifiers:
myClass = tag.modifiers[ "class" ]
if myClass not in self.classes:
self.classes[ myClass ] = []
self.classes[ myClass ].append( tag.id )
if tag.type not in self.types:
self.types[ tag.type ] = []
self.types[ tag.type ].append( tag.id )
if tag.type == "head": self.head = tag.id
if tag.type == "body": self.body = tag.id
if tag.type == "html": self.html = tag.id
if tag.type == "!DOCTYPE": self.doctype = tag.id
return tag.id
# ---------------------------------------------#
# *HTML.spiritToTag( spirit )
# Converts spirit object with tokenizer information to html tag
def spiritToTag(self, spirit):
tag = HTMLTag()
tag.modifiers = spirit.modifiers
tag.type = spirit.type
return tag
# ---------------------------------------------#
# *HTML.buildTag( tokens, start pos, parent id )
# Builds tag from previously created token list
def buildTag(self, tokens, start, parent):
spirit = tokens[ start ]
tag = self.spiritToTag( spirit )
tag.parent = parent
self.pushTag( tag )
if spirit.isClosing: return tag.id, start
pos = start + 1
while pos < len(tokens):
tkn = tokens[pos]
if isinstance(tkn, str):
tag.push( html_tools.replaceHTMLEnts( tkn ) )
elif tkn.isClosing and tkn.type == tag.type:
return tag.id, pos
else:
add, pos = self.buildTag(tokens, pos, tag.id )
tag.push(add)
pos += 1
raise ValueError("Tag " + tag.type + " has start but has no end")
#---------------------------------------------#
# *HTML.build( tokens )
# Builds tag from previously created token list
def build( self, tokens ):
pos = 0
while pos < len(tokens):
if isinstance(tokens[pos], html_tokenizer.HTMLSpirit):
add, pos = self.buildTag(tokens, pos, -1)
pos += 1
def checkParent(self, filter, tag, pos):
node = filter[pos]
if "class" in node:
if "class" not in tag.modifiers or tag.modifiers["class"] != node["class"]:
return False
if "type" in node:
if tag.type != node["type"]:
return False
if "id" in node:
if "id" not in tag.modifiers or tag.modifiers["id"] != node["id"]:
return False
if len( filter ) - 1 > pos:
return self.checkParent( filter, self.tags[ tag.parent ], pos+1 )
else:
return True
#----------------------------------------------#
#
def getSelectorList(self, selector):
datas = selector.split(",")
out = []
for element in datas:
element = element.replace( ">", " " )
element = element.replace( "+", " " )
combinators = element.split(" ")
filter = []
for comb in combinators:
node = {}
if comb[0] == "#":
node[ "class" ] = comb[1:]
elif comb[0] == ".":
node[ "id" ] = comb[ 1: ]
else:
node[ "type" ] = comb
filter.append( node )
for a in filter:
for id in range( len(self.tags) ):
tag = self.tags[ id ]
if self.checkParent( filter, tag, 0 ):
out.append( id )
return out
def applyCSS( self, css ):
for selector, data in css.items():
print( selector, self.getSelectorList( selector ) )
#for property, value in data.items():
#---------------------------------------------#
# *processHTML( html source )
# Returns HTML object representing provided html source
def processHTML( html ):
html = html_tools.removeSpaces( html )
html = html_tokenizer.tokenizeHTML( html )
out = HTML()
out.build( html )
return out