Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 1 | ''' |
| 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 | |
| 18 | import sys |
| 19 | import os |
| 20 | import formatter |
| 21 | import htmllib |
| 22 | import string |
| 23 | import getopt |
| 24 | |
| 25 | |
| 26 | |
| 27 | # moved all the triple_quote up here because my syntax-coloring editor |
| 28 | # sucks a little bit. |
| 29 | usage_mode = ''' |
| 30 | Usage: make_chm.py [-c] [-k] [-p] [-v 1.5[.x]] filename |
| 31 | -c: does not build filename.hhc (Table of Contents) |
| 32 | -k: does not build filename.hhk (Index) |
| 33 | -p: does not build filename.hhp (Project File) |
| 34 | -v 1.5[.x]: makes help for the python 1.5[.x] docs |
| 35 | (default is python 1.5.2 docs) |
| 36 | ''' |
| 37 | |
| 38 | # project file (*.hhp) template. there are seven %s |
| 39 | project_template = ''' |
| 40 | [OPTIONS] |
| 41 | Compatibility=1.1 |
| 42 | Compiled file=%s.chm |
| 43 | Contents file=%s.hhc |
| 44 | Default Window=%s |
| 45 | Default topic=index.html |
| 46 | Display compile progress=No |
| 47 | Full-text search=Yes |
| 48 | Index file=%s.hhk |
| 49 | Language=0x409 |
| 50 | Title=Python %s Documentation |
| 51 | |
| 52 | [WINDOWS] |
| 53 | %s="Python %s Documentation","%s.hhc","%s.hhk","index.html","index.html",\ |
Tim Peters | a905363 | 2002-04-19 16:46:43 +0000 | [diff] [blame^] | 54 | ,,,,0x63520,220,0x384e,[271,372,740,718],,,,,,,0 |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 55 | |
| 56 | [FILES] |
| 57 | ''' |
| 58 | |
| 59 | contents_header = ''' |
| 60 | <OBJECT type="text/site properties"> |
| 61 | <param name="Window Styles" value="0x801227"> |
Tim Peters | a905363 | 2002-04-19 16:46:43 +0000 | [diff] [blame^] | 62 | <param name="ImageType" value="Folder"> |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 63 | </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 | |
| 72 | contents_footer = ''' |
| 73 | </UL></UL> |
| 74 | ''' |
| 75 | |
| 76 | object_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 | # |
| 86 | supported_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 | |
| 163 | class 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 | |
| 176 | class 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 | |
| 221 | class IdxHlpHtmlParser(HelpHtmlParser) : |
| 222 | # nothing special here, seems enough with parent class |
| 223 | pass |
| 224 | |
| 225 | class 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 | |
| 248 | def 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 | |
| 258 | def 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 | |
| 268 | def 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 | |
| 277 | def 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 | |
| 287 | def do_project( library, output, arch, version) : |
| 288 | output.write( project_template % \ |
| 289 | (arch, arch, arch, arch, version, arch, version, arch, arch) ) |
| 290 | for book in library : |
| 291 | for page in os.listdir(book[0]) : |
| 292 | if page[string.rfind(page, '.'):] == '.html' or \ |
| 293 | page[string.rfind(page, '.'):] == '.css': |
| 294 | output.write(book[0]+ '\\' + page + '\n') |
| 295 | |
| 296 | |
| 297 | def openfile(file) : |
| 298 | try : |
| 299 | p = open(file, "w") |
| 300 | except IOError, msg : |
| 301 | print file, ":", msg |
| 302 | sys.exit(1) |
| 303 | return p |
| 304 | |
| 305 | def usage() : |
| 306 | print usage_mode |
| 307 | sys.exit(0) |
| 308 | |
| 309 | |
| 310 | |
| 311 | def do_it(args = None) : |
| 312 | if not args : |
| 313 | args = sys.argv[1:] |
| 314 | |
| 315 | if not args : |
| 316 | usage() |
| 317 | |
| 318 | try : |
| 319 | optlist, args = getopt.getopt(args, 'ckpv:') |
| 320 | except getopt.error, msg : |
| 321 | print msg |
| 322 | usage() |
| 323 | |
| 324 | if not args or len(args) > 1 : |
| 325 | usage() |
| 326 | arch = args[0] |
| 327 | |
| 328 | version = None |
| 329 | for opt in optlist: |
| 330 | if opt[0] == '-v': |
| 331 | version = opt[1] |
| 332 | break |
| 333 | if not version: |
| 334 | usage() |
| 335 | |
| 336 | library = supported_libraries[ version ] |
| 337 | |
| 338 | if not (('-p','') in optlist) : |
| 339 | f = openfile(arch + '.hhp') |
| 340 | print "Building Project..." |
| 341 | do_project(library, f, arch, version) |
| 342 | if version == '2.0.0': |
| 343 | for image in os.listdir('icons'): |
| 344 | f.write('icons'+ '\\' + image + '\n') |
| 345 | |
| 346 | f.close() |
| 347 | |
| 348 | if not (('-c','') in optlist) : |
| 349 | f = openfile(arch + '.hhc') |
| 350 | print "Building Table of Content..." |
| 351 | do_content(library, version, f) |
| 352 | f.close() |
| 353 | |
| 354 | if not (('-k','') in optlist) : |
| 355 | f = openfile(arch + '.hhk') |
| 356 | print "Building Index..." |
| 357 | do_index(library, f) |
| 358 | f.close() |
| 359 | |
| 360 | if __name__ == '__main__' : |
| 361 | do_it() |
| 362 | |
| 363 | |