blob: faee6cf404aa4cc0327bb4c72d569150331c9c8b [file] [log] [blame]
Fred Drake8b880931999-03-03 20:24:30 +00001#! /usr/bin/env python
2# -*- Python -*-
3"""usage: %(program)s [options...] file ...
4
5Options 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
14HTML 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 Drake52ea0ce1999-09-22 19:55:35 +000019 --image-type Specify the image type to use in HTML output;
20 values: gif (default), png.
Fred Drake9a257b42000-03-31 20:27:36 +000021 --numeric Don't rename the HTML files; just keep node#.html for
22 the filenames.
Fred Drakefcb87252000-08-29 18:15:05 +000023 --style Specify the CSS file to use for the output (filename,
24 not a URL).
Fred Drake8b880931999-03-03 20:24:30 +000025
26Other options:
27 --a4 Format for A4 paper.
28 --letter Format for US letter paper (the default).
29 --help, -H Show this text.
30 --logging, -l Log stdout and stderr to a file (*.how).
31 --debugging, -D Echo commands as they are executed.
32 --keep, -k Keep temporary files around.
33 --quiet, -q Do not print command output to stdout.
34 (stderr is also lost, sorry; see *.how for errors)
35"""
36
37import getopt
38import glob
39import os
Fred Drakea871c2e1999-05-06 19:37:38 +000040import re
Fred Drake8b880931999-03-03 20:24:30 +000041import shutil
42import string
43import sys
44import tempfile
45
46
Fred Drakefcb87252000-08-29 18:15:05 +000047MYDIR = os.path.abspath(sys.path[0])
48TOPDIR = os.path.dirname(MYDIR)
Fred Drake8b880931999-03-03 20:24:30 +000049
50ISTFILE = os.path.join(TOPDIR, "texinputs", "python.ist")
51NODE2LABEL_SCRIPT = os.path.join(MYDIR, "node2label.pl")
52L2H_INIT_FILE = os.path.join(TOPDIR, "perl", "l2hinit.perl")
53
54BIBTEX_BINARY = "bibtex"
55DVIPS_BINARY = "dvips"
56LATEX_BINARY = "latex"
57LATEX2HTML_BINARY = "latex2html"
58LYNX_BINARY = "lynx"
59MAKEINDEX_BINARY = "makeindex"
60PDFLATEX_BINARY = "pdflatex"
61PERL_BINARY = "perl"
62PYTHON_BINARY = "python"
63
64
65def usage(options):
66 print __doc__ % options
67
68def error(options, message, err=2):
69 sys.stdout = sys.stderr
70 print message
71 print
72 usage(options)
73 sys.exit(2)
74
75
76class Options:
77 program = os.path.basename(sys.argv[0])
78 #
79 address = ''
80 debugging = 0
81 discard_temps = 1
82 have_temps = 0
83 icon_server = None
Fred Drake52ea0ce1999-09-22 19:55:35 +000084 image_type = "gif"
Fred Drake8b880931999-03-03 20:24:30 +000085 logging = 0
86 max_link_depth = 3
87 max_split_depth = 6
88 paper = "letter"
89 quiet = 0
Fred Drake52ea0ce1999-09-22 19:55:35 +000090 runs = 0
Fred Drake9a257b42000-03-31 20:27:36 +000091 numeric = 0
Fred Drake8b880931999-03-03 20:24:30 +000092 style_file = os.path.join(TOPDIR, "html", "style.css")
Fred Drakecf1b06e1999-09-23 16:55:09 +000093 about_file = os.path.join(TOPDIR, "html", "about.dat")
Fred Drake8b880931999-03-03 20:24:30 +000094 #
95 DEFAULT_FORMATS = ("pdf",)
96 ALL_FORMATS = ("dvi", "html", "pdf", "ps", "text")
97
98 def __init__(self):
Fred Drake8b880931999-03-03 20:24:30 +000099 self.formats = []
Fred Drake8bc627a2000-08-31 06:14:38 +0000100 self.l2h_init_files = []
Fred Drake8b880931999-03-03 20:24:30 +0000101
102 def __getitem__(self, key):
103 # This is used when formatting the usage message.
104 try:
105 return getattr(self, key)
106 except AttributeError:
107 raise KeyError, key
108
109 def parse(self, args):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000110 opts, args = getopt.getopt(args, "Hi:a:s:lDkqr:",
Fred Drake8b880931999-03-03 20:24:30 +0000111 ["all", "postscript", "help", "iconserver=",
Fred Drake8bc627a2000-08-31 06:14:38 +0000112 "address=", "a4", "letter", "l2h-init=",
Fred Drake8b880931999-03-03 20:24:30 +0000113 "link=", "split=", "logging", "debugging",
Fred Drakecf1b06e1999-09-23 16:55:09 +0000114 "keep", "quiet", "runs=", "image-type=",
Fred Drakefcb87252000-08-29 18:15:05 +0000115 "about=", "numeric", "style="]
Fred Drake52ea0ce1999-09-22 19:55:35 +0000116 + list(self.ALL_FORMATS))
Fred Drake8b880931999-03-03 20:24:30 +0000117 for opt, arg in opts:
118 if opt == "--all":
119 self.formats = list(self.ALL_FORMATS)
120 elif opt in ("-H", "--help"):
121 usage(self)
122 sys.exit()
123 elif opt == "--iconserver":
124 self.icon_server = arg
125 elif opt in ("-a", "--address"):
126 self.address = arg
127 elif opt == "--a4":
128 self.paper = "a4"
129 elif opt == "--letter":
130 self.paper = "letter"
Fred Drake8b880931999-03-03 20:24:30 +0000131 elif opt == "--link":
132 self.max_link_depth = int(arg)
133 elif opt in ("-s", "--split"):
134 self.max_split_depth = int(arg)
135 elif opt in ("-l", "--logging"):
136 self.logging = self.logging + 1
137 elif opt in ("-D", "--debugging"):
138 self.debugging = self.debugging + 1
139 elif opt in ("-k", "--keep"):
140 self.discard_temps = 0
141 elif opt in ("-q", "--quiet"):
142 self.quiet = 1
Fred Drake52ea0ce1999-09-22 19:55:35 +0000143 elif opt in ("-r", "--runs"):
144 self.runs = int(arg)
145 elif opt == "--image-type":
146 self.image_type = arg
Fred Drakecf1b06e1999-09-23 16:55:09 +0000147 elif opt == "--about":
148 # always make this absolute:
149 self.about_file = os.path.normpath(
Fred Drakefcb87252000-08-29 18:15:05 +0000150 os.path.abspath(arg))
Fred Drake9a257b42000-03-31 20:27:36 +0000151 elif opt == "--numeric":
152 self.numeric = 1
Fred Drakefcb87252000-08-29 18:15:05 +0000153 elif opt == "--style":
154 self.style_file = os.path.abspath(arg)
Fred Drake8bc627a2000-08-31 06:14:38 +0000155 elif opt == "--l2h-init":
156 self.l2h_init_files.append(os.path.abspath(arg))
Fred Drake8b880931999-03-03 20:24:30 +0000157 #
158 # Format specifiers:
159 #
160 elif opt[2:] in self.ALL_FORMATS:
161 self.add_format(opt[2:])
162 elif opt == "--postscript":
163 # synonym for --ps
164 self.add_format("ps")
165 self.initialize()
166 #
167 # return the args to allow the caller access:
168 #
169 return args
170
171 def add_format(self, format):
172 """Add a format to the formats list if not present."""
173 if not format in self.formats:
174 self.formats.append(format)
175
176 def initialize(self):
177 """Complete initialization. This is needed if parse() isn't used."""
178 # add the default format if no formats were specified:
179 if not self.formats:
180 self.formats = self.DEFAULT_FORMATS
181 # determine the base set of texinputs directories:
182 texinputs = string.split(os.environ.get("TEXINPUTS", ""), os.pathsep)
183 if not texinputs:
184 texinputs = ['']
185 self.base_texinputs = [
186 os.path.join(TOPDIR, "paper-" + self.paper),
187 os.path.join(TOPDIR, "texinputs"),
188 ] + texinputs
189
190
191class Job:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000192 latex_runs = 0
193
Fred Drake8b880931999-03-03 20:24:30 +0000194 def __init__(self, options, path):
195 self.options = options
Fred Drakea871c2e1999-05-06 19:37:38 +0000196 self.doctype = get_doctype(path)
Fred Drake8b880931999-03-03 20:24:30 +0000197 self.filedir, self.doc = split_pathname(path)
198 self.log_filename = self.doc + ".how"
199 if os.path.exists(self.log_filename):
200 os.unlink(self.log_filename)
201 if os.path.exists(self.doc + ".l2h"):
202 self.l2h_aux_init_file = tempfile.mktemp()
203 else:
204 self.l2h_aux_init_file = self.doc + ".l2h"
205 self.write_l2h_aux_init_file()
206
207 def build(self):
208 self.setup_texinputs()
209 formats = self.options.formats
210 if "dvi" in formats or "ps" in formats:
211 self.build_dvi()
212 if "pdf" in formats:
213 self.build_pdf()
214 if "ps" in formats:
215 self.build_ps()
216 if "html" in formats:
217 self.require_temps()
218 self.build_html(self.doc)
219 if self.options.icon_server == ".":
Fred Drake52ea0ce1999-09-22 19:55:35 +0000220 pattern = os.path.join(TOPDIR, "html", "icons",
221 "*." + self.options.image_type)
222 imgs = glob.glob(pattern)
223 if not imgs:
224 self.warning(
225 "Could not locate support images of type %s."
226 % `self.options.image_type`)
227 for fn in imgs:
Fred Drake8b880931999-03-03 20:24:30 +0000228 new_fn = os.path.join(self.doc, os.path.basename(fn))
229 shutil.copyfile(fn, new_fn)
230 if "text" in formats:
231 self.require_temps()
232 tempdir = self.doc
233 need_html = "html" not in formats
234 if self.options.max_split_depth != 1:
235 fp = open(self.l2h_aux_init_file, "a")
236 fp.write("# re-hack this file for --text:\n")
237 l2hoption(fp, "MAX_SPLIT_DEPTH", "1")
238 fp.write("1;\n")
239 fp.close()
240 tempdir = self.doc + "-temp-html"
241 need_html = 1
242 if need_html:
243 self.build_html(tempdir, max_split_depth=1)
244 self.build_text(tempdir)
245 if self.options.discard_temps:
246 self.cleanup()
247
248 def setup_texinputs(self):
249 texinputs = [self.filedir] + list(self.options.base_texinputs)
250 os.environ["TEXINPUTS"] = string.join(texinputs, os.pathsep)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000251 self.message("TEXINPUTS=" + os.environ["TEXINPUTS"])
Fred Drake8b880931999-03-03 20:24:30 +0000252
Fred Drake8b880931999-03-03 20:24:30 +0000253 def build_aux(self, binary=None):
254 if binary is None:
255 binary = LATEX_BINARY
256 new_index( "%s.ind" % self.doc, "genindex")
257 new_index("mod%s.ind" % self.doc, "modindex")
258 self.run("%s %s" % (binary, self.doc))
259 self.use_bibtex = check_for_bibtex(self.doc + ".aux")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000260 self.latex_runs = 1
Fred Drake8b880931999-03-03 20:24:30 +0000261
262 def build_dvi(self):
263 self.use_latex(LATEX_BINARY)
264
265 def build_pdf(self):
266 self.use_latex(PDFLATEX_BINARY)
267
268 def use_latex(self, binary):
269 self.require_temps(binary=binary)
270 if os.path.isfile("mod%s.idx" % self.doc):
271 self.run("%s mod%s.idx" % (MAKEINDEX_BINARY, self.doc))
272 if os.path.isfile(self.doc + ".idx"):
273 # call to Doc/tools/fix_hack omitted; doesn't appear necessary
274 self.run("%s %s.idx" % (MAKEINDEX_BINARY, self.doc))
275 import indfix
276 indfix.process(self.doc + ".ind")
277 if self.use_bibtex:
278 self.run("%s %s" % (BIBTEX_BINARY, self.doc))
Fred Drakea871c2e1999-05-06 19:37:38 +0000279 self.process_synopsis_files()
280 #
281 # let the doctype-specific handler do some intermediate work:
282 #
283 if self.doctype == "manual":
284 self.use_latex_manual(binary=binary)
285 elif self.doctype == "howto":
286 self.use_latex_howto(binary=binary)
287 else:
288 raise RuntimeError, "unsupported document type: " + self.doctype
289 #
290 # and now finish it off:
291 #
292 if os.path.isfile(self.doc + ".toc") and binary == PDFLATEX_BINARY:
293 import toc2bkm
294 toc2bkm.process(self.doc + ".toc", self.doc + ".bkm", "section")
295 if self.use_bibtex:
296 self.run("%s %s" % (BIBTEX_BINARY, self.doc))
297 self.run("%s %s" % (binary, self.doc))
298
299 def use_latex_howto(self, binary):
Fred Drake8b880931999-03-03 20:24:30 +0000300 self.run("%s %s" % (binary, self.doc))
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))
304 if os.path.isfile(self.doc + ".idx"):
305 self.run("%s -s %s %s.idx" % (MAKEINDEX_BINARY, ISTFILE, self.doc))
Fred Drakea871c2e1999-05-06 19:37:38 +0000306 self.process_synopsis_files()
307
308 def use_latex_manual(self, binary):
Fred Drake8bc627a2000-08-31 06:14:38 +0000309 self.use_latex_howto(binary)
Fred Drakea871c2e1999-05-06 19:37:38 +0000310
311 def process_synopsis_files(self):
312 synopsis_files = glob.glob(self.doc + "*.syn")
313 for path in synopsis_files:
314 uniqify_module_table(path)
Fred Drake8b880931999-03-03 20:24:30 +0000315
316 def build_ps(self):
317 self.run("%s -N0 -o %s.ps %s" % (DVIPS_BINARY, self.doc, self.doc))
318
319 def build_html(self, builddir=None, max_split_depth=None):
320 if builddir is None:
321 builddir = self.doc
322 if max_split_depth is None:
323 max_split_depth = self.options.max_split_depth
324 texfile = None
325 for p in string.split(os.environ["TEXINPUTS"], os.pathsep):
326 fn = os.path.join(p, self.doc + ".tex")
327 if os.path.isfile(fn):
328 texfile = fn
329 break
330 if not texfile:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000331 self.warning("Could not locate %s.tex; aborting." % self.doc)
Fred Drake8b880931999-03-03 20:24:30 +0000332 sys.exit(1)
333 # remove leading ./ (or equiv.); might avoid problems w/ dvips
334 if texfile[:2] == os.curdir + os.sep:
335 texfile = texfile[2:]
336 # build the command line and run LaTeX2HTML:
Fred Drakeba828782000-04-03 04:19:14 +0000337 if not os.path.isdir(builddir):
338 os.mkdir(builddir)
Fred Drake8b880931999-03-03 20:24:30 +0000339 args = [LATEX2HTML_BINARY,
Fred Drake8b880931999-03-03 20:24:30 +0000340 "-init_file", self.l2h_aux_init_file,
341 "-dir", builddir,
342 texfile
343 ]
344 self.run(string.join(args)) # XXX need quoting!
345 # ... postprocess
346 shutil.copyfile(self.options.style_file,
347 os.path.join(builddir, self.doc + ".css"))
Fred Drake4437fdf1999-05-03 14:29:07 +0000348 shutil.copyfile(os.path.join(builddir, self.doc + ".html"),
349 os.path.join(builddir, "index.html"))
Fred Drake9a257b42000-03-31 20:27:36 +0000350 if max_split_depth != 1 and not self.options.numeric:
Fred Drake8b880931999-03-03 20:24:30 +0000351 pwd = os.getcwd()
352 try:
353 os.chdir(builddir)
354 self.run("%s %s *.html" % (PERL_BINARY, NODE2LABEL_SCRIPT))
355 finally:
356 os.chdir(pwd)
357
358 def build_text(self, tempdir=None):
359 if tempdir is None:
360 tempdir = self.doc
361 indexfile = os.path.join(tempdir, "index.html")
362 self.run("%s -nolist -dump %s >%s.txt"
363 % (LYNX_BINARY, indexfile, self.doc))
364
365 def require_temps(self, binary=None):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000366 if not self.latex_runs:
Fred Drake8b880931999-03-03 20:24:30 +0000367 self.build_aux(binary=binary)
368
369 def write_l2h_aux_init_file(self):
Fred Drake8bc627a2000-08-31 06:14:38 +0000370 options = self.options
Fred Drake8b880931999-03-03 20:24:30 +0000371 fp = open(self.l2h_aux_init_file, "w")
Fred Drake19157542000-07-31 17:47:49 +0000372 d = string_to_perl(os.path.dirname(L2H_INIT_FILE))
373 fp.write("package main;\n"
374 "push (@INC, '%s');\n"
375 "$mydir = '%s';\n"
376 % (d, d))
Fred Drake498c18f2000-07-24 23:03:32 +0000377 fp.write(open(L2H_INIT_FILE).read())
Fred Drake8bc627a2000-08-31 06:14:38 +0000378 for filename in options.l2h_init_files:
379 fp.write("\n# initialization code incorporated from:\n# ")
380 fp.write(filename)
381 fp.write("\n")
382 fp.write(open(filename).read())
Fred Drake498c18f2000-07-24 23:03:32 +0000383 fp.write("\n"
384 "# auxillary init file for latex2html\n"
Fred Drake8b880931999-03-03 20:24:30 +0000385 "# generated by mkhowto\n"
Fred Drake4437fdf1999-05-03 14:29:07 +0000386 "$NO_AUTO_LINK = 1;\n"
Fred Drake8b880931999-03-03 20:24:30 +0000387 )
Fred Drakecf1b06e1999-09-23 16:55:09 +0000388 l2hoption(fp, "ABOUT_FILE", options.about_file)
Fred Drake8b880931999-03-03 20:24:30 +0000389 l2hoption(fp, "ICONSERVER", options.icon_server)
Fred Drake52ea0ce1999-09-22 19:55:35 +0000390 l2hoption(fp, "IMAGE_TYPE", options.image_type)
Fred Drake8b880931999-03-03 20:24:30 +0000391 l2hoption(fp, "ADDRESS", options.address)
392 l2hoption(fp, "MAX_LINK_DEPTH", options.max_link_depth)
393 l2hoption(fp, "MAX_SPLIT_DEPTH", options.max_split_depth)
394 fp.write("1;\n")
395 fp.close()
396
397 def cleanup(self):
398 self.__have_temps = 0
399 for pattern in ("%s.aux", "%s.log", "%s.out", "%s.toc", "%s.bkm",
Fred Drakea871c2e1999-05-06 19:37:38 +0000400 "%s.idx", "%s.ilg", "%s.ind", "%s.pla",
Fred Drake8b880931999-03-03 20:24:30 +0000401 "%s.bbl", "%s.blg",
402 "mod%s.idx", "mod%s.ind", "mod%s.ilg",
403 ):
404 safe_unlink(pattern % self.doc)
Fred Drakea871c2e1999-05-06 19:37:38 +0000405 map(safe_unlink, glob.glob(self.doc + "*.syn"))
Fred Drake8b880931999-03-03 20:24:30 +0000406 for spec in ("IMG*", "*.pl", "WARNINGS", "index.dat", "modindex.dat"):
407 pattern = os.path.join(self.doc, spec)
408 map(safe_unlink, glob.glob(pattern))
409 if "dvi" not in self.options.formats:
410 safe_unlink(self.doc + ".dvi")
411 if os.path.isdir(self.doc + "-temp-html"):
412 shutil.rmtree(self.doc + "-temp-html", ignore_errors=1)
413 if not self.options.logging:
414 os.unlink(self.log_filename)
415 if not self.options.debugging:
416 os.unlink(self.l2h_aux_init_file)
417
418 def run(self, command):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000419 self.message(command)
420 rc = os.system("(%s) </dev/null >>%s 2>&1"
421 % (command, self.log_filename))
Fred Drake8b880931999-03-03 20:24:30 +0000422 if rc:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000423 self.warning(
424 "Session transcript and error messages are in %s."
Fred Drake8b880931999-03-03 20:24:30 +0000425 % self.log_filename)
426 sys.exit(rc)
427
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000428 def message(self, msg):
429 msg = "+++ " + msg
430 if not self.options.quiet:
431 print msg
Fred Drake52ea0ce1999-09-22 19:55:35 +0000432 self.log(msg + "\n")
433
434 def warning(self, msg):
435 msg = "*** %s\n" % msg
436 sys.stderr.write(msg)
437 self.log(msg)
438
439 def log(self, msg):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000440 fp = open(self.log_filename, "a")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000441 fp.write(msg)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000442 fp.close()
443
Fred Drake8b880931999-03-03 20:24:30 +0000444
445def safe_unlink(path):
446 try:
447 os.unlink(path)
448 except os.error:
449 pass
450
451
Fred Drakea871c2e1999-05-06 19:37:38 +0000452def split_pathname(path):
453 path = os.path.normpath(os.path.join(os.getcwd(), path))
454 dirname, basename = os.path.split(path)
Fred Drake8b880931999-03-03 20:24:30 +0000455 if basename[-4:] == ".tex":
456 basename = basename[:-4]
457 return dirname, basename
458
459
Fred Drakea871c2e1999-05-06 19:37:38 +0000460_doctype_rx = re.compile(r"\\documentclass(?:\[[^]]*\])?{([a-zA-Z]*)}")
461def get_doctype(path):
462 fp = open(path)
463 doctype = None
464 while 1:
465 line = fp.readline()
466 if not line:
467 break
468 m = _doctype_rx.match(line)
469 if m:
470 doctype = m.group(1)
471 break
472 fp.close()
473 return doctype
474
475
Fred Drake8b880931999-03-03 20:24:30 +0000476def main():
477 options = Options()
478 try:
479 args = options.parse(sys.argv[1:])
480 except getopt.error, msg:
481 error(options, msg)
482 if not args:
483 # attempt to locate single .tex file in current directory:
484 args = glob.glob("*.tex")
485 if not args:
486 error(options, "No file to process.")
487 if len(args) > 1:
488 error(options, "Could not deduce which files should be processed.")
489 #
490 # parameters are processed, let's go!
491 #
492 for path in args:
493 Job(options, path).build()
494
495
496def l2hoption(fp, option, value):
497 if value:
498 fp.write('$%s = "%s";\n' % (option, string_to_perl(str(value))))
499
500
501_to_perl = {}
502for c in map(chr, range(1, 256)):
503 _to_perl[c] = c
504_to_perl["@"] = "\\@"
505_to_perl["$"] = "\\$"
506_to_perl['"'] = '\\"'
507
508def string_to_perl(s):
509 return string.join(map(_to_perl.get, s), '')
510
511
512def check_for_bibtex(filename):
513 fp = open(filename)
514 pos = string.find(fp.read(), r"\bibdata{")
515 fp.close()
516 return pos >= 0
517
518def uniqify_module_table(filename):
519 lines = open(filename).readlines()
520 if len(lines) > 1:
521 if lines[-1] == lines[-2]:
522 del lines[-1]
523 open(filename, "w").writelines(lines)
524
525
526def new_index(filename, label="genindex"):
527 fp = open(filename, "w")
528 fp.write(r"""\
529\begin{theindex}
530\label{%s}
531\end{theindex}
532""" % label)
533 fp.close()
534
535
536if __name__ == "__main__":
537 main()