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