blob: 9c94d8ce10f676d12bbfd9040529022e3e5da042 [file] [log] [blame]
Tim Peters52cfa332002-04-19 16:09:26 +00001'''
2 Makes the necesary files to convert from plain html of
3 Python 1.5 and 1.5.x Documentation to
4 Microsoft HTML Help format version 1.1
5 Doesn't change the html's docs.
6
7 by hernan.foffani@iname.com
8 no copyright and no responsabilities.
9
10 modified by Dale Nagata for Python 1.5.2
11
12 Renamed from make_chm.py to prechm.py, and checked into the Python
13 project, 19-Apr-2002 by Tim Peters. Assorted modifications by Tim
14 and Fred Drake. Obtained from Robin Dunn's .chm packaging of the
15 Python 2.2 docs, at <http://alldunn.com/python/>.
16'''
17
18import sys
19import os
20import formatter
21import htmllib
22import string
23import getopt
24
Tim Peters52cfa332002-04-19 16:09:26 +000025usage_mode = '''
26Usage: make_chm.py [-c] [-k] [-p] [-v 1.5[.x]] filename
27 -c: does not build filename.hhc (Table of Contents)
28 -k: does not build filename.hhk (Index)
29 -p: does not build filename.hhp (Project File)
30 -v 1.5[.x]: makes help for the python 1.5[.x] docs
31 (default is python 1.5.2 docs)
32'''
33
Tim Peterse6b63e62002-04-19 18:07:52 +000034# Project file (*.hhp) template. 'arch' is the file basename (like
35# the pythlp in pythlp.hhp); 'version' is the doc version number (like
36# the 2.2 in Python 2.2).
37# The magical numbers in the long line under [WINDOWS] set most of the
38# user-visible features (visible buttons, tabs, etc).
Tim Peters52cfa332002-04-19 16:09:26 +000039project_template = '''
40[OPTIONS]
41Compatibility=1.1
Tim Peterse6b63e62002-04-19 18:07:52 +000042Compiled file=%(arch)s.chm
43Contents file=%(arch)s.hhc
44Default Window=%(arch)s
Tim Peters52cfa332002-04-19 16:09:26 +000045Default topic=index.html
46Display compile progress=No
47Full-text search=Yes
Tim Peterse6b63e62002-04-19 18:07:52 +000048Index file=%(arch)s.hhk
Tim Peters52cfa332002-04-19 16:09:26 +000049Language=0x409
Tim Peterse6b63e62002-04-19 18:07:52 +000050Title=Python %(version)s Documentation
Tim Peters52cfa332002-04-19 16:09:26 +000051
52[WINDOWS]
Tim Peterse6b63e62002-04-19 18:07:52 +000053%(arch)s="Python %(version)s Documentation","%(arch)s.hhc","%(arch)s.hhk",\
54"index.html","index.html",,,,,0x63520,220,0x384e,[271,372,740,718],,,,,,,0
Tim Peters52cfa332002-04-19 16:09:26 +000055
56[FILES]
57'''
58
59contents_header = '''
60<OBJECT type="text/site properties">
61 <param name="Window Styles" value="0x801227">
Tim Petersa9053632002-04-19 16:46:43 +000062 <param name="ImageType" value="Folder">
Tim Peters52cfa332002-04-19 16:09:26 +000063</OBJECT>
64<UL>
65<LI> <OBJECT type="text/sitemap">
66 <param name="Name" value="Python %s Docs">
67 <param name="Local" value="./index.html">
68 </OBJECT>
69<UL>
70'''
71
72contents_footer = '''
73</UL></UL>
74'''
75
76object_sitemap = '''
77 <LI> <OBJECT type="text/sitemap">
78 <param name="Local" value="%s">
79 <param name="Name" value="%s">
80 </OBJECT>
81'''
82
83# Library Doc list of tuples:
84# each 'book' : ( Dir, Title, First page, Content page, Index page)
85#
86supported_libraries = {
87 '2.2': ### Beta!!! fix for actual release
88 [
89 ('.', 'Global Module Index', 'modindex.html', None, None),
90 ('tut','Tutorial','tut.html','node2.html',None),
91 ('lib','Library Reference','lib.html','contents.html','genindex.html'),
92 ('ref','Language Reference','ref.html','contents.html','genindex.html'),
93 ('mac','Macintosh Reference','mac.html','contents.html','genindex.html'),
94 ('ext','Extending and Embedding','ext.html','contents.html',None),
95 ('api','Python/C API','api.html','contents.html','genindex.html'),
96 ('doc','Documenting Python','doc.html','contents.html',None),
97 ('inst','Installing Python Modules', 'inst.html', 'index.html', None),
98 ('dist','Distributing Python Modules', 'dist.html', 'index.html', None),
99 ],
100
101 '2.1.1':
102 [
103 ('.', 'Global Module Index', 'modindex.html', None, None),
104 ('tut','Tutorial','tut.html','node2.html',None),
105 ('lib','Library Reference','lib.html','contents.html','genindex.html'),
106 ('ref','Language Reference','ref.html','contents.html','genindex.html'),
107 ('mac','Macintosh Reference','mac.html','contents.html','genindex.html'),
108 ('ext','Extending and Embedding','ext.html','contents.html',None),
109 ('api','Python/C API','api.html','contents.html','genindex.html'),
110 ('doc','Documenting Python','doc.html','contents.html',None),
111 ('inst','Installing Python Modules', 'inst.html', 'index.html', None),
112 ('dist','Distributing Python Modules', 'dist.html', 'index.html', None),
113 ],
114
115 '2.0.0':
116 [
117 ('.', 'Global Module Index', 'modindex.html', None, None),
118 ('tut','Tutorial','tut.html','node2.html',None),
119 ('lib','Library Reference','lib.html','contents.html','genindex.html'),
120 ('ref','Language Reference','ref.html','contents.html','genindex.html'),
121 ('mac','Macintosh Reference','mac.html','contents.html','genindex.html'),
122 ('ext','Extending and Embedding','ext.html','contents.html',None),
123 ('api','Python/C API','api.html','contents.html','genindex.html'),
124 ('doc','Documenting Python','doc.html','contents.html',None),
125 ('inst','Installing Python Modules', 'inst.html', 'contents.html', None),
126 ('dist','Distributing Python Modules', 'dist.html', 'contents.html', None),
127 ],
128
129 # <dnagata@creo.com> Apr 17/99: library for 1.5.2 version:
130 # <hernan.foffani@iname.com> May 01/99: library for 1.5.2 (04/30/99):
131 '1.5.2':
132 [
133 ('tut','Tutorial','tut.html','node2.html',None),
134 ('lib','Library Reference','lib.html','contents.html','genindex.html'),
135 ('ref','Language Reference','ref.html','contents.html','genindex.html'),
136 ('mac','Macintosh Reference','mac.html','contents.html','genindex.html'),
137 ('ext','Extending and Embedding','ext.html','contents.html',None),
138 ('api','Python/C API','api.html','contents.html','genindex.html'),
139 ('doc','Documenting Python','doc.html','contents.html',None)
140 ],
141
142 # library for 1.5.1 version:
143 '1.5.1':
144 [
145 ('tut','Tutorial','tut.html','contents.html',None),
146 ('lib','Library Reference','lib.html','contents.html','genindex.html'),
147 ('ref','Language Reference','ref-1.html','ref-2.html','ref-11.html'),
148 ('ext','Extending and Embedding','ext.html','contents.html',None),
149 ('api','Python/C API','api.html','contents.html','genindex.html')
150 ],
151
152 # library for 1.5 version:
153 '1.5':
154 [
155 ('tut','Tutorial','tut.html','node1.html',None),
156 ('lib','Library Reference','lib.html','node1.html','node268.html'),
157 ('ref','Language Reference','ref-1.html','ref-2.html','ref-11.html'),
158 ('ext','Extending and Embedding','ext.html','node1.html',None),
159 ('api','Python/C API','api.html','node1.html','node48.html')
160 ]
161}
162
163class AlmostNullWriter(formatter.NullWriter) :
164 savedliteral = ''
165
166 def send_flowing_data(self, data) :
167 # need the text tag for later
168 datastriped = string.strip(data)
169 if self.savedliteral == '':
170 self.savedliteral = datastriped
171 else:
172 self.savedliteral = string.strip(self.savedliteral +
173 ' ' + datastriped)
174
175
176class HelpHtmlParser(htmllib.HTMLParser) :
177 indent = 0 # number of tabs for pritty printing of files
178 ft = None # output file
179 path = None # relative path
180 proc = 0 # if true I process, if false I skip
181 # (some headers, footers, etc.)
182
183 def begin_group(self) :
184 if not self.proc :
185 # first level, start processing
186 self.proc = 1
187 self.indent = self.indent + 1
188
189 def finnish_group(self) :
190 self.indent = self.indent - 1
191 if self.proc and self.indent == 0 :
192 # if processing and back to root, then stop
193 self.proc = 0
194
195 def anchor_bgn(self, href, name, type) :
196 if self.proc :
197 self.formatter.writer.savedliteral = ''
198 self.ft.write('<OBJECT type="text/sitemap">\n')
199 self.ft.write('\t' * self.indent + \
200 '\t<param name="Local" value="' + self.path + \
201 '/' + href + '">\n')
202
203 def anchor_end(self) :
204 if self.proc :
205 self.ft.write('\t' * self.indent + \
206 '\t<param name="Name" value="' + \
207 self.formatter.writer.savedliteral + '">\n')
208 self.ft.write('\t' * self.indent + '\t</OBJECT>\n' )
209
210 def start_dl(self, atr_val) :
211 self.begin_group()
212
213 def end_dl(self) :
214 self.finnish_group()
215
216 def do_dt(self, atr_val) :
217 # no trailing newline on pourpose!
218 self.ft.write("\t" * self.indent + "<LI>")
219
220
221class IdxHlpHtmlParser(HelpHtmlParser) :
222 # nothing special here, seems enough with parent class
223 pass
224
225class TocHlpHtmlParser(HelpHtmlParser) :
226
227 def start_dl(self, atr_val) :
228 self.begin_group()
229 self.ft.write('\t' * self.indent + '<UL>\n')
230
231 def end_dl(self) :
232 self.finnish_group()
233 self.ft.write('</UL>\n')
234
235 def start_ul(self, atr_val) :
236 self.begin_group()
237 self.ft.write('\t' * self.indent + '<UL>\n')
238
239 def end_ul(self) :
240 self.finnish_group()
241 self.ft.write('</UL>\n')
242
243 def do_li(self, atr_val) :
244 # no trailing newline on pourpose!
245 self.ft.write("\t" * self.indent + "<LI>")
246
247
248def index(path, archivo, output) :
249 f = formatter.AbstractFormatter(AlmostNullWriter())
250 parser = IdxHlpHtmlParser(f)
251 parser.path = path
252 parser.ft = output
253 fil = path + '/' + archivo
254 parser.feed(open(fil).read())
255 parser.close()
256
257
258def content(path, archivo, output) :
259 f = formatter.AbstractFormatter(AlmostNullWriter())
260 parser = TocHlpHtmlParser(f)
261 parser.path = path
262 parser.ft = output
263 fil = path + '/' + archivo
264 parser.feed(open(fil).read())
265 parser.close()
266
267
268def do_index(library, output) :
269 output.write('<UL>\n')
270 for book in library :
271 print '\t', book[2]
272 if book[4] :
273 index(book[0], book[4], output)
274 output.write('</UL>\n')
275
276
277def do_content(library, version, output) :
278 output.write(contents_header % version)
279 for book in library :
280 print '\t', book[2]
281 output.write(object_sitemap % (book[0]+"/"+book[2], book[1]))
282 if book[3] :
283 content(book[0], book[3], output)
284 output.write(contents_footer)
285
286
287def do_project( library, output, arch, version) :
Tim Peterse6b63e62002-04-19 18:07:52 +0000288 output.write(project_template % locals())
Tim Peters52cfa332002-04-19 16:09:26 +0000289 for book in library :
290 for page in os.listdir(book[0]) :
291 if page[string.rfind(page, '.'):] == '.html' or \
292 page[string.rfind(page, '.'):] == '.css':
293 output.write(book[0]+ '\\' + page + '\n')
294
295
296def openfile(file) :
297 try :
298 p = open(file, "w")
299 except IOError, msg :
300 print file, ":", msg
301 sys.exit(1)
302 return p
303
304def usage() :
305 print usage_mode
306 sys.exit(0)
307
308
309
310def do_it(args = None) :
311 if not args :
312 args = sys.argv[1:]
313
314 if not args :
315 usage()
316
317 try :
318 optlist, args = getopt.getopt(args, 'ckpv:')
319 except getopt.error, msg :
320 print msg
321 usage()
322
323 if not args or len(args) > 1 :
324 usage()
325 arch = args[0]
326
327 version = None
328 for opt in optlist:
329 if opt[0] == '-v':
330 version = opt[1]
331 break
332 if not version:
333 usage()
334
335 library = supported_libraries[ version ]
336
337 if not (('-p','') in optlist) :
338 f = openfile(arch + '.hhp')
339 print "Building Project..."
340 do_project(library, f, arch, version)
341 if version == '2.0.0':
342 for image in os.listdir('icons'):
343 f.write('icons'+ '\\' + image + '\n')
344
345 f.close()
346
347 if not (('-c','') in optlist) :
348 f = openfile(arch + '.hhc')
349 print "Building Table of Content..."
350 do_content(library, version, f)
351 f.close()
352
353 if not (('-k','') in optlist) :
354 f = openfile(arch + '.hhk')
355 print "Building Index..."
356 do_index(library, f)
357 f.close()
358
359if __name__ == '__main__' :
360 do_it()
361
362