Tim Peters | 4f109c1 | 2002-04-19 18:41:46 +0000 | [diff] [blame] | 1 | """ |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 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/>. |
Tim Peters | 4f109c1 | 2002-04-19 18:41:46 +0000 | [diff] [blame] | 16 | """ |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 17 | |
| 18 | import sys |
| 19 | import os |
| 20 | import formatter |
| 21 | import htmllib |
| 22 | import string |
| 23 | import getopt |
| 24 | |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 25 | usage_mode = ''' |
| 26 | Usage: 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 Peters | e6b63e6 | 2002-04-19 18:07:52 +0000 | [diff] [blame] | 34 | # 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 Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 39 | project_template = ''' |
| 40 | [OPTIONS] |
Tim Peters | e6b63e6 | 2002-04-19 18:07:52 +0000 | [diff] [blame] | 41 | Compiled file=%(arch)s.chm |
| 42 | Contents file=%(arch)s.hhc |
| 43 | Default Window=%(arch)s |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 44 | Default topic=index.html |
| 45 | Display compile progress=No |
Tim Peters | 4f109c1 | 2002-04-19 18:41:46 +0000 | [diff] [blame] | 46 | Full text search stop list file=%(arch)s.stp |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 47 | Full-text search=Yes |
Tim Peters | e6b63e6 | 2002-04-19 18:07:52 +0000 | [diff] [blame] | 48 | Index file=%(arch)s.hhk |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 49 | Language=0x409 |
Tim Peters | e6b63e6 | 2002-04-19 18:07:52 +0000 | [diff] [blame] | 50 | Title=Python %(version)s Documentation |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 51 | |
| 52 | [WINDOWS] |
Tim Peters | e6b63e6 | 2002-04-19 18:07:52 +0000 | [diff] [blame] | 53 | %(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 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 | |
Tim Peters | 4f109c1 | 2002-04-19 18:41:46 +0000 | [diff] [blame] | 83 | |
| 84 | # List of words the full text search facility shouldn't index. This |
| 85 | # becomes file ARCH.stp. Note that this list must be pretty small! |
| 86 | # Different versions of the MS docs claim the file has a maximum size of |
| 87 | # 256 or 512 bytes (including \r\n at the end of each line). |
| 88 | # Note that "and", "or", "not" and "near" are operators in the search |
| 89 | # language, so not point indexing them even if wanted to. |
| 90 | stop_list = ''' |
| 91 | a an and |
| 92 | is |
| 93 | near |
| 94 | not |
| 95 | of |
| 96 | or |
| 97 | the |
| 98 | ''' |
| 99 | |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 100 | # Library Doc list of tuples: |
| 101 | # each 'book' : ( Dir, Title, First page, Content page, Index page) |
| 102 | # |
| 103 | supported_libraries = { |
| 104 | '2.2': ### Beta!!! fix for actual release |
| 105 | [ |
| 106 | ('.', 'Global Module Index', 'modindex.html', None, None), |
Tim Peters | 5d5e193 | 2002-04-20 02:07:58 +0000 | [diff] [blame] | 107 | ('whatsnew', "What's New", 'index.html', 'contents.html', None), |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 108 | ('tut','Tutorial','tut.html','node2.html',None), |
| 109 | ('lib','Library Reference','lib.html','contents.html','genindex.html'), |
| 110 | ('ref','Language Reference','ref.html','contents.html','genindex.html'), |
| 111 | ('mac','Macintosh Reference','mac.html','contents.html','genindex.html'), |
| 112 | ('ext','Extending and Embedding','ext.html','contents.html',None), |
| 113 | ('api','Python/C API','api.html','contents.html','genindex.html'), |
| 114 | ('doc','Documenting Python','doc.html','contents.html',None), |
| 115 | ('inst','Installing Python Modules', 'inst.html', 'index.html', None), |
| 116 | ('dist','Distributing Python Modules', 'dist.html', 'index.html', None), |
| 117 | ], |
| 118 | |
| 119 | '2.1.1': |
| 120 | [ |
| 121 | ('.', 'Global Module Index', 'modindex.html', None, None), |
| 122 | ('tut','Tutorial','tut.html','node2.html',None), |
| 123 | ('lib','Library Reference','lib.html','contents.html','genindex.html'), |
| 124 | ('ref','Language Reference','ref.html','contents.html','genindex.html'), |
| 125 | ('mac','Macintosh Reference','mac.html','contents.html','genindex.html'), |
| 126 | ('ext','Extending and Embedding','ext.html','contents.html',None), |
| 127 | ('api','Python/C API','api.html','contents.html','genindex.html'), |
| 128 | ('doc','Documenting Python','doc.html','contents.html',None), |
| 129 | ('inst','Installing Python Modules', 'inst.html', 'index.html', None), |
| 130 | ('dist','Distributing Python Modules', 'dist.html', 'index.html', None), |
| 131 | ], |
| 132 | |
| 133 | '2.0.0': |
| 134 | [ |
| 135 | ('.', 'Global Module Index', 'modindex.html', None, None), |
| 136 | ('tut','Tutorial','tut.html','node2.html',None), |
| 137 | ('lib','Library Reference','lib.html','contents.html','genindex.html'), |
| 138 | ('ref','Language Reference','ref.html','contents.html','genindex.html'), |
| 139 | ('mac','Macintosh Reference','mac.html','contents.html','genindex.html'), |
| 140 | ('ext','Extending and Embedding','ext.html','contents.html',None), |
| 141 | ('api','Python/C API','api.html','contents.html','genindex.html'), |
| 142 | ('doc','Documenting Python','doc.html','contents.html',None), |
| 143 | ('inst','Installing Python Modules', 'inst.html', 'contents.html', None), |
| 144 | ('dist','Distributing Python Modules', 'dist.html', 'contents.html', None), |
| 145 | ], |
| 146 | |
| 147 | # <dnagata@creo.com> Apr 17/99: library for 1.5.2 version: |
| 148 | # <hernan.foffani@iname.com> May 01/99: library for 1.5.2 (04/30/99): |
| 149 | '1.5.2': |
| 150 | [ |
| 151 | ('tut','Tutorial','tut.html','node2.html',None), |
| 152 | ('lib','Library Reference','lib.html','contents.html','genindex.html'), |
| 153 | ('ref','Language Reference','ref.html','contents.html','genindex.html'), |
| 154 | ('mac','Macintosh Reference','mac.html','contents.html','genindex.html'), |
| 155 | ('ext','Extending and Embedding','ext.html','contents.html',None), |
| 156 | ('api','Python/C API','api.html','contents.html','genindex.html'), |
| 157 | ('doc','Documenting Python','doc.html','contents.html',None) |
| 158 | ], |
| 159 | |
| 160 | # library for 1.5.1 version: |
| 161 | '1.5.1': |
| 162 | [ |
| 163 | ('tut','Tutorial','tut.html','contents.html',None), |
| 164 | ('lib','Library Reference','lib.html','contents.html','genindex.html'), |
| 165 | ('ref','Language Reference','ref-1.html','ref-2.html','ref-11.html'), |
| 166 | ('ext','Extending and Embedding','ext.html','contents.html',None), |
| 167 | ('api','Python/C API','api.html','contents.html','genindex.html') |
| 168 | ], |
| 169 | |
| 170 | # library for 1.5 version: |
| 171 | '1.5': |
| 172 | [ |
| 173 | ('tut','Tutorial','tut.html','node1.html',None), |
| 174 | ('lib','Library Reference','lib.html','node1.html','node268.html'), |
| 175 | ('ref','Language Reference','ref-1.html','ref-2.html','ref-11.html'), |
| 176 | ('ext','Extending and Embedding','ext.html','node1.html',None), |
| 177 | ('api','Python/C API','api.html','node1.html','node48.html') |
| 178 | ] |
| 179 | } |
| 180 | |
| 181 | class AlmostNullWriter(formatter.NullWriter) : |
| 182 | savedliteral = '' |
| 183 | |
| 184 | def send_flowing_data(self, data) : |
| 185 | # need the text tag for later |
| 186 | datastriped = string.strip(data) |
| 187 | if self.savedliteral == '': |
| 188 | self.savedliteral = datastriped |
| 189 | else: |
| 190 | self.savedliteral = string.strip(self.savedliteral + |
| 191 | ' ' + datastriped) |
| 192 | |
| 193 | |
| 194 | class HelpHtmlParser(htmllib.HTMLParser) : |
| 195 | indent = 0 # number of tabs for pritty printing of files |
| 196 | ft = None # output file |
| 197 | path = None # relative path |
| 198 | proc = 0 # if true I process, if false I skip |
| 199 | # (some headers, footers, etc.) |
| 200 | |
| 201 | def begin_group(self) : |
| 202 | if not self.proc : |
| 203 | # first level, start processing |
| 204 | self.proc = 1 |
| 205 | self.indent = self.indent + 1 |
| 206 | |
| 207 | def finnish_group(self) : |
| 208 | self.indent = self.indent - 1 |
| 209 | if self.proc and self.indent == 0 : |
| 210 | # if processing and back to root, then stop |
| 211 | self.proc = 0 |
| 212 | |
| 213 | def anchor_bgn(self, href, name, type) : |
| 214 | if self.proc : |
| 215 | self.formatter.writer.savedliteral = '' |
| 216 | self.ft.write('<OBJECT type="text/sitemap">\n') |
| 217 | self.ft.write('\t' * self.indent + \ |
| 218 | '\t<param name="Local" value="' + self.path + \ |
| 219 | '/' + href + '">\n') |
| 220 | |
| 221 | def anchor_end(self) : |
| 222 | if self.proc : |
| 223 | self.ft.write('\t' * self.indent + \ |
| 224 | '\t<param name="Name" value="' + \ |
| 225 | self.formatter.writer.savedliteral + '">\n') |
| 226 | self.ft.write('\t' * self.indent + '\t</OBJECT>\n' ) |
| 227 | |
| 228 | def start_dl(self, atr_val) : |
| 229 | self.begin_group() |
| 230 | |
| 231 | def end_dl(self) : |
| 232 | self.finnish_group() |
| 233 | |
| 234 | def do_dt(self, atr_val) : |
| 235 | # no trailing newline on pourpose! |
| 236 | self.ft.write("\t" * self.indent + "<LI>") |
| 237 | |
| 238 | |
| 239 | class IdxHlpHtmlParser(HelpHtmlParser) : |
| 240 | # nothing special here, seems enough with parent class |
| 241 | pass |
| 242 | |
| 243 | class TocHlpHtmlParser(HelpHtmlParser) : |
| 244 | |
| 245 | def start_dl(self, atr_val) : |
| 246 | self.begin_group() |
| 247 | self.ft.write('\t' * self.indent + '<UL>\n') |
| 248 | |
| 249 | def end_dl(self) : |
| 250 | self.finnish_group() |
| 251 | self.ft.write('</UL>\n') |
| 252 | |
| 253 | def start_ul(self, atr_val) : |
| 254 | self.begin_group() |
| 255 | self.ft.write('\t' * self.indent + '<UL>\n') |
| 256 | |
| 257 | def end_ul(self) : |
| 258 | self.finnish_group() |
| 259 | self.ft.write('</UL>\n') |
| 260 | |
| 261 | def do_li(self, atr_val) : |
| 262 | # no trailing newline on pourpose! |
| 263 | self.ft.write("\t" * self.indent + "<LI>") |
| 264 | |
| 265 | |
| 266 | def index(path, archivo, output) : |
| 267 | f = formatter.AbstractFormatter(AlmostNullWriter()) |
| 268 | parser = IdxHlpHtmlParser(f) |
| 269 | parser.path = path |
| 270 | parser.ft = output |
| 271 | fil = path + '/' + archivo |
| 272 | parser.feed(open(fil).read()) |
| 273 | parser.close() |
| 274 | |
| 275 | |
| 276 | def content(path, archivo, output) : |
| 277 | f = formatter.AbstractFormatter(AlmostNullWriter()) |
| 278 | parser = TocHlpHtmlParser(f) |
| 279 | parser.path = path |
| 280 | parser.ft = output |
| 281 | fil = path + '/' + archivo |
| 282 | parser.feed(open(fil).read()) |
| 283 | parser.close() |
| 284 | |
| 285 | |
| 286 | def do_index(library, output) : |
| 287 | output.write('<UL>\n') |
| 288 | for book in library : |
| 289 | print '\t', book[2] |
| 290 | if book[4] : |
| 291 | index(book[0], book[4], output) |
| 292 | output.write('</UL>\n') |
| 293 | |
| 294 | |
| 295 | def do_content(library, version, output) : |
| 296 | output.write(contents_header % version) |
| 297 | for book in library : |
| 298 | print '\t', book[2] |
| 299 | output.write(object_sitemap % (book[0]+"/"+book[2], book[1])) |
| 300 | if book[3] : |
| 301 | content(book[0], book[3], output) |
| 302 | output.write(contents_footer) |
| 303 | |
Tim Peters | 460643b | 2002-04-20 02:37:07 +0000 | [diff] [blame^] | 304 | # Fill in the [FILES] section of the project (.hhp) file. |
| 305 | # 'library' is the list of directory description tuples from |
| 306 | # supported_libraries for the version of the docs getting generated. |
| 307 | def do_project(library, output, arch, version): |
Tim Peters | e6b63e6 | 2002-04-19 18:07:52 +0000 | [diff] [blame] | 308 | output.write(project_template % locals()) |
Tim Peters | 460643b | 2002-04-20 02:37:07 +0000 | [diff] [blame^] | 309 | for book in library: |
| 310 | directory = book[0] |
| 311 | path = directory + '\\%s\n' |
| 312 | for page in os.listdir(directory): |
| 313 | if page.endswith('.html') or page.endswith('.css'): |
| 314 | output.write(path % page) |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 315 | |
| 316 | |
| 317 | def openfile(file) : |
| 318 | try : |
| 319 | p = open(file, "w") |
| 320 | except IOError, msg : |
| 321 | print file, ":", msg |
| 322 | sys.exit(1) |
| 323 | return p |
| 324 | |
| 325 | def usage() : |
| 326 | print usage_mode |
| 327 | sys.exit(0) |
| 328 | |
| 329 | |
| 330 | |
| 331 | def do_it(args = None) : |
| 332 | if not args : |
| 333 | args = sys.argv[1:] |
| 334 | |
| 335 | if not args : |
| 336 | usage() |
| 337 | |
| 338 | try : |
| 339 | optlist, args = getopt.getopt(args, 'ckpv:') |
| 340 | except getopt.error, msg : |
| 341 | print msg |
| 342 | usage() |
| 343 | |
| 344 | if not args or len(args) > 1 : |
| 345 | usage() |
| 346 | arch = args[0] |
| 347 | |
| 348 | version = None |
| 349 | for opt in optlist: |
| 350 | if opt[0] == '-v': |
| 351 | version = opt[1] |
| 352 | break |
| 353 | if not version: |
| 354 | usage() |
| 355 | |
| 356 | library = supported_libraries[ version ] |
| 357 | |
| 358 | if not (('-p','') in optlist) : |
Tim Peters | 4f109c1 | 2002-04-19 18:41:46 +0000 | [diff] [blame] | 359 | fname = arch + '.stp' |
| 360 | f = openfile(fname) |
| 361 | print "Building stoplist", fname, "..." |
| 362 | words = stop_list.split() |
| 363 | words.sort() |
| 364 | for word in words: |
| 365 | print >> f, word |
| 366 | f.close() |
| 367 | |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 368 | f = openfile(arch + '.hhp') |
| 369 | print "Building Project..." |
| 370 | do_project(library, f, arch, version) |
| 371 | if version == '2.0.0': |
| 372 | for image in os.listdir('icons'): |
| 373 | f.write('icons'+ '\\' + image + '\n') |
| 374 | |
| 375 | f.close() |
| 376 | |
| 377 | if not (('-c','') in optlist) : |
| 378 | f = openfile(arch + '.hhc') |
| 379 | print "Building Table of Content..." |
| 380 | do_content(library, version, f) |
| 381 | f.close() |
| 382 | |
| 383 | if not (('-k','') in optlist) : |
| 384 | f = openfile(arch + '.hhk') |
| 385 | print "Building Index..." |
| 386 | do_index(library, f) |
| 387 | f.close() |
| 388 | |
| 389 | if __name__ == '__main__' : |
| 390 | do_it() |