-
Notifications
You must be signed in to change notification settings - Fork 4
/
GenText_HTML.py
421 lines (332 loc) · 12.3 KB
/
GenText_HTML.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
from GenText import * # file parser and text rendering
# =============================================================================
# GenHTML subclass
# =============================================================================
class GenHTML(GenHelp):
def __init__(self, matlab=False, toolbox=None, **kwargs):
super(GenHTML, self).__init__(**kwargs)
self.matlab = matlab
if toolbox == 'rtb':
self.toolboxname = "Robotics Toolbox for MATLAB"
self.toolboxurl = "http://www.petercorke.com/robot"
elif toolbox == 'mvtb':
self.toolboxname = "Machine Vision Toolbox for MATLAB"
self.toolboxurl = "http://www.petercorke.com/vision"
def done(self):
pass
def substitutions(self, s):
# HTML specific fixups
s = s.replace('<', '<')
s = s.replace('>', '>')
s = s.replace('&', '&')
s = s.replace('^', 'ˆ')
return s
def emphFunction(self, s):
return '<span style="color:red">%s</span>' % s
def emphVar(self, s):
return '<strong>%s</strong>' % s
def emphPath(self, s):
return '<strong>%s</strong>' % s
@trace
def startModule(self, funcname, text, tag=None, titlebar=False, ismethod=False):
def gen_titlebar(funcname):
if self.matlab:
out = '''<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="http://www.petercorke.com/RVC/common/toolboxhelp.css">
<title>M-File Help: %(function)s</title>
</head>
<body>
<table border="0" cellspacing="0" width="100%%">
<tr class="subheader">
<td class="headertitle">M-File Help: %(function)s</td>
<td class="subheader-left"><a href="matlab:open %(function)s">View code for %(function)s</a></td>
</tr>
</table>
''' % {'function' : funcname}
else:
out = '''<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="http://www.petercorke.com/RVC/common/toolboxhelp.css">
<title>%(function)s</title>
</head>
<body>
''' % {'function' : funcname}
return out
# create HTML header block + title bar
if titlebar:
self.out += gen_titlebar(funcname)
if tag:
self.out += '<a name="%s">' % tag
self.out += '<h1>%s</h1>' % funcname
if tag:
self.out += '</a>\n'
#out += '<p><span class="helptopic">%s</span> %s</p>' % (funcname, split_first_word(text[0])[1])
self.out += '<p><span class="helptopic">%s</span></p>' % (split_first_word(text[0])[1])
self.vars = set()
def endModule(self):
self.out += '<hr>\n'
today = date.today()
out = '<address style="text-align:right">Generated %s by <strong><a href="xx">%s</a></strong> © 2014 Peter Corke</address>\n' % (today.isoformat(), sys.argv[0])
if self.matlab:
self.out += '''
<table border="0" width="100%" cellpadding="0" cellspacing="0">
<tr class="subheader" valign="top"><td> </td></tr></table>
<p class="copy">© 1990-2014 Peter Corke.</p>
</body></html>'''
else:
self.out += '''
<p class="copy"><a href="%s">%s</a> © 1990-2014 Peter Corke.</p>
</body></html>''' % (self.toolboxurl, self.toolboxname)
@trace
def endMethod(self):
self.out += '<hr>\n'
@trace
def heading(self, text):
self.out += '<h2>%s</h2>\n' % text
@trace
def startTable(self):
self.out += '<table class="list">\n'
@trace
def addTable(self, col1, col2):
self.out += ' <tr><td style="white-space: nowrap;" class="col1">%s</td> <td>%s</td></tr>\n' % (col1, col2)
@trace
def addTableSep(self):
self.out += ' <tr></tr>\n <tr></tr>'
@trace
def endTable(self):
self.out += '</table>\n'
@trace
def startCode(self):
self.out += '<pre style="width: 90%%;" class="examples">\n'
@trace
def addCode(self, text):
self.out += '%s\n' % text.replace(' ', ' ')
@trace
def endCode(self):
self.out += '</pre>\n'
@trace
def startList(self):
self.out += '<ul>\n'
@trace
def addList(self, text):
self.out += ' <li>%s</li>\n' % text
@trace
def endList(self):
self.out += '</ul>\n'
@trace
def startPara(self):
self.out += '<p>\n'
@trace
def addPara(self, text, **args):
self.startPara()
self.findvars(text, classname=args['classname'])
s = self.transform(text, **args)
self.out += s + '\n\n'
self.endPara()
@trace
def endPara(self):
self.out += '</p>\n'
@trace
def startAlso(self):
self.alsoCount = 0
self.startPara()
@trace
def addAlso(self, text):
if self.alsoCount > 0:
self.out += ', '
if self.matlab:
self.out += '<a href="%s.html">%s</a>' % (text,text)
else:
self.out += '<a href="%s.html">%s</a>' % (text,text)
self.alsoCount += 1
@trace
def endAlso(self):
self.endPara()
# Generate code document for a regular m-file
def format_code(self, filename, pname=None):
def fixspace(s):
return s.replace(' ', ' ')
re_comment = re.compile(r'(%.*)$')
# open the output file
outfile = os.path.splitext(filename.lstrip('@'))[0]+'_code.html'
out = open(outfile, 'w')
funcname = os.path.splitext(os.path.basename(filename))[0]
out.write('''<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="http://www.petercorke.com/RVC/common/book.css">
<title>M-File Help: %(function)s</title>
</head>
<body>
''' % {'function' : funcname})
with open(filename, 'r') as f:
out.write('<h1>%s</h1>' % funcname)
out.write('<table class="codelistingtable">')
for (num,line) in enumerate(f):
line = line.replace(' ', ' ')
line = re_comment.sub('<span style="color:blue">\\1</span>', line)
out.write('<tr><td class="codelistingnum">%d</td><td><pre class="codelistingcode">%s</pre></td></tr>\n' % (num+1, line))
out.write('</table>\n')
today = date.today()
out.write('<hr><address style="text-align:right">Generated %s by <strong><a href="xx">%s</a></strong> © 2014 Peter Corke</address>\n' % (today.isoformat(), pname))
out.write('</body></html>\n')# =============================================================================
# GenMD subclass to create MarkDown output
# =============================================================================
class GenMarkDown(GenHelp):
def __init__(self, matlab=False, toolbox=None, jekyll=False, **kwargs):
super(GenMarkDown, self).__init__(**kwargs)
self.matlab = matlab
self.re_dims2 = re.compile(r'(\b[0-9A-Z]+)x([0-9A-Z]+)\b')
self.re_dims3 = re.compile(r'(\b[0-9A-Z]+)x([0-9A-Z]+)x([0-9A-Z]+)\b')
self.re_exp = re.compile(r'\^([0-9a-zA-Z-]+)')
self.re_exp2 = re.compile(r'[^{}]\^[^{}]')
self.jekyll = jekyll
if toolbox == 'rtb':
self.toolboxname = "Robotics Toolbox for MATLAB"
self.toolboxurl = "http://www.petercorke.com/robot"
elif toolbox == 'mvtb':
self.toolboxname = "Machine Vision Toolbox for MATLAB"
self.toolboxurl = "http://www.petercorke.com/vision"
def done(self):
pass
def substitutions(self, s):
# MarkDown specific fixups
# NxM -> N × M
s = self.re_dims2.sub(r'\1×\2', s)
# NxMxK -> N × M × K
s = self.re_dims3.sub(r'\1×\2× \3', s)
# A^2 -> A<sup>2</sup>
s = self.re_exp.sub(r'<sup>\1</sup>', s)
s = s.replace('\n', ' ')
return s
def emphFunction(self, s):
return '**%s**' % s
def emphVar(self, s):
return '`%s`' % s
def emphPath(self, s):
return '`%s`' % s
@trace
def startModule(self, funcname, text, tag=None, titlebar=False, ismethod=False):
# for Jekyll static page generator need to add special header, simple version here
if self.jekyll:
self.out += '---\n---\n'
self.out += '# %s\n' % funcname
self.out += '_%s_\n' % (split_first_word(text)[1])
self.vars = set()
def endModule(self):
pass
@trace
def endMethod(self):
self.out += '<hr>\n\n'
@trace
def heading(self, text):
self.out += '### %s\n' % text
@trace
def startTable(self):
self.out += '| | |\n|---|---|\n'
@trace
def addTable(self, col1, col2):
self.out += '| `%s` | %s |\n' % (col1, col2)
@trace
def addTableSep(self):
self.out += ''
@trace
def endTable(self):
self.out += '\n\n'
@trace
def startCode(self):
print ' start code'
self.out += '```matlab\n'
@trace
def addCode(self, text):
print ' add code'
self.out += '%s\n' % text
@trace
def endCode(self):
print ' end code'
self.out += '```\n'
@trace
def startList(self):
self.out += ''
@trace
def addList(self, text):
self.out += '* %s\n' % self.transform(text)
@trace
def endList(self):
self.out += '\n'
@trace
def startPara(self):
self.out += '\n'
@trace
def addPara(self, text, definition, **args):
print " add to para", text, definition
self.startPara()
if definition:
self.out += "```" + definition + "```"
s = self.transform(text, **args)
self.out += s
@trace
def endPara(self):
self.out += '\n'
@trace
def startAlso(self):
self.alsoCount = 0
self.startPara()
@trace
def addAlso(self, text):
if self.alsoCount > 0:
self.out += ', '
self.out += '[%s](%s.md)' % (text,text)
self.alsoCount += 1
@trace
def endAlso(self):
self.endPara()
# Generate code document for a regular m-file
def format_code(self, filename, pname=None):
# open the output file
outfile = os.path.splitext(filename.lstrip('@'))[0]+'_code.md'
out = open(outfile, 'w')
funcname = os.path.splitext(os.path.basename(filename))[0]
out.write('''
## M-File Help: %(function)s
''' % {'function' : funcname})
with open(filename, 'r') as f:
out.write('# %s\n' % funcname)
out.write('```matlab\n')
for (num,line) in enumerate(f):
out.write(line+'\n')
out.write('```\n')
today = date.today()
out.write('---\nGenerated %s by *%s © 2019 Peter Corke\n' % (today.isoformat(), pname))
def write_indices(self, all, bytag, prefix='', jekyll=False):
# make the alphabetic list
print all
funcs = sorted(all.keys())
with open('TOC_alpha.md', 'w') as f:
if jekyll:
f.write('---\n---\n')
f.write('# Alphabetic list of functions\n')
f.write('\n| Function | Description|\n|---|---|\n')
for func in funcs:
f.write("|[`%s`](%s.html) | %s |\n" % (func, os.path.join(prefix,func), all[func]))
# make the per tag indices
for tag in bytag.keys():
funcs = sorted(bytag[tag])
with open('TOC_%s.md' % (tag,), 'w') as f:
if jekyll:
f.write('---\n---\n')
f.write('# List of %s functions\n' % (tag,))
f.write('\n| Function | Description|\n|---|---|\n')
for func in funcs:
f.write("|[`%s`](index_%s.html) | %s |\n" % (func, os.path.join(prefix,func), all[func]))
with open('TOC.md', 'w') as f:
if jekyll:
f.write('---\n---\n')
f.write('# Function indices\n\n')
f.write(" * [All functions](%s)\n" % (os.path.join(prefix,'index_alpha.html'),))
f.write(" * By tag:\n")
for tag in sorted(bytag.keys()):
f.write(" - [%s related](%s)\n" % (tag, os.path.join(prefix, tag+'.html')))