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