1
"""take all the PyGame source and create html documentation"""
3
import fnmatch, glob, os, types
6
DOCPATTERN = '*static char*doc_*=*'
8
SOURCES = ['../../src/*.c']
9
IGNORE_SOURCES = ['rwobject.c']
10
PYTHONSRC =['cursors', 'version', 'sprite']
13
PAGETEMPLATE = open('pagelate.html').readlines()
14
DOCTEMPLATE = open('doclate.html').readlines()
15
LISTTEMPLATE = open('listlate.html').readlines()
16
INDEXTEMPLATE = ['<a href=ref/{category}.html#{name}>{mod}.{name}</a> - {quick}<br>']
18
INDEXSTART = "\n<br><hr><br><font size=+1><b>Full Index</b></font><ul>\n<!--FULLINDEX-->\n"
19
INDEXEND = "<!--ENDINDEX-->\n</ul>\n"
25
The <b>pygame documentation</b> is mainly generated automatically from the
26
documentation. Each module and object in the package is broken into its
27
own page in the reference documentation. The names of the objects are
28
capitalized, while the regular module names are lower case.
30
The <b>pygame documentation</b> also comes with a full set of tutorials.
31
You can find links to these tutorials and other documentation files below.
37
def filltemplate(template, info):
43
pos = line.find('{', pos)
45
end = line.find('}', pos)
49
lookname = line[pos+1:end]
50
match = info.get(lookname, '')
54
try:line = line[:pos] + match + line[end+1:]
56
print lookname, type(match), match
58
pos += len(match) - (end-pos)
60
return '\n'.join(output) + '\n'
64
def readsource(filename):
69
line = file.readline()
71
if fnmatch.fnmatch(line, DOCPATTERN) and line.find(';') == -1:
74
line = file.readline()
75
if not line or line.find('"') == -1: break
76
line = line[line.find('"')+1:line.rfind('"')]
79
line = '<br> <br>'
80
elif line.endswith('\\n'):
83
documents.append(lines)
87
def getpydoclines(doc):
88
if doc is None: return
90
for line in doc.split('\n'):
92
line = '<br> <br>'
97
def readpysource(name):
98
modulename = 'pygame.' + name
100
module = getattr(__import__(modulename), name)
101
title = ' /*DOC*/ static char doc_pygame_' + name + '_MODULE[] =\n'
102
documents.append([title] + getpydoclines(module.__doc__))
104
for name, obj in module.__dict__.items():
105
if type(obj) is types.ClassType:
106
title = ' /*DOC*/ static char doc_%s[] =\n'%(name)
108
if hasattr(obj, '__init__'):
109
init = getattr(obj, '__init__')
110
if hasattr(init, '__doc__'):
111
initdocs = getpydoclines(init.__doc__)
112
if not initdocs: initdocs = []
114
docs = getpydoclines(obj.__doc__)
116
quick = '<b>(class)</b> - ' + docs[0]
117
usage = 'pygame.%s.%s()'%(modname,name)
119
usage = 'pygame.%s.%s'%(modname,name) + initdocs[0][initdocs[0].find('('):]
120
docs = [usage, quick, '']+docs+['<br> <br>']+initdocs[2:]
121
documents.append([title] + docs)
123
except AttributeError:
124
documents.append([title] + ['%s.%s'&(modname,name),'noclassdocs'])
125
for methname, meth in obj.__dict__.items():
126
if methname is '__init__': continue
127
title = ' /*DOC*/ static char doc_%s_%s[] =\n'%(name,methname)
129
docs = getpydoclines(meth.__doc__)
131
docs[0] = ('pygame.%s.%s?'%(modname,name))+docs[0]
132
documents.append([title] + docs)
133
except AttributeError: pass
135
elif hasattr(obj, '__doc__'):
136
title = ' /*DOC*/ static char doc_' + name + '[] =\n'
137
documents.append([title] + getpydoclines(obj.__doc__))
147
modpos = d[0].find('_MODULE')
148
extpos = d[0].find('_EXTRA')
151
start = d[0].rfind(' ', 0, modpos)
152
name = d[0][start+5:modpos]
153
modules[name] = '\n'.join(d[1:])
155
start = d[0].rfind(' ', 0, extpos)
156
name = d[0][start+5:extpos]
157
extras[name] = '\n'.join(d[1:])
159
obj = {'docs':['no documentation']}
160
name = d[1][:d[1].find('(')]
161
dot = name.rfind('.')
162
name = name.replace('?', '.')
164
obj['category'] = 'misc'
166
obj['fullname'] = name
168
obj['category'] = name[:dot].replace('.', '_')
169
obj['name'] = name[dot+1:]
170
obj['fullname'] = name
172
obj['usage'] = d[1].replace('?', '.')
175
except IndexError: pass
178
return [modules, extras, funcs]
181
def getdocinfo(file, prefix=''):
182
path = os.path.join(prefix, file[3:])
183
prettyname = (os.path.splitext(os.path.split(file)[-1])[0])
184
prettyname = prettyname[0].capitalize() + prettyname[1:]
185
if file.find('html') >= 0:
186
info = open(file).readlines(2)[1]
188
info = open(file).readline().strip().capitalize()
189
return path, prettyname, info
193
fileline = '<li><a href=%s>%s</a> - %s</li>'
194
texthead = '<font size=+1><b>Basic Documentation</b></font><br>'
195
tuthead = '<font size=+1><b>Tutorials / Introductions</b></font><br>'
196
texts1 = ['../../readme.html', '../../install.html', '../LGPL', '../logos.html']
197
texts = [fileline%x for x in [getdocinfo(x) for x in texts1]]
198
finaltext = texthead + '\n'.join(texts)
199
tuts1 = glob.glob('../tut/*.html')
201
tuts = [fileline%(x[0],x[1],x[2][9:]) for x in [getdocinfo(x) for x in tuts1] if x[2].startswith('TUTORIAL:')]
202
finaltut = tuthead + '\n'.join(tuts)
203
return finaltext + '<br> <br>' + finaltut
206
def lookupdoc(docs, name, category):
208
if d['fullname'] == category + '.' + name:
210
if d['fullname'] == name or d['name'] == name:
214
def htmlize(doc, obj, func):
215
for i in range(len(doc)):
217
line = line.replace('<<', '<<').replace('>>', '>>')
220
pos = line.find('(', pos+1)
222
if line[pos-1].isspace(): continue
223
start = line.rfind(' ', 0, pos)
224
start2 = line.rfind('\n', 0, pos)
225
start = max(max(start, start2), 0)
226
lookname = line[start+1:pos]
227
if lookname.startswith('ygame'):
228
lookname = 'p' + lookname
230
elif lookname[1:].startswith('pygame'):
231
lookname = lookname[1:]
233
match = lookupdoc(func, lookname, obj['category'])
235
#print 'NOMATCH: "'+ obj['category'] +'" "' + lookname + '"'
237
end = line.find(')', pos)+1
238
if match['fullname'] == obj['fullname']:
239
link = '<u>%s</u>' % line[start+1:end]
241
if match['category'] == obj['category']:
242
dest = '#%s' % (match['name'])
244
dest = '%s.html#%s' % (match['category'], match['name'])
245
link = '<a href=%s>%s</a>' % (dest, line[start+1:end])
246
line = line[:start+1] + link + line[end:]
247
pos += len(link) - (pos-start)
251
def buildlinks(alldocs):
252
mod, ext, func = alldocs
255
htmlize(doc, obj, func)
256
for k,i in mod.items():
258
obj = {'category': k, 'fullname':''}
259
htmlize(doc, obj, func)
261
for k,i in ext.items():
263
obj = {'category': k, 'fullname':''}
264
htmlize(doc, obj, func)
268
def categorize(allfuncs):
272
if not cats.has_key(cat):
279
def create_toc(allfuncs, prefix=''):
288
for m in mods.keys():
289
if m[7:] in PYTHONSRC:
291
elif m[0].lower() == m[0]:
296
file = m.replace('.', '_') + '.html'
297
if m[:7] == 'pygame_':
299
str = '<a href=%s%s>%s</a>' % (prefix, file, m)
308
for x in range(0, len(l_mod), items_per_line):
309
row = l_mod[x:x+items_per_line]
310
str += '|| ' + ' || \n'.join(row) + ' ||<br>\n'
312
for x in range(0, len(l_type), items_per_line):
313
row = l_type[x:x+items_per_line]
314
str += '|| ' + ' || \n'.join(row) + ' ||<br>\n'
316
for x in range(0, len(l_py), items_per_line):
317
row = l_py[x:x+items_per_line]
318
str += '|| ' + ' || \n'.join(row) + ' ||<br>\n'
323
def namesort(a,b): return cmp(a['name'], b['name'])
326
def writefuncdoc(alldocs):
327
modules, extras, funcs = alldocs
328
for cat, docs in funcs.items():
333
d['docs'] = '\n'.join(d['docs'])
334
htmldocs.append(filltemplate(DOCTEMPLATE, d))
335
htmllist.append(filltemplate(LISTTEMPLATE, d))
336
modinfo = modules.get(cat, '')
337
extrainfo = extras.get(cat, None)
339
modinfo += '<p> </p>' + extrainfo
341
finalinfo = {'title': cat.replace('_', '.'),
342
'docs': '\n'.join(htmldocs),
343
'index': '\n'.join(htmllist),
346
'mainpage': '../index.html',
347
'logo': '../pygame_tiny.gif'}
348
page = filltemplate(PAGETEMPLATE, finalinfo)
349
file = open(OUTPUTDIR + cat + '.html', 'w')
355
def makefullindex(alldocs):
356
modules, extras, funcs = alldocs
358
for cat, docs in funcs.items():
363
d['mod'] = d['category'].replace('_', '.')
364
s = filltemplate(INDEXTEMPLATE, d)
367
return INDEXSTART + ''.join(fullindex) + INDEXEND
375
files += glob.glob(s)
377
if os.path.split(f)[1] in IGNORE_SOURCES:
381
print 'read c sources...'
384
rawdocs += readsource(f)
386
print 'read python sources...'
388
rawdocs += readpysource(f)
392
alldocs = parsedocs(rawdocs)
394
#find and create hyperlinks
397
#create table of contents
399
pathed_toc = create_toc(alldocs[2], 'ref/')
400
MODULETOC = create_toc(alldocs[2])
403
alldocs[2] = categorize(alldocs[2])
406
writefuncdoc(alldocs)
408
fulldocs = findtutorials() + makefullindex(alldocs)
414
finalinfo = {'title': 'Pygame Documentation',
416
'index': mainindex_desc,
419
'mainpage': 'index.html',
420
'logo': 'pygame_tiny.gif',
422
page = filltemplate(PAGETEMPLATE, finalinfo)
423
file = open('../index.html', 'w')
428
if __name__ == '__main__':