-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_html_api.py
More file actions
executable file
·304 lines (291 loc) · 12.2 KB
/
gen_html_api.py
File metadata and controls
executable file
·304 lines (291 loc) · 12.2 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
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
#!/usr/bin/env python
""" build different versions of GA API documentation (C,Fortran,C++,Python)"""
""" in HTML. This script works with Python 2.7.8 but fails with later """
""" versions (3.7.2). The script can be invoked with """
""" gen_html_api.py <directory> <one of c,f,cxx,py> [optional output name] """
""" This script currently seems to parse all files in the directory with """
""" the .tex extension so you should run this script in a directory that """
""" only contains the file you want to work on """
import os
import re
import sys
have_pygments = True
try:
from pygments import highlight
from pygments.lexers import CLexer
from pygments.lexers import FortranLexer
from pygments.lexers import PythonLexer
from pygments.lexers import CppLexer
from pygments.formatters import HtmlFormatter
lang_to_lexer = {
"c": CLexer(stripall=True),
"f": FortranLexer(stripall=True),
"py": PythonLexer(stripall=True),
"cxx": CppLexer(stripall=True),
}
formatter = HtmlFormatter()
extra_header = '<style type="text/css">\n'
extra_header += formatter.get_style_defs('.highlight')
extra_header += '</style>\n'
except ImportError:
have_pygments = False
extra_header = ""
raise
extra_header += '''<style type="text/css">
table {
border-collapse:collapse;
}
table,th,td {
/*border:1px solid gray;*/
padding-left:10px;
padding-right:10px;
font-size:small;
}
</style>
'''
apih = re.compile(r'^\\apih{(.*)}{(.*)}$')
inarg = re.compile(r'^\\inarg{(.*)}{(.*)}{(.*)}$')
outarg = re.compile(r'^\\outarg{(.*)}{(.*)}{(.*)}$')
inoutarg = re.compile(r'^\\inoutarg{(.*)}{(.*)}{(.*)}$')
includegraphics = re.compile(r'\\includegraphics(?:\[[^\]]*\])?{([^}]*)}')
seealso = re.compile(r'^\\seealso{(.*)}$')
style_out="color: rgb(102, 0, 0);"
style_in ="color: rgb(0, 102, 0);"
lang_to_pretty = {
"c": "C",
"f": "Fortran",
"py": "Python",
"cxx": "C++",
}
pre="""
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>PNNL: Global Arrays Toolkit %%s API</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<meta name="description" content="The Global Arrays (GA) toolkit
provides an efficient and portable "shared-memory"
programming interface for distributed-memory computers." />
<meta name="keywords" content="PNNL, Global Arrays, EMSL" />
<!--#include virtual="/docs/global/shared/globals.inc"-->
%s
</head>
<body>
""" % extra_header
post="""
</body>
</html>
"""
def normalize_whitespace(str):
return re.sub(r'\s+', ' ', str.strip())
def main():
if len(sys.argv) != 3 and len(sys.argv) != 4:
raise ValueError("usage: ./gen_html_api.py input_dir language [output_filename]")
print(extra_header)
path = sys.argv[1]
lang = sys.argv[2]
name = ""
if len(sys.argv) == 4:
name = sys.argv[3]
else:
name = "%s_op_api.html" % lang
assert(lang in ["c", "f", "py", "cxx"])
if path[-1] != '/':
path += '/'
files = os.listdir(path)
out = open(name,'w')
pre1 = pre
pre1 = re.sub(r'(?<!%)%(?!s)', '%%', pre1) # Replace % not followed by 's'
out.write(pre1 % lang_to_pretty[lang])
need_rule = False
in_code = False
code = ""
in_funcargs = False
in_api = False
in_desc = False
in_verb = False
in_figure = False
in_itemize = False
itemize_buffer = ""
# sort filenames but ignore the .tex extension
for file in sorted(files, key=lambda x: x[:-4]):
if file[-4:] != '.tex' or file[0] == '.':
print("skipping file %s" % path+file)
continue
if os.path.isdir(path+file):
print("skipping dir %s" % path+file)
continue
print("parsing %s" % path+file)
for line in open(path+file):
if 'apih' in line:
line = line.strip()
# skip writing the first horizontal rule
if need_rule:
out.write("<hr/>\n")
else:
need_rule = True
match = apih.match(line)
if not match:
raise ValueError("apih error, line %s" % line)
name,desc = match.groups()
link = name.replace(" ", "_")
out.write('<h3 id="%s">%s</h3>\n' % (link, name))
out.write('<i>%s</i>\n' % desc)
elif ((lang == "c" and "begin{capi}" in line)
or (lang == "f"
and ("begin{fapi}" in line or "begin{f2dapi}" in line))
or (lang == "py" and "begin{pyapi}" in line)
or (lang == "cxx" and "begin{cxxapi}" in line)):
in_api = True
elif ((lang == "c" and "end{capi}" in line)
or (lang == "f"
and ("end{fapi}" in line or "end{f2dapi}" in line))
or (lang == "py" and "end{pyapi}" in line)
or (lang == "cxx" and "end{cxxapi}" in line)):
in_api = False
elif in_api and "begin" in line and "code" in line:
in_code = True
code = ""
elif in_api and "end" in line and "code" in line:
in_code = False
if have_pygments:
lexer = lang_to_lexer[lang]
out.write(highlight(code, lexer, formatter))
else:
out.write('<pre><span style="color: rgb(255, 0, 0);">\n')
out.write(code)
out.write('</span></pre>\n')
elif in_code:
code += line
elif in_api and "begin{funcargs}" in line:
in_funcargs = True
out.write("<table>\n")
out.write("<tr><th>Type</th><th>Name</th><th>Description</th><th>Intent</th></tr>\n")
elif in_api and "end{funcargs}" in line:
in_funcargs = False
out.write("</table>\n")
elif in_funcargs:
if 'inoutarg' in line:
match = inoutarg.match(line.strip())
if not match:
print("in %s\nline: %s" % (path+file,line))
assert(False)
type,name,desc = match.groups()
out.write('<tr><td>%s</td><td>%s</td><td>%s</td><td><span style="%s">input</span>/<span style="%s">output</span></td>\n' % (
type,name,desc,style_in,style_out))
elif 'inarg' in line:
match = inarg.match(line.strip())
if not match:
print("in %s\nline: %s" % (path+file,line))
assert(False)
type,name,desc = match.groups()
out.write('<tr><td>%s</td><td>%s</td><td>%s</td><td><span style="%s">input</span></td>\n' % (
type,name,desc,style_in))
elif 'outarg' in line:
match = outarg.match(line.strip())
if not match:
print("in %s\nline: %s" % (path+file,line))
assert(False)
type,name,desc = match.groups()
out.write('<tr><td>%s</td><td>%s</td><td>%s</td><td><span style="%s">output</span></td>\n' % (
type,name,desc,style_out))
else:
print("in %s\nline: %s" % (path+file,line))
assert(False)
elif "\\wcoll" in line:
out.write("<p>Collective on the world processor group.</p>\n")
elif "\\dcoll" in line:
out.write("<p>Collective on the default processor group.</p>\n")
elif "\\gcoll" in line:
out.write("<p>Collective on the processor group inferred from the arguments.</p>\n")
elif "\\ncoll" in line:
out.write("<p>One-sided (non-collective).</p>\n")
elif "\\local" in line:
out.write("<p>Local operation.</p>\n")
elif ((lang == "c" and "begin{cdesc}" in line)
or (lang == "f" and "begin{fdesc}" in line)
or (lang == "py" and "begin{pydesc}" in line)
or (lang == "cxx" and "begin{cxxdesc}" in line)
or ("begin{desc}" in line)):
in_desc = True
out.write("<p>\n")
elif ((lang == "c" and "end{cdesc}" in line)
or (lang == "f" and "end{fdesc}" in line)
or (lang == "py" and "end{pydesc}" in line)
or (lang == "cxx" and "end{cxxdesc}" in line)
or ("end{desc}" in line)):
in_desc = False
out.write("</p>\n")
elif in_desc:
if "begin{verbatim}" in line:
in_verb = True
out.write("</p>\n<pre>\n")
elif "end{verbatim}" in line:
in_verb = False
out.write("</pre>\n<p>\n")
elif in_verb:
out.write(line)
elif "begin{figure}" in line:
in_figure = True
elif "end{figure}" in line:
in_figure = False
elif in_figure:
if "includegraphics" in line:
match = includegraphics.match(line.strip())
if not match:
print("in %s\nline: %s" % (path+file,line))
assert(False)
graphics_file = match.group(1)
graphics_file += ".png"
out.write('<img src="figures/%s" />\n' % graphics_file)
elif "begin{itemize}" in line:
in_itemize = True
itemize_buffer = ""
elif "end{itemize}" in line:
in_itemize = False
items = itemize_buffer.split(r'\item')
out.write('<ul>\n')
for item in items:
item = item.strip()
if item: # skip blank items
out.write('<li>%s</li>\n'%item)
out.write('</ul>\n')
elif in_itemize:
itemize_buffer += line.strip() + ' '
else:
line = line.strip() + ' '
if line == ' ': # empty line, begin new paragraph
out.write("</p>\n<p>\n")
if r'\ref' in line:
line = line.replace(r'\ref{', '"')
line = line.replace(r'}', '" below ')
#line = re.sub(r'\ref{.*}', "below", line)
# replace tex escape sequences
if r'\href' in line:
line = line.replace(r'\href{', '<a href = "')
line = line.replace(r'}{', '" target="_blank">')
line = line.replace(r'}', '</a>')
line = line.replace(r'\{', '{')
line = line.replace(r'\}', '}')
line = line.replace(r'``', '"')
line = line.replace(r"''", '"')
line = line.replace(r"\&", '&')
line = line.replace(r"$", '')
out.write(line)
elif "seealso" in line:
match = seealso.match(line.strip())
arg = match.group(1)
names = arg.split(',')
out.write('<h4>See Also:</h4>\n')
buf = ""
for name in names:
link = name.replace(" ", "_")
buf += '<a target="api" href="#%s">%s</a>'%(link, name)
buf += ', '
out.write(buf[:-2] + '\n')
out.write(post)
out.close()
if __name__ == '__main__':
main()