-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeref.py
More file actions
446 lines (352 loc) · 12.1 KB
/
makeref.py
File metadata and controls
446 lines (352 loc) · 12.1 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
#!/usr/bin/env python
import os, glob
def sortkey(x):
return os.path.basename(x).lower()
def sort_list_by_keyfunc(alist, akey):
""" sort(key=sortkey) is only in python2.4.
this is not inplace like list.sort()
"""
# make a list of tupples with the key as the first.
keys_and_list = list( zip(map(akey, alist), alist) )
keys_and_list.sort()
alist = list( map(lambda x:x[1], keys_and_list) )
return alist
def collect_doc_files():
# ABSPATH ONLY WORKS FOR docs_as_dict
#
# if __name__ == '__main__': Run()
# must be ran from in trunk dir
# get files and shuffle ordering
trunk_dir = os.path.abspath(os.path.dirname(__file__))
src_dir = os.path.join(trunk_dir, 'src')
lib_dir = os.path.join(trunk_dir, 'lib')
pygame_doc = os.path.join(src_dir, "pygame.doc")
files = (
glob.glob(os.path.join(src_dir,'*.doc')) +
glob.glob(os.path.join(lib_dir,'*.doc'))
)
files.remove(pygame_doc)
#XXX: sort(key=) is only available in >= python2.4
# files.sort(key=sortkey)
files = sort_list_by_keyfunc(files, sortkey)
files.insert(0, pygame_doc)
return files
def Run():
from optparse import OptionParser
parser = OptionParser()
parser.add_option("", "--no-code-docs", dest="have_code_docs",
action="store_false", default=True,
help="No python documentation in code.")
(options, args) = parser.parse_args()
files = collect_doc_files()
for file in files:
# print file
print (os.path.basename(file))
docs = []
pages = []
for f in files:
name = os.path.splitext(os.path.basename(f))[0]
pages.append(name)
d = name, Doc('', open(f, "U"))
docs.append(d)
#pages.sort(key=str.lower)
pages = sort_list_by_keyfunc(pages, str.lower)
pages.insert(0, "index")
index = {}
justDocs = []
for name, doc in docs:
justDocs.append(doc)
MakeIndex(name, doc, index)
for name, doc in docs:
fullname = os.path.join("docs","ref","%s.html") % name
outFile = open(fullname, "w")
outFile.write(HTMLHeader % name)
WritePageLinks(outFile, pages)
outFile.write(HTMLMid)
HtmlOut(doc, index, outFile)
outFile.write(HTMLFinish)
outFile.close()
outFile = open(os.path.join("src", "pygamedocs.h"), "w")
outFile.write("/* Auto generated file: with makeref.py . Docs go in src/ *.doc . */\n")
for doc in justDocs:
WriteDocHeader(outFile, doc, options.have_code_docs)
outFile.write("\n\n/* Docs in a comments... slightly easier to read. */\n\n\n/*")
# add the docs as comments to the header file.
for doc in justDocs:
WriteDocHeaderComments(outFile, doc)
outFile.write("\n\n*/\n\n")
topDoc = LayoutDocs(justDocs)
outFile = open(os.path.join("docs","ref","index.html"), "w")
outFile.write(HTMLHeader % "Index")
WritePageLinks(outFile, pages)
outFile.write(HTMLMid)
outFile.write("<ul>\n\n")
WriteIndex(outFile, index, topDoc)
outFile.write("\n\n</ul>\n")
outFile.write(HTMLFinish)
outFile.close()
def HtmlOut(doc, index, f):
f.write('\n\n<a name="%s">\n' % doc.fullname)
f.write("<big><b>%s</big></b><br><ul>\n" % doc.fullname)
if doc.descr:
f.write(" <i>%s</i><br>\n" % doc.descr)
if doc.protos:
for p in doc.protos:
f.write(" <tt>%s</tt><br>\n" % p)
if doc.kids:
f.write("<ul><small><table>\n")
for kid in doc.kids:
f.write(" <tr><td>%s</td><td>%s</td></tr>\n"
% (index.get(kid.fullname + "()"), kid.descr or ""))
f.write("</table></small></ul>\n")
if doc.docs:
pre = False
for d in doc.docs:
if d[0] == '*':
f.write("<ul>\n")
for li in d[1:].split('*'):
txt = HtmlPrettyWord(li)
f.write(" <li>%s</li>\n" % txt)
f.write("</ul>\n")
else:
txt, pre = HtmlPrettyLine(d, index, pre)
f.write(txt)
if pre:
f.write("</pre>\n")
else:
f.write(" <br> \n")
f.write("<!--COMMENTS:"+doc.fullname+"-->")
f.write(" <br> \n")
if doc.kids:
for k in doc.kids:
HtmlOut(k, index, f)
f.write("<br></ul>\n")
def HtmlPrettyWord(word):
if "." in word[:-1] or word.isupper():
return "<tt>%s</tt>" % word
return word
def HtmlPrettyLine(line, index, pre):
pretty = ""
if line[0].isspace():
if not pre:
pretty += "<pre>"
pre = True
elif pre:
pre = False
pretty += "</pre>"
if not pre:
pretty += "<p>"
for word in line.split():
if word[-1] in ",.":
finish = word[-1]
word = word[:-1]
else:
finish = ""
link = index.get(word)
if link:
pretty += "<tt>%s</tt>%s " % (link, finish)
elif word.isupper() or "." in word[1:-1]:
pretty += "<tt>%s</tt>%s " % (word, finish)
else:
pretty += "%s%s " % (word, finish)
pretty += "</p>\n"
else:
pretty += line + "\n"
return pretty, pre
def WritePageLinks(outFile, pages):
links = []
for page in pages[1:]:
link = '<a href="%s.html">%s</a>' % (page, page.title())
links.append(link)
outFile.write(" || \n".join(links))
#outFile.write("\n</p>\n\n")
def MakeIndex(name, doc, index):
if doc.fullname:
link = '<a href="%s.html#%s">%s</a> - <font size=-1>%s</font>' % (name, doc.fullname, doc.fullname, doc.descr)
index[doc.fullname + "()"] = link
if doc.kids:
for kid in doc.kids:
MakeIndex(name, kid, index)
def LayoutDocs(docs):
levels = {}
for doc in docs:
if doc.fullname:
topName = doc.fullname.split(".")[-1]
levels[topName] = doc
top = levels["pygame"]
for doc in docs:
if doc is top:
continue
#print (doc)
if doc.fullname:
parentName = doc.fullname.split(".")[-2]
else:
parentName = ""
parent = levels.get(parentName)
if parent is not None:
parent.kids.append(doc)
return top
def WriteIndex(outFile, index, doc):
link = index.get(doc.fullname + "()", doc.fullname)
outFile.write("<li>%s</li>\n" % link)
if doc.kids:
outFile.write("<ul>\n")
sortKids = list(doc.kids)
#print(sortKids)
sortKids = sort_list_by_keyfunc(sortKids, lambda x: x.fullname)
#sortKids = sorted( sortKids )
for kid in sortKids:
WriteIndex(outFile, index, kid)
outFile.write("</ul>\n")
def WriteDocHeader(f, doc, have_code_docs ):
name = doc.fullname.replace(".", "")
name = name.replace("_", "")
name = name.upper()
defineName = "DOC_" + name
text = ""
if have_code_docs:
if doc.protos:
text = "\\n".join(doc.protos)
if doc.descr:
if text:
text += "\\n"
text += doc.descr
f.write('#define %s "%s"\n\n' % (defineName, text))
if doc.kids:
for kid in doc.kids:
WriteDocHeader(f, kid, have_code_docs)
def WriteDocHeaderComments(f, doc):
name = doc.fullname
defineName = name
text = ""
if doc.protos:
text = "\n".join(doc.protos)
if doc.descr:
if text:
text += "\n"
text += doc.descr
text = text.replace("\\n", "\n")
#f.write('\n\n/*\n%s\n %s\n\n*/' % (defineName, text))
f.write('\n\n%s\n %s\n\n' % (defineName, text))
if doc.kids:
for kid in doc.kids:
WriteDocHeaderComments(f, kid)
class Doc(object):
def __init__(self, parentname, f):
self.kids = None
self.protos = []
self.docs = None
self.descr = ""
self.name = ""
self.fullname = ""
self.finished = False
curdocline = ""
while True:
line = f.readline()
if not line:
break
line = line.rstrip()
if line == "<END>":
if curdocline:
self.docs.append(curdocline)
curdocline = ""
self.finished = True
break
if self.kids is not None:
kid = Doc(self.fullname, f)
if kid:
self.kids.append(kid)
if line == "<SECTION>":
if curdocline:
self.docs.append(curdocline)
curdocline = ""
self.kids = []
continue
if line:
if self.docs is not None:
if line[0].isspace():
if curdocline:
self.docs.append(curdocline)
curdocline = ""
self.docs.append(line)
else:
curdocline += line + " "
elif not self.name:
self.name = line
if len(line) > 1 and line[0] == '"' and line[-1] == '"':
self.fullname = line[1:-1]
elif parentname:
splitparent = parentname.split(".")
if splitparent[-1][0].isupper():
self.fullname = splitparent[-1] + "." + line
else:
self.fullname = parentname + "." + line
else:
self.fullname = line
elif not self.descr:
self.descr = line
else:
self.protos.append(line)
else:
if self.docs is not None:
if curdocline:
self.docs.append(curdocline)
curdocline = ""
elif self.name and self.kids is None:
self.docs = []
def __repr__(self):
return "<Doc '%s'>" % self.name
def __nonzero__(self):
return self.finished
def __cmp__(self, b):
return cmp(self.name.lower(), b.name.lower())
def docs_as_dict():
"""
Dict Format:
{'pygame.rect.Rect.center': 'Rect.center: ...' ...}
Generally works, has some workarounds, inspect results manually.
"""
import pygame
files = collect_doc_files()
def make_mapping(doc, parent_name):
docs = {}
for k in doc.kids:
if k.docs:
kid_name = k.fullname
if parent_name == 'pygame':
if hasattr(pygame.base, k.name):
kid_name = '%s.%s' % ('pygame.base', k.name)
elif not kid_name.startswith(parent_name):
kid_name = '%s.%s' % (parent_name, kid_name)
docs[kid_name] = '\n'.join(k.docs)
if k.kids:
docs.update(make_mapping(k, parent_name))
return docs
mapping = {}
for f in files:
doc = Doc('', open(f, "U"))
mapping.update(make_mapping(doc, doc.name.lower()))
return mapping
HTMLHeader = """
<html>
<title>%s - Pygame Documentation</title>
<body bgcolor=#aaeebb text=#000000 link=#331111 vlink=#331111>
<table cellpadding=0 cellspacing=0 border=0 style='border: 3px solid black;' width='100%%'>
<tr>
<td bgcolor='#c2fc20' style='padding: 6px;' align=center valign=center><a href='http://www.pygame.org/'><img src='../pygame_tiny.gif' border=0 width=200 height=60></a><br><b>pygame documentation</b></td>
<td bgcolor='#6aee28' style='border-left: 3px solid black; padding: 6px;' align=center valign=center>
||
<a href=http://www.pygame.org>Pygame Home</a> ||
<a href=../index.html>Help Contents</a> ||
<a href=index.html>Reference Index</a> ||
<br> <br>
"""
HTMLMid = """
</td></tr></table>
<br>
"""
HTMLFinish = """
</body></html>
"""
if __name__ == '__main__':
Run()