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
|
#!/usr/bin/python2.4
# Copyright 2009, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""HTML formatter for ezt docs."""
import os
import os.path
import re
import shutil
import sys
class DoxygenJavascriptifier(object):
"""Convert C++ documentation into Javascript style.
Documentation may be automatically generated using doxygen. However,
doxygen only outputs C++ formatted documentation, that is it uses C++
conventions for specifying many items even if the java option is selected in
the configuration file.
In order to make the documentation look like Javascript documentation,
additional corrections must be made which this class takes care of.
Since doxygen allows you to name functions and class whatever you wish, this
class assumes that you have already chosen to name them with standard
Javascript conventions (such as camel-case) and therefore does not try to
correct that.
Though many of the replacements are simply eliminated, they are left as
individual items in the dictionary for clarity.
Attributes:
cpp_to_js: a list of c++ to javascript replacement tuples
static_function_regex: regular expression describing a static function
declaration
"""
global_function_tag = '[<a class="doxygen-global">global function</a>]'
static_function2_regex = re.compile(r'<li>(.*)static(.*)$')
cpp_to_js = [(re.compile(r'::'), '.'),
(re.compile(r'\[pure\]'), ''),
(re.compile(r'\[pure virtual\]'), ''),
(re.compile(r'\[virtual\]'), ''),
(re.compile(r'\[protected\]'), ''),
(re.compile(r'\[static\]'), global_function_tag),
(re.compile(r'\[explicit\]'), ''),
(re.compile(r'\bvirtual\b'), ''),
(re.compile(r'\bpure\b'), ''),
(re.compile(r'\bprotected\b'), ''),
(re.compile(r'\bvoid\b'), ''),
(re.compile(r'\bunsigned\b'), ''),
(re.compile(r'\bstatic\b'), ''),
(re.compile(r'\bStatic\b'), 'Global'),
(re.compile(r'\bint\b'), 'Number'),
(re.compile(r'\bfloat\b'), 'Number'),
(re.compile(r'\bNULL\b'), 'null'),
(re.compile(r'=0'), ''),
# Replace "std.vector<class_name>" with "Array" since vectors
# don't exist in Javascript. There is no type for Array in
# Javascript.
(re.compile(r'std.vector<[\w\s<>"/.=+]*>'), 'Array'),
# Remove mention of Vectormath.Aos namespace since it is
# invisible to Javascript.
(re.compile(r'Vectormath.Aos.Vector3'), 'Vector3'),
(re.compile(r'Vectormath.Aos.Vector4'), 'Vector4'),
(re.compile(r'Vectormath.Aos.Point3'), 'Point3'),
(re.compile(r'Vectormath.Aos.Matrix4'), 'Matrix4'),
(re.compile(r'Vectormath.Aos.Quat'), 'Quat'),
(re.compile(r'\bconst\b'), '')]
def Javascriptify(self, lines_list):
"""Replaces C++-style items with more Javascript-like ones.
This includes removing many identifiers such as pure, virtual, protected,
static and explicit which are not used in Javascript and replacing
notation such as '::' with its corresponding Javascript equivalent,
'.' in this case.
Args:
lines_list: the list of lines to correct.
Returns:
the corrected lines in javascript format.
"""
for index, line in enumerate(lines_list):
line = self.LabelStaticAsGlobal(line)
for cpp_expression, js_expression in self.cpp_to_js:
line = re.sub(cpp_expression, js_expression, line)
lines_list[index] = line
return lines_list
def LabelStaticAsGlobal(self, line):
"""If a function is static, we indicate it is a global function.
Since static does not make sense for Javascript we refer to these
functions as global. To designate global function, we place a
[global function] tag after the function declaration. Doxygen by
default leaves the static descriptor in front of the function so
this function looks for the descriptor, removes it and puts the
tag at the end.
Args:
line: the line to correct.
Returns:
the corrected line.
"""
match = re.search(self.static_function2_regex, line)
if match is not None:
line = '<li>%s%s %s\n' % (match.group(1), match.group(2),
self.global_function_tag)
return line
class EZTFormatter(object):
"""This class converts doxygen output into ezt ready files.
When doxygen outputs files, it contains its own formatting such as adding
navigation tabs to the top of each page. This class contains several
functions for stripping down doxygen output and formatting doxygen output
to be easily parsed by ezt.
This class assumes that any header/footer boilerplate code (that is code
added by the user rather than doxygen at the start or end of a file) is
indicated by special indicator strings surrounding it. Namely at the
start of boilerplate code: 'BOILERPLATE - DO NOT EDIT THIS BLOCK' and
at the end of boilerplate code: 'END OF BOILERPLATE'.
Attributes:
content_start_regex: regular expression for end of boilerplate code
content_end_regex: regular expression for start of boilerplate code
div_regex: regular expression of html div
div_end_regex: regular expression of closing html div
navigation_regex: regular expression indicating doxygen navigation tabs
html_suffix_regex: regular expression html file suffix
current_directory: location of files being worked on
originals_directory: directory to store copies of original html files
modifiers: list of functions to apply to files passed in
"""
content_start_regex = re.compile('END OF BOILERPLATE')
content_end_regex = re.compile('BOILERPLATE - DO NOT EDIT THIS BLOCK')
div_regex = re.compile('<div(.*)>')
div_end_regex = re.compile('</div>')
navigation_regex = re.compile('<div class=\"tabs\"(.*)>')
html_suffix_regex = re.compile('\.html$')
def __init__(self, current_directory):
self.current_directory = current_directory
self.originals_directory = os.path.join(current_directory, 'original_html')
if not os.path.exists(self.originals_directory):
os.mkdir(self.originals_directory)
self.modifiers = []
def RegisterModifier(self, modifier):
"""Adds the modifier function to the list of modifier functions.
Args:
modifier: the function modifies a list of strings of code
"""
self.modifiers += [modifier]
def MakeBackupCopy(self, input_file):
"""Makes backup copy of input_file by copying to originals directory.
Args:
input_file: basename of file to backup.
"""
shutil.copy(os.path.join(self.current_directory, input_file),
self.originals_directory)
def PerformFunctionsOnFile(self, input_file):
"""Opens a file as a list, modifies the list and writes to same file.
Args:
input_file: the file being operated on.
"""
infile = open(input_file, 'r')
lines_list = infile.readlines()
infile.close()
for function in self.modifiers:
lines_list = function(lines_list)
outfile = open(input_file, 'w')
outfile.writelines(lines_list)
outfile.close()
def FixBrackets(self, lines_list):
"""Adjust brackets to be ezt style.
Fix the brackets such that any open bracket '[' becomes '[[]' in order to
not conflict with the brackets ezt uses as special characters.
EZT uses boilerplate code at the beginning and end of each file. When
editing a file, however, we don't want to touch the boilerplate code.
We can look for special indicators through regular expressions of
where the content lies between the boilerplate sections and then only
apply the desired function line by line to the html between those
sections.
This is a destructive operation and will write over the input file.
Args:
lines_list: the file to fix broken into lines.
Returns:
the lines_list with correctly formatted brackets.
"""
outside_boilerplate = False
for index, line in enumerate(lines_list):
if self.content_end_regex.search(line) and outside_boilerplate:
# We've reached the end of content; return our work
return lines_list
elif self.content_start_regex.search(line):
# We've found the start of real content, end of boilerplate
outside_boilerplate = True
elif outside_boilerplate:
# Inside real content; fix brackets
lines_list[index] = line.replace('[', '[[]')
else:
pass # Inside boilerplate.
return lines_list
def RemoveNavigation(self, lines_list):
"""Remove html defining navigation tabs.
This function removes the lines between the <div class ="tabs">...</div>
which will eliminate the navigation tabs Doxygen outputs at the top of
its documentation. These are extraneous for Google's codesite which
produces its own navigation using the ezt templates.
Nested <div>'s are handled, but it currently expects no more than one
<div>...</div> set per line.
This is a destructive operation and will write over the input file.
Args:
lines_list: the lines of the file to fix.
Returns:
the lines_list reformatted to not include tabs div set.
"""
start_index = -1
div_queue = []
start_end_pairs = {}
for index, line in enumerate(lines_list):
# Start by looking for the start of the navigation section
if self.navigation_regex.search(line):
start_index = index
if self.div_regex.search(line):
if start_index > -1:
div_queue.append(index)
if self.div_end_regex.search(line):
if start_index > -1:
# If we pop our start_index, we have found the matching </div>
# to our <div class="tabs">
if div_queue.pop() == start_index:
start_end_pairs[start_index] = index
start_index = -1
# Delete the offending lines
keys = start_end_pairs.keys()
keys.sort()
keys.reverse()
for k in keys:
del lines_list[k:start_end_pairs[k]+1]
return lines_list
def RenameFiles(self, input_file):
"""Fix file suffixes from .html to .ezt.
This will also backup original html files to the originals directory.
This operation will overwrite the previous ezt file.
NOTE: If the originals directory is not a directory, the file will
be overwritten.
Args:
input_file: the file to fix.
"""
# If the file does not end in .html, we do not want to continue
if self.html_suffix_regex.search(input_file):
new_name = re.sub(self.html_suffix_regex, '.ezt', input_file)
shutil.copy(input_file, self.originals_directory)
# Rename file. If file exists, rename will not overwrite, and produce
# OSError. So delete the old file first in that case.
try:
os.rename(input_file, new_name)
except OSError:
os.remove(new_name)
os.rename(input_file, new_name)
def main():
usage_string = ('This script is meant to convert .html files into .ezt files '
'in addition to eliminating any C++-style notations and '
'replacing them with their Javascript-style counter parts.\n'
'Note: This will write over existing files as well as nuke '
'any associated directories such as "original_html" located '
'in the same directory as the first argument\n'
'Usage: ezt_formatter.py <html_file(s)>')
files = sys.argv[1:]
if not files:
# We have no files, so return, showing usage
print usage_string
return
# current_directory should be the same for all files
current_directory = os.path.dirname(files[0])
formatter = EZTFormatter(current_directory)
dj = DoxygenJavascriptifier()
formatter.RegisterModifier(formatter.RemoveNavigation)
formatter.RegisterModifier(dj.Javascriptify)
formatter.RegisterModifier(formatter.FixBrackets)
for f in files:
formatter.PerformFunctionsOnFile(f)
formatter.RenameFiles(f)
formatter.MakeBackupCopy('stylesheet.css')
if __name__ == '__main__':
main()
|