9
print("python pytowiki.py module_name output_file")
12
class CodeplexFormatter:
17
def header1( self, s ):
18
return "! " + s + self.endl()
20
def header2( self, s ):
21
return "!! " + s + self.endl()
23
def header3( self, s ):
24
return "!!! " + s + self.endl()
26
def header4( self, s ):
27
return "!!!! " + s + self.endl()
29
def bulletItem( self, s ):
30
return "* " + s + self.endl()
32
def escapeMarkup( self, s ):
33
return "{\"" + s + "\"}"
35
def link( self, text, link ):
36
return "[" + text + "|#" + link + "]"
38
def anchor( self, link ):
39
return "{anchor:" + link + "}" + self.endl()
41
class MarkdownFormatter:
46
def header1( self, s ):
47
return "# " + s + self.endl()
49
def header2( self, s ):
50
return "## " + s + self.endl()
52
def header3( self, s ):
53
return "### " + s + self.endl()
55
def header4( self, s ):
56
return "#### " + s + self.endl()
58
def bulletItem( self, s ):
59
return "* " + s + self.endl()
61
def escapeMarkup( self, s ):
62
return self.endl() + "```" + self.endl() + s + self.endl() + "```" + self.endl()
64
def link( self, text, link ):
65
return "[" + text + "](#" + link + ")"
67
def anchor( self, link ):
68
return "<a name=\"" + link + "\"></a>" + self.endl()
70
def escapeSymbols(self, text):
85
def __init__ (self, module):
86
self.funcs = sorted( [ item for item in module.__dict__.values() if type(item).__name__ == "function" ], key=lambda x: x.__name__ )
87
self.classes = sorted( [ item for item in module.__dict__.values() if type(item).__name__ == "class" or type(item).__name__ == "type" ], key=lambda x: x.__name__ )
89
for cls in self.classes:
90
cls.methods = sorted( [ item for item in cls.__dict__.values() if type(item).__name__ == "function" ], key=lambda x: x.__name__ )
91
cls.properties = sorted( [ item for item in cls.__dict__.items() if type(item[1]).__name__ == "property" ], key=lambda x: x[0] )
92
cls.enum = cls.__dict__.get("names", [] )
95
def buildDoc( ioStream, formatter, apiInfo ):
97
ioStream.write( formatter.header2( "Functions" ) )
99
for func in apiInfo.funcs:
100
ioStream.write( formatter.bulletItem( formatter.link( func.__name__, func.__name__ ) ) )
102
ioStream.write( formatter.endl() )
104
ioStream.write( formatter.header2( "Classes" ) )
106
for cls in apiInfo.classes:
107
ioStream.write( formatter.bulletItem( formatter.link( cls.__name__, cls.__name__ ) ) )
109
ioStream.write( formatter.endl() )
111
for func in apiInfo.funcs:
112
ioStream.write( formatter.anchor( func.__name__ ) )
113
ioStream.write( formatter.header3( "Function " + func.__name__ ) )
114
if func.__doc__ != None:
115
ioStream.write( formatter.escapeMarkup( func.__doc__) + formatter.endl() )
118
for cls in apiInfo.classes:
119
ioStream.write( formatter.anchor( cls.__name__ ) )
120
ioStream.write( formatter.header3( "Class " + cls.__name__ ) )
121
if cls.__doc__ != None:
122
ioStream.write( formatter.escapeMarkup( cls.__doc__) + formatter.endl() )
124
ioStream.write( formatter.header4( "Base classes:") )
125
for b in cls.__bases__:
126
if b in apiInfo.classes:
127
ioStream.write( formatter.link( b.__name__, b.__name__ ) + formatter.endl() )
129
ioStream.write( b.__name__ + formatter.endl() )
132
ioStream.write( formatter.header4( "Properties:") )
133
for p in cls.properties:
134
ioStream.write( formatter.bulletItem( formatter.link( p[0], cls.__name__ + "." + p[0]) ) )
135
ioStream.write( formatter.endl() )
137
methods = list(filter( lambda m: m.__doc__ != None, cls.methods ) )
140
ioStream.write( formatter.header4( "Methods:") )
142
ioStream.write( formatter.bulletItem( formatter.link( formatter.escapeSymbols(m.__name__), cls.__name__ + "." + m.__name__)) )
143
ioStream.write( formatter.endl() )
146
ioStream.write( formatter.header4( "Values:") )
147
for v in cls.enum.items():
148
ioStream.write( formatter.bulletItem( "%s: %d" % v ) )
149
ioStream.write( formatter.endl() )
152
for p in cls.properties:
153
if p[1].__doc__ != None:
154
ioStream.write( formatter.anchor( cls.__name__ + "." + p[0] ) )
155
ioStream.write( formatter.header4( "Property " + cls.__name__ + "." + p[0] ) )
156
ioStream.write( formatter.escapeMarkup( p[1].__doc__ ) + formatter.endl() )
160
ioStream.write( formatter.anchor( cls.__name__ + "." + m.__name__ ) )
161
ioStream.write( formatter.header4( formatter.escapeSymbols("Method " + cls.__name__ + "." + m.__name__ )) )
162
ioStream.write( formatter.escapeMarkup( m.__doc__ ) + formatter.endl() )
166
if len(sys.argv) < 3:
170
moduleName = sys.argv[1]
171
fileName = sys.argv[2]
175
module = __import__( moduleName )
177
with open( fileName, "w" ) as wikiIo:
179
apiInfo = ModuleInfo( module )
181
#formatter = CodeplexFormatter()
182
formatter = MarkdownFormatter()
184
buildDoc( wikiIo, formatter, apiInfo )
186
except ImportWarning:
188
print("failed to import module ", moduleName)
191
if __name__ == "__main__":