| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python | 
 | 2 | #  -*- Python -*- | 
 | 3 | """usage: %(program)s [options...] file ... | 
 | 4 |  | 
 | 5 | Options specifying formats to build: | 
 | 6 |     --html		HyperText Markup Language | 
 | 7 |     --pdf		Portable Document Format (default) | 
 | 8 |     --ps		PostScript | 
 | 9 |     --dvi		'DeVice Indepentent' format from TeX | 
 | 10 |     --text		ASCII text (requires lynx) | 
 | 11 |  | 
 | 12 |     More than one output format may be specified, or --all. | 
 | 13 |  | 
 | 14 | HTML options: | 
 | 15 |     --address, -a	Specify an address for page footers. | 
 | 16 |     --link		Specify the number of levels to include on each page. | 
 | 17 |     --split, -s		Specify a section level for page splitting, default: %(max_split_depth)s. | 
 | 18 |     --iconserver, -i	Specify location of icons (default: ../). | 
| Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 19 |     --image-type	Specify the image type to use in HTML output; | 
 | 20 |                         values: gif (default), png. | 
| Fred Drake | 9a257b4 | 2000-03-31 20:27:36 +0000 | [diff] [blame] | 21 |     --numeric           Don't rename the HTML files; just keep node#.html for | 
 | 22 |                         the filenames. | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 23 |  | 
 | 24 | Other options: | 
 | 25 |     --a4		Format for A4 paper. | 
 | 26 |     --letter		Format for US letter paper (the default). | 
 | 27 |     --help, -H		Show this text. | 
 | 28 |     --logging, -l	Log stdout and stderr to a file (*.how). | 
 | 29 |     --debugging, -D	Echo commands as they are executed. | 
 | 30 |     --keep, -k		Keep temporary files around. | 
 | 31 |     --quiet, -q		Do not print command output to stdout. | 
 | 32 | 			(stderr is also lost,  sorry; see *.how for errors) | 
 | 33 | """ | 
 | 34 |  | 
 | 35 | import getopt | 
 | 36 | import glob | 
 | 37 | import os | 
| Fred Drake | a871c2e | 1999-05-06 19:37:38 +0000 | [diff] [blame] | 38 | import re | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 39 | import shutil | 
 | 40 | import string | 
 | 41 | import sys | 
 | 42 | import tempfile | 
 | 43 |  | 
 | 44 |  | 
 | 45 | MYDIR = os.path.normpath(os.path.join(os.getcwd(), sys.path[0])) | 
 | 46 | TOPDIR = os.path.normpath(os.path.join(MYDIR, os.pardir)) | 
 | 47 |  | 
 | 48 | ISTFILE = os.path.join(TOPDIR, "texinputs", "python.ist") | 
 | 49 | NODE2LABEL_SCRIPT = os.path.join(MYDIR, "node2label.pl") | 
 | 50 | L2H_INIT_FILE = os.path.join(TOPDIR, "perl", "l2hinit.perl") | 
 | 51 |  | 
 | 52 | BIBTEX_BINARY = "bibtex" | 
 | 53 | DVIPS_BINARY = "dvips" | 
 | 54 | LATEX_BINARY = "latex" | 
 | 55 | LATEX2HTML_BINARY = "latex2html" | 
 | 56 | LYNX_BINARY = "lynx" | 
 | 57 | MAKEINDEX_BINARY = "makeindex" | 
 | 58 | PDFLATEX_BINARY = "pdflatex" | 
 | 59 | PERL_BINARY = "perl" | 
 | 60 | PYTHON_BINARY = "python" | 
 | 61 |  | 
 | 62 |  | 
 | 63 | def usage(options): | 
 | 64 |     print __doc__ % options | 
 | 65 |  | 
 | 66 | def error(options, message, err=2): | 
 | 67 |     sys.stdout = sys.stderr | 
 | 68 |     print message | 
 | 69 |     print | 
 | 70 |     usage(options) | 
 | 71 |     sys.exit(2) | 
 | 72 |  | 
 | 73 |  | 
 | 74 | class Options: | 
 | 75 |     program = os.path.basename(sys.argv[0]) | 
 | 76 |     # | 
 | 77 |     address = '' | 
 | 78 |     debugging = 0 | 
 | 79 |     discard_temps = 1 | 
 | 80 |     have_temps = 0 | 
 | 81 |     icon_server = None | 
| Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 82 |     image_type = "gif" | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 83 |     logging = 0 | 
 | 84 |     max_link_depth = 3 | 
 | 85 |     max_split_depth = 6 | 
 | 86 |     paper = "letter" | 
 | 87 |     quiet = 0 | 
| Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 88 |     runs = 0 | 
| Fred Drake | 9a257b4 | 2000-03-31 20:27:36 +0000 | [diff] [blame] | 89 |     numeric = 0 | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 90 |     style_file = os.path.join(TOPDIR, "html", "style.css") | 
| Fred Drake | cf1b06e | 1999-09-23 16:55:09 +0000 | [diff] [blame] | 91 |     about_file = os.path.join(TOPDIR, "html", "about.dat") | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 92 |     # | 
 | 93 |     DEFAULT_FORMATS = ("pdf",) | 
 | 94 |     ALL_FORMATS = ("dvi", "html", "pdf", "ps", "text") | 
 | 95 |  | 
 | 96 |     def __init__(self): | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 97 |         self.formats = [] | 
 | 98 |  | 
 | 99 |     def __getitem__(self, key): | 
 | 100 |         # This is used when formatting the usage message. | 
 | 101 |         try: | 
 | 102 |             return getattr(self, key) | 
 | 103 |         except AttributeError: | 
 | 104 |             raise KeyError, key | 
 | 105 |  | 
 | 106 |     def parse(self, args): | 
| Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 107 |         opts, args = getopt.getopt(args, "Hi:a:s:lDkqr:", | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 108 |                                    ["all", "postscript", "help", "iconserver=", | 
| Fred Drake | cc7371c | 2000-06-29 23:01:40 +0000 | [diff] [blame] | 109 |                                     "address=", "a4", "letter", | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 110 |                                     "link=", "split=", "logging", "debugging", | 
| Fred Drake | cf1b06e | 1999-09-23 16:55:09 +0000 | [diff] [blame] | 111 |                                     "keep", "quiet", "runs=", "image-type=", | 
| Fred Drake | 9a257b4 | 2000-03-31 20:27:36 +0000 | [diff] [blame] | 112 |                                     "about=", "numeric"] | 
| Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 113 |                                    + list(self.ALL_FORMATS)) | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 114 |         for opt, arg in opts: | 
 | 115 |             if opt == "--all": | 
 | 116 |                 self.formats = list(self.ALL_FORMATS) | 
 | 117 |             elif opt in ("-H", "--help"): | 
 | 118 |                 usage(self) | 
 | 119 |                 sys.exit() | 
 | 120 |             elif opt == "--iconserver": | 
 | 121 |                 self.icon_server = arg | 
 | 122 |             elif opt in ("-a", "--address"): | 
 | 123 |                 self.address = arg | 
 | 124 |             elif opt == "--a4": | 
 | 125 |                 self.paper = "a4" | 
 | 126 |             elif opt == "--letter": | 
 | 127 |                 self.paper = "letter" | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 128 |             elif opt == "--link": | 
 | 129 |                 self.max_link_depth = int(arg) | 
 | 130 |             elif opt in ("-s", "--split"): | 
 | 131 |                 self.max_split_depth = int(arg) | 
 | 132 |             elif opt in ("-l", "--logging"): | 
 | 133 |                 self.logging = self.logging + 1 | 
 | 134 |             elif opt in ("-D", "--debugging"): | 
 | 135 |                 self.debugging = self.debugging + 1 | 
 | 136 |             elif opt in ("-k", "--keep"): | 
 | 137 |                 self.discard_temps = 0 | 
 | 138 |             elif opt in ("-q", "--quiet"): | 
 | 139 |                 self.quiet = 1 | 
| Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 140 |             elif opt in ("-r", "--runs"): | 
 | 141 |                 self.runs = int(arg) | 
 | 142 |             elif opt == "--image-type": | 
 | 143 |                 self.image_type = arg | 
| Fred Drake | cf1b06e | 1999-09-23 16:55:09 +0000 | [diff] [blame] | 144 |             elif opt == "--about": | 
 | 145 |                 # always make this absolute: | 
 | 146 |                 self.about_file = os.path.normpath( | 
 | 147 |                     os.path.join(os.getcwd(), arg)) | 
| Fred Drake | 9a257b4 | 2000-03-31 20:27:36 +0000 | [diff] [blame] | 148 |             elif opt == "--numeric": | 
 | 149 |                 self.numeric = 1 | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 150 |             # | 
 | 151 |             # Format specifiers: | 
 | 152 |             # | 
 | 153 |             elif opt[2:] in self.ALL_FORMATS: | 
 | 154 |                 self.add_format(opt[2:]) | 
 | 155 |             elif opt == "--postscript": | 
 | 156 |                 # synonym for --ps | 
 | 157 |                 self.add_format("ps") | 
 | 158 |         self.initialize() | 
 | 159 |         # | 
 | 160 |         # return the args to allow the caller access: | 
 | 161 |         # | 
 | 162 |         return args | 
 | 163 |  | 
 | 164 |     def add_format(self, format): | 
 | 165 |         """Add a format to the formats list if not present.""" | 
 | 166 |         if not format in self.formats: | 
 | 167 |             self.formats.append(format) | 
 | 168 |  | 
 | 169 |     def initialize(self): | 
 | 170 |         """Complete initialization.  This is needed if parse() isn't used.""" | 
 | 171 |         # add the default format if no formats were specified: | 
 | 172 |         if not self.formats: | 
 | 173 |             self.formats = self.DEFAULT_FORMATS | 
 | 174 |         # determine the base set of texinputs directories: | 
 | 175 |         texinputs = string.split(os.environ.get("TEXINPUTS", ""), os.pathsep) | 
 | 176 |         if not texinputs: | 
 | 177 |             texinputs = [''] | 
 | 178 |         self.base_texinputs = [ | 
 | 179 |             os.path.join(TOPDIR, "paper-" + self.paper), | 
 | 180 |             os.path.join(TOPDIR, "texinputs"), | 
 | 181 |             ] + texinputs | 
 | 182 |  | 
 | 183 |  | 
 | 184 | class Job: | 
| Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 185 |     latex_runs = 0 | 
 | 186 |  | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 187 |     def __init__(self, options, path): | 
 | 188 |         self.options = options | 
| Fred Drake | a871c2e | 1999-05-06 19:37:38 +0000 | [diff] [blame] | 189 |         self.doctype = get_doctype(path) | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 190 |         self.filedir, self.doc = split_pathname(path) | 
 | 191 |         self.log_filename = self.doc + ".how" | 
 | 192 |         if os.path.exists(self.log_filename): | 
 | 193 |             os.unlink(self.log_filename) | 
 | 194 |         if os.path.exists(self.doc + ".l2h"): | 
 | 195 |             self.l2h_aux_init_file = tempfile.mktemp() | 
 | 196 |         else: | 
 | 197 |             self.l2h_aux_init_file = self.doc + ".l2h" | 
 | 198 |         self.write_l2h_aux_init_file() | 
 | 199 |  | 
 | 200 |     def build(self): | 
 | 201 |         self.setup_texinputs() | 
 | 202 |         formats = self.options.formats | 
 | 203 |         if "dvi" in formats or "ps" in formats: | 
 | 204 |             self.build_dvi() | 
 | 205 |         if "pdf" in formats: | 
 | 206 |             self.build_pdf() | 
 | 207 |         if "ps" in formats: | 
 | 208 |             self.build_ps() | 
 | 209 |         if "html" in formats: | 
 | 210 |             self.require_temps() | 
 | 211 |             self.build_html(self.doc) | 
 | 212 |             if self.options.icon_server == ".": | 
| Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 213 |                 pattern = os.path.join(TOPDIR, "html", "icons", | 
 | 214 |                                        "*." + self.options.image_type) | 
 | 215 |                 imgs = glob.glob(pattern) | 
 | 216 |                 if not imgs: | 
 | 217 |                     self.warning( | 
 | 218 |                         "Could not locate support images of type %s." | 
 | 219 |                         % `self.options.image_type`) | 
 | 220 |                 for fn in imgs: | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 221 |                     new_fn = os.path.join(self.doc, os.path.basename(fn)) | 
 | 222 |                     shutil.copyfile(fn, new_fn) | 
 | 223 |         if "text" in formats: | 
 | 224 |             self.require_temps() | 
 | 225 |             tempdir = self.doc | 
 | 226 |             need_html = "html" not in formats | 
 | 227 |             if self.options.max_split_depth != 1: | 
 | 228 |                 fp = open(self.l2h_aux_init_file, "a") | 
 | 229 |                 fp.write("# re-hack this file for --text:\n") | 
 | 230 |                 l2hoption(fp, "MAX_SPLIT_DEPTH", "1") | 
 | 231 |                 fp.write("1;\n") | 
 | 232 |                 fp.close() | 
 | 233 |                 tempdir = self.doc + "-temp-html" | 
 | 234 |                 need_html = 1 | 
 | 235 |             if need_html: | 
 | 236 |                 self.build_html(tempdir, max_split_depth=1) | 
 | 237 |             self.build_text(tempdir) | 
 | 238 |         if self.options.discard_temps: | 
 | 239 |             self.cleanup() | 
 | 240 |  | 
 | 241 |     def setup_texinputs(self): | 
 | 242 |         texinputs = [self.filedir] + list(self.options.base_texinputs) | 
 | 243 |         os.environ["TEXINPUTS"] = string.join(texinputs, os.pathsep) | 
| Fred Drake | aaa0d9a | 1999-03-03 21:57:58 +0000 | [diff] [blame] | 244 |         self.message("TEXINPUTS=" + os.environ["TEXINPUTS"]) | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 245 |  | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 246 |     def build_aux(self, binary=None): | 
 | 247 |         if binary is None: | 
 | 248 |             binary = LATEX_BINARY | 
 | 249 |         new_index(   "%s.ind" % self.doc, "genindex") | 
 | 250 |         new_index("mod%s.ind" % self.doc, "modindex") | 
 | 251 |         self.run("%s %s" % (binary, self.doc)) | 
 | 252 |         self.use_bibtex = check_for_bibtex(self.doc + ".aux") | 
| Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 253 |         self.latex_runs = 1 | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 254 |  | 
 | 255 |     def build_dvi(self): | 
 | 256 |         self.use_latex(LATEX_BINARY) | 
 | 257 |  | 
 | 258 |     def build_pdf(self): | 
 | 259 |         self.use_latex(PDFLATEX_BINARY) | 
 | 260 |  | 
 | 261 |     def use_latex(self, binary): | 
 | 262 |         self.require_temps(binary=binary) | 
 | 263 |         if os.path.isfile("mod%s.idx" % self.doc): | 
 | 264 |             self.run("%s mod%s.idx" % (MAKEINDEX_BINARY, self.doc)) | 
 | 265 |         if os.path.isfile(self.doc + ".idx"): | 
 | 266 |             # call to Doc/tools/fix_hack omitted; doesn't appear necessary | 
 | 267 |             self.run("%s %s.idx" % (MAKEINDEX_BINARY, self.doc)) | 
 | 268 |             import indfix | 
 | 269 |             indfix.process(self.doc + ".ind") | 
 | 270 |         if self.use_bibtex: | 
 | 271 |             self.run("%s %s" % (BIBTEX_BINARY, self.doc)) | 
| Fred Drake | a871c2e | 1999-05-06 19:37:38 +0000 | [diff] [blame] | 272 |         self.process_synopsis_files() | 
 | 273 |         # | 
 | 274 |         # let the doctype-specific handler do some intermediate work: | 
 | 275 |         # | 
 | 276 |         if self.doctype == "manual": | 
 | 277 |             self.use_latex_manual(binary=binary) | 
 | 278 |         elif self.doctype == "howto": | 
 | 279 |             self.use_latex_howto(binary=binary) | 
 | 280 |         else: | 
 | 281 |             raise RuntimeError, "unsupported document type: " + self.doctype | 
 | 282 |         # | 
 | 283 |         # and now finish it off: | 
 | 284 |         # | 
 | 285 |         if os.path.isfile(self.doc + ".toc") and binary == PDFLATEX_BINARY: | 
 | 286 |             import toc2bkm | 
 | 287 |             toc2bkm.process(self.doc + ".toc", self.doc + ".bkm", "section") | 
 | 288 |         if self.use_bibtex: | 
 | 289 |             self.run("%s %s" % (BIBTEX_BINARY, self.doc)) | 
 | 290 |         self.run("%s %s" % (binary, self.doc)) | 
 | 291 |  | 
 | 292 |     def use_latex_howto(self, binary): | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 293 |         self.run("%s %s" % (binary, self.doc)) | 
 | 294 |         if os.path.isfile("mod%s.idx" % self.doc): | 
 | 295 |             self.run("%s -s %s mod%s.idx" | 
 | 296 |                      % (MAKEINDEX_BINARY, ISTFILE, self.doc)) | 
 | 297 |         if os.path.isfile(self.doc + ".idx"): | 
 | 298 |             self.run("%s -s %s %s.idx" % (MAKEINDEX_BINARY, ISTFILE, self.doc)) | 
| Fred Drake | a871c2e | 1999-05-06 19:37:38 +0000 | [diff] [blame] | 299 |         self.process_synopsis_files() | 
 | 300 |  | 
 | 301 |     def use_latex_manual(self, binary): | 
 | 302 |         pass | 
 | 303 |  | 
 | 304 |     def process_synopsis_files(self): | 
 | 305 |         synopsis_files = glob.glob(self.doc + "*.syn") | 
 | 306 |         for path in synopsis_files: | 
 | 307 |             uniqify_module_table(path) | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 308 |  | 
 | 309 |     def build_ps(self): | 
 | 310 |         self.run("%s -N0 -o %s.ps %s" % (DVIPS_BINARY, self.doc, self.doc)) | 
 | 311 |  | 
 | 312 |     def build_html(self, builddir=None, max_split_depth=None): | 
 | 313 |         if builddir is None: | 
 | 314 |             builddir = self.doc | 
 | 315 |         if max_split_depth is None: | 
 | 316 |             max_split_depth = self.options.max_split_depth | 
 | 317 |         texfile = None | 
 | 318 |         for p in string.split(os.environ["TEXINPUTS"], os.pathsep): | 
 | 319 |             fn = os.path.join(p, self.doc + ".tex") | 
 | 320 |             if os.path.isfile(fn): | 
 | 321 |                 texfile = fn | 
 | 322 |                 break | 
 | 323 |         if not texfile: | 
| Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 324 |             self.warning("Could not locate %s.tex; aborting." % self.doc) | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 325 |             sys.exit(1) | 
 | 326 |         # remove leading ./ (or equiv.); might avoid problems w/ dvips | 
 | 327 |         if texfile[:2] == os.curdir + os.sep: | 
 | 328 |             texfile = texfile[2:] | 
 | 329 |         # build the command line and run LaTeX2HTML: | 
| Fred Drake | ba82878 | 2000-04-03 04:19:14 +0000 | [diff] [blame] | 330 |         if not os.path.isdir(builddir): | 
 | 331 |             os.mkdir(builddir) | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 332 |         args = [LATEX2HTML_BINARY, | 
 | 333 |                 "-init_file", L2H_INIT_FILE, | 
 | 334 |                 "-init_file", self.l2h_aux_init_file, | 
 | 335 |                 "-dir", builddir, | 
 | 336 |                 texfile | 
 | 337 |                 ] | 
 | 338 |         self.run(string.join(args))     # XXX need quoting! | 
 | 339 |         # ... postprocess | 
 | 340 |         shutil.copyfile(self.options.style_file, | 
 | 341 |                         os.path.join(builddir, self.doc + ".css")) | 
| Fred Drake | 4437fdf | 1999-05-03 14:29:07 +0000 | [diff] [blame] | 342 |         shutil.copyfile(os.path.join(builddir, self.doc + ".html"), | 
 | 343 |                         os.path.join(builddir, "index.html")) | 
| Fred Drake | 9a257b4 | 2000-03-31 20:27:36 +0000 | [diff] [blame] | 344 |         if max_split_depth != 1 and not self.options.numeric: | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 345 |             pwd = os.getcwd() | 
 | 346 |             try: | 
 | 347 |                 os.chdir(builddir) | 
 | 348 |                 self.run("%s %s *.html" % (PERL_BINARY, NODE2LABEL_SCRIPT)) | 
 | 349 |             finally: | 
 | 350 |                 os.chdir(pwd) | 
 | 351 |  | 
 | 352 |     def build_text(self, tempdir=None): | 
 | 353 |         if tempdir is None: | 
 | 354 |             tempdir = self.doc | 
 | 355 |         indexfile = os.path.join(tempdir, "index.html") | 
 | 356 |         self.run("%s -nolist -dump %s >%s.txt" | 
 | 357 |                  % (LYNX_BINARY, indexfile, self.doc)) | 
 | 358 |  | 
 | 359 |     def require_temps(self, binary=None): | 
| Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 360 |         if not self.latex_runs: | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 361 |             self.build_aux(binary=binary) | 
 | 362 |  | 
 | 363 |     def write_l2h_aux_init_file(self): | 
 | 364 |         fp = open(self.l2h_aux_init_file, "w") | 
 | 365 |         fp.write("# auxillary init file for latex2html\n" | 
 | 366 |                  "# generated by mkhowto\n" | 
| Fred Drake | 4437fdf | 1999-05-03 14:29:07 +0000 | [diff] [blame] | 367 |                  "$NO_AUTO_LINK = 1;\n" | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 368 |                  ) | 
 | 369 |         options = self.options | 
| Fred Drake | cf1b06e | 1999-09-23 16:55:09 +0000 | [diff] [blame] | 370 |         l2hoption(fp, "ABOUT_FILE", options.about_file) | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 371 |         l2hoption(fp, "ICONSERVER", options.icon_server) | 
| Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 372 |         l2hoption(fp, "IMAGE_TYPE", options.image_type) | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 373 |         l2hoption(fp, "ADDRESS", options.address) | 
 | 374 |         l2hoption(fp, "MAX_LINK_DEPTH", options.max_link_depth) | 
 | 375 |         l2hoption(fp, "MAX_SPLIT_DEPTH", options.max_split_depth) | 
| Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 376 |         # this line needed in case $IMAGE_TYPE changed | 
 | 377 |         fp.write("adjust_icon_information();\n") | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 378 |         fp.write("1;\n") | 
 | 379 |         fp.close() | 
 | 380 |  | 
 | 381 |     def cleanup(self): | 
 | 382 |         self.__have_temps = 0 | 
 | 383 |         for pattern in ("%s.aux", "%s.log", "%s.out", "%s.toc", "%s.bkm", | 
| Fred Drake | a871c2e | 1999-05-06 19:37:38 +0000 | [diff] [blame] | 384 |                         "%s.idx", "%s.ilg", "%s.ind", "%s.pla", | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 385 |                         "%s.bbl", "%s.blg", | 
 | 386 |                         "mod%s.idx", "mod%s.ind", "mod%s.ilg", | 
 | 387 |                         ): | 
 | 388 |             safe_unlink(pattern % self.doc) | 
| Fred Drake | a871c2e | 1999-05-06 19:37:38 +0000 | [diff] [blame] | 389 |         map(safe_unlink, glob.glob(self.doc + "*.syn")) | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 390 |         for spec in ("IMG*", "*.pl", "WARNINGS", "index.dat", "modindex.dat"): | 
 | 391 |             pattern = os.path.join(self.doc, spec) | 
 | 392 |             map(safe_unlink, glob.glob(pattern)) | 
 | 393 |         if "dvi" not in self.options.formats: | 
 | 394 |             safe_unlink(self.doc + ".dvi") | 
 | 395 |         if os.path.isdir(self.doc + "-temp-html"): | 
 | 396 |             shutil.rmtree(self.doc + "-temp-html", ignore_errors=1) | 
 | 397 |         if not self.options.logging: | 
 | 398 |             os.unlink(self.log_filename) | 
 | 399 |         if not self.options.debugging: | 
 | 400 |             os.unlink(self.l2h_aux_init_file) | 
 | 401 |  | 
 | 402 |     def run(self, command): | 
| Fred Drake | aaa0d9a | 1999-03-03 21:57:58 +0000 | [diff] [blame] | 403 |         self.message(command) | 
 | 404 |         rc = os.system("(%s) </dev/null >>%s 2>&1" | 
 | 405 |                        % (command, self.log_filename)) | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 406 |         if rc: | 
| Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 407 |             self.warning( | 
 | 408 |                 "Session transcript and error messages are in %s." | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 409 |                 % self.log_filename) | 
 | 410 |             sys.exit(rc) | 
 | 411 |  | 
| Fred Drake | aaa0d9a | 1999-03-03 21:57:58 +0000 | [diff] [blame] | 412 |     def message(self, msg): | 
 | 413 |         msg = "+++ " + msg | 
 | 414 |         if not self.options.quiet: | 
 | 415 |             print msg | 
| Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 416 |         self.log(msg + "\n") | 
 | 417 |  | 
 | 418 |     def warning(self, msg): | 
 | 419 |         msg = "*** %s\n" % msg | 
 | 420 |         sys.stderr.write(msg) | 
 | 421 |         self.log(msg) | 
 | 422 |  | 
 | 423 |     def log(self, msg): | 
| Fred Drake | aaa0d9a | 1999-03-03 21:57:58 +0000 | [diff] [blame] | 424 |         fp = open(self.log_filename, "a") | 
| Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 425 |         fp.write(msg) | 
| Fred Drake | aaa0d9a | 1999-03-03 21:57:58 +0000 | [diff] [blame] | 426 |         fp.close() | 
 | 427 |  | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 428 |  | 
 | 429 | def safe_unlink(path): | 
 | 430 |     try: | 
 | 431 |         os.unlink(path) | 
 | 432 |     except os.error: | 
 | 433 |         pass | 
 | 434 |  | 
 | 435 |  | 
| Fred Drake | a871c2e | 1999-05-06 19:37:38 +0000 | [diff] [blame] | 436 | def split_pathname(path): | 
 | 437 |     path = os.path.normpath(os.path.join(os.getcwd(), path)) | 
 | 438 |     dirname, basename = os.path.split(path) | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 439 |     if basename[-4:] == ".tex": | 
 | 440 |         basename = basename[:-4] | 
 | 441 |     return dirname, basename | 
 | 442 |  | 
 | 443 |  | 
| Fred Drake | a871c2e | 1999-05-06 19:37:38 +0000 | [diff] [blame] | 444 | _doctype_rx = re.compile(r"\\documentclass(?:\[[^]]*\])?{([a-zA-Z]*)}") | 
 | 445 | def get_doctype(path): | 
 | 446 |     fp = open(path) | 
 | 447 |     doctype = None | 
 | 448 |     while 1: | 
 | 449 |         line = fp.readline() | 
 | 450 |         if not line: | 
 | 451 |             break | 
 | 452 |         m = _doctype_rx.match(line) | 
 | 453 |         if m: | 
 | 454 |             doctype = m.group(1) | 
 | 455 |             break | 
 | 456 |     fp.close() | 
 | 457 |     return doctype | 
 | 458 |  | 
 | 459 |  | 
| Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 460 | def main(): | 
 | 461 |     options = Options() | 
 | 462 |     try: | 
 | 463 |         args = options.parse(sys.argv[1:]) | 
 | 464 |     except getopt.error, msg: | 
 | 465 |         error(options, msg) | 
 | 466 |     if not args: | 
 | 467 |         # attempt to locate single .tex file in current directory: | 
 | 468 |         args = glob.glob("*.tex") | 
 | 469 |         if not args: | 
 | 470 |             error(options, "No file to process.") | 
 | 471 |         if len(args) > 1: | 
 | 472 |             error(options, "Could not deduce which files should be processed.") | 
 | 473 |     # | 
 | 474 |     # parameters are processed, let's go! | 
 | 475 |     # | 
 | 476 |     for path in args: | 
 | 477 |         Job(options, path).build() | 
 | 478 |  | 
 | 479 |  | 
 | 480 | def l2hoption(fp, option, value): | 
 | 481 |     if value: | 
 | 482 |         fp.write('$%s = "%s";\n' % (option, string_to_perl(str(value)))) | 
 | 483 |  | 
 | 484 |  | 
 | 485 | _to_perl = {} | 
 | 486 | for c in map(chr, range(1, 256)): | 
 | 487 |     _to_perl[c] = c | 
 | 488 | _to_perl["@"] = "\\@" | 
 | 489 | _to_perl["$"] = "\\$" | 
 | 490 | _to_perl['"'] = '\\"' | 
 | 491 |  | 
 | 492 | def string_to_perl(s): | 
 | 493 |     return string.join(map(_to_perl.get, s), '') | 
 | 494 |  | 
 | 495 |  | 
 | 496 | def check_for_bibtex(filename): | 
 | 497 |     fp = open(filename) | 
 | 498 |     pos = string.find(fp.read(), r"\bibdata{") | 
 | 499 |     fp.close() | 
 | 500 |     return pos >= 0 | 
 | 501 |  | 
 | 502 | def uniqify_module_table(filename): | 
 | 503 |     lines = open(filename).readlines() | 
 | 504 |     if len(lines) > 1: | 
 | 505 |         if lines[-1] == lines[-2]: | 
 | 506 |             del lines[-1] | 
 | 507 |     open(filename, "w").writelines(lines) | 
 | 508 |  | 
 | 509 |  | 
 | 510 | def new_index(filename, label="genindex"): | 
 | 511 |     fp = open(filename, "w") | 
 | 512 |     fp.write(r"""\ | 
 | 513 | \begin{theindex} | 
 | 514 | \label{%s} | 
 | 515 | \end{theindex} | 
 | 516 | """ % label) | 
 | 517 |     fp.close() | 
 | 518 |  | 
 | 519 |  | 
 | 520 | if __name__ == "__main__": | 
 | 521 |     main() |