blob: f9f60ad1bd8ee66d8ebd28ed47a46e17d067844e [file] [log] [blame]
carlosmscabralf40ecd12013-02-01 18:15:58 -02001#!/usr/bin/python
2
3"""
4Convert simple documentation to epydoc/pydoctor-compatible markup
5"""
6
7from sys import stdin, stdout, argv
8import os
9from tempfile import mkstemp
10from subprocess import call
11
12import re
13
14spaces = re.compile( r'\s+' )
15singleLineExp = re.compile( r'\s+"([^"]+)"' )
16commentStartExp = re.compile( r'\s+"""' )
17commentEndExp = re.compile( r'"""$' )
18returnExp = re.compile( r'\s+(returns:.*)' )
19lastindent = ''
20
21
22comment = False
23
24def fixParam( line ):
25 "Change foo: bar to @foo bar"
26 result = re.sub( r'(\w+):', r'@param \1', line )
27 result = re.sub( r' @', r'@', result)
28 return result
29
30def fixReturns( line ):
31 "Change returns: foo to @return foo"
32 return re.sub( 'returns:', r'@returns', line )
33
34def fixLine( line ):
35 global comment
36 match = spaces.match( line )
37 if not match:
38 return line
39 else:
40 indent = match.group(0)
41 if singleLineExp.match( line ):
42 return re.sub( '"', '"""', line )
43 if commentStartExp.match( line ):
44 comment = True
45 if comment:
46 line = fixReturns( line )
47 line = fixParam( line )
48 if commentEndExp.search( line ):
49 comment = False
50 return line
51
52
53def test():
54 "Test transformations"
55 assert fixLine(' "foo"') == ' """foo"""'
56 assert fixParam( 'foo: bar' ) == '@param foo bar'
57 assert commentStartExp.match( ' """foo"""')
58
59def funTest():
60 testFun = (
61 'def foo():\n'
62 ' "Single line comment"\n'
63 ' """This is a test"""\n'
64 ' bar: int\n'
65 ' baz: string\n'
66 ' returns: junk"""\n'
67 ' if True:\n'
68 ' print "OK"\n'
69 ).splitlines( True )
70
71 fixLines( testFun )
72
73def fixLines( lines, fid ):
74 for line in lines:
75 os.write( fid, fixLine( line ) )
76
77if __name__ == '__main__':
78 if False:
79 funTest()
80 infile = open( argv[1] )
81 outfid, outname = mkstemp()
82 fixLines( infile.readlines(), outfid )
83 infile.close()
84 os.close( outfid )
85 call( [ 'doxypy', outname ] )
86
87
88
89