blob: 1903a53f168115f26a48a12698b6ef1950fb784d [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 = []
100
101 def __getitem__(self, key):
102 # This is used when formatting the usage message.
103 try:
104 return getattr(self, key)
105 except AttributeError:
106 raise KeyError, key
107
108 def parse(self, args):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000109 opts, args = getopt.getopt(args, "Hi:a:s:lDkqr:",
Fred Drake8b880931999-03-03 20:24:30 +0000110 ["all", "postscript", "help", "iconserver=",
Fred Drakecc7371c2000-06-29 23:01:40 +0000111 "address=", "a4", "letter",
Fred Drake8b880931999-03-03 20:24:30 +0000112 "link=", "split=", "logging", "debugging",
Fred Drakecf1b06e1999-09-23 16:55:09 +0000113 "keep", "quiet", "runs=", "image-type=",
Fred Drakefcb87252000-08-29 18:15:05 +0000114 "about=", "numeric", "style="]
Fred Drake52ea0ce1999-09-22 19:55:35 +0000115 + list(self.ALL_FORMATS))
Fred Drake8b880931999-03-03 20:24:30 +0000116 for opt, arg in opts:
117 if opt == "--all":
118 self.formats = list(self.ALL_FORMATS)
119 elif opt in ("-H", "--help"):
120 usage(self)
121 sys.exit()
122 elif opt == "--iconserver":
123 self.icon_server = arg
124 elif opt in ("-a", "--address"):
125 self.address = arg
126 elif opt == "--a4":
127 self.paper = "a4"
128 elif opt == "--letter":
129 self.paper = "letter"
Fred Drake8b880931999-03-03 20:24:30 +0000130 elif opt == "--link":
131 self.max_link_depth = int(arg)
132 elif opt in ("-s", "--split"):
133 self.max_split_depth = int(arg)
134 elif opt in ("-l", "--logging"):
135 self.logging = self.logging + 1
136 elif opt in ("-D", "--debugging"):
137 self.debugging = self.debugging + 1
138 elif opt in ("-k", "--keep"):
139 self.discard_temps = 0
140 elif opt in ("-q", "--quiet"):
141 self.quiet = 1
Fred Drake52ea0ce1999-09-22 19:55:35 +0000142 elif opt in ("-r", "--runs"):
143 self.runs = int(arg)
144 elif opt == "--image-type":
145 self.image_type = arg
Fred Drakecf1b06e1999-09-23 16:55:09 +0000146 elif opt == "--about":
147 # always make this absolute:
148 self.about_file = os.path.normpath(
Fred Drakefcb87252000-08-29 18:15:05 +0000149 os.path.abspath(arg))
Fred Drake9a257b42000-03-31 20:27:36 +0000150 elif opt == "--numeric":
151 self.numeric = 1
Fred Drakefcb87252000-08-29 18:15:05 +0000152 elif opt == "--style":
153 self.style_file = os.path.abspath(arg)
Fred Drake8b880931999-03-03 20:24:30 +0000154 #
155 # Format specifiers:
156 #
157 elif opt[2:] in self.ALL_FORMATS:
158 self.add_format(opt[2:])
159 elif opt == "--postscript":
160 # synonym for --ps
161 self.add_format("ps")
162 self.initialize()
163 #
164 # return the args to allow the caller access:
165 #
166 return args
167
168 def add_format(self, format):
169 """Add a format to the formats list if not present."""
170 if not format in self.formats:
171 self.formats.append(format)
172
173 def initialize(self):
174 """Complete initialization. This is needed if parse() isn't used."""
175 # add the default format if no formats were specified:
176 if not self.formats:
177 self.formats = self.DEFAULT_FORMATS
178 # determine the base set of texinputs directories:
179 texinputs = string.split(os.environ.get("TEXINPUTS", ""), os.pathsep)
180 if not texinputs:
181 texinputs = ['']
182 self.base_texinputs = [
183 os.path.join(TOPDIR, "paper-" + self.paper),
184 os.path.join(TOPDIR, "texinputs"),
185 ] + texinputs
186
187
188class Job:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000189 latex_runs = 0
190
Fred Drake8b880931999-03-03 20:24:30 +0000191 def __init__(self, options, path):
192 self.options = options
Fred Drakea871c2e1999-05-06 19:37:38 +0000193 self.doctype = get_doctype(path)
Fred Drake8b880931999-03-03 20:24:30 +0000194 self.filedir, self.doc = split_pathname(path)
195 self.log_filename = self.doc + ".how"
196 if os.path.exists(self.log_filename):
197 os.unlink(self.log_filename)
198 if os.path.exists(self.doc + ".l2h"):
199 self.l2h_aux_init_file = tempfile.mktemp()
200 else:
201 self.l2h_aux_init_file = self.doc + ".l2h"
202 self.write_l2h_aux_init_file()
203
204 def build(self):
205 self.setup_texinputs()
206 formats = self.options.formats
207 if "dvi" in formats or "ps" in formats:
208 self.build_dvi()
209 if "pdf" in formats:
210 self.build_pdf()
211 if "ps" in formats:
212 self.build_ps()
213 if "html" in formats:
214 self.require_temps()
215 self.build_html(self.doc)
216 if self.options.icon_server == ".":
Fred Drake52ea0ce1999-09-22 19:55:35 +0000217 pattern = os.path.join(TOPDIR, "html", "icons",
218 "*." + self.options.image_type)
219 imgs = glob.glob(pattern)
220 if not imgs:
221 self.warning(
222 "Could not locate support images of type %s."
223 % `self.options.image_type`)
224 for fn in imgs:
Fred Drake8b880931999-03-03 20:24:30 +0000225 new_fn = os.path.join(self.doc, os.path.basename(fn))
226 shutil.copyfile(fn, new_fn)
227 if "text" in formats:
228 self.require_temps()
229 tempdir = self.doc
230 need_html = "html" not in formats
231 if self.options.max_split_depth != 1:
232 fp = open(self.l2h_aux_init_file, "a")
233 fp.write("# re-hack this file for --text:\n")
234 l2hoption(fp, "MAX_SPLIT_DEPTH", "1")
235 fp.write("1;\n")
236 fp.close()
237 tempdir = self.doc + "-temp-html"
238 need_html = 1
239 if need_html:
240 self.build_html(tempdir, max_split_depth=1)
241 self.build_text(tempdir)
242 if self.options.discard_temps:
243 self.cleanup()
244
245 def setup_texinputs(self):
246 texinputs = [self.filedir] + list(self.options.base_texinputs)
247 os.environ["TEXINPUTS"] = string.join(texinputs, os.pathsep)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000248 self.message("TEXINPUTS=" + os.environ["TEXINPUTS"])
Fred Drake8b880931999-03-03 20:24:30 +0000249
Fred Drake8b880931999-03-03 20:24:30 +0000250 def build_aux(self, binary=None):
251 if binary is None:
252 binary = LATEX_BINARY
253 new_index( "%s.ind" % self.doc, "genindex")
254 new_index("mod%s.ind" % self.doc, "modindex")
255 self.run("%s %s" % (binary, self.doc))
256 self.use_bibtex = check_for_bibtex(self.doc + ".aux")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000257 self.latex_runs = 1
Fred Drake8b880931999-03-03 20:24:30 +0000258
259 def build_dvi(self):
260 self.use_latex(LATEX_BINARY)
261
262 def build_pdf(self):
263 self.use_latex(PDFLATEX_BINARY)
264
265 def use_latex(self, binary):
266 self.require_temps(binary=binary)
267 if os.path.isfile("mod%s.idx" % self.doc):
268 self.run("%s mod%s.idx" % (MAKEINDEX_BINARY, self.doc))
269 if os.path.isfile(self.doc + ".idx"):
270 # call to Doc/tools/fix_hack omitted; doesn't appear necessary
271 self.run("%s %s.idx" % (MAKEINDEX_BINARY, self.doc))
272 import indfix
273 indfix.process(self.doc + ".ind")
274 if self.use_bibtex:
275 self.run("%s %s" % (BIBTEX_BINARY, self.doc))
Fred Drakea871c2e1999-05-06 19:37:38 +0000276 self.process_synopsis_files()
277 #
278 # let the doctype-specific handler do some intermediate work:
279 #
280 if self.doctype == "manual":
281 self.use_latex_manual(binary=binary)
282 elif self.doctype == "howto":
283 self.use_latex_howto(binary=binary)
284 else:
285 raise RuntimeError, "unsupported document type: " + self.doctype
286 #
287 # and now finish it off:
288 #
289 if os.path.isfile(self.doc + ".toc") and binary == PDFLATEX_BINARY:
290 import toc2bkm
291 toc2bkm.process(self.doc + ".toc", self.doc + ".bkm", "section")
292 if self.use_bibtex:
293 self.run("%s %s" % (BIBTEX_BINARY, self.doc))
294 self.run("%s %s" % (binary, self.doc))
295
296 def use_latex_howto(self, binary):
Fred Drake8b880931999-03-03 20:24:30 +0000297 self.run("%s %s" % (binary, self.doc))
298 if os.path.isfile("mod%s.idx" % self.doc):
299 self.run("%s -s %s mod%s.idx"
300 % (MAKEINDEX_BINARY, ISTFILE, self.doc))
301 if os.path.isfile(self.doc + ".idx"):
302 self.run("%s -s %s %s.idx" % (MAKEINDEX_BINARY, ISTFILE, self.doc))
Fred Drakea871c2e1999-05-06 19:37:38 +0000303 self.process_synopsis_files()
304
305 def use_latex_manual(self, binary):
306 pass
307
308 def process_synopsis_files(self):
309 synopsis_files = glob.glob(self.doc + "*.syn")
310 for path in synopsis_files:
311 uniqify_module_table(path)
Fred Drake8b880931999-03-03 20:24:30 +0000312
313 def build_ps(self):
314 self.run("%s -N0 -o %s.ps %s" % (DVIPS_BINARY, self.doc, self.doc))
315
316 def build_html(self, builddir=None, max_split_depth=None):
317 if builddir is None:
318 builddir = self.doc
319 if max_split_depth is None:
320 max_split_depth = self.options.max_split_depth
321 texfile = None
322 for p in string.split(os.environ["TEXINPUTS"], os.pathsep):
323 fn = os.path.join(p, self.doc + ".tex")
324 if os.path.isfile(fn):
325 texfile = fn
326 break
327 if not texfile:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000328 self.warning("Could not locate %s.tex; aborting." % self.doc)
Fred Drake8b880931999-03-03 20:24:30 +0000329 sys.exit(1)
330 # remove leading ./ (or equiv.); might avoid problems w/ dvips
331 if texfile[:2] == os.curdir + os.sep:
332 texfile = texfile[2:]
333 # build the command line and run LaTeX2HTML:
Fred Drakeba828782000-04-03 04:19:14 +0000334 if not os.path.isdir(builddir):
335 os.mkdir(builddir)
Fred Drake8b880931999-03-03 20:24:30 +0000336 args = [LATEX2HTML_BINARY,
Fred Drake8b880931999-03-03 20:24:30 +0000337 "-init_file", self.l2h_aux_init_file,
338 "-dir", builddir,
339 texfile
340 ]
341 self.run(string.join(args)) # XXX need quoting!
342 # ... postprocess
343 shutil.copyfile(self.options.style_file,
344 os.path.join(builddir, self.doc + ".css"))
Fred Drake4437fdf1999-05-03 14:29:07 +0000345 shutil.copyfile(os.path.join(builddir, self.doc + ".html"),
346 os.path.join(builddir, "index.html"))
Fred Drake9a257b42000-03-31 20:27:36 +0000347 if max_split_depth != 1 and not self.options.numeric:
Fred Drake8b880931999-03-03 20:24:30 +0000348 pwd = os.getcwd()
349 try:
350 os.chdir(builddir)
351 self.run("%s %s *.html" % (PERL_BINARY, NODE2LABEL_SCRIPT))
352 finally:
353 os.chdir(pwd)
354
355 def build_text(self, tempdir=None):
356 if tempdir is None:
357 tempdir = self.doc
358 indexfile = os.path.join(tempdir, "index.html")
359 self.run("%s -nolist -dump %s >%s.txt"
360 % (LYNX_BINARY, indexfile, self.doc))
361
362 def require_temps(self, binary=None):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000363 if not self.latex_runs:
Fred Drake8b880931999-03-03 20:24:30 +0000364 self.build_aux(binary=binary)
365
366 def write_l2h_aux_init_file(self):
367 fp = open(self.l2h_aux_init_file, "w")
Fred Drake19157542000-07-31 17:47:49 +0000368 d = string_to_perl(os.path.dirname(L2H_INIT_FILE))
369 fp.write("package main;\n"
370 "push (@INC, '%s');\n"
371 "$mydir = '%s';\n"
372 % (d, d))
Fred Drake498c18f2000-07-24 23:03:32 +0000373 fp.write(open(L2H_INIT_FILE).read())
374 fp.write("\n"
375 "# auxillary init file for latex2html\n"
Fred Drake8b880931999-03-03 20:24:30 +0000376 "# generated by mkhowto\n"
Fred Drake4437fdf1999-05-03 14:29:07 +0000377 "$NO_AUTO_LINK = 1;\n"
Fred Drake8b880931999-03-03 20:24:30 +0000378 )
379 options = self.options
Fred Drakecf1b06e1999-09-23 16:55:09 +0000380 l2hoption(fp, "ABOUT_FILE", options.about_file)
Fred Drake8b880931999-03-03 20:24:30 +0000381 l2hoption(fp, "ICONSERVER", options.icon_server)
Fred Drake52ea0ce1999-09-22 19:55:35 +0000382 l2hoption(fp, "IMAGE_TYPE", options.image_type)
Fred Drake8b880931999-03-03 20:24:30 +0000383 l2hoption(fp, "ADDRESS", options.address)
384 l2hoption(fp, "MAX_LINK_DEPTH", options.max_link_depth)
385 l2hoption(fp, "MAX_SPLIT_DEPTH", options.max_split_depth)
386 fp.write("1;\n")
387 fp.close()
388
389 def cleanup(self):
390 self.__have_temps = 0
391 for pattern in ("%s.aux", "%s.log", "%s.out", "%s.toc", "%s.bkm",
Fred Drakea871c2e1999-05-06 19:37:38 +0000392 "%s.idx", "%s.ilg", "%s.ind", "%s.pla",
Fred Drake8b880931999-03-03 20:24:30 +0000393 "%s.bbl", "%s.blg",
394 "mod%s.idx", "mod%s.ind", "mod%s.ilg",
395 ):
396 safe_unlink(pattern % self.doc)
Fred Drakea871c2e1999-05-06 19:37:38 +0000397 map(safe_unlink, glob.glob(self.doc + "*.syn"))
Fred Drake8b880931999-03-03 20:24:30 +0000398 for spec in ("IMG*", "*.pl", "WARNINGS", "index.dat", "modindex.dat"):
399 pattern = os.path.join(self.doc, spec)
400 map(safe_unlink, glob.glob(pattern))
401 if "dvi" not in self.options.formats:
402 safe_unlink(self.doc + ".dvi")
403 if os.path.isdir(self.doc + "-temp-html"):
404 shutil.rmtree(self.doc + "-temp-html", ignore_errors=1)
405 if not self.options.logging:
406 os.unlink(self.log_filename)
407 if not self.options.debugging:
408 os.unlink(self.l2h_aux_init_file)
409
410 def run(self, command):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000411 self.message(command)
412 rc = os.system("(%s) </dev/null >>%s 2>&1"
413 % (command, self.log_filename))
Fred Drake8b880931999-03-03 20:24:30 +0000414 if rc:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000415 self.warning(
416 "Session transcript and error messages are in %s."
Fred Drake8b880931999-03-03 20:24:30 +0000417 % self.log_filename)
418 sys.exit(rc)
419
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000420 def message(self, msg):
421 msg = "+++ " + msg
422 if not self.options.quiet:
423 print msg
Fred Drake52ea0ce1999-09-22 19:55:35 +0000424 self.log(msg + "\n")
425
426 def warning(self, msg):
427 msg = "*** %s\n" % msg
428 sys.stderr.write(msg)
429 self.log(msg)
430
431 def log(self, msg):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000432 fp = open(self.log_filename, "a")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000433 fp.write(msg)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000434 fp.close()
435
Fred Drake8b880931999-03-03 20:24:30 +0000436
437def safe_unlink(path):
438 try:
439 os.unlink(path)
440 except os.error:
441 pass
442
443
Fred Drakea871c2e1999-05-06 19:37:38 +0000444def split_pathname(path):
445 path = os.path.normpath(os.path.join(os.getcwd(), path))
446 dirname, basename = os.path.split(path)
Fred Drake8b880931999-03-03 20:24:30 +0000447 if basename[-4:] == ".tex":
448 basename = basename[:-4]
449 return dirname, basename
450
451
Fred Drakea871c2e1999-05-06 19:37:38 +0000452_doctype_rx = re.compile(r"\\documentclass(?:\[[^]]*\])?{([a-zA-Z]*)}")
453def get_doctype(path):
454 fp = open(path)
455 doctype = None
456 while 1:
457 line = fp.readline()
458 if not line:
459 break
460 m = _doctype_rx.match(line)
461 if m:
462 doctype = m.group(1)
463 break
464 fp.close()
465 return doctype
466
467
Fred Drake8b880931999-03-03 20:24:30 +0000468def main():
469 options = Options()
470 try:
471 args = options.parse(sys.argv[1:])
472 except getopt.error, msg:
473 error(options, msg)
474 if not args:
475 # attempt to locate single .tex file in current directory:
476 args = glob.glob("*.tex")
477 if not args:
478 error(options, "No file to process.")
479 if len(args) > 1:
480 error(options, "Could not deduce which files should be processed.")
481 #
482 # parameters are processed, let's go!
483 #
484 for path in args:
485 Job(options, path).build()
486
487
488def l2hoption(fp, option, value):
489 if value:
490 fp.write('$%s = "%s";\n' % (option, string_to_perl(str(value))))
491
492
493_to_perl = {}
494for c in map(chr, range(1, 256)):
495 _to_perl[c] = c
496_to_perl["@"] = "\\@"
497_to_perl["$"] = "\\$"
498_to_perl['"'] = '\\"'
499
500def string_to_perl(s):
501 return string.join(map(_to_perl.get, s), '')
502
503
504def check_for_bibtex(filename):
505 fp = open(filename)
506 pos = string.find(fp.read(), r"\bibdata{")
507 fp.close()
508 return pos >= 0
509
510def uniqify_module_table(filename):
511 lines = open(filename).readlines()
512 if len(lines) > 1:
513 if lines[-1] == lines[-2]:
514 del lines[-1]
515 open(filename, "w").writelines(lines)
516
517
518def new_index(filename, label="genindex"):
519 fp = open(filename, "w")
520 fp.write(r"""\
521\begin{theindex}
522\label{%s}
523\end{theindex}
524""" % label)
525 fp.close()
526
527
528if __name__ == "__main__":
529 main()