-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextExtractor.py
executable file
·229 lines (196 loc) · 7.11 KB
/
textExtractor.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
#!/usr/bin/python
from HTMLParser import HTMLParser
# constants
##################################################
FILE_EXTENSIONS = '.jsp,'
OMMIT_DIRECTORIES = ['depricated','target','exchange-admin-portal']
WHITE_LISTED_CHARACTERS=['*','|','?','.',':','(',')','%','+','-','\'','$','[',']',',',':: ::','!','`']
class MyHTMLParser(HTMLParser):
text = []
current_tag = ''
def handle_starttag(self, tag, attrs):
self.current_tag = tag
for attr in attrs:
if (attr[0] == "title" or attr[0] == "alt"):self.text.append(attr[1])
def handle_endtag(self, tag):
self.current_tag = ''
def handle_data(self, data):
if self.current_tag != 'script' and self.current_tag != 'style':
data_strip = data.strip()
if data_strip:
self.text.append(data_strip)
def get_text(self):
return self.text
def sanitizeData(data):
global current_file_path
stripped_text = reduce_data(data)
sanitizedData = removeTagContent("<%--","--%>", stripped_text)
sanitizedData = removeTagContent("<%@","%>", sanitizedData)
sanitizedData = removeTagContent("<!--","-->", sanitizedData)
sanitizedData = removeBreakTag(sanitizedData)
sanitizedData = removeTag("strong", sanitizedData)
sanitizedData = removeMessageTagContent(sanitizedData)
sanitizedData = removeFormatTagContent(sanitizedData)
sanitizedData = replaceAngularTagWithEl(sanitizedData)
sanitizedData = removeCharacterEntityReference(sanitizedData)
sanitizedData = removeTag("c:if", sanitizedData)
return sanitizedData
def reduce_data(data):
output_str = ''
for d in data:
output_str = output_str + re.sub("\n|\t|\r","", d)
return output_str
def removeAngularBracketsInAttrs(data):
result = re.findall("(?<!>)\"(?!(?:\s*/?>)|(?:\s*<))[^\"]+\"", data)
for r in result:
cleanedString = re.sub("<|>","",r)
data = data.replace(r,cleanedString)
return data
def removeBreakTag(data):
brRegex = "(?:<br>)|(?:</br>)|(?:<br/>)"
return re.sub(brRegex, '', data)
def removeTag(tag, data):
regex = "(?:<"+tag+".*?>)|(?:</"+tag+">)"
return re.sub(regex, '', data)
def sanitizeFile(path):
with open(path) as f:
data = sanitizeData(f.readlines())
createTmpFile(data, path)
def createTmpFile(data, path):
tmp_path = path + ".tmp"
f_out = open(tmp_path, "w")
f_out.write(data)
f_out.close()
def removeCharacterEntityReference(data):
return re.sub("(<)|(>)","", data)
def replaceAngularTagWithEl(data):
return data.replace("{{","${").replace("}}","}")
def removeTagContent(tagStartRegex, tagEndRegex ,data):
regex = tagStartRegex + ".*?" + tagEndRegex
return re.sub(regex, '', data)
def removeMessageTagContent(data):
regex = "(?:<s:message.*?/?>)|(?:</s:message>)"
return re.sub(regex, '', data)
def removeFormatTagContent(data):
regex = "(?:<fmt:formatDate.*?/?>)|(?:</fmt:formatDate>)"
return re.sub(regex, '', data)
# process plain text
def process_text(text):
global current_file_path, file_path_static_text_map
text_strip = text.strip()
if text_strip and not white_listed(text_strip):
file_path_static_text_map.setdefault(current_file_path, [])
file_path_static_text_map[current_file_path].append(text)
return text
def white_listed(text):
return text.find("<") != -1 or text.find(">") != -1 or (text in WHITE_LISTED_CHARACTERS)
# process a line
def process_line(l):
str_parts = []
open_tags = 0
open_el = -1
tmp_str=''
for i, char in enumerate(l):
if open_el == -1:
if char == '$':
if i+1 < len(l) and l[i+1] == '{':
if open_tags == 0:
tmp_str = process_text(tmp_str)
str_parts.append(tmp_str)
tmp_str = ''
open_el = 0
tmp_str += char
elif char == '<':
if open_tags == 0:
tmp_str = process_text(tmp_str)
str_parts.append(tmp_str)
tmp_str = ''
tmp_str += char
open_tags += 1
elif char == '>':
tmp_str += char
open_tags -= 1
if open_tags == 0:
str_parts.append(tmp_str)
tmp_str = ''
else:
tmp_str += char
else:
if char == '{':
open_el += 1
tmp_str += char
elif char == '}':
open_el -= 1
tmp_str += char
if (open_el == 0):
str_parts.append(tmp_str)
tmp_str = ''
open_el = -1
else:
tmp_str += char
tmp_str = process_text(tmp_str)
str_parts.append(tmp_str)
return "".join(str_parts)
# process a file
def process_file(path):
global current_file_path
current_file_path = path
sanitizeFile(path)
parser = MyHTMLParser()
parser.feed(open(path+'.tmp').read())
#print parser.text
for l in parser.text:
process_line(l)
del parser.text[:]
os.remove(path+'.tmp')
# process a directory
def process_dir(path):
for dirpath, dirnames, filenames in os.walk(path):
if not ommitted_directory(dirpath):
for fname in filenames:
fext = os.path.splitext(fname)[1]
if fext != '' and fext in FILE_EXTENSIONS.split(','):
fpath = os.path.join(dirpath, fname)
process_file(fpath)
def ommitted_directory(dirpath):
for ommitted_directory in OMMIT_DIRECTORIES:
if ommitted_directory in dirpath:
return True
return False
def process_path(path):
if os.path.exists(path):
if(os.path.isdir(path)):
process_dir(path)
else:
process_file(path)
def print_file_path_static_text_map(outputFile='output.html'):
global file_path_static_text_map
#print file_path_static_text_map
if file_path_static_text_map:
table_str = ""
for file_name, static_text_arr in file_path_static_text_map.iteritems():
table_str += "<tr>"
table_str += "<td rowspan='"+str(len(static_text_arr))+"'>" + file_name + "</td>"
for i, static_text in enumerate(static_text_arr):
static_text_col = "<td>"+static_text+"</td>"
if i == 0:
table_str += static_text_col
else:
table_str += "<tr>"+static_text_col+"</tr>"
table_str += "</tr>"
html_str = "<html><head><style>table, th, td {border: 1px solid black;}</style></head><body><table>"+table_str+"</table></body></html>"
f = open(outputFile,"w")
f.write(html_str)
f.close()
sys.exit(0)
else:
sys.exit(1)
# main
##################################################
import sys, os, re
#properties = {}
current_file_path = ''
file_path_static_text_map = {}
for path in sys.argv[1:-1]:
process_path(path)
print_file_path_static_text_map(sys.argv[-1])