forked from RWTH-EBC/HTML-Tidy-Modelica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml_tidy.py
248 lines (229 loc) · 9.77 KB
/
html_tidy.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
import os
import io
import argparse
import shutil
# todo: tutorial oder bash script schreiben, dass tidylib unter windows installiert
# todo: add flag for removing "level of development"
def run_files(rootDir, correct_overwrite, correct_backup, log):
# Make sure that the parameter rootDir points to a Modelica package.
topPackage = os.path.join(rootDir, "package.mo")
errMsg = list()
if not os.path.isfile(topPackage):
raise ValueError("Argument rootDir=%s is not a \
Modelica package. Expected file '%s'."
% (rootDir, topPackage))
file_counter = 0
for root, _, files in os.walk(rootDir, topdown=True):
for moFil in files:
# find the .mo files
if moFil.endswith('.mo'):
file_counter +=1
moFulNam = os.path.join(root, moFil)
results = _CheckFile(moFulNam)
document_corr = results[0]
err = results[1]
if err is not "":
# write error to error message
errMsg.append("[-- %s ]\n%s" % (moFulNam, err))
if correct_backup:
_backup_old_files(rootDir, moFulNam, document_corr, file_counter)
if correct_overwrite:
_correct_overwrite(moFulNam, document_corr)
if log:
_return_logfile(rootDir, errMsg)
print("##########################################################")
print("you can find your logfile under " + rootDir + "\logfile.txt")
def _correct_overwrite(moFulNam, document_corr):
"""
This function overwrites the old modeilca files with
the corrected files
"""
os.remove(moFulNam)
#todo: uncode##print(moFulNam)
newfile = open(moFulNam, "w")
newfile.write(document_corr.encode("utf-8"))
def _backup_old_files(rootDir, moFulNam, document_corr, file_counter):
"""
This function backups the root folder and creates
the corrected files
"""
# todo: richtigen error einfuegen
if os.path.exists(rootDir + "_backup") is False and file_counter == 1:
shutil.copytree(rootDir, rootDir + "_backup")
print("you can find your backup under " + rootDir + "_backup")
#todo unconde ##print(moFulNam)
os.remove(moFulNam)
newfile = open(moFulNam, "w")
newfile.write(document_corr.encode("utf-8"))
def _return_logfile(rootDir, errMsg):
"""
This function creates the logfile
"""
logfile = open("logfile.txt", "w")
if len(errMsg)>=0:
for line in errMsg:
logfile.write(line+'\n')
def _CheckFile(moFile):
"""
This function returns a list that contain the html code of the
info and revision sections. Each element of the list
is a string.
:param moFile: The name of a Modelica source file.
:return: list The list of strings of the info and revisions
section.
"""
# Open file.
print(moFile)
with io.open(moFile, mode="r", encoding="utf-8-sig") as f:
lines = f.readlines()
nLin = len(lines)
isTagClosed = True
code = list()
htmlCode = list()
errors = list()
for i in range(nLin):
if isTagClosed:
# search for opening tag
idxO = lines[i].find("<html>")
if idxO > -1:
# if found opening tag insert everything up to opening tag into the code list
code.append(lines[i][:idxO+6])
# search for closing tag on same line as opening tag
# check for both, correct and incorrect html tags, because dymola except also <\html>
idxC1 = lines[i].find("</html>")
idxC2 = lines[i].find("<\html>")
if idxC1 > -1:
idxC = idxC1
elif idxC2 > -1:
idxC = idxC2
else:
idxC = -1
if idxC > -1:
htmlCode.append(lines[i][idxO:idxC+6])
code.append(_htmlCorrection(htmlCode)[0])
errors.append(_htmlCorrection(htmlCode)[1])
code.append(lines[i][idxC:])
# clear htmlcode list
htmlCode = list()
isTagClosed = True
else:
htmlCode.append(lines[i][idxO+6:])
isTagClosed = False
else:
code.append(lines[i])
isTagClosed = True
else:
# check for both, correct and incorrect html tags, because dymola except also <\html>
idxC1 = lines[i].find("</html>")
idxC2 = lines[i].find("<\html>")
if idxC1 > -1:
idxC = idxC1
elif idxC2 > -1:
idxC = idxC2
else:
idxC = -1
if idxC > -1:
htmlCode.append(lines[i][idxO:idxC + 6])
code.append(_htmlCorrection(htmlCode)[0])
errors.append(_htmlCorrection(htmlCode)[1])
code.append(lines[i][idxC:])
# clear htmlcode list
htmlCode = list()
# check if there is a new opening tag on the same line
idxO = lines[i].find("<html>")
if idxO > -1:
isTagClosed=False
else:
isTagClosed = True
else:
htmlCode.append(lines[i])
isTagClosed = False
document_corr = ""
if len(code) > 0:
for lines in code:
document_corr += lines
errors_string =""
if len(errors) > 0:
for lines in errors:
errors_string +=lines
# correct img attribute and closing tag
document_corr_img =""
CloseFound=True
for line in document_corr.splitlines():
line, CloseFound = correct_img_atr(line,CloseFound)
document_corr_img += line + '\n'
return document_corr_img, errors_string
def correct_img_atr(line,CloseFound):
#check for missing alt attributed
if CloseFound == True:
imgTag = line.encode("utf-8").find("img")
if imgTag> -1:
imgCloseTagIndex = line.find(">", imgTag)
imgAltIndex = line.find("alt", imgTag)
# if close tag exists but no alt attribute, insert alt attribute and change > to />
if imgCloseTagIndex > -1 and imgAltIndex == -1:
line = line[:imgTag] + line[imgTag:].replace(">", ' alt="" />',1)
CloseFound = True
# if close tag exists and alt attribute exists, only change > to />
elif imgCloseTagIndex > -1 and imgAltIndex > -1:
line = line[:imgTag] + line[imgTag:].replace(">", ' />', 1)
CloseFound = True
# if close tag is not in the same line
elif imgCloseTagIndex == -1:
line = line
CloseFound = False
else:
# if no close tag was found in previous line, but opening tag found search for close on this line with same
# procedure
imgCloseTagIndex = line.find(">")
imgAltIndex = line.find("alt")
if imgCloseTagIndex > -1 and imgAltIndex == -1:
line = line[:imgCloseTagIndex] + line[imgCloseTagIndex:].replace(">", ' alt="" />',1)
CloseFound = True
elif imgCloseTagIndex > -1 and imgAltIndex > -1:
line = line[:imgCloseTagIndex] + line[imgCloseTagIndex:].replace(">", ' />', 1)
CloseFound = True
elif imgCloseTagIndex == -1:
CloseFound = False
line = line
return line, CloseFound
def _htmlCorrection(htmlCode):
from tidylib import tidy_document
body = ""
for line in htmlCode:
body += line + '\n'
body = body.replace('\\"', '"')
# Validate the string
htmlCorrect, errors = tidy_document(r"%s" % body, options={'doctype': 'omit', 'show-body-only': 1,
'numeric-entities': 1,
'output-html': 1, 'wrap': 72, 'alt-text': '',})
replacements = {
'"': '\\"',
'<br>': '<br/>',
}
document_corr = htmlCorrect
for old, new in replacements.iteritems():
document_corr = document_corr.replace(old, new)
return document_corr, errors
if __name__ == '__main__':
# todo: maybe set environment variables with something like:
# # Set environment variables
# if platform.system() == "Windows":
# _setEnvironmentVariables("PATH",
# os.path.join(os.path.abspath('.'),
# "Resources", "Library", "win32"))
parser = argparse.ArgumentParser(description='Run HTML correction on files, print found errors or backup old files'
' before correction')
parser.add_argument("--correct-overwrite", action="store_true", default=False, help="correct html code in modelica "
"files and overwrite old files")
parser.add_argument("--correct-backup", action="store_true", default=False, help="correct html code in modelica "
"files and backup old files")
parser.add_argument("--log", action="store_true", default=False, help="print logfile of errors found")
args = parser.parse_args()
if args.correct_overwrite is False and args.correct_backup is False and args.log is False:
print("please use -h or --help for help")
elif args.correct_backup is True:
run_files(os.getcwd(), False, args.correct_backup, args.log)
else:
run_files(os.getcwd(), args.correct_overwrite, args.correct_backup, args.log)
#run_files(os.getcwd(), True, False, True)