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