blob: cf8a3cca829c160859d3ef71bce0bc19e6aade27 [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 Drake8b880931999-03-03 20:24:30 +000023
24Other 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
35import getopt
36import glob
37import os
Fred Drakea871c2e1999-05-06 19:37:38 +000038import re
Fred Drake8b880931999-03-03 20:24:30 +000039import shutil
40import string
41import sys
42import tempfile
43
44
45MYDIR = os.path.normpath(os.path.join(os.getcwd(), sys.path[0]))
46TOPDIR = os.path.normpath(os.path.join(MYDIR, os.pardir))
47
48ISTFILE = os.path.join(TOPDIR, "texinputs", "python.ist")
49NODE2LABEL_SCRIPT = os.path.join(MYDIR, "node2label.pl")
50L2H_INIT_FILE = os.path.join(TOPDIR, "perl", "l2hinit.perl")
51
52BIBTEX_BINARY = "bibtex"
53DVIPS_BINARY = "dvips"
54LATEX_BINARY = "latex"
55LATEX2HTML_BINARY = "latex2html"
56LYNX_BINARY = "lynx"
57MAKEINDEX_BINARY = "makeindex"
58PDFLATEX_BINARY = "pdflatex"
59PERL_BINARY = "perl"
60PYTHON_BINARY = "python"
61
62
63def usage(options):
64 print __doc__ % options
65
66def error(options, message, err=2):
67 sys.stdout = sys.stderr
68 print message
69 print
70 usage(options)
71 sys.exit(2)
72
73
74class 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 Drake52ea0ce1999-09-22 19:55:35 +000082 image_type = "gif"
Fred Drake8b880931999-03-03 20:24:30 +000083 logging = 0
84 max_link_depth = 3
85 max_split_depth = 6
86 paper = "letter"
87 quiet = 0
Fred Drake52ea0ce1999-09-22 19:55:35 +000088 runs = 0
Fred Drake9a257b42000-03-31 20:27:36 +000089 numeric = 0
Fred Drake8b880931999-03-03 20:24:30 +000090 style_file = os.path.join(TOPDIR, "html", "style.css")
Fred Drakecf1b06e1999-09-23 16:55:09 +000091 about_file = os.path.join(TOPDIR, "html", "about.dat")
Fred Drake8b880931999-03-03 20:24:30 +000092 #
93 DEFAULT_FORMATS = ("pdf",)
94 ALL_FORMATS = ("dvi", "html", "pdf", "ps", "text")
95
96 def __init__(self):
Fred Drake8b880931999-03-03 20:24:30 +000097 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 Drake52ea0ce1999-09-22 19:55:35 +0000107 opts, args = getopt.getopt(args, "Hi:a:s:lDkqr:",
Fred Drake8b880931999-03-03 20:24:30 +0000108 ["all", "postscript", "help", "iconserver=",
Fred Drakecc7371c2000-06-29 23:01:40 +0000109 "address=", "a4", "letter",
Fred Drake8b880931999-03-03 20:24:30 +0000110 "link=", "split=", "logging", "debugging",
Fred Drakecf1b06e1999-09-23 16:55:09 +0000111 "keep", "quiet", "runs=", "image-type=",
Fred Drake9a257b42000-03-31 20:27:36 +0000112 "about=", "numeric"]
Fred Drake52ea0ce1999-09-22 19:55:35 +0000113 + list(self.ALL_FORMATS))
Fred Drake8b880931999-03-03 20:24:30 +0000114 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 Drake8b880931999-03-03 20:24:30 +0000128 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 Drake52ea0ce1999-09-22 19:55:35 +0000140 elif opt in ("-r", "--runs"):
141 self.runs = int(arg)
142 elif opt == "--image-type":
143 self.image_type = arg
Fred Drakecf1b06e1999-09-23 16:55:09 +0000144 elif opt == "--about":
145 # always make this absolute:
146 self.about_file = os.path.normpath(
147 os.path.join(os.getcwd(), arg))
Fred Drake9a257b42000-03-31 20:27:36 +0000148 elif opt == "--numeric":
149 self.numeric = 1
Fred Drake8b880931999-03-03 20:24:30 +0000150 #
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
184class Job:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000185 latex_runs = 0
186
Fred Drake8b880931999-03-03 20:24:30 +0000187 def __init__(self, options, path):
188 self.options = options
Fred Drakea871c2e1999-05-06 19:37:38 +0000189 self.doctype = get_doctype(path)
Fred Drake8b880931999-03-03 20:24:30 +0000190 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 Drake52ea0ce1999-09-22 19:55:35 +0000213 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 Drake8b880931999-03-03 20:24:30 +0000221 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 Drakeaaa0d9a1999-03-03 21:57:58 +0000244 self.message("TEXINPUTS=" + os.environ["TEXINPUTS"])
Fred Drake8b880931999-03-03 20:24:30 +0000245
Fred Drake8b880931999-03-03 20:24:30 +0000246 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 Drake52ea0ce1999-09-22 19:55:35 +0000253 self.latex_runs = 1
Fred Drake8b880931999-03-03 20:24:30 +0000254
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 Drakea871c2e1999-05-06 19:37:38 +0000272 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 Drake8b880931999-03-03 20:24:30 +0000293 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 Drakea871c2e1999-05-06 19:37:38 +0000299 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 Drake8b880931999-03-03 20:24:30 +0000308
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 Drake52ea0ce1999-09-22 19:55:35 +0000324 self.warning("Could not locate %s.tex; aborting." % self.doc)
Fred Drake8b880931999-03-03 20:24:30 +0000325 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 Drakeba828782000-04-03 04:19:14 +0000330 if not os.path.isdir(builddir):
331 os.mkdir(builddir)
Fred Drake8b880931999-03-03 20:24:30 +0000332 args = [LATEX2HTML_BINARY,
Fred Drake8b880931999-03-03 20:24:30 +0000333 "-init_file", self.l2h_aux_init_file,
334 "-dir", builddir,
335 texfile
336 ]
337 self.run(string.join(args)) # XXX need quoting!
338 # ... postprocess
339 shutil.copyfile(self.options.style_file,
340 os.path.join(builddir, self.doc + ".css"))
Fred Drake4437fdf1999-05-03 14:29:07 +0000341 shutil.copyfile(os.path.join(builddir, self.doc + ".html"),
342 os.path.join(builddir, "index.html"))
Fred Drake9a257b42000-03-31 20:27:36 +0000343 if max_split_depth != 1 and not self.options.numeric:
Fred Drake8b880931999-03-03 20:24:30 +0000344 pwd = os.getcwd()
345 try:
346 os.chdir(builddir)
347 self.run("%s %s *.html" % (PERL_BINARY, NODE2LABEL_SCRIPT))
348 finally:
349 os.chdir(pwd)
350
351 def build_text(self, tempdir=None):
352 if tempdir is None:
353 tempdir = self.doc
354 indexfile = os.path.join(tempdir, "index.html")
355 self.run("%s -nolist -dump %s >%s.txt"
356 % (LYNX_BINARY, indexfile, self.doc))
357
358 def require_temps(self, binary=None):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000359 if not self.latex_runs:
Fred Drake8b880931999-03-03 20:24:30 +0000360 self.build_aux(binary=binary)
361
362 def write_l2h_aux_init_file(self):
363 fp = open(self.l2h_aux_init_file, "w")
Fred Drake19157542000-07-31 17:47:49 +0000364 d = string_to_perl(os.path.dirname(L2H_INIT_FILE))
365 fp.write("package main;\n"
366 "push (@INC, '%s');\n"
367 "$mydir = '%s';\n"
368 % (d, d))
Fred Drake498c18f2000-07-24 23:03:32 +0000369 fp.write(open(L2H_INIT_FILE).read())
370 fp.write("\n"
371 "# auxillary init file for latex2html\n"
Fred Drake8b880931999-03-03 20:24:30 +0000372 "# generated by mkhowto\n"
Fred Drake4437fdf1999-05-03 14:29:07 +0000373 "$NO_AUTO_LINK = 1;\n"
Fred Drake8b880931999-03-03 20:24:30 +0000374 )
375 options = self.options
Fred Drakecf1b06e1999-09-23 16:55:09 +0000376 l2hoption(fp, "ABOUT_FILE", options.about_file)
Fred Drake8b880931999-03-03 20:24:30 +0000377 l2hoption(fp, "ICONSERVER", options.icon_server)
Fred Drake52ea0ce1999-09-22 19:55:35 +0000378 l2hoption(fp, "IMAGE_TYPE", options.image_type)
Fred Drake8b880931999-03-03 20:24:30 +0000379 l2hoption(fp, "ADDRESS", options.address)
380 l2hoption(fp, "MAX_LINK_DEPTH", options.max_link_depth)
381 l2hoption(fp, "MAX_SPLIT_DEPTH", options.max_split_depth)
382 fp.write("1;\n")
383 fp.close()
384
385 def cleanup(self):
386 self.__have_temps = 0
387 for pattern in ("%s.aux", "%s.log", "%s.out", "%s.toc", "%s.bkm",
Fred Drakea871c2e1999-05-06 19:37:38 +0000388 "%s.idx", "%s.ilg", "%s.ind", "%s.pla",
Fred Drake8b880931999-03-03 20:24:30 +0000389 "%s.bbl", "%s.blg",
390 "mod%s.idx", "mod%s.ind", "mod%s.ilg",
391 ):
392 safe_unlink(pattern % self.doc)
Fred Drakea871c2e1999-05-06 19:37:38 +0000393 map(safe_unlink, glob.glob(self.doc + "*.syn"))
Fred Drake8b880931999-03-03 20:24:30 +0000394 for spec in ("IMG*", "*.pl", "WARNINGS", "index.dat", "modindex.dat"):
395 pattern = os.path.join(self.doc, spec)
396 map(safe_unlink, glob.glob(pattern))
397 if "dvi" not in self.options.formats:
398 safe_unlink(self.doc + ".dvi")
399 if os.path.isdir(self.doc + "-temp-html"):
400 shutil.rmtree(self.doc + "-temp-html", ignore_errors=1)
401 if not self.options.logging:
402 os.unlink(self.log_filename)
403 if not self.options.debugging:
404 os.unlink(self.l2h_aux_init_file)
405
406 def run(self, command):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000407 self.message(command)
408 rc = os.system("(%s) </dev/null >>%s 2>&1"
409 % (command, self.log_filename))
Fred Drake8b880931999-03-03 20:24:30 +0000410 if rc:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000411 self.warning(
412 "Session transcript and error messages are in %s."
Fred Drake8b880931999-03-03 20:24:30 +0000413 % self.log_filename)
414 sys.exit(rc)
415
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000416 def message(self, msg):
417 msg = "+++ " + msg
418 if not self.options.quiet:
419 print msg
Fred Drake52ea0ce1999-09-22 19:55:35 +0000420 self.log(msg + "\n")
421
422 def warning(self, msg):
423 msg = "*** %s\n" % msg
424 sys.stderr.write(msg)
425 self.log(msg)
426
427 def log(self, msg):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000428 fp = open(self.log_filename, "a")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000429 fp.write(msg)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000430 fp.close()
431
Fred Drake8b880931999-03-03 20:24:30 +0000432
433def safe_unlink(path):
434 try:
435 os.unlink(path)
436 except os.error:
437 pass
438
439
Fred Drakea871c2e1999-05-06 19:37:38 +0000440def split_pathname(path):
441 path = os.path.normpath(os.path.join(os.getcwd(), path))
442 dirname, basename = os.path.split(path)
Fred Drake8b880931999-03-03 20:24:30 +0000443 if basename[-4:] == ".tex":
444 basename = basename[:-4]
445 return dirname, basename
446
447
Fred Drakea871c2e1999-05-06 19:37:38 +0000448_doctype_rx = re.compile(r"\\documentclass(?:\[[^]]*\])?{([a-zA-Z]*)}")
449def get_doctype(path):
450 fp = open(path)
451 doctype = None
452 while 1:
453 line = fp.readline()
454 if not line:
455 break
456 m = _doctype_rx.match(line)
457 if m:
458 doctype = m.group(1)
459 break
460 fp.close()
461 return doctype
462
463
Fred Drake8b880931999-03-03 20:24:30 +0000464def main():
465 options = Options()
466 try:
467 args = options.parse(sys.argv[1:])
468 except getopt.error, msg:
469 error(options, msg)
470 if not args:
471 # attempt to locate single .tex file in current directory:
472 args = glob.glob("*.tex")
473 if not args:
474 error(options, "No file to process.")
475 if len(args) > 1:
476 error(options, "Could not deduce which files should be processed.")
477 #
478 # parameters are processed, let's go!
479 #
480 for path in args:
481 Job(options, path).build()
482
483
484def l2hoption(fp, option, value):
485 if value:
486 fp.write('$%s = "%s";\n' % (option, string_to_perl(str(value))))
487
488
489_to_perl = {}
490for c in map(chr, range(1, 256)):
491 _to_perl[c] = c
492_to_perl["@"] = "\\@"
493_to_perl["$"] = "\\$"
494_to_perl['"'] = '\\"'
495
496def string_to_perl(s):
497 return string.join(map(_to_perl.get, s), '')
498
499
500def check_for_bibtex(filename):
501 fp = open(filename)
502 pos = string.find(fp.read(), r"\bibdata{")
503 fp.close()
504 return pos >= 0
505
506def uniqify_module_table(filename):
507 lines = open(filename).readlines()
508 if len(lines) > 1:
509 if lines[-1] == lines[-2]:
510 del lines[-1]
511 open(filename, "w").writelines(lines)
512
513
514def new_index(filename, label="genindex"):
515 fp = open(filename, "w")
516 fp.write(r"""\
517\begin{theindex}
518\label{%s}
519\end{theindex}
520""" % label)
521 fp.close()
522
523
524if __name__ == "__main__":
525 main()