-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpier.py
More file actions
executable file
·497 lines (406 loc) · 15 KB
/
pier.py
File metadata and controls
executable file
·497 lines (406 loc) · 15 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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#! /usr/bin/python
import re, os, os.path
"""
Parses code and spits out block comments, nicely broken down.
@see parseComments
"""
class Parser:
langs = {
'py' : {
'class' : r'class (?P<name>[^\(:]+)',
'function' : r'def (?P<definition>(?P<name>[^\(]+).+):',
'variable' : r'(?P<name>.+) =',
'comment_begin' : '"""',
'comment_middle' : '',
'comment_end' : '"""',
},
'php' : {
'class' : r'class (?P<name>[^\(\s\{]+)',
'function' : r'function (?P<definition>(?P<name>[^\(]+).+)',
'variable' : r'(?P<name>\$.+) =',
'comment_begin' : '/*',
'comment_middle' : '*',
'comment_end' : '*/',
},
}
"""
Parse comments in the given file.
A code example:
def echo(s):
print s
@param {String} file Path of file.
@return {Array}
@see parseComments
@see MarkdownTemplate::renderComments Parser.renderComments
"""
def parseFile(self, file):
import os.path
if os.path.isfile(file):
(path, ext) = os.path.splitext(file)
if ext[1:] in self.langs:
self.lang = self.langs[ext[1:]]
self.filename = os.path.basename(path)
f = open(file)
comments = self.parseComments(f.read())
f.close()
return comments
print "This file type is not supported yet ("+file+")"
return []
"""
Parse comments in the given code.
@param {String} str String to parse.
@return {Array}
@see parseComment
"""
def parseComments(self, str):
comments = []
buf = ''
ignore = False
within = False
comment_begin = self.lang['comment_begin']
comment_end = self.lang['comment_end']
len_begin = len(comment_begin)
len_end = len(comment_end)
whitespace = ['', ' ', '\n', '\t']
i = 0
while i < len(str):
# start comment
if within == False and str[i:i+len_begin] == comment_begin and (str[i-1] in whitespace):
# code following previous comment
if buf.strip() and len(comments) > 0:
comment = comments[len(comments) - 1]
comment['code'] = buf.strip()
comment['ctx'] = self.parseCodeContext(comment['code'])
buf = ''
ignore = ('!' == str[i + len_begin])
i += len_begin + (1 if ignore else 0)
within = True
# end comment
elif within and str[i:i+len_end] == comment_end and (str[i-1] in whitespace):
i += len_end
buf = re.sub(re.escape(self.lang['comment_middle']), '', buf)
comment = self.parseComment(buf)
comment['ignore'] = ignore
comments.append(comment)
within = False
ignore = False
buf = ''
# buffer comment or code
else:
buf += str[i]
i += 1
# trailing code
if len(buf.strip()) and len(comments) > 0:
comment = comments[len(comments) - 1]
comment['code'] = buf.strip()
comment['ctx'] = self.parseCodeContext(comment['code'])
return comments
"""
Parse the given comment `s`.
The comment object returned contains the following
- `tags` array of tag objects
- `description` the first line of the comment
- `body` lines following the description
- `content` both the description and the body
- `isPrivate` True when "@api private" is used
@param {String} s
@return {Dictionary}
@see parseTag
@api public
"""
def parseComment(self, s):
comment = {'tags' : []}
description = {}
# remove the same number of spaces/tabs before each line of the comment (removes indenting)
spaces = re.match('^\n(\s*)', s).group(1)
# -- warning --
# this is a nasty little workaround
# I couldn't make the line below work:
#s = re.sub('('+spaces+')', '', s)
# what's wrong with that? oh well, this works
# -- end warning --
lines = ''
for l in s.split('\n'):
lines += l.replace(spaces, '', 1)+'\n'
s = lines.strip()
# split the description and tags
pieces = re.split('\s+@', s)
description['full'] = pieces[0] or s
# split summary and body (two line breaks, both possibly followed by spaces/tabs
desc = description['full'].split('\n\n', 1)
description['summary'] = desc[0]
description['body'] = ''
if len(desc) > 1:
description['body'] = desc[1]
comment['description'] = description
# parse tags
if len(pieces) > 1:
comment['tags'] = map(self.parseTag, pieces[1:])
for t in comment['tags']:
if t['type'] == 'api' and t['visibility'] == 'private':
comment['isPrivate'] = True
break
return comment
"""
Parse tag string "@param {Array} name description" etc.
@param {String} str
@return {Dictionary}
@api public
"""
def parseTag(self, str):
parts = str.strip().split(' ')
tag = {}
tag['type'] = parts.pop(0)
type = tag['type']
if type == 'param':
tag['types'] = self.parseTagTypes(parts[0])
tag['name'] = parts[1] or ''
tag['description'] = ' '.join(parts[2:] or '')
elif type == 'return':
tag['types'] = self.parseTagTypes(parts[0])
tag['description'] = ' '.join(parts[1:])
elif type == 'see':
tag['title'] = parts.pop(0) or ''
tag['url'] = ' '.join(parts)
elif type == 'api':
tag['visibility'] = parts[0]
elif type == 'type':
tag['types'] = self.parseTagTypes(parts[0])
return tag
"""
Parse tag type string "{Array|Object}" etc.
@param {String} str
@return {Array}
@api public
"""
def parseTagTypes(self, str):
return re.split('[,|/]', str[1:-1])
"""
Parse the context from the given `str` of code.
This method attempts to discover the context
for the comment based on it's code.
@param {String} str
@return {Dictionary}
@api public
"""
def parseCodeContext(self, str):
str = str.splitlines()[0].strip()
for k,exp in self.lang.iteritems():
if k.startswith('comment'):
continue
match = re.search(exp, str)
if match != None:
ctx = {
'type' : k,
'name' : match.group('name'),
'string' : match.group(0),
}
if k == 'function':
ctx['definition'] = match.group('definition')
return ctx
if self.filename:
return {'type' : 'file', 'name' : self.filename}
"""
Turns comments into markdown.
"""
class MarkdownTemplate:
def __init__(self, base_url = ''):
self.base_url = base_url or ''
"""
Renders a bunch of comments as markdown.
"""
def renderComments(self, comments, filename = ''):
output = ''
for c in comments:
if ('isPrivate' in c and c['isPrivate']) or c['ignore']:
continue
output += self.renderComment(c, filename)
return output
"""
Renders a comment as markdown.
@api public
"""
def renderComment(self, comment, filename):
output = ''
# class/function header
output += self._header(comment)
# function definition
output += self._definition(comment)
# description
output += self._description(comment)
see = ''
params = ''
returns = ''
for t in comment['tags']:
type = t['type']
if type == 'param':
params += self._param_tag(t)
elif type == 'return':
returns += self._return_tag(t)
elif type == 'see':
(title, url) = self._see_tag(t)
see += '['+title+']('+url+')\n\n'
elif type == 'api':
#output += 'Visibility: '+t['visibility']+'\n\n'
pass
elif type == 'type':
output += self._see_tag(t)
if params != '':
output += self._params(params)
if returns != '':
output += self._returns(returns)
if see != '':
output += self._see(see)
return output
def _header(self, comment):
type = comment['ctx']['type']
name = comment['ctx']['name']
if type == 'class' or type == 'file':
return "# "+name+"\n\n"
else:
return "## "+name+"\n\n"
def _definition(self, comment):
if type == 'function':
return " "+comment['ctx']['definition']+"\n\n"
return ''
def _description(self, comment):
return comment['description']['full']+"\n\n"
def _param_tag(self, t):
return t['name']+' '+'|'.join(t['types'])+' '+t['description']+'\n'
def _return_tag(self, t):
return '{'+' | '.join(t['types'])+'}\n\n'+t['description']+'\n\n'
def _see_tag(self, t):
url = t['url']
i = url.rfind('.')
url = url.replace('.', '/')
if i > -1:
url = self.base_url+url[:i]+'#'+url[i+1:]
return (t['title'], url)
def _type_tag(self, t):
return '{'+'|'.join(t['types'])+'}\n\n'
def _params(self, params):
return "### Parameters\n\n"+params+"\n"
def _returns(self, returns):
return "### Return\n"+returns
def _see(self, see):
return "### See\n"+see
class HTMLTemplate(MarkdownTemplate):
def renderComment(self, comment, filename):
self.setup_pygment(filename)
text = MarkdownTemplate.renderComment(self, comment, filename)
import markdown
return markdown.markdown(text)
def setup_pygment(self, filename):
from pygments.formatters import HtmlFormatter
from pygments.lexers import get_lexer_for_filename
self.lexer = get_lexer_for_filename(filename)
self.formatter = HtmlFormatter()
def _header(self, comment):
# class/function header
type = comment['ctx']['type']
name = comment['ctx']['name']
if type == 'class' or type == 'file':
return "# "+name+"\n\n"
else:
return "<a name='"+name+"'><h2>"+name+"</a></h2>\n\n"
def _definition(self, comment):
# function definition
if type == 'function':
return highlight(comment['ctx']['definition'], self.lexer, self.formatter)+"\n\n"
return ''
def _description(self, comment):
from pygments import highlight
# highlight each line of code
# insert the highlighted code, replacing the original line of code
lines = ''
code = ''
for l in comment['description']['full'].splitlines():
if len(l.strip()) > 0 and l.startswith(' '):
code += l[4:]+'\n'
elif len(code) > 0:
lines += highlight(code, self.lexer, self.formatter)+l+'\n'
code = ''
else:
lines += l+'\n'
if len(code) > 0:
lines += highlight(code, self.lexer, self.formatter)+'\n'
return lines+"\n\n"
def _params(self, params):
return "### Parameters\n<table>\n"+params+"</table>\n\n"
def _param_tag(self, t):
return ' <tr><td>'+t['name']+'</td><td>'+'|'.join(t['types'])+'</td><td>'+t['description']+'</td></tr>\n'
def _see_tag(self, t):
url = t['url']
if url == '':
url = '#'+t['title']
else:
i = url.rfind('.')
url = url.replace('.', '/')
if i > -1:
url = self.base_url+url[:i]+'.html#'+url[i+1:]
else:
url = self.base_url+url+'.html'
return (t['title'], url)
class MarcdocTemplate(MarkdownTemplate):
def renderComment(self, comment, filename):
return MarkdownTemplate.renderComment(self, comment, filename)
def _header(self, comment):
# class/function header
type = comment['ctx']['type']
name = comment['ctx']['name']
if type == 'class' or type == 'file':
return "# "+name+"\n\n"
else:
return "<a name='"+name+"'><h2>"+name+"</a></h2>\n\n"
class Renderer:
def __init__(self, output_type, base_url = ''):
self.parser = Parser()
self.output_html = (output_type == 'html')
if output_type == 'html':
self.template = HTMLTemplate(base_url)
elif output_type == 'marcdoc':
self.template = MarcdocTemplate(base_url)
else:
self.template = MarkdownTemplate(base_url)
def renderFile(self, file, out_file):
comments = self.parser.parseFile(file)
if len(comments) > 0:
text = self.template.renderComments(comments, file)
(path, ext) = os.path.splitext(out_file)
if self.output_html:
out_file = path+'.html'
else:
out_file = path+'.md'
f = open(out_file, "w+")
f.write(text)
f.close()
def renderDirectory(self, dir, out_dir):
for f in os.listdir(dir):
# skip dot files
if f[0] == '.':
continue
out = out_dir+'/'+f
f = dir+'/'+f
if os.path.isfile(f):
self.renderFile(f, out)
elif os.path.isdir(f):
self.renderDirectory(f, out)
if __name__ == "__main__":
def opt_parser():
from optparse import OptionParser
parser = OptionParser("usage: %prog [options] file1 [file2...]")
parser.add_option("-d", "--directory", dest="directory", help="writes file(s) to DIR", metavar="DIR", default=".")
parser.add_option("-o", "--output", dest="output", help="outputs files as one of: html, marcdoc, markdown", default='markdown')
parser.add_option("-b", "--base-url", dest="base_url", help="base url for links")
return parser
opt_parser = opt_parser()
(options, args) = opt_parser.parse_args()
if len(args) < 1:
opt_parser.error("Need at least one file to parse.")
renderer = Renderer(options.output, options.base_url)
for f in args:
out = options.directory+'/'+os.path.basename(f)
if os.path.isfile(f):
renderer.renderFile(f, out)
elif os.path.isdir(f):
renderer.renderDirectory(f, out)