blob: 5ae9bbb78bec3578cc006767b7c8cc30f3043d8e [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 Drake8b880931999-03-03 20:24:30 +000021
22Other options:
23 --a4 Format for A4 paper.
24 --letter Format for US letter paper (the default).
25 --help, -H Show this text.
26 --logging, -l Log stdout and stderr to a file (*.how).
27 --debugging, -D Echo commands as they are executed.
28 --keep, -k Keep temporary files around.
29 --quiet, -q Do not print command output to stdout.
30 (stderr is also lost, sorry; see *.how for errors)
31"""
32
33import getopt
34import glob
35import os
Fred Drakea871c2e1999-05-06 19:37:38 +000036import re
Fred Drake8b880931999-03-03 20:24:30 +000037import shutil
38import string
39import sys
40import tempfile
41
42
43MYDIR = os.path.normpath(os.path.join(os.getcwd(), sys.path[0]))
44TOPDIR = os.path.normpath(os.path.join(MYDIR, os.pardir))
45
46ISTFILE = os.path.join(TOPDIR, "texinputs", "python.ist")
47NODE2LABEL_SCRIPT = os.path.join(MYDIR, "node2label.pl")
48L2H_INIT_FILE = os.path.join(TOPDIR, "perl", "l2hinit.perl")
49
50BIBTEX_BINARY = "bibtex"
51DVIPS_BINARY = "dvips"
52LATEX_BINARY = "latex"
53LATEX2HTML_BINARY = "latex2html"
54LYNX_BINARY = "lynx"
55MAKEINDEX_BINARY = "makeindex"
56PDFLATEX_BINARY = "pdflatex"
57PERL_BINARY = "perl"
58PYTHON_BINARY = "python"
59
60
61def usage(options):
62 print __doc__ % options
63
64def error(options, message, err=2):
65 sys.stdout = sys.stderr
66 print message
67 print
68 usage(options)
69 sys.exit(2)
70
71
72class Options:
73 program = os.path.basename(sys.argv[0])
74 #
75 address = ''
76 debugging = 0
77 discard_temps = 1
78 have_temps = 0
79 icon_server = None
Fred Drake52ea0ce1999-09-22 19:55:35 +000080 image_type = "gif"
Fred Drake8b880931999-03-03 20:24:30 +000081 logging = 0
82 max_link_depth = 3
83 max_split_depth = 6
84 paper = "letter"
85 quiet = 0
Fred Drake52ea0ce1999-09-22 19:55:35 +000086 runs = 0
Fred Drake8b880931999-03-03 20:24:30 +000087 style_file = os.path.join(TOPDIR, "html", "style.css")
Fred Drakecf1b06e1999-09-23 16:55:09 +000088 about_file = os.path.join(TOPDIR, "html", "about.dat")
Fred Drake8b880931999-03-03 20:24:30 +000089 #
90 DEFAULT_FORMATS = ("pdf",)
91 ALL_FORMATS = ("dvi", "html", "pdf", "ps", "text")
92
93 def __init__(self):
94 self.config_files = []
95 self.formats = []
96
97 def __getitem__(self, key):
98 # This is used when formatting the usage message.
99 try:
100 return getattr(self, key)
101 except AttributeError:
102 raise KeyError, key
103
104 def parse(self, args):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000105 opts, args = getopt.getopt(args, "Hi:a:s:lDkqr:",
Fred Drake8b880931999-03-03 20:24:30 +0000106 ["all", "postscript", "help", "iconserver=",
107 "address=", "a4", "l2h-config=", "letter",
108 "link=", "split=", "logging", "debugging",
Fred Drakecf1b06e1999-09-23 16:55:09 +0000109 "keep", "quiet", "runs=", "image-type=",
110 "about="]
Fred Drake52ea0ce1999-09-22 19:55:35 +0000111 + list(self.ALL_FORMATS))
Fred Drake8b880931999-03-03 20:24:30 +0000112 for opt, arg in opts:
113 if opt == "--all":
114 self.formats = list(self.ALL_FORMATS)
115 elif opt in ("-H", "--help"):
116 usage(self)
117 sys.exit()
118 elif opt == "--iconserver":
119 self.icon_server = arg
120 elif opt in ("-a", "--address"):
121 self.address = arg
122 elif opt == "--a4":
123 self.paper = "a4"
124 elif opt == "--letter":
125 self.paper = "letter"
126 elif opt == "--l2h-config":
127 self.config_files.append(arg)
128 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 Drake8b880931999-03-03 20:24:30 +0000148 #
149 # Format specifiers:
150 #
151 elif opt[2:] in self.ALL_FORMATS:
152 self.add_format(opt[2:])
153 elif opt == "--postscript":
154 # synonym for --ps
155 self.add_format("ps")
156 self.initialize()
157 #
158 # return the args to allow the caller access:
159 #
160 return args
161
162 def add_format(self, format):
163 """Add a format to the formats list if not present."""
164 if not format in self.formats:
165 self.formats.append(format)
166
167 def initialize(self):
168 """Complete initialization. This is needed if parse() isn't used."""
169 # add the default format if no formats were specified:
170 if not self.formats:
171 self.formats = self.DEFAULT_FORMATS
172 # determine the base set of texinputs directories:
173 texinputs = string.split(os.environ.get("TEXINPUTS", ""), os.pathsep)
174 if not texinputs:
175 texinputs = ['']
176 self.base_texinputs = [
177 os.path.join(TOPDIR, "paper-" + self.paper),
178 os.path.join(TOPDIR, "texinputs"),
179 ] + texinputs
180
181
182class Job:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000183 latex_runs = 0
184
Fred Drake8b880931999-03-03 20:24:30 +0000185 def __init__(self, options, path):
186 self.options = options
Fred Drakea871c2e1999-05-06 19:37:38 +0000187 self.doctype = get_doctype(path)
Fred Drake8b880931999-03-03 20:24:30 +0000188 self.filedir, self.doc = split_pathname(path)
189 self.log_filename = self.doc + ".how"
190 if os.path.exists(self.log_filename):
191 os.unlink(self.log_filename)
192 if os.path.exists(self.doc + ".l2h"):
193 self.l2h_aux_init_file = tempfile.mktemp()
194 else:
195 self.l2h_aux_init_file = self.doc + ".l2h"
196 self.write_l2h_aux_init_file()
197
198 def build(self):
199 self.setup_texinputs()
200 formats = self.options.formats
201 if "dvi" in formats or "ps" in formats:
202 self.build_dvi()
203 if "pdf" in formats:
204 self.build_pdf()
205 if "ps" in formats:
206 self.build_ps()
207 if "html" in formats:
208 self.require_temps()
209 self.build_html(self.doc)
210 if self.options.icon_server == ".":
Fred Drake52ea0ce1999-09-22 19:55:35 +0000211 pattern = os.path.join(TOPDIR, "html", "icons",
212 "*." + self.options.image_type)
213 imgs = glob.glob(pattern)
214 if not imgs:
215 self.warning(
216 "Could not locate support images of type %s."
217 % `self.options.image_type`)
218 for fn in imgs:
Fred Drake8b880931999-03-03 20:24:30 +0000219 new_fn = os.path.join(self.doc, os.path.basename(fn))
220 shutil.copyfile(fn, new_fn)
221 if "text" in formats:
222 self.require_temps()
223 tempdir = self.doc
224 need_html = "html" not in formats
225 if self.options.max_split_depth != 1:
226 fp = open(self.l2h_aux_init_file, "a")
227 fp.write("# re-hack this file for --text:\n")
228 l2hoption(fp, "MAX_SPLIT_DEPTH", "1")
229 fp.write("1;\n")
230 fp.close()
231 tempdir = self.doc + "-temp-html"
232 need_html = 1
233 if need_html:
234 self.build_html(tempdir, max_split_depth=1)
235 self.build_text(tempdir)
236 if self.options.discard_temps:
237 self.cleanup()
238
239 def setup_texinputs(self):
240 texinputs = [self.filedir] + list(self.options.base_texinputs)
241 os.environ["TEXINPUTS"] = string.join(texinputs, os.pathsep)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000242 self.message("TEXINPUTS=" + os.environ["TEXINPUTS"])
Fred Drake8b880931999-03-03 20:24:30 +0000243
Fred Drake8b880931999-03-03 20:24:30 +0000244 def build_aux(self, binary=None):
245 if binary is None:
246 binary = LATEX_BINARY
247 new_index( "%s.ind" % self.doc, "genindex")
248 new_index("mod%s.ind" % self.doc, "modindex")
249 self.run("%s %s" % (binary, self.doc))
250 self.use_bibtex = check_for_bibtex(self.doc + ".aux")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000251 self.latex_runs = 1
Fred Drake8b880931999-03-03 20:24:30 +0000252
253 def build_dvi(self):
254 self.use_latex(LATEX_BINARY)
255
256 def build_pdf(self):
257 self.use_latex(PDFLATEX_BINARY)
258
259 def use_latex(self, binary):
260 self.require_temps(binary=binary)
261 if os.path.isfile("mod%s.idx" % self.doc):
262 self.run("%s mod%s.idx" % (MAKEINDEX_BINARY, self.doc))
263 if os.path.isfile(self.doc + ".idx"):
264 # call to Doc/tools/fix_hack omitted; doesn't appear necessary
265 self.run("%s %s.idx" % (MAKEINDEX_BINARY, self.doc))
266 import indfix
267 indfix.process(self.doc + ".ind")
268 if self.use_bibtex:
269 self.run("%s %s" % (BIBTEX_BINARY, self.doc))
Fred Drakea871c2e1999-05-06 19:37:38 +0000270 self.process_synopsis_files()
271 #
272 # let the doctype-specific handler do some intermediate work:
273 #
274 if self.doctype == "manual":
275 self.use_latex_manual(binary=binary)
276 elif self.doctype == "howto":
277 self.use_latex_howto(binary=binary)
278 else:
279 raise RuntimeError, "unsupported document type: " + self.doctype
280 #
281 # and now finish it off:
282 #
283 if os.path.isfile(self.doc + ".toc") and binary == PDFLATEX_BINARY:
284 import toc2bkm
285 toc2bkm.process(self.doc + ".toc", self.doc + ".bkm", "section")
286 if self.use_bibtex:
287 self.run("%s %s" % (BIBTEX_BINARY, self.doc))
288 self.run("%s %s" % (binary, self.doc))
289
290 def use_latex_howto(self, binary):
Fred Drake8b880931999-03-03 20:24:30 +0000291 self.run("%s %s" % (binary, self.doc))
292 if os.path.isfile("mod%s.idx" % self.doc):
293 self.run("%s -s %s mod%s.idx"
294 % (MAKEINDEX_BINARY, ISTFILE, self.doc))
295 if os.path.isfile(self.doc + ".idx"):
296 self.run("%s -s %s %s.idx" % (MAKEINDEX_BINARY, ISTFILE, self.doc))
Fred Drakea871c2e1999-05-06 19:37:38 +0000297 self.process_synopsis_files()
298
299 def use_latex_manual(self, binary):
300 pass
301
302 def process_synopsis_files(self):
303 synopsis_files = glob.glob(self.doc + "*.syn")
304 for path in synopsis_files:
305 uniqify_module_table(path)
Fred Drake8b880931999-03-03 20:24:30 +0000306
307 def build_ps(self):
308 self.run("%s -N0 -o %s.ps %s" % (DVIPS_BINARY, self.doc, self.doc))
309
310 def build_html(self, builddir=None, max_split_depth=None):
311 if builddir is None:
312 builddir = self.doc
313 if max_split_depth is None:
314 max_split_depth = self.options.max_split_depth
315 texfile = None
316 for p in string.split(os.environ["TEXINPUTS"], os.pathsep):
317 fn = os.path.join(p, self.doc + ".tex")
318 if os.path.isfile(fn):
319 texfile = fn
320 break
321 if not texfile:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000322 self.warning("Could not locate %s.tex; aborting." % self.doc)
Fred Drake8b880931999-03-03 20:24:30 +0000323 sys.exit(1)
324 # remove leading ./ (or equiv.); might avoid problems w/ dvips
325 if texfile[:2] == os.curdir + os.sep:
326 texfile = texfile[2:]
327 # build the command line and run LaTeX2HTML:
328 args = [LATEX2HTML_BINARY,
329 "-init_file", L2H_INIT_FILE,
330 "-init_file", self.l2h_aux_init_file,
331 "-dir", builddir,
332 texfile
333 ]
334 self.run(string.join(args)) # XXX need quoting!
335 # ... postprocess
336 shutil.copyfile(self.options.style_file,
337 os.path.join(builddir, self.doc + ".css"))
Fred Drake4437fdf1999-05-03 14:29:07 +0000338 shutil.copyfile(os.path.join(builddir, self.doc + ".html"),
339 os.path.join(builddir, "index.html"))
Fred Drake8b880931999-03-03 20:24:30 +0000340 if max_split_depth != 1:
341 pwd = os.getcwd()
342 try:
343 os.chdir(builddir)
344 self.run("%s %s *.html" % (PERL_BINARY, NODE2LABEL_SCRIPT))
345 finally:
346 os.chdir(pwd)
347
348 def build_text(self, tempdir=None):
349 if tempdir is None:
350 tempdir = self.doc
351 indexfile = os.path.join(tempdir, "index.html")
352 self.run("%s -nolist -dump %s >%s.txt"
353 % (LYNX_BINARY, indexfile, self.doc))
354
355 def require_temps(self, binary=None):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000356 if not self.latex_runs:
Fred Drake8b880931999-03-03 20:24:30 +0000357 self.build_aux(binary=binary)
358
359 def write_l2h_aux_init_file(self):
360 fp = open(self.l2h_aux_init_file, "w")
361 fp.write("# auxillary init file for latex2html\n"
362 "# generated by mkhowto\n"
Fred Drake4437fdf1999-05-03 14:29:07 +0000363 "$NO_AUTO_LINK = 1;\n"
Fred Drake8b880931999-03-03 20:24:30 +0000364 )
365 options = self.options
366 for fn in options.config_files:
367 fp.write(open(fn).read())
368 fp.write("\n"
369 "\n"
370 'print "\nInitializing from file: %s\";\n\n'
371 % string_to_perl(fn))
Fred Drakecf1b06e1999-09-23 16:55:09 +0000372 l2hoption(fp, "ABOUT_FILE", options.about_file)
Fred Drake8b880931999-03-03 20:24:30 +0000373 l2hoption(fp, "ICONSERVER", options.icon_server)
Fred Drake52ea0ce1999-09-22 19:55:35 +0000374 l2hoption(fp, "IMAGE_TYPE", options.image_type)
Fred Drake8b880931999-03-03 20:24:30 +0000375 l2hoption(fp, "ADDRESS", options.address)
376 l2hoption(fp, "MAX_LINK_DEPTH", options.max_link_depth)
377 l2hoption(fp, "MAX_SPLIT_DEPTH", options.max_split_depth)
Fred Drake52ea0ce1999-09-22 19:55:35 +0000378 # this line needed in case $IMAGE_TYPE changed
379 fp.write("adjust_icon_information();\n")
Fred Drake8b880931999-03-03 20:24:30 +0000380 fp.write("1;\n")
381 fp.close()
382
383 def cleanup(self):
384 self.__have_temps = 0
385 for pattern in ("%s.aux", "%s.log", "%s.out", "%s.toc", "%s.bkm",
Fred Drakea871c2e1999-05-06 19:37:38 +0000386 "%s.idx", "%s.ilg", "%s.ind", "%s.pla",
Fred Drake8b880931999-03-03 20:24:30 +0000387 "%s.bbl", "%s.blg",
388 "mod%s.idx", "mod%s.ind", "mod%s.ilg",
389 ):
390 safe_unlink(pattern % self.doc)
Fred Drakea871c2e1999-05-06 19:37:38 +0000391 map(safe_unlink, glob.glob(self.doc + "*.syn"))
Fred Drake8b880931999-03-03 20:24:30 +0000392 for spec in ("IMG*", "*.pl", "WARNINGS", "index.dat", "modindex.dat"):
393 pattern = os.path.join(self.doc, spec)
394 map(safe_unlink, glob.glob(pattern))
395 if "dvi" not in self.options.formats:
396 safe_unlink(self.doc + ".dvi")
397 if os.path.isdir(self.doc + "-temp-html"):
398 shutil.rmtree(self.doc + "-temp-html", ignore_errors=1)
399 if not self.options.logging:
400 os.unlink(self.log_filename)
401 if not self.options.debugging:
402 os.unlink(self.l2h_aux_init_file)
403
404 def run(self, command):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000405 self.message(command)
406 rc = os.system("(%s) </dev/null >>%s 2>&1"
407 % (command, self.log_filename))
Fred Drake8b880931999-03-03 20:24:30 +0000408 if rc:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000409 self.warning(
410 "Session transcript and error messages are in %s."
Fred Drake8b880931999-03-03 20:24:30 +0000411 % self.log_filename)
412 sys.exit(rc)
413
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000414 def message(self, msg):
415 msg = "+++ " + msg
416 if not self.options.quiet:
417 print msg
Fred Drake52ea0ce1999-09-22 19:55:35 +0000418 self.log(msg + "\n")
419
420 def warning(self, msg):
421 msg = "*** %s\n" % msg
422 sys.stderr.write(msg)
423 self.log(msg)
424
425 def log(self, msg):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000426 fp = open(self.log_filename, "a")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000427 fp.write(msg)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000428 fp.close()
429
Fred Drake8b880931999-03-03 20:24:30 +0000430
431def safe_unlink(path):
432 try:
433 os.unlink(path)
434 except os.error:
435 pass
436
437
Fred Drakea871c2e1999-05-06 19:37:38 +0000438def split_pathname(path):
439 path = os.path.normpath(os.path.join(os.getcwd(), path))
440 dirname, basename = os.path.split(path)
Fred Drake8b880931999-03-03 20:24:30 +0000441 if basename[-4:] == ".tex":
442 basename = basename[:-4]
443 return dirname, basename
444
445
Fred Drakea871c2e1999-05-06 19:37:38 +0000446_doctype_rx = re.compile(r"\\documentclass(?:\[[^]]*\])?{([a-zA-Z]*)}")
447def get_doctype(path):
448 fp = open(path)
449 doctype = None
450 while 1:
451 line = fp.readline()
452 if not line:
453 break
454 m = _doctype_rx.match(line)
455 if m:
456 doctype = m.group(1)
457 break
458 fp.close()
459 return doctype
460
461
Fred Drake8b880931999-03-03 20:24:30 +0000462def main():
463 options = Options()
464 try:
465 args = options.parse(sys.argv[1:])
466 except getopt.error, msg:
467 error(options, msg)
468 if not args:
469 # attempt to locate single .tex file in current directory:
470 args = glob.glob("*.tex")
471 if not args:
472 error(options, "No file to process.")
473 if len(args) > 1:
474 error(options, "Could not deduce which files should be processed.")
475 #
476 # parameters are processed, let's go!
477 #
478 for path in args:
479 Job(options, path).build()
480
481
482def l2hoption(fp, option, value):
483 if value:
484 fp.write('$%s = "%s";\n' % (option, string_to_perl(str(value))))
485
486
487_to_perl = {}
488for c in map(chr, range(1, 256)):
489 _to_perl[c] = c
490_to_perl["@"] = "\\@"
491_to_perl["$"] = "\\$"
492_to_perl['"'] = '\\"'
493
494def string_to_perl(s):
495 return string.join(map(_to_perl.get, s), '')
496
497
498def check_for_bibtex(filename):
499 fp = open(filename)
500 pos = string.find(fp.read(), r"\bibdata{")
501 fp.close()
502 return pos >= 0
503
504def uniqify_module_table(filename):
505 lines = open(filename).readlines()
506 if len(lines) > 1:
507 if lines[-1] == lines[-2]:
508 del lines[-1]
509 open(filename, "w").writelines(lines)
510
511
512def new_index(filename, label="genindex"):
513 fp = open(filename, "w")
514 fp.write(r"""\
515\begin{theindex}
516\label{%s}
517\end{theindex}
518""" % label)
519 fp.close()
520
521
522if __name__ == "__main__":
523 main()