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 |
Tim Peters | e21095e | 2002-04-20 08:36:42 +0000 | [diff] [blame] | 20 | from formatter import NullWriter, AbstractFormatter |
| 21 | from htmllib import HTMLParser |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 22 | import getopt |
Tim Peters | 4545407 | 2002-04-20 20:26:26 +0000 | [diff] [blame] | 23 | import cgi |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 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 | dc374e0 | 2002-04-21 02:01:01 +0000 | [diff] [blame] | 39 | # About 0x10384e: This defines the buttons in the help viewer. The |
| 40 | # following defns are taken from htmlhelp.h. Not all possibilities |
| 41 | # actually work, and not all those that work are available from the Help |
| 42 | # Workshop GUI. In particular, the Zoom/Font button works and is not |
| 43 | # available from the GUI. The ones we're using are marked with 'x': |
| 44 | # |
| 45 | # 0x000002 Hide/Show x |
| 46 | # 0x000004 Back x |
| 47 | # 0x000008 Forward x |
| 48 | # 0x000010 Stop |
| 49 | # 0x000020 Refresh |
| 50 | # 0x000040 Home x |
| 51 | # 0x000080 Forward |
| 52 | # 0x000100 Back |
| 53 | # 0x000200 Notes |
| 54 | # 0x000400 Contents |
| 55 | # 0x000800 Locate x |
| 56 | # 0x001000 Options x |
| 57 | # 0x002000 Print x |
| 58 | # 0x004000 Index |
| 59 | # 0x008000 Search |
| 60 | # 0x010000 History |
| 61 | # 0x020000 Favorites |
| 62 | # 0x040000 Jump 1 |
| 63 | # 0x080000 Jump 2 |
| 64 | # 0x100000 Zoom/Font x |
| 65 | # 0x200000 TOC Next |
| 66 | # 0x400000 TOC Prev |
| 67 | |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 68 | project_template = ''' |
| 69 | [OPTIONS] |
Tim Peters | e6b63e6 | 2002-04-19 18:07:52 +0000 | [diff] [blame] | 70 | Compiled file=%(arch)s.chm |
| 71 | Contents file=%(arch)s.hhc |
| 72 | Default Window=%(arch)s |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 73 | Default topic=index.html |
| 74 | Display compile progress=No |
Tim Peters | 4f109c1 | 2002-04-19 18:41:46 +0000 | [diff] [blame] | 75 | Full text search stop list file=%(arch)s.stp |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 76 | Full-text search=Yes |
Tim Peters | e6b63e6 | 2002-04-19 18:07:52 +0000 | [diff] [blame] | 77 | Index file=%(arch)s.hhk |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 78 | Language=0x409 |
Tim Peters | e6b63e6 | 2002-04-19 18:07:52 +0000 | [diff] [blame] | 79 | Title=Python %(version)s Documentation |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 80 | |
| 81 | [WINDOWS] |
Tim Peters | e6b63e6 | 2002-04-19 18:07:52 +0000 | [diff] [blame] | 82 | %(arch)s="Python %(version)s Documentation","%(arch)s.hhc","%(arch)s.hhk",\ |
Tim Peters | dc374e0 | 2002-04-21 02:01:01 +0000 | [diff] [blame] | 83 | "index.html","index.html",,,,,0x63520,220,0x10384e,[271,372,740,718],,,,,,,0 |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 84 | |
| 85 | [FILES] |
| 86 | ''' |
| 87 | |
Tim Peters | 4545407 | 2002-04-20 20:26:26 +0000 | [diff] [blame] | 88 | contents_header = '''\ |
| 89 | <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> |
| 90 | <HTML> |
| 91 | <HEAD> |
| 92 | <meta name="GENERATOR" content="Microsoft® HTML Help Workshop 4.1"> |
| 93 | <!-- Sitemap 1.0 --> |
| 94 | </HEAD><BODY> |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 95 | <OBJECT type="text/site properties"> |
| 96 | <param name="Window Styles" value="0x801227"> |
Tim Peters | a905363 | 2002-04-19 16:46:43 +0000 | [diff] [blame] | 97 | <param name="ImageType" value="Folder"> |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 98 | </OBJECT> |
| 99 | <UL> |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 100 | ''' |
| 101 | |
Tim Peters | 4545407 | 2002-04-20 20:26:26 +0000 | [diff] [blame] | 102 | contents_footer = '''\ |
Tim Peters | c8490c7 | 2002-04-20 21:34:34 +0000 | [diff] [blame] | 103 | </UL></BODY></HTML> |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 104 | ''' |
| 105 | |
Tim Peters | 4545407 | 2002-04-20 20:26:26 +0000 | [diff] [blame] | 106 | object_sitemap = '''\ |
| 107 | <OBJECT type="text/sitemap"> |
| 108 | <param name="Name" value="%s"> |
| 109 | <param name="Local" value="%s"> |
| 110 | </OBJECT> |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 111 | ''' |
| 112 | |
Tim Peters | 4f109c1 | 2002-04-19 18:41:46 +0000 | [diff] [blame] | 113 | # List of words the full text search facility shouldn't index. This |
| 114 | # becomes file ARCH.stp. Note that this list must be pretty small! |
| 115 | # Different versions of the MS docs claim the file has a maximum size of |
| 116 | # 256 or 512 bytes (including \r\n at the end of each line). |
| 117 | # Note that "and", "or", "not" and "near" are operators in the search |
Tim Peters | 8d62ad7 | 2002-04-20 02:56:20 +0000 | [diff] [blame] | 118 | # language, so no point indexing them even if we wanted to. |
Tim Peters | 4f109c1 | 2002-04-19 18:41:46 +0000 | [diff] [blame] | 119 | stop_list = ''' |
| 120 | a an and |
| 121 | is |
| 122 | near |
| 123 | not |
| 124 | of |
| 125 | or |
| 126 | the |
| 127 | ''' |
| 128 | |
Tim Peters | d9a1050 | 2002-04-20 03:25:02 +0000 | [diff] [blame] | 129 | # s is a string or None. If None or empty, return None. Else tack '.html' |
| 130 | # on to the end, unless it's already there. |
| 131 | def addhtml(s): |
| 132 | if s: |
| 133 | if not s.endswith('.html'): |
| 134 | s += '.html' |
| 135 | return s |
| 136 | |
| 137 | # Convenience class to hold info about "a book" in HTMLHelp terms == a doc |
| 138 | # directory in Python terms. |
| 139 | class Book: |
| 140 | def __init__(self, directory, title, firstpage, |
| 141 | contentpage=None, indexpage=None): |
| 142 | self.directory = directory |
| 143 | self.title = title |
| 144 | self.firstpage = addhtml(firstpage) |
| 145 | self.contentpage = addhtml(contentpage) |
| 146 | self.indexpage = addhtml(indexpage) |
| 147 | |
| 148 | # Library Doc list of books: |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 149 | # each 'book' : (Dir, Title, First page, Content page, Index page) |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 150 | supported_libraries = { |
Tim Peters | c8490c7 | 2002-04-20 21:34:34 +0000 | [diff] [blame] | 151 | '2.2': |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 152 | [ |
Tim Peters | c8490c7 | 2002-04-20 21:34:34 +0000 | [diff] [blame] | 153 | Book('.', 'Main page', 'index'), |
Tim Peters | d9a1050 | 2002-04-20 03:25:02 +0000 | [diff] [blame] | 154 | Book('.', 'Global Module Index', 'modindex'), |
| 155 | Book('whatsnew', "What's New", 'index', 'contents'), |
| 156 | Book('tut','Tutorial','tut','node2'), |
| 157 | Book('lib','Library Reference','lib','contents','genindex'), |
| 158 | Book('ref','Language Reference','ref','contents','genindex'), |
| 159 | Book('mac','Macintosh Reference','mac','contents','genindex'), |
| 160 | Book('ext','Extending and Embedding','ext','contents'), |
| 161 | Book('api','Python/C API','api','contents','genindex'), |
| 162 | Book('doc','Documenting Python','doc','contents'), |
| 163 | Book('inst','Installing Python Modules', 'inst', 'index'), |
| 164 | Book('dist','Distributing Python Modules', 'dist', 'index'), |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 165 | ], |
| 166 | |
| 167 | '2.1.1': |
| 168 | [ |
Tim Peters | c8490c7 | 2002-04-20 21:34:34 +0000 | [diff] [blame] | 169 | Book('.', 'Main page', 'index'), |
Tim Peters | d9a1050 | 2002-04-20 03:25:02 +0000 | [diff] [blame] | 170 | Book('.', 'Global Module Index', 'modindex'), |
| 171 | Book('tut','Tutorial','tut','node2'), |
| 172 | Book('lib','Library Reference','lib','contents','genindex'), |
| 173 | Book('ref','Language Reference','ref','contents','genindex'), |
| 174 | Book('mac','Macintosh Reference','mac','contents','genindex'), |
| 175 | Book('ext','Extending and Embedding','ext','contents'), |
| 176 | Book('api','Python/C API','api','contents','genindex'), |
| 177 | Book('doc','Documenting Python','doc','contents'), |
| 178 | Book('inst','Installing Python Modules', 'inst', 'index'), |
| 179 | Book('dist','Distributing Python Modules', 'dist', 'index'), |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 180 | ], |
| 181 | |
| 182 | '2.0.0': |
| 183 | [ |
Tim Peters | d9a1050 | 2002-04-20 03:25:02 +0000 | [diff] [blame] | 184 | Book('.', 'Global Module Index', 'modindex'), |
| 185 | Book('tut','Tutorial','tut','node2'), |
| 186 | Book('lib','Library Reference','lib','contents','genindex'), |
| 187 | Book('ref','Language Reference','ref','contents','genindex'), |
| 188 | Book('mac','Macintosh Reference','mac','contents','genindex'), |
| 189 | Book('ext','Extending and Embedding','ext','contents'), |
| 190 | Book('api','Python/C API','api','contents','genindex'), |
| 191 | Book('doc','Documenting Python','doc','contents'), |
| 192 | Book('inst','Installing Python Modules', 'inst', 'contents'), |
| 193 | Book('dist','Distributing Python Modules', 'dist', 'contents'), |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 194 | ], |
| 195 | |
| 196 | # <dnagata@creo.com> Apr 17/99: library for 1.5.2 version: |
| 197 | # <hernan.foffani@iname.com> May 01/99: library for 1.5.2 (04/30/99): |
| 198 | '1.5.2': |
| 199 | [ |
Tim Peters | d9a1050 | 2002-04-20 03:25:02 +0000 | [diff] [blame] | 200 | Book('tut','Tutorial','tut','node2'), |
| 201 | Book('lib','Library Reference','lib','contents','genindex'), |
| 202 | Book('ref','Language Reference','ref','contents','genindex'), |
| 203 | Book('mac','Macintosh Reference','mac','contents','genindex'), |
| 204 | Book('ext','Extending and Embedding','ext','contents'), |
| 205 | Book('api','Python/C API','api','contents','genindex'), |
| 206 | Book('doc','Documenting Python','doc','contents') |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 207 | ], |
| 208 | |
| 209 | # library for 1.5.1 version: |
| 210 | '1.5.1': |
| 211 | [ |
Tim Peters | d9a1050 | 2002-04-20 03:25:02 +0000 | [diff] [blame] | 212 | Book('tut','Tutorial','tut','contents'), |
| 213 | Book('lib','Library Reference','lib','contents','genindex'), |
| 214 | Book('ref','Language Reference','ref-1','ref-2','ref-11'), |
| 215 | Book('ext','Extending and Embedding','ext','contents'), |
| 216 | Book('api','Python/C API','api','contents','genindex') |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 217 | ], |
| 218 | |
| 219 | # library for 1.5 version: |
| 220 | '1.5': |
| 221 | [ |
Tim Peters | d9a1050 | 2002-04-20 03:25:02 +0000 | [diff] [blame] | 222 | Book('tut','Tutorial','tut','node1'), |
| 223 | Book('lib','Library Reference','lib','node1','node268'), |
| 224 | Book('ref','Language Reference','ref-1','ref-2','ref-11'), |
| 225 | Book('ext','Extending and Embedding','ext','node1'), |
| 226 | Book('api','Python/C API','api','node1','node48') |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 227 | ] |
| 228 | } |
| 229 | |
Tim Peters | e21095e | 2002-04-20 08:36:42 +0000 | [diff] [blame] | 230 | # AlmostNullWriter doesn't print anything; it just arranges to save the |
| 231 | # text sent to send_flowing_data(). This is used to capture the text |
| 232 | # between an anchor begin/end pair, e.g. for TOC entries. |
| 233 | |
| 234 | class AlmostNullWriter(NullWriter): |
| 235 | |
| 236 | def __init__(self): |
| 237 | NullWriter.__init__(self) |
| 238 | self.saved_clear() |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 239 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 240 | def send_flowing_data(self, data): |
Tim Peters | e21095e | 2002-04-20 08:36:42 +0000 | [diff] [blame] | 241 | stripped = data.strip() |
| 242 | if stripped: # don't bother to save runs of whitespace |
| 243 | self.saved.append(stripped) |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 244 | |
Tim Peters | e21095e | 2002-04-20 08:36:42 +0000 | [diff] [blame] | 245 | # Forget all saved text. |
| 246 | def saved_clear(self): |
| 247 | self.saved = [] |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 248 | |
Tim Peters | e21095e | 2002-04-20 08:36:42 +0000 | [diff] [blame] | 249 | # Return all saved text as a string. |
| 250 | def saved_get(self): |
| 251 | return ' '.join(self.saved) |
| 252 | |
| 253 | class HelpHtmlParser(HTMLParser): |
| 254 | |
| 255 | def __init__(self, formatter, path, output): |
| 256 | HTMLParser.__init__(self, formatter) |
| 257 | self.path = path # relative path |
| 258 | self.ft = output # output file |
| 259 | self.indent = 0 # number of tabs for pretty printing of files |
| 260 | self.proc = False # True when actively processing, else False |
| 261 | # (headers, footers, etc) |
Tim Peters | 4545407 | 2002-04-20 20:26:26 +0000 | [diff] [blame] | 262 | # XXX This shouldn't need to be a stack -- anchors shouldn't nest. |
| 263 | # XXX See SF bug <http://www.python.org/sf/546579>. |
| 264 | self.hrefstack = [] # stack of hrefs from anchor begins |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 265 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 266 | def begin_group(self): |
Tim Peters | e21095e | 2002-04-20 08:36:42 +0000 | [diff] [blame] | 267 | self.indent += 1 |
| 268 | self.proc = True |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 269 | |
Tim Peters | e21095e | 2002-04-20 08:36:42 +0000 | [diff] [blame] | 270 | def finish_group(self): |
| 271 | self.indent -= 1 |
| 272 | # stop processing when back to top level |
| 273 | self.proc = self.indent > 0 |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 274 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 275 | def anchor_bgn(self, href, name, type): |
| 276 | if self.proc: |
Tim Peters | 4545407 | 2002-04-20 20:26:26 +0000 | [diff] [blame] | 277 | # XXX See SF bug <http://www.python.org/sf/546579>. |
Tim Peters | 4a0db06 | 2002-04-21 04:44:11 +0000 | [diff] [blame^] | 278 | # XXX index.html for the 2.2.1 language reference manual contains |
Tim Peters | 4545407 | 2002-04-20 20:26:26 +0000 | [diff] [blame] | 279 | # XXX nested <a></a> tags in the entry for the section on blank |
| 280 | # XXX lines. We want to ignore the nested part completely. |
| 281 | if len(self.hrefstack) == 0: |
Tim Peters | 4a0db06 | 2002-04-21 04:44:11 +0000 | [diff] [blame^] | 282 | self.saved_clear() |
| 283 | self.hrefstack.append(href) |
| 284 | |
| 285 | def anchor_end(self): |
| 286 | if self.proc: |
| 287 | # XXX See XXX above. |
| 288 | if self.hrefstack: |
| 289 | title = cgi.escape(self.saved_get(), True) |
| 290 | path = self.path + '/' + self.hrefstack.pop() |
Tim Peters | 4545407 | 2002-04-20 20:26:26 +0000 | [diff] [blame] | 291 | self.tab(object_sitemap % (title, path)) |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 292 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 293 | def start_dl(self, atr_val): |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 294 | self.begin_group() |
| 295 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 296 | def end_dl(self): |
Tim Peters | e21095e | 2002-04-20 08:36:42 +0000 | [diff] [blame] | 297 | self.finish_group() |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 298 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 299 | def do_dt(self, atr_val): |
Tim Peters | e21095e | 2002-04-20 08:36:42 +0000 | [diff] [blame] | 300 | # no trailing newline on purpose! |
| 301 | self.tab("<LI>") |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 302 | |
Tim Peters | e21095e | 2002-04-20 08:36:42 +0000 | [diff] [blame] | 303 | # Write text to output file. |
| 304 | def write(self, text): |
| 305 | self.ft.write(text) |
| 306 | |
| 307 | # Write text to output file after indenting by self.indent tabs. |
| 308 | def tab(self, text=''): |
| 309 | self.write('\t' * self.indent) |
| 310 | if text: |
| 311 | self.write(text) |
| 312 | |
| 313 | # Forget all saved text. |
| 314 | def saved_clear(self): |
| 315 | self.formatter.writer.saved_clear() |
| 316 | |
| 317 | # Return all saved text as a string. |
| 318 | def saved_get(self): |
| 319 | return self.formatter.writer.saved_get() |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 320 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 321 | class IdxHlpHtmlParser(HelpHtmlParser): |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 322 | # nothing special here, seems enough with parent class |
| 323 | pass |
| 324 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 325 | class TocHlpHtmlParser(HelpHtmlParser): |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 326 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 327 | def start_dl(self, atr_val): |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 328 | self.begin_group() |
Tim Peters | e21095e | 2002-04-20 08:36:42 +0000 | [diff] [blame] | 329 | self.tab('<UL>\n') |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 330 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 331 | def end_dl(self): |
Tim Peters | e21095e | 2002-04-20 08:36:42 +0000 | [diff] [blame] | 332 | self.finish_group() |
| 333 | self.tab('</UL>\n') |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 334 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 335 | def start_ul(self, atr_val): |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 336 | self.begin_group() |
Tim Peters | e21095e | 2002-04-20 08:36:42 +0000 | [diff] [blame] | 337 | self.tab('<UL>\n') |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 338 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 339 | def end_ul(self): |
Tim Peters | e21095e | 2002-04-20 08:36:42 +0000 | [diff] [blame] | 340 | self.finish_group() |
| 341 | self.tab('</UL>\n') |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 342 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 343 | def do_li(self, atr_val): |
Tim Peters | e21095e | 2002-04-20 08:36:42 +0000 | [diff] [blame] | 344 | # no trailing newline on purpose! |
| 345 | self.tab("<LI>") |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 346 | |
Tim Peters | d9a1050 | 2002-04-20 03:25:02 +0000 | [diff] [blame] | 347 | def index(path, indexpage, output): |
Tim Peters | e21095e | 2002-04-20 08:36:42 +0000 | [diff] [blame] | 348 | parser = IdxHlpHtmlParser(AbstractFormatter(AlmostNullWriter()), |
| 349 | path, output) |
Tim Peters | d9a1050 | 2002-04-20 03:25:02 +0000 | [diff] [blame] | 350 | f = open(path + '/' + indexpage) |
| 351 | parser.feed(f.read()) |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 352 | parser.close() |
Tim Peters | d9a1050 | 2002-04-20 03:25:02 +0000 | [diff] [blame] | 353 | f.close() |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 354 | |
Tim Peters | d9a1050 | 2002-04-20 03:25:02 +0000 | [diff] [blame] | 355 | def content(path, contentpage, output): |
Tim Peters | e21095e | 2002-04-20 08:36:42 +0000 | [diff] [blame] | 356 | parser = TocHlpHtmlParser(AbstractFormatter(AlmostNullWriter()), |
| 357 | path, output) |
Tim Peters | d9a1050 | 2002-04-20 03:25:02 +0000 | [diff] [blame] | 358 | f = open(path + '/' + contentpage) |
| 359 | parser.feed(f.read()) |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 360 | parser.close() |
Tim Peters | d9a1050 | 2002-04-20 03:25:02 +0000 | [diff] [blame] | 361 | f.close() |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 362 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 363 | def do_index(library, output): |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 364 | output.write('<UL>\n') |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 365 | for book in library: |
Tim Peters | d9a1050 | 2002-04-20 03:25:02 +0000 | [diff] [blame] | 366 | print '\t', book.title, '-', book.indexpage |
| 367 | if book.indexpage: |
| 368 | index(book.directory, book.indexpage, output) |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 369 | output.write('</UL>\n') |
| 370 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 371 | def do_content(library, version, output): |
Tim Peters | c8490c7 | 2002-04-20 21:34:34 +0000 | [diff] [blame] | 372 | output.write(contents_header) |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 373 | for book in library: |
Tim Peters | d9a1050 | 2002-04-20 03:25:02 +0000 | [diff] [blame] | 374 | print '\t', book.title, '-', book.firstpage |
Tim Peters | 4545407 | 2002-04-20 20:26:26 +0000 | [diff] [blame] | 375 | path = book.directory + "/" + book.firstpage |
| 376 | output.write('<LI>') |
| 377 | output.write(object_sitemap % (book.title, path)) |
Tim Peters | d9a1050 | 2002-04-20 03:25:02 +0000 | [diff] [blame] | 378 | if book.contentpage: |
| 379 | content(book.directory, book.contentpage, output) |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 380 | output.write(contents_footer) |
| 381 | |
Tim Peters | 460643b | 2002-04-20 02:37:07 +0000 | [diff] [blame] | 382 | # Fill in the [FILES] section of the project (.hhp) file. |
| 383 | # 'library' is the list of directory description tuples from |
| 384 | # supported_libraries for the version of the docs getting generated. |
| 385 | def do_project(library, output, arch, version): |
Tim Peters | e6b63e6 | 2002-04-19 18:07:52 +0000 | [diff] [blame] | 386 | output.write(project_template % locals()) |
Tim Peters | c8490c7 | 2002-04-20 21:34:34 +0000 | [diff] [blame] | 387 | pathseen = {} |
Tim Peters | 460643b | 2002-04-20 02:37:07 +0000 | [diff] [blame] | 388 | for book in library: |
Tim Peters | d9a1050 | 2002-04-20 03:25:02 +0000 | [diff] [blame] | 389 | directory = book.directory |
Tim Peters | 460643b | 2002-04-20 02:37:07 +0000 | [diff] [blame] | 390 | path = directory + '\\%s\n' |
| 391 | for page in os.listdir(directory): |
| 392 | if page.endswith('.html') or page.endswith('.css'): |
Tim Peters | c8490c7 | 2002-04-20 21:34:34 +0000 | [diff] [blame] | 393 | fullpath = path % page |
| 394 | if fullpath not in pathseen: |
| 395 | output.write(fullpath) |
| 396 | pathseen[fullpath] = True |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 397 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 398 | def openfile(file): |
| 399 | try: |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 400 | p = open(file, "w") |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 401 | except IOError, msg: |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 402 | print file, ":", msg |
| 403 | sys.exit(1) |
| 404 | return p |
| 405 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 406 | def usage(): |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 407 | print usage_mode |
| 408 | sys.exit(0) |
| 409 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 410 | def do_it(args = None): |
| 411 | if not args: |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 412 | args = sys.argv[1:] |
| 413 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 414 | if not args: |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 415 | usage() |
| 416 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 417 | try: |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 418 | optlist, args = getopt.getopt(args, 'ckpv:') |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 419 | except getopt.error, msg: |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 420 | print msg |
| 421 | usage() |
| 422 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 423 | if not args or len(args) > 1: |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 424 | usage() |
| 425 | arch = args[0] |
| 426 | |
| 427 | version = None |
| 428 | for opt in optlist: |
| 429 | if opt[0] == '-v': |
| 430 | version = opt[1] |
| 431 | break |
| 432 | if not version: |
| 433 | usage() |
| 434 | |
Tim Peters | d9a1050 | 2002-04-20 03:25:02 +0000 | [diff] [blame] | 435 | library = supported_libraries[version] |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 436 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 437 | if not (('-p','') in optlist): |
Tim Peters | 4f109c1 | 2002-04-19 18:41:46 +0000 | [diff] [blame] | 438 | fname = arch + '.stp' |
| 439 | f = openfile(fname) |
| 440 | print "Building stoplist", fname, "..." |
| 441 | words = stop_list.split() |
| 442 | words.sort() |
| 443 | for word in words: |
| 444 | print >> f, word |
| 445 | f.close() |
| 446 | |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 447 | f = openfile(arch + '.hhp') |
| 448 | print "Building Project..." |
| 449 | do_project(library, f, arch, version) |
| 450 | if version == '2.0.0': |
| 451 | for image in os.listdir('icons'): |
| 452 | f.write('icons'+ '\\' + image + '\n') |
| 453 | |
| 454 | f.close() |
| 455 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 456 | if not (('-c','') in optlist): |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 457 | f = openfile(arch + '.hhc') |
| 458 | print "Building Table of Content..." |
| 459 | do_content(library, version, f) |
| 460 | f.close() |
| 461 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 462 | if not (('-k','') in optlist): |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 463 | f = openfile(arch + '.hhk') |
| 464 | print "Building Index..." |
| 465 | do_index(library, f) |
| 466 | f.close() |
| 467 | |
Tim Peters | 661e492 | 2002-04-20 02:39:44 +0000 | [diff] [blame] | 468 | if __name__ == '__main__': |
Tim Peters | 52cfa33 | 2002-04-19 16:09:26 +0000 | [diff] [blame] | 469 | do_it() |