blob: 8f506c5ba553b8cd4413096ee94d24f82741ae84 [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:
Fred Drake55994412001-01-30 22:30:01 +00006 --html HyperText Markup Language (default)
7 --pdf Portable Document Format
Fred Drake8b880931999-03-03 20:24:30 +00008 --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.
Fred Drake73ec9832002-10-01 15:24:03 +000016 --dir Specify the directory for HTML output.
Fred Drake8b880931999-03-03 20:24:30 +000017 --link Specify the number of levels to include on each page.
18 --split, -s Specify a section level for page splitting, default: %(max_split_depth)s.
Fred Drake15a159c2002-10-01 15:20:20 +000019 --iconserver, -i Specify location of icons (default: ./).
Fred Drake52ea0ce1999-09-22 19:55:35 +000020 --image-type Specify the image type to use in HTML output;
21 values: gif (default), png.
Fred Drake9a257b42000-03-31 20:27:36 +000022 --numeric Don't rename the HTML files; just keep node#.html for
23 the filenames.
Fred Drakefcb87252000-08-29 18:15:05 +000024 --style Specify the CSS file to use for the output (filename,
25 not a URL).
Fred Drakedfa539d2000-08-31 06:58:34 +000026 --up-link URL to a parent document.
27 --up-title Title of a parent document.
Fred Drake56c8c272002-10-30 17:02:21 +000028 --favicon Icon to display in the browsers location bar.
Fred Drake8b880931999-03-03 20:24:30 +000029
30Other options:
31 --a4 Format for A4 paper.
32 --letter Format for US letter paper (the default).
33 --help, -H Show this text.
34 --logging, -l Log stdout and stderr to a file (*.how).
35 --debugging, -D Echo commands as they are executed.
36 --keep, -k Keep temporary files around.
37 --quiet, -q Do not print command output to stdout.
38 (stderr is also lost, sorry; see *.how for errors)
39"""
40
41import getopt
42import glob
43import os
Fred Drakea871c2e1999-05-06 19:37:38 +000044import re
Fred Drake8b880931999-03-03 20:24:30 +000045import shutil
Fred Drake8b880931999-03-03 20:24:30 +000046import sys
Fred Drake8b880931999-03-03 20:24:30 +000047
48
Fred Drakefcb87252000-08-29 18:15:05 +000049MYDIR = os.path.abspath(sys.path[0])
50TOPDIR = os.path.dirname(MYDIR)
Fred Drake8b880931999-03-03 20:24:30 +000051
52ISTFILE = os.path.join(TOPDIR, "texinputs", "python.ist")
53NODE2LABEL_SCRIPT = os.path.join(MYDIR, "node2label.pl")
54L2H_INIT_FILE = os.path.join(TOPDIR, "perl", "l2hinit.perl")
55
56BIBTEX_BINARY = "bibtex"
57DVIPS_BINARY = "dvips"
58LATEX_BINARY = "latex"
59LATEX2HTML_BINARY = "latex2html"
60LYNX_BINARY = "lynx"
61MAKEINDEX_BINARY = "makeindex"
62PDFLATEX_BINARY = "pdflatex"
63PERL_BINARY = "perl"
64PYTHON_BINARY = "python"
65
66
67def usage(options):
68 print __doc__ % options
69
70def error(options, message, err=2):
71 sys.stdout = sys.stderr
72 print message
73 print
74 usage(options)
75 sys.exit(2)
76
77
78class Options:
79 program = os.path.basename(sys.argv[0])
80 #
81 address = ''
Fred Drake50d1fcf2001-02-19 19:18:09 +000082 builddir = None
Fred Drake8b880931999-03-03 20:24:30 +000083 debugging = 0
84 discard_temps = 1
85 have_temps = 0
Fred Drake15a159c2002-10-01 15:20:20 +000086 icon_server = "."
Fred Drake52ea0ce1999-09-22 19:55:35 +000087 image_type = "gif"
Fred Drake8b880931999-03-03 20:24:30 +000088 logging = 0
89 max_link_depth = 3
90 max_split_depth = 6
91 paper = "letter"
92 quiet = 0
Fred Drake52ea0ce1999-09-22 19:55:35 +000093 runs = 0
Fred Drake9a257b42000-03-31 20:27:36 +000094 numeric = 0
Fred Drake42181db2001-01-09 22:02:10 +000095 global_module_index = None
Fred Drake8b880931999-03-03 20:24:30 +000096 style_file = os.path.join(TOPDIR, "html", "style.css")
Fred Drakecf1b06e1999-09-23 16:55:09 +000097 about_file = os.path.join(TOPDIR, "html", "about.dat")
Fred Drakedfa539d2000-08-31 06:58:34 +000098 up_link = None
99 up_title = None
Fred Drake56c8c272002-10-30 17:02:21 +0000100 favicon = None
Fred Drake8b880931999-03-03 20:24:30 +0000101 #
Fred Drake3ce28e42001-10-30 16:09:51 +0000102 # 'dvips_safe' is a weird option. It is used mostly to make
103 # LaTeX2HTML not try to be too smart about protecting the user
104 # from a bad version of dvips -- some versions would core dump if
105 # the path to the source DVI contained a dot, and it's appearantly
106 # difficult to determine if the version available has that bug.
107 # This option gets set when PostScript output is requested
108 # (because we're going to run dvips regardless, and we'll either
109 # know it succeeds before LaTeX2HTML is run, or we'll have
110 # detected the failure and bailed), or the user asserts that it's
111 # safe from the command line.
112 #
113 # So, why does LaTeX2HTML think it appropriate to protect the user
114 # from a dvips that's only potentially going to core dump? Only
115 # because they want to avoid doing a lot of work just to have to
116 # bail later with no useful intermediates. Unfortunately, they
117 # bail *before* they know whether dvips will be needed at all.
118 # I've gone around the bush a few times with the LaTeX2HTML
119 # developers over whether this is appropriate behavior, and they
120 # don't seem interested in changing their position.
121 #
122 dvips_safe = 0
123 #
Fred Drake55994412001-01-30 22:30:01 +0000124 DEFAULT_FORMATS = ("html",)
Fred Drake8b880931999-03-03 20:24:30 +0000125 ALL_FORMATS = ("dvi", "html", "pdf", "ps", "text")
126
127 def __init__(self):
Fred Drake8b880931999-03-03 20:24:30 +0000128 self.formats = []
Fred Drake8bc627a2000-08-31 06:14:38 +0000129 self.l2h_init_files = []
Fred Drake8b880931999-03-03 20:24:30 +0000130
131 def __getitem__(self, key):
132 # This is used when formatting the usage message.
133 try:
134 return getattr(self, key)
135 except AttributeError:
136 raise KeyError, key
137
138 def parse(self, args):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000139 opts, args = getopt.getopt(args, "Hi:a:s:lDkqr:",
Fred Drake8b880931999-03-03 20:24:30 +0000140 ["all", "postscript", "help", "iconserver=",
Fred Drake8bc627a2000-08-31 06:14:38 +0000141 "address=", "a4", "letter", "l2h-init=",
Fred Drake8b880931999-03-03 20:24:30 +0000142 "link=", "split=", "logging", "debugging",
Fred Drakecf1b06e1999-09-23 16:55:09 +0000143 "keep", "quiet", "runs=", "image-type=",
Fred Drake50d1fcf2001-02-19 19:18:09 +0000144 "about=", "numeric", "style=", "paper=",
145 "up-link=", "up-title=", "dir=",
Fred Drake56c8c272002-10-30 17:02:21 +0000146 "global-module-index=", "dvips-safe",
147 "favicon="]
Fred Drake52ea0ce1999-09-22 19:55:35 +0000148 + list(self.ALL_FORMATS))
Fred Drake8b880931999-03-03 20:24:30 +0000149 for opt, arg in opts:
150 if opt == "--all":
151 self.formats = list(self.ALL_FORMATS)
Fred Drake3ce28e42001-10-30 16:09:51 +0000152 self.dvips_safe = "ps" in self.formats
Fred Drake8b880931999-03-03 20:24:30 +0000153 elif opt in ("-H", "--help"):
154 usage(self)
155 sys.exit()
156 elif opt == "--iconserver":
157 self.icon_server = arg
158 elif opt in ("-a", "--address"):
159 self.address = arg
160 elif opt == "--a4":
161 self.paper = "a4"
162 elif opt == "--letter":
163 self.paper = "letter"
Fred Drake8b880931999-03-03 20:24:30 +0000164 elif opt == "--link":
165 self.max_link_depth = int(arg)
166 elif opt in ("-s", "--split"):
167 self.max_split_depth = int(arg)
168 elif opt in ("-l", "--logging"):
169 self.logging = self.logging + 1
170 elif opt in ("-D", "--debugging"):
171 self.debugging = self.debugging + 1
172 elif opt in ("-k", "--keep"):
173 self.discard_temps = 0
174 elif opt in ("-q", "--quiet"):
175 self.quiet = 1
Fred Drake52ea0ce1999-09-22 19:55:35 +0000176 elif opt in ("-r", "--runs"):
177 self.runs = int(arg)
178 elif opt == "--image-type":
179 self.image_type = arg
Fred Drakecf1b06e1999-09-23 16:55:09 +0000180 elif opt == "--about":
181 # always make this absolute:
182 self.about_file = os.path.normpath(
Fred Drakefcb87252000-08-29 18:15:05 +0000183 os.path.abspath(arg))
Fred Drake9a257b42000-03-31 20:27:36 +0000184 elif opt == "--numeric":
185 self.numeric = 1
Fred Drakefcb87252000-08-29 18:15:05 +0000186 elif opt == "--style":
187 self.style_file = os.path.abspath(arg)
Fred Drake8bc627a2000-08-31 06:14:38 +0000188 elif opt == "--l2h-init":
189 self.l2h_init_files.append(os.path.abspath(arg))
Fred Drake56c8c272002-10-30 17:02:21 +0000190 elif opt == "--favicon":
191 self.favicon = arg
Fred Drakedfa539d2000-08-31 06:58:34 +0000192 elif opt == "--up-link":
193 self.up_link = arg
194 elif opt == "--up-title":
195 self.up_title = arg
Fred Drake42181db2001-01-09 22:02:10 +0000196 elif opt == "--global-module-index":
197 self.global_module_index = arg
Fred Drake50d1fcf2001-02-19 19:18:09 +0000198 elif opt == "--dir":
Fred Drake1cb560a2001-08-10 20:17:09 +0000199 if os.sep == "\\":
Thomas Heller6122c122003-09-27 19:35:37 +0000200 arg = re.sub("/", "\\\\", arg)
Fred Drakec0e066a2002-10-01 15:30:56 +0000201 self.builddir = os.path.expanduser(arg)
Fred Drake50d1fcf2001-02-19 19:18:09 +0000202 elif opt == "--paper":
203 self.paper = arg
Fred Drake3ce28e42001-10-30 16:09:51 +0000204 elif opt == "--dvips-safe":
205 self.dvips_safe = 1
Fred Drake8b880931999-03-03 20:24:30 +0000206 #
207 # Format specifiers:
208 #
209 elif opt[2:] in self.ALL_FORMATS:
210 self.add_format(opt[2:])
211 elif opt == "--postscript":
212 # synonym for --ps
213 self.add_format("ps")
214 self.initialize()
215 #
216 # return the args to allow the caller access:
217 #
218 return args
219
220 def add_format(self, format):
221 """Add a format to the formats list if not present."""
222 if not format in self.formats:
Fred Drake3ce28e42001-10-30 16:09:51 +0000223 if format == "ps":
224 # assume this is safe since we're going to run it anyway
225 self.dvips_safe = 1
Fred Drake8b880931999-03-03 20:24:30 +0000226 self.formats.append(format)
227
228 def initialize(self):
229 """Complete initialization. This is needed if parse() isn't used."""
230 # add the default format if no formats were specified:
231 if not self.formats:
232 self.formats = self.DEFAULT_FORMATS
233 # determine the base set of texinputs directories:
Fred Drake2ee37ff2003-09-27 07:05:12 +0000234 texinputs = os.environ.get("TEXINPUTS", "").split(os.pathsep)
Fred Drake8b880931999-03-03 20:24:30 +0000235 if not texinputs:
236 texinputs = ['']
Fred Drake2ee37ff2003-09-27 07:05:12 +0000237 mydirs = [os.path.join(TOPDIR, "paper-" + self.paper),
238 os.path.join(TOPDIR, "texinputs"),
239 ]
240 if '' in texinputs:
241 i = texinputs.index('')
242 texinputs[i:i] = mydirs
243 else:
244 texinputs += mydirs
245 self.base_texinputs = texinputs
Fred Drakebfd80dd2001-06-23 03:06:01 +0000246 if self.builddir:
247 self.builddir = os.path.abspath(self.builddir)
Fred Drake8b880931999-03-03 20:24:30 +0000248
249
250class Job:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000251 latex_runs = 0
252
Fred Drake8b880931999-03-03 20:24:30 +0000253 def __init__(self, options, path):
254 self.options = options
Fred Drakea871c2e1999-05-06 19:37:38 +0000255 self.doctype = get_doctype(path)
Fred Drake8b880931999-03-03 20:24:30 +0000256 self.filedir, self.doc = split_pathname(path)
Fred Drakebfd80dd2001-06-23 03:06:01 +0000257 self.builddir = os.path.abspath(options.builddir or self.doc)
Fred Drakeaebbca32001-07-17 14:46:09 +0000258 if ("html" in options.formats or "text" in options.formats):
259 if not os.path.exists(self.builddir):
260 os.mkdir(self.builddir)
261 self.log_filename = os.path.join(self.builddir, self.doc + ".how")
262 else:
263 self.log_filename = os.path.abspath(self.doc + ".how")
Fred Drake8b880931999-03-03 20:24:30 +0000264 if os.path.exists(self.log_filename):
265 os.unlink(self.log_filename)
Fred Drake246beb22002-08-27 16:34:54 +0000266 l2hconf = self.doc + ".l2h"
267 if os.path.exists(l2hconf):
268 if os.path.exists(l2hconf + "~"):
269 os.unlink(l2hconf + "~")
270 os.rename(l2hconf, l2hconf + "~")
271 self.l2h_aux_init_file = self.doc + ".l2h"
Fred Drake8b880931999-03-03 20:24:30 +0000272 self.write_l2h_aux_init_file()
273
274 def build(self):
275 self.setup_texinputs()
276 formats = self.options.formats
277 if "dvi" in formats or "ps" in formats:
278 self.build_dvi()
279 if "pdf" in formats:
280 self.build_pdf()
281 if "ps" in formats:
282 self.build_ps()
283 if "html" in formats:
284 self.require_temps()
Fred Drakebfd80dd2001-06-23 03:06:01 +0000285 self.build_html(self.builddir)
Fred Drake8b880931999-03-03 20:24:30 +0000286 if self.options.icon_server == ".":
Fred Drake52ea0ce1999-09-22 19:55:35 +0000287 pattern = os.path.join(TOPDIR, "html", "icons",
288 "*." + self.options.image_type)
289 imgs = glob.glob(pattern)
290 if not imgs:
291 self.warning(
292 "Could not locate support images of type %s."
293 % `self.options.image_type`)
294 for fn in imgs:
Barry Warsawcfb30e22002-10-01 15:38:01 +0000295 new_fn = os.path.join(self.builddir, os.path.basename(fn))
Fred Drake8b880931999-03-03 20:24:30 +0000296 shutil.copyfile(fn, new_fn)
297 if "text" in formats:
298 self.require_temps()
299 tempdir = self.doc
300 need_html = "html" not in formats
301 if self.options.max_split_depth != 1:
302 fp = open(self.l2h_aux_init_file, "a")
303 fp.write("# re-hack this file for --text:\n")
304 l2hoption(fp, "MAX_SPLIT_DEPTH", "1")
305 fp.write("1;\n")
306 fp.close()
307 tempdir = self.doc + "-temp-html"
308 need_html = 1
309 if need_html:
310 self.build_html(tempdir, max_split_depth=1)
311 self.build_text(tempdir)
312 if self.options.discard_temps:
313 self.cleanup()
314
315 def setup_texinputs(self):
Fred Drake2ee37ff2003-09-27 07:05:12 +0000316 texinputs = [self.filedir] + self.options.base_texinputs
317 os.environ["TEXINPUTS"] = os.pathsep.join(texinputs)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000318 self.message("TEXINPUTS=" + os.environ["TEXINPUTS"])
Fred Drake8b880931999-03-03 20:24:30 +0000319
Fred Drake8b880931999-03-03 20:24:30 +0000320 def build_aux(self, binary=None):
321 if binary is None:
322 binary = LATEX_BINARY
323 new_index( "%s.ind" % self.doc, "genindex")
324 new_index("mod%s.ind" % self.doc, "modindex")
325 self.run("%s %s" % (binary, self.doc))
326 self.use_bibtex = check_for_bibtex(self.doc + ".aux")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000327 self.latex_runs = 1
Fred Drake8b880931999-03-03 20:24:30 +0000328
329 def build_dvi(self):
330 self.use_latex(LATEX_BINARY)
331
332 def build_pdf(self):
333 self.use_latex(PDFLATEX_BINARY)
334
335 def use_latex(self, binary):
336 self.require_temps(binary=binary)
Fred Drakedf84fac2000-09-20 05:49:09 +0000337 if self.latex_runs < 2:
338 if os.path.isfile("mod%s.idx" % self.doc):
339 self.run("%s mod%s.idx" % (MAKEINDEX_BINARY, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000340 use_indfix = 0
Fred Drakedf84fac2000-09-20 05:49:09 +0000341 if os.path.isfile(self.doc + ".idx"):
Fred Drake9dce7b32000-11-03 02:57:31 +0000342 use_indfix = 1
Fred Drakedf84fac2000-09-20 05:49:09 +0000343 # call to Doc/tools/fix_hack omitted; doesn't appear necessary
344 self.run("%s %s.idx" % (MAKEINDEX_BINARY, self.doc))
345 import indfix
346 indfix.process(self.doc + ".ind")
347 if self.use_bibtex:
348 self.run("%s %s" % (BIBTEX_BINARY, self.doc))
349 self.process_synopsis_files()
Fred Drakedf84fac2000-09-20 05:49:09 +0000350 self.run("%s %s" % (binary, self.doc))
Fred Drakeb258bed2001-02-12 15:30:22 +0000351 self.latex_runs = self.latex_runs + 1
Fred Drakedf84fac2000-09-20 05:49:09 +0000352 if os.path.isfile("mod%s.idx" % self.doc):
353 self.run("%s -s %s mod%s.idx"
354 % (MAKEINDEX_BINARY, ISTFILE, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000355 if use_indfix:
Fred Drakedf84fac2000-09-20 05:49:09 +0000356 self.run("%s -s %s %s.idx"
357 % (MAKEINDEX_BINARY, ISTFILE, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000358 indfix.process(self.doc + ".ind")
Fred Drakedf84fac2000-09-20 05:49:09 +0000359 self.process_synopsis_files()
Fred Drakea871c2e1999-05-06 19:37:38 +0000360 #
361 # and now finish it off:
362 #
363 if os.path.isfile(self.doc + ".toc") and binary == PDFLATEX_BINARY:
364 import toc2bkm
Fred Drake239e1d52000-09-05 21:45:11 +0000365 if self.doctype == "manual":
366 bigpart = "chapter"
367 else:
368 bigpart = "section"
369 toc2bkm.process(self.doc + ".toc", self.doc + ".bkm", bigpart)
Fred Drakea871c2e1999-05-06 19:37:38 +0000370 if self.use_bibtex:
371 self.run("%s %s" % (BIBTEX_BINARY, self.doc))
372 self.run("%s %s" % (binary, self.doc))
Fred Drakeb258bed2001-02-12 15:30:22 +0000373 self.latex_runs = self.latex_runs + 1
Fred Drakea871c2e1999-05-06 19:37:38 +0000374
375 def process_synopsis_files(self):
376 synopsis_files = glob.glob(self.doc + "*.syn")
377 for path in synopsis_files:
378 uniqify_module_table(path)
Fred Drake8b880931999-03-03 20:24:30 +0000379
380 def build_ps(self):
381 self.run("%s -N0 -o %s.ps %s" % (DVIPS_BINARY, self.doc, self.doc))
382
Fred Drakeaebbca32001-07-17 14:46:09 +0000383 def build_html(self, builddir, max_split_depth=None):
Fred Drake8b880931999-03-03 20:24:30 +0000384 if max_split_depth is None:
385 max_split_depth = self.options.max_split_depth
386 texfile = None
Fred Drake2ee37ff2003-09-27 07:05:12 +0000387 for p in os.environ["TEXINPUTS"].split(os.pathsep):
Fred Drake8b880931999-03-03 20:24:30 +0000388 fn = os.path.join(p, self.doc + ".tex")
389 if os.path.isfile(fn):
390 texfile = fn
391 break
392 if not texfile:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000393 self.warning("Could not locate %s.tex; aborting." % self.doc)
Fred Drake8b880931999-03-03 20:24:30 +0000394 sys.exit(1)
395 # remove leading ./ (or equiv.); might avoid problems w/ dvips
396 if texfile[:2] == os.curdir + os.sep:
397 texfile = texfile[2:]
398 # build the command line and run LaTeX2HTML:
Fred Drakeba828782000-04-03 04:19:14 +0000399 if not os.path.isdir(builddir):
400 os.mkdir(builddir)
Fred Drakef3d41272000-09-14 22:25:47 +0000401 else:
402 for fname in glob.glob(os.path.join(builddir, "*.html")):
403 os.unlink(fname)
Fred Drake8b880931999-03-03 20:24:30 +0000404 args = [LATEX2HTML_BINARY,
Fred Drake8b880931999-03-03 20:24:30 +0000405 "-init_file", self.l2h_aux_init_file,
406 "-dir", builddir,
407 texfile
408 ]
Fred Drake2ee37ff2003-09-27 07:05:12 +0000409 self.run(" ".join(args)) # XXX need quoting!
Fred Drake8b880931999-03-03 20:24:30 +0000410 # ... postprocess
411 shutil.copyfile(self.options.style_file,
412 os.path.join(builddir, self.doc + ".css"))
Fred Drake4437fdf1999-05-03 14:29:07 +0000413 shutil.copyfile(os.path.join(builddir, self.doc + ".html"),
414 os.path.join(builddir, "index.html"))
Fred Drakecfef00962001-03-02 16:26:45 +0000415 if max_split_depth != 1:
Fred Drakeaf922182001-05-09 04:03:16 +0000416 label_file = os.path.join(builddir, "labels.pl")
417 fp = open(label_file)
418 about_node = None
419 target = " = q/about/;\n"
420 x = len(target)
421 while 1:
422 line = fp.readline()
423 if not line:
424 break
425 if line[-x:] == target:
Fred Drakecfef00962001-03-02 16:26:45 +0000426 line = fp.readline()
Fred Drakeaf922182001-05-09 04:03:16 +0000427 m = re.search(r"\|(node\d+\.[a-z]+)\|", line)
428 about_node = m.group(1)
429 shutil.copyfile(os.path.join(builddir, about_node),
430 os.path.join(builddir, "about.html"))
431 break
432 if not self.options.numeric:
Fred Drakecfef00962001-03-02 16:26:45 +0000433 pwd = os.getcwd()
434 try:
435 os.chdir(builddir)
436 self.run("%s %s *.html" % (PERL_BINARY, NODE2LABEL_SCRIPT))
437 finally:
438 os.chdir(pwd)
Fred Drake376f0ef2003-01-17 21:25:04 +0000439 # These files need to be cleaned up here since builddir there
440 # can be more than one, so we clean each of them.
441 if self.options.discard_temps:
442 for fn in ("images.tex", "images.log", "images.aux"):
443 safe_unlink(os.path.join(builddir, fn))
Fred Drake8b880931999-03-03 20:24:30 +0000444
445 def build_text(self, tempdir=None):
446 if tempdir is None:
447 tempdir = self.doc
448 indexfile = os.path.join(tempdir, "index.html")
449 self.run("%s -nolist -dump %s >%s.txt"
450 % (LYNX_BINARY, indexfile, self.doc))
451
452 def require_temps(self, binary=None):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000453 if not self.latex_runs:
Fred Drake8b880931999-03-03 20:24:30 +0000454 self.build_aux(binary=binary)
455
456 def write_l2h_aux_init_file(self):
Fred Drake8bc627a2000-08-31 06:14:38 +0000457 options = self.options
Fred Drake8b880931999-03-03 20:24:30 +0000458 fp = open(self.l2h_aux_init_file, "w")
Fred Drake19157542000-07-31 17:47:49 +0000459 d = string_to_perl(os.path.dirname(L2H_INIT_FILE))
460 fp.write("package main;\n"
461 "push (@INC, '%s');\n"
462 "$mydir = '%s';\n"
463 % (d, d))
Fred Drake498c18f2000-07-24 23:03:32 +0000464 fp.write(open(L2H_INIT_FILE).read())
Fred Drake8bc627a2000-08-31 06:14:38 +0000465 for filename in options.l2h_init_files:
466 fp.write("\n# initialization code incorporated from:\n# ")
467 fp.write(filename)
468 fp.write("\n")
469 fp.write(open(filename).read())
Fred Drake498c18f2000-07-24 23:03:32 +0000470 fp.write("\n"
471 "# auxillary init file for latex2html\n"
Fred Drake8b880931999-03-03 20:24:30 +0000472 "# generated by mkhowto\n"
Fred Drake4437fdf1999-05-03 14:29:07 +0000473 "$NO_AUTO_LINK = 1;\n"
Fred Drake8b880931999-03-03 20:24:30 +0000474 )
Fred Drakecf1b06e1999-09-23 16:55:09 +0000475 l2hoption(fp, "ABOUT_FILE", options.about_file)
Fred Drake8b880931999-03-03 20:24:30 +0000476 l2hoption(fp, "ICONSERVER", options.icon_server)
Fred Drake52ea0ce1999-09-22 19:55:35 +0000477 l2hoption(fp, "IMAGE_TYPE", options.image_type)
Fred Drake8b880931999-03-03 20:24:30 +0000478 l2hoption(fp, "ADDRESS", options.address)
479 l2hoption(fp, "MAX_LINK_DEPTH", options.max_link_depth)
480 l2hoption(fp, "MAX_SPLIT_DEPTH", options.max_split_depth)
Fred Drakedfa539d2000-08-31 06:58:34 +0000481 l2hoption(fp, "EXTERNAL_UP_LINK", options.up_link)
482 l2hoption(fp, "EXTERNAL_UP_TITLE", options.up_title)
Fred Drake56c8c272002-10-30 17:02:21 +0000483 l2hoption(fp, "FAVORITES_ICON", options.favicon)
Fred Drake42181db2001-01-09 22:02:10 +0000484 l2hoption(fp, "GLOBAL_MODULE_INDEX", options.global_module_index)
Fred Drake3ce28e42001-10-30 16:09:51 +0000485 l2hoption(fp, "DVIPS_SAFE", options.dvips_safe)
Fred Drake8b880931999-03-03 20:24:30 +0000486 fp.write("1;\n")
487 fp.close()
488
489 def cleanup(self):
490 self.__have_temps = 0
491 for pattern in ("%s.aux", "%s.log", "%s.out", "%s.toc", "%s.bkm",
Fred Drakea871c2e1999-05-06 19:37:38 +0000492 "%s.idx", "%s.ilg", "%s.ind", "%s.pla",
Fred Drake8b880931999-03-03 20:24:30 +0000493 "%s.bbl", "%s.blg",
494 "mod%s.idx", "mod%s.ind", "mod%s.ilg",
495 ):
496 safe_unlink(pattern % self.doc)
Fred Drakea871c2e1999-05-06 19:37:38 +0000497 map(safe_unlink, glob.glob(self.doc + "*.syn"))
Fred Drake8b880931999-03-03 20:24:30 +0000498 for spec in ("IMG*", "*.pl", "WARNINGS", "index.dat", "modindex.dat"):
499 pattern = os.path.join(self.doc, spec)
500 map(safe_unlink, glob.glob(pattern))
501 if "dvi" not in self.options.formats:
502 safe_unlink(self.doc + ".dvi")
503 if os.path.isdir(self.doc + "-temp-html"):
504 shutil.rmtree(self.doc + "-temp-html", ignore_errors=1)
505 if not self.options.logging:
506 os.unlink(self.log_filename)
507 if not self.options.debugging:
508 os.unlink(self.l2h_aux_init_file)
509
510 def run(self, command):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000511 self.message(command)
Fred Drake1cb560a2001-08-10 20:17:09 +0000512 if sys.platform.startswith("win"):
513 rc = os.system(command)
514 else:
515 rc = os.system("(%s) </dev/null >>%s 2>&1"
516 % (command, self.log_filename))
Fred Drake8b880931999-03-03 20:24:30 +0000517 if rc:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000518 self.warning(
519 "Session transcript and error messages are in %s."
Fred Drake8b880931999-03-03 20:24:30 +0000520 % self.log_filename)
Fred Drakec868d162003-05-14 04:16:14 +0000521 result = 1
Fred Drake1cb560a2001-08-10 20:17:09 +0000522 if hasattr(os, "WIFEXITED"):
523 if os.WIFEXITED(rc):
Fred Drakec868d162003-05-14 04:16:14 +0000524 result = os.WEXITSTATUS(rc)
525 self.warning("Exited with status %s." % result)
Fred Drake1cb560a2001-08-10 20:17:09 +0000526 else:
527 self.warning("Killed by signal %s." % os.WSTOPSIG(rc))
528 else:
529 self.warning("Return code: %s" % rc)
Fred Drake4e3f2752001-02-04 15:20:26 +0000530 sys.stderr.write("The relevant lines from the transcript are:\n")
531 sys.stderr.write("-" * 72 + "\n")
532 sys.stderr.writelines(get_run_transcript(self.log_filename))
Fred Drakec868d162003-05-14 04:16:14 +0000533 sys.exit(result)
Fred Drake8b880931999-03-03 20:24:30 +0000534
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000535 def message(self, msg):
536 msg = "+++ " + msg
537 if not self.options.quiet:
538 print msg
Fred Drake52ea0ce1999-09-22 19:55:35 +0000539 self.log(msg + "\n")
540
541 def warning(self, msg):
542 msg = "*** %s\n" % msg
543 sys.stderr.write(msg)
544 self.log(msg)
545
546 def log(self, msg):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000547 fp = open(self.log_filename, "a")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000548 fp.write(msg)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000549 fp.close()
550
Fred Drake8b880931999-03-03 20:24:30 +0000551
Fred Drake4e3f2752001-02-04 15:20:26 +0000552def get_run_transcript(filename):
553 """Return lines from the transcript file for the most recent run() call."""
554 fp = open(filename)
555 lines = fp.readlines()
556 fp.close()
557 lines.reverse()
558 L = []
559 for line in lines:
560 L.append(line)
561 if line[:4] == "+++ ":
562 break
563 L.reverse()
564 return L
565
566
Fred Drake8b880931999-03-03 20:24:30 +0000567def safe_unlink(path):
Fred Drake4e3f2752001-02-04 15:20:26 +0000568 """Unlink a file without raising an error if it doesn't exist."""
Fred Drake8b880931999-03-03 20:24:30 +0000569 try:
570 os.unlink(path)
571 except os.error:
572 pass
573
574
Fred Drakea871c2e1999-05-06 19:37:38 +0000575def split_pathname(path):
Fred Drakebfd80dd2001-06-23 03:06:01 +0000576 path = os.path.abspath(path)
Fred Drakea871c2e1999-05-06 19:37:38 +0000577 dirname, basename = os.path.split(path)
Fred Drake8b880931999-03-03 20:24:30 +0000578 if basename[-4:] == ".tex":
579 basename = basename[:-4]
580 return dirname, basename
581
582
Fred Drakea871c2e1999-05-06 19:37:38 +0000583_doctype_rx = re.compile(r"\\documentclass(?:\[[^]]*\])?{([a-zA-Z]*)}")
584def get_doctype(path):
585 fp = open(path)
586 doctype = None
587 while 1:
588 line = fp.readline()
589 if not line:
590 break
591 m = _doctype_rx.match(line)
592 if m:
593 doctype = m.group(1)
594 break
595 fp.close()
596 return doctype
597
598
Fred Drake8b880931999-03-03 20:24:30 +0000599def main():
600 options = Options()
601 try:
602 args = options.parse(sys.argv[1:])
603 except getopt.error, msg:
604 error(options, msg)
605 if not args:
606 # attempt to locate single .tex file in current directory:
607 args = glob.glob("*.tex")
608 if not args:
609 error(options, "No file to process.")
610 if len(args) > 1:
611 error(options, "Could not deduce which files should be processed.")
612 #
613 # parameters are processed, let's go!
614 #
615 for path in args:
616 Job(options, path).build()
617
618
619def l2hoption(fp, option, value):
620 if value:
621 fp.write('$%s = "%s";\n' % (option, string_to_perl(str(value))))
622
623
624_to_perl = {}
625for c in map(chr, range(1, 256)):
626 _to_perl[c] = c
627_to_perl["@"] = "\\@"
628_to_perl["$"] = "\\$"
629_to_perl['"'] = '\\"'
630
631def string_to_perl(s):
Fred Drake2ee37ff2003-09-27 07:05:12 +0000632 return ''.join(map(_to_perl.get, s))
Fred Drake8b880931999-03-03 20:24:30 +0000633
634
635def check_for_bibtex(filename):
636 fp = open(filename)
Fred Drake2ee37ff2003-09-27 07:05:12 +0000637 pos = fp.read().find(r"\bibdata{")
Fred Drake8b880931999-03-03 20:24:30 +0000638 fp.close()
639 return pos >= 0
640
641def uniqify_module_table(filename):
642 lines = open(filename).readlines()
643 if len(lines) > 1:
644 if lines[-1] == lines[-2]:
645 del lines[-1]
646 open(filename, "w").writelines(lines)
647
648
649def new_index(filename, label="genindex"):
650 fp = open(filename, "w")
651 fp.write(r"""\
652\begin{theindex}
653\label{%s}
654\end{theindex}
655""" % label)
656 fp.close()
657
658
659if __name__ == "__main__":
660 main()