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