-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlChecker.py
More file actions
175 lines (107 loc) · 3.92 KB
/
Copy pathhtmlChecker.py
File metadata and controls
175 lines (107 loc) · 3.92 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
# File: htmlChecker.py
# Description: Checks the tags of an html page
# Student's Name: William Sears
# Student's UT EID: ---
# Course Name: CS 313E
# Unique Number: ---
#
# Date Created: 10/01/2016
# Date Last Modified: 10/07/2016
class Stack (object):
def __init__(self):
self.items = [ ]
def isEmpty (self):
return self.items == [ ]
def push (self, item):
self.items.append (item)
def pop (self):
return self.items.pop ()
def peek (self):
return self.items [len(self.items)-1]
def size (self):
return len(self.items)
def __str__(self):
return str(self.items)
##################################################
def getTag(string): #Checks a string for tags
tagList = []
tagFound = False
tempString = ""
VALIDTAGS = []
exceptions = ["br","hr","meta"]
for character in string:
if tagFound == True and (character == ">" or character.strip() == ""):
tagFound = False
if tempString in exceptions: # If it's an exception, ignore it
print("Tag", '"' + tempString + '"', "does not need to match, Exceptions:", exceptions)
tempString = ""
elif tempString in VALIDTAGS: #if it's already in VALIDTAGS, add it to the list of tags, but not to valid tags
tagList.append(tempString)
tempString = ""
elif tempString[0] != "/": #If it's not in VALIDTAGS, add it
print("New tag",'"' + tempString + '"', "found and added to list of valid tags")
VALIDTAGS.append(tempString)
tagList.append(tempString)
tempString = ""
else:
tagList.append(tempString)
tempString = ""
if tagFound:
tempString += character
if character == "<":
tagFound = True
print("")
#print(VALIDTAGS)
return (tagList, VALIDTAGS, exceptions)
def checkTag(alist):
print("")
theStack = Stack()
index = 0
balanced = True
opened = False
for tag in alist: #checks for matches in the stack, pops and pushes when needed, produces an error for unmatching tags
if "/" in tag:
if tag[1:] == theStack.peek():
theStack.pop()
print("Tag:", tag, ", matches top of stack. The stack is now:", theStack)
else:
print("Error: tag is", tag, "but top of stack is", theStack.peek())
exit()
else:
theStack.push(tag)
print("Tag", tag, "pushed: stack is now", theStack)
#while index < len(alist) and balanced:
# if tag[0] == "/":
# if theStack.items[-1] in tag:
# theStack.pop()
# index += 1
return theStack
##################################################
def main():
txtFile = open("htmlfile.txt", "r")
fileString = ""
#theStack = Stack()
for line in txtFile:
fileString += line
tagsAndValEx = getTag(fileString)
tags = tagsAndValEx[0]
valid = tagsAndValEx[1]
exceptions = tagsAndValEx[2]
valid.sort()
print("")
print("List of tags:", tags)
theStack = checkTag(tags)
print("")
if theStack.items == []:
print("Processing complete. No mismatches found.")
else:
print("Processing complete. Unmatched tags remain on stack:", theStack.items)
print(theStack)
print("")
print ("Valid Tags:", valid)
print ("Exceptions:", exceptions)
#print(theStack)
#print(getTag(fileString))
#print(fileString)
txtFile.close()
main()