blob: 7163974a2b2c30429ea66f8a058aa8b020c2ff9c [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
46import string
47import sys
Fred Drake8b880931999-03-03 20:24:30 +000048
49
Fred Drake964c0742001-05-29 16:10:07 +000050if not hasattr(os.path, "abspath"):
Fred Drakebfd80dd2001-06-23 03:06:01 +000051 # Python 1.5.1 or earlier
Fred Drake964c0742001-05-29 16:10:07 +000052 def abspath(path):
53 """Return an absolute path."""
54 if not os.path.isabs(path):
55 path = os.path.join(os.getcwd(), path)
56 return os.path.normpath(path)
57
58 os.path.abspath = abspath
59
60
Fred Drakefcb87252000-08-29 18:15:05 +000061MYDIR = os.path.abspath(sys.path[0])
62TOPDIR = os.path.dirname(MYDIR)
Fred Drake8b880931999-03-03 20:24:30 +000063
64ISTFILE = os.path.join(TOPDIR, "texinputs", "python.ist")
65NODE2LABEL_SCRIPT = os.path.join(MYDIR, "node2label.pl")
66L2H_INIT_FILE = os.path.join(TOPDIR, "perl", "l2hinit.perl")
67
68BIBTEX_BINARY = "bibtex"
69DVIPS_BINARY = "dvips"
70LATEX_BINARY = "latex"
71LATEX2HTML_BINARY = "latex2html"
72LYNX_BINARY = "lynx"
73MAKEINDEX_BINARY = "makeindex"
74PDFLATEX_BINARY = "pdflatex"
75PERL_BINARY = "perl"
76PYTHON_BINARY = "python"
77
78
79def usage(options):
80 print __doc__ % options
81
82def error(options, message, err=2):
83 sys.stdout = sys.stderr
84 print message
85 print
86 usage(options)
87 sys.exit(2)
88
89
90class Options:
91 program = os.path.basename(sys.argv[0])
92 #
93 address = ''
Fred Drake50d1fcf2001-02-19 19:18:09 +000094 builddir = None
Fred Drake8b880931999-03-03 20:24:30 +000095 debugging = 0
96 discard_temps = 1
97 have_temps = 0
Fred Drake15a159c2002-10-01 15:20:20 +000098 icon_server = "."
Fred Drake52ea0ce1999-09-22 19:55:35 +000099 image_type = "gif"
Fred Drake8b880931999-03-03 20:24:30 +0000100 logging = 0
101 max_link_depth = 3
102 max_split_depth = 6
103 paper = "letter"
104 quiet = 0
Fred Drake52ea0ce1999-09-22 19:55:35 +0000105 runs = 0
Fred Drake9a257b42000-03-31 20:27:36 +0000106 numeric = 0
Fred Drake42181db2001-01-09 22:02:10 +0000107 global_module_index = None
Fred Drake8b880931999-03-03 20:24:30 +0000108 style_file = os.path.join(TOPDIR, "html", "style.css")
Fred Drakecf1b06e1999-09-23 16:55:09 +0000109 about_file = os.path.join(TOPDIR, "html", "about.dat")
Fred Drakedfa539d2000-08-31 06:58:34 +0000110 up_link = None
111 up_title = None
Fred Drake56c8c272002-10-30 17:02:21 +0000112 favicon = None
Fred Drake8b880931999-03-03 20:24:30 +0000113 #
Fred Drake3ce28e42001-10-30 16:09:51 +0000114 # 'dvips_safe' is a weird option. It is used mostly to make
115 # LaTeX2HTML not try to be too smart about protecting the user
116 # from a bad version of dvips -- some versions would core dump if
117 # the path to the source DVI contained a dot, and it's appearantly
118 # difficult to determine if the version available has that bug.
119 # This option gets set when PostScript output is requested
120 # (because we're going to run dvips regardless, and we'll either
121 # know it succeeds before LaTeX2HTML is run, or we'll have
122 # detected the failure and bailed), or the user asserts that it's
123 # safe from the command line.
124 #
125 # So, why does LaTeX2HTML think it appropriate to protect the user
126 # from a dvips that's only potentially going to core dump? Only
127 # because they want to avoid doing a lot of work just to have to
128 # bail later with no useful intermediates. Unfortunately, they
129 # bail *before* they know whether dvips will be needed at all.
130 # I've gone around the bush a few times with the LaTeX2HTML
131 # developers over whether this is appropriate behavior, and they
132 # don't seem interested in changing their position.
133 #
134 dvips_safe = 0
135 #
Fred Drake55994412001-01-30 22:30:01 +0000136 DEFAULT_FORMATS = ("html",)
Fred Drake8b880931999-03-03 20:24:30 +0000137 ALL_FORMATS = ("dvi", "html", "pdf", "ps", "text")
138
139 def __init__(self):
Fred Drake8b880931999-03-03 20:24:30 +0000140 self.formats = []
Fred Drake8bc627a2000-08-31 06:14:38 +0000141 self.l2h_init_files = []
Fred Drake8b880931999-03-03 20:24:30 +0000142
143 def __getitem__(self, key):
144 # This is used when formatting the usage message.
145 try:
146 return getattr(self, key)
147 except AttributeError:
148 raise KeyError, key
149
150 def parse(self, args):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000151 opts, args = getopt.getopt(args, "Hi:a:s:lDkqr:",
Fred Drake8b880931999-03-03 20:24:30 +0000152 ["all", "postscript", "help", "iconserver=",
Fred Drake8bc627a2000-08-31 06:14:38 +0000153 "address=", "a4", "letter", "l2h-init=",
Fred Drake8b880931999-03-03 20:24:30 +0000154 "link=", "split=", "logging", "debugging",
Fred Drakecf1b06e1999-09-23 16:55:09 +0000155 "keep", "quiet", "runs=", "image-type=",
Fred Drake50d1fcf2001-02-19 19:18:09 +0000156 "about=", "numeric", "style=", "paper=",
157 "up-link=", "up-title=", "dir=",
Fred Drake56c8c272002-10-30 17:02:21 +0000158 "global-module-index=", "dvips-safe",
159 "favicon="]
Fred Drake52ea0ce1999-09-22 19:55:35 +0000160 + list(self.ALL_FORMATS))
Fred Drake8b880931999-03-03 20:24:30 +0000161 for opt, arg in opts:
162 if opt == "--all":
163 self.formats = list(self.ALL_FORMATS)
Fred Drake3ce28e42001-10-30 16:09:51 +0000164 self.dvips_safe = "ps" in self.formats
Fred Drake8b880931999-03-03 20:24:30 +0000165 elif opt in ("-H", "--help"):
166 usage(self)
167 sys.exit()
168 elif opt == "--iconserver":
169 self.icon_server = arg
170 elif opt in ("-a", "--address"):
171 self.address = arg
172 elif opt == "--a4":
173 self.paper = "a4"
174 elif opt == "--letter":
175 self.paper = "letter"
Fred Drake8b880931999-03-03 20:24:30 +0000176 elif opt == "--link":
177 self.max_link_depth = int(arg)
178 elif opt in ("-s", "--split"):
179 self.max_split_depth = int(arg)
180 elif opt in ("-l", "--logging"):
181 self.logging = self.logging + 1
182 elif opt in ("-D", "--debugging"):
183 self.debugging = self.debugging + 1
184 elif opt in ("-k", "--keep"):
185 self.discard_temps = 0
186 elif opt in ("-q", "--quiet"):
187 self.quiet = 1
Fred Drake52ea0ce1999-09-22 19:55:35 +0000188 elif opt in ("-r", "--runs"):
189 self.runs = int(arg)
190 elif opt == "--image-type":
191 self.image_type = arg
Fred Drakecf1b06e1999-09-23 16:55:09 +0000192 elif opt == "--about":
193 # always make this absolute:
194 self.about_file = os.path.normpath(
Fred Drakefcb87252000-08-29 18:15:05 +0000195 os.path.abspath(arg))
Fred Drake9a257b42000-03-31 20:27:36 +0000196 elif opt == "--numeric":
197 self.numeric = 1
Fred Drakefcb87252000-08-29 18:15:05 +0000198 elif opt == "--style":
199 self.style_file = os.path.abspath(arg)
Fred Drake8bc627a2000-08-31 06:14:38 +0000200 elif opt == "--l2h-init":
201 self.l2h_init_files.append(os.path.abspath(arg))
Fred Drake56c8c272002-10-30 17:02:21 +0000202 elif opt == "--favicon":
203 self.favicon = arg
Fred Drakedfa539d2000-08-31 06:58:34 +0000204 elif opt == "--up-link":
205 self.up_link = arg
206 elif opt == "--up-title":
207 self.up_title = arg
Fred Drake42181db2001-01-09 22:02:10 +0000208 elif opt == "--global-module-index":
209 self.global_module_index = arg
Fred Drake50d1fcf2001-02-19 19:18:09 +0000210 elif opt == "--dir":
Fred Drake1cb560a2001-08-10 20:17:09 +0000211 if os.sep == "\\":
212 arg = re.sub("/", "\\", arg)
Fred Drakec0e066a2002-10-01 15:30:56 +0000213 self.builddir = os.path.expanduser(arg)
Fred Drake50d1fcf2001-02-19 19:18:09 +0000214 elif opt == "--paper":
215 self.paper = arg
Fred Drake3ce28e42001-10-30 16:09:51 +0000216 elif opt == "--dvips-safe":
217 self.dvips_safe = 1
Fred Drake8b880931999-03-03 20:24:30 +0000218 #
219 # Format specifiers:
220 #
221 elif opt[2:] in self.ALL_FORMATS:
222 self.add_format(opt[2:])
223 elif opt == "--postscript":
224 # synonym for --ps
225 self.add_format("ps")
226 self.initialize()
227 #
228 # return the args to allow the caller access:
229 #
230 return args
231
232 def add_format(self, format):
233 """Add a format to the formats list if not present."""
234 if not format in self.formats:
Fred Drake3ce28e42001-10-30 16:09:51 +0000235 if format == "ps":
236 # assume this is safe since we're going to run it anyway
237 self.dvips_safe = 1
Fred Drake8b880931999-03-03 20:24:30 +0000238 self.formats.append(format)
239
240 def initialize(self):
241 """Complete initialization. This is needed if parse() isn't used."""
242 # add the default format if no formats were specified:
243 if not self.formats:
244 self.formats = self.DEFAULT_FORMATS
245 # determine the base set of texinputs directories:
246 texinputs = string.split(os.environ.get("TEXINPUTS", ""), os.pathsep)
247 if not texinputs:
248 texinputs = ['']
249 self.base_texinputs = [
250 os.path.join(TOPDIR, "paper-" + self.paper),
251 os.path.join(TOPDIR, "texinputs"),
252 ] + texinputs
Fred Drakebfd80dd2001-06-23 03:06:01 +0000253 if self.builddir:
254 self.builddir = os.path.abspath(self.builddir)
Fred Drake8b880931999-03-03 20:24:30 +0000255
256
257class Job:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000258 latex_runs = 0
259
Fred Drake8b880931999-03-03 20:24:30 +0000260 def __init__(self, options, path):
261 self.options = options
Fred Drakea871c2e1999-05-06 19:37:38 +0000262 self.doctype = get_doctype(path)
Fred Drake8b880931999-03-03 20:24:30 +0000263 self.filedir, self.doc = split_pathname(path)
Fred Drakebfd80dd2001-06-23 03:06:01 +0000264 self.builddir = os.path.abspath(options.builddir or self.doc)
Fred Drakeaebbca32001-07-17 14:46:09 +0000265 if ("html" in options.formats or "text" in options.formats):
266 if not os.path.exists(self.builddir):
267 os.mkdir(self.builddir)
268 self.log_filename = os.path.join(self.builddir, self.doc + ".how")
269 else:
270 self.log_filename = os.path.abspath(self.doc + ".how")
Fred Drake8b880931999-03-03 20:24:30 +0000271 if os.path.exists(self.log_filename):
272 os.unlink(self.log_filename)
Fred Drake246beb22002-08-27 16:34:54 +0000273 l2hconf = self.doc + ".l2h"
274 if os.path.exists(l2hconf):
275 if os.path.exists(l2hconf + "~"):
276 os.unlink(l2hconf + "~")
277 os.rename(l2hconf, l2hconf + "~")
278 self.l2h_aux_init_file = self.doc + ".l2h"
Fred Drake8b880931999-03-03 20:24:30 +0000279 self.write_l2h_aux_init_file()
280
281 def build(self):
282 self.setup_texinputs()
283 formats = self.options.formats
284 if "dvi" in formats or "ps" in formats:
285 self.build_dvi()
286 if "pdf" in formats:
287 self.build_pdf()
288 if "ps" in formats:
289 self.build_ps()
290 if "html" in formats:
291 self.require_temps()
Fred Drakebfd80dd2001-06-23 03:06:01 +0000292 self.build_html(self.builddir)
Fred Drake8b880931999-03-03 20:24:30 +0000293 if self.options.icon_server == ".":
Fred Drake52ea0ce1999-09-22 19:55:35 +0000294 pattern = os.path.join(TOPDIR, "html", "icons",
295 "*." + self.options.image_type)
296 imgs = glob.glob(pattern)
297 if not imgs:
298 self.warning(
299 "Could not locate support images of type %s."
300 % `self.options.image_type`)
301 for fn in imgs:
Barry Warsawcfb30e22002-10-01 15:38:01 +0000302 new_fn = os.path.join(self.builddir, os.path.basename(fn))
Fred Drake8b880931999-03-03 20:24:30 +0000303 shutil.copyfile(fn, new_fn)
304 if "text" in formats:
305 self.require_temps()
306 tempdir = self.doc
307 need_html = "html" not in formats
308 if self.options.max_split_depth != 1:
309 fp = open(self.l2h_aux_init_file, "a")
310 fp.write("# re-hack this file for --text:\n")
311 l2hoption(fp, "MAX_SPLIT_DEPTH", "1")
312 fp.write("1;\n")
313 fp.close()
314 tempdir = self.doc + "-temp-html"
315 need_html = 1
316 if need_html:
317 self.build_html(tempdir, max_split_depth=1)
318 self.build_text(tempdir)
319 if self.options.discard_temps:
320 self.cleanup()
321
322 def setup_texinputs(self):
323 texinputs = [self.filedir] + list(self.options.base_texinputs)
324 os.environ["TEXINPUTS"] = string.join(texinputs, os.pathsep)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000325 self.message("TEXINPUTS=" + os.environ["TEXINPUTS"])
Fred Drake8b880931999-03-03 20:24:30 +0000326
Fred Drake8b880931999-03-03 20:24:30 +0000327 def build_aux(self, binary=None):
328 if binary is None:
329 binary = LATEX_BINARY
330 new_index( "%s.ind" % self.doc, "genindex")
331 new_index("mod%s.ind" % self.doc, "modindex")
332 self.run("%s %s" % (binary, self.doc))
333 self.use_bibtex = check_for_bibtex(self.doc + ".aux")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000334 self.latex_runs = 1
Fred Drake8b880931999-03-03 20:24:30 +0000335
336 def build_dvi(self):
337 self.use_latex(LATEX_BINARY)
338
339 def build_pdf(self):
340 self.use_latex(PDFLATEX_BINARY)
341
342 def use_latex(self, binary):
343 self.require_temps(binary=binary)
Fred Drakedf84fac2000-09-20 05:49:09 +0000344 if self.latex_runs < 2:
345 if os.path.isfile("mod%s.idx" % self.doc):
346 self.run("%s mod%s.idx" % (MAKEINDEX_BINARY, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000347 use_indfix = 0
Fred Drakedf84fac2000-09-20 05:49:09 +0000348 if os.path.isfile(self.doc + ".idx"):
Fred Drake9dce7b32000-11-03 02:57:31 +0000349 use_indfix = 1
Fred Drakedf84fac2000-09-20 05:49:09 +0000350 # call to Doc/tools/fix_hack omitted; doesn't appear necessary
351 self.run("%s %s.idx" % (MAKEINDEX_BINARY, self.doc))
352 import indfix
353 indfix.process(self.doc + ".ind")
354 if self.use_bibtex:
355 self.run("%s %s" % (BIBTEX_BINARY, self.doc))
356 self.process_synopsis_files()
Fred Drakedf84fac2000-09-20 05:49:09 +0000357 self.run("%s %s" % (binary, self.doc))
Fred Drakeb258bed2001-02-12 15:30:22 +0000358 self.latex_runs = self.latex_runs + 1
Fred Drakedf84fac2000-09-20 05:49:09 +0000359 if os.path.isfile("mod%s.idx" % self.doc):
360 self.run("%s -s %s mod%s.idx"
361 % (MAKEINDEX_BINARY, ISTFILE, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000362 if use_indfix:
Fred Drakedf84fac2000-09-20 05:49:09 +0000363 self.run("%s -s %s %s.idx"
364 % (MAKEINDEX_BINARY, ISTFILE, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000365 indfix.process(self.doc + ".ind")
Fred Drakedf84fac2000-09-20 05:49:09 +0000366 self.process_synopsis_files()
Fred Drakea871c2e1999-05-06 19:37:38 +0000367 #
368 # and now finish it off:
369 #
370 if os.path.isfile(self.doc + ".toc") and binary == PDFLATEX_BINARY:
371 import toc2bkm
Fred Drake239e1d52000-09-05 21:45:11 +0000372 if self.doctype == "manual":
373 bigpart = "chapter"
374 else:
375 bigpart = "section"
376 toc2bkm.process(self.doc + ".toc", self.doc + ".bkm", bigpart)
Fred Drakea871c2e1999-05-06 19:37:38 +0000377 if self.use_bibtex:
378 self.run("%s %s" % (BIBTEX_BINARY, self.doc))
379 self.run("%s %s" % (binary, self.doc))
Fred Drakeb258bed2001-02-12 15:30:22 +0000380 self.latex_runs = self.latex_runs + 1
Fred Drakea871c2e1999-05-06 19:37:38 +0000381
382 def process_synopsis_files(self):
383 synopsis_files = glob.glob(self.doc + "*.syn")
384 for path in synopsis_files:
385 uniqify_module_table(path)
Fred Drake8b880931999-03-03 20:24:30 +0000386
387 def build_ps(self):
388 self.run("%s -N0 -o %s.ps %s" % (DVIPS_BINARY, self.doc, self.doc))
389
Fred Drakeaebbca32001-07-17 14:46:09 +0000390 def build_html(self, builddir, max_split_depth=None):
Fred Drake8b880931999-03-03 20:24:30 +0000391 if max_split_depth is None:
392 max_split_depth = self.options.max_split_depth
393 texfile = None
394 for p in string.split(os.environ["TEXINPUTS"], os.pathsep):
395 fn = os.path.join(p, self.doc + ".tex")
396 if os.path.isfile(fn):
397 texfile = fn
398 break
399 if not texfile:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000400 self.warning("Could not locate %s.tex; aborting." % self.doc)
Fred Drake8b880931999-03-03 20:24:30 +0000401 sys.exit(1)
402 # remove leading ./ (or equiv.); might avoid problems w/ dvips
403 if texfile[:2] == os.curdir + os.sep:
404 texfile = texfile[2:]
405 # build the command line and run LaTeX2HTML:
Fred Drakeba828782000-04-03 04:19:14 +0000406 if not os.path.isdir(builddir):
407 os.mkdir(builddir)
Fred Drakef3d41272000-09-14 22:25:47 +0000408 else:
409 for fname in glob.glob(os.path.join(builddir, "*.html")):
410 os.unlink(fname)
Fred Drake8b880931999-03-03 20:24:30 +0000411 args = [LATEX2HTML_BINARY,
Fred Drake8b880931999-03-03 20:24:30 +0000412 "-init_file", self.l2h_aux_init_file,
413 "-dir", builddir,
414 texfile
415 ]
416 self.run(string.join(args)) # XXX need quoting!
417 # ... postprocess
418 shutil.copyfile(self.options.style_file,
419 os.path.join(builddir, self.doc + ".css"))
Fred Drake4437fdf1999-05-03 14:29:07 +0000420 shutil.copyfile(os.path.join(builddir, self.doc + ".html"),
421 os.path.join(builddir, "index.html"))
Fred Drakecfef00962001-03-02 16:26:45 +0000422 if max_split_depth != 1:
Fred Drakeaf922182001-05-09 04:03:16 +0000423 label_file = os.path.join(builddir, "labels.pl")
424 fp = open(label_file)
425 about_node = None
426 target = " = q/about/;\n"
427 x = len(target)
428 while 1:
429 line = fp.readline()
430 if not line:
431 break
432 if line[-x:] == target:
Fred Drakecfef00962001-03-02 16:26:45 +0000433 line = fp.readline()
Fred Drakeaf922182001-05-09 04:03:16 +0000434 m = re.search(r"\|(node\d+\.[a-z]+)\|", line)
435 about_node = m.group(1)
436 shutil.copyfile(os.path.join(builddir, about_node),
437 os.path.join(builddir, "about.html"))
438 break
439 if not self.options.numeric:
Fred Drakecfef00962001-03-02 16:26:45 +0000440 pwd = os.getcwd()
441 try:
442 os.chdir(builddir)
443 self.run("%s %s *.html" % (PERL_BINARY, NODE2LABEL_SCRIPT))
444 finally:
445 os.chdir(pwd)
Fred Drake376f0ef2003-01-17 21:25:04 +0000446 # These files need to be cleaned up here since builddir there
447 # can be more than one, so we clean each of them.
448 if self.options.discard_temps:
449 for fn in ("images.tex", "images.log", "images.aux"):
450 safe_unlink(os.path.join(builddir, fn))
Fred Drake8b880931999-03-03 20:24:30 +0000451
452 def build_text(self, tempdir=None):
453 if tempdir is None:
454 tempdir = self.doc
455 indexfile = os.path.join(tempdir, "index.html")
456 self.run("%s -nolist -dump %s >%s.txt"
457 % (LYNX_BINARY, indexfile, self.doc))
458
459 def require_temps(self, binary=None):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000460 if not self.latex_runs:
Fred Drake8b880931999-03-03 20:24:30 +0000461 self.build_aux(binary=binary)
462
463 def write_l2h_aux_init_file(self):
Fred Drake8bc627a2000-08-31 06:14:38 +0000464 options = self.options
Fred Drake8b880931999-03-03 20:24:30 +0000465 fp = open(self.l2h_aux_init_file, "w")
Fred Drake19157542000-07-31 17:47:49 +0000466 d = string_to_perl(os.path.dirname(L2H_INIT_FILE))
467 fp.write("package main;\n"
468 "push (@INC, '%s');\n"
469 "$mydir = '%s';\n"
470 % (d, d))
Fred Drake498c18f2000-07-24 23:03:32 +0000471 fp.write(open(L2H_INIT_FILE).read())
Fred Drake8bc627a2000-08-31 06:14:38 +0000472 for filename in options.l2h_init_files:
473 fp.write("\n# initialization code incorporated from:\n# ")
474 fp.write(filename)
475 fp.write("\n")
476 fp.write(open(filename).read())
Fred Drake498c18f2000-07-24 23:03:32 +0000477 fp.write("\n"
478 "# auxillary init file for latex2html\n"
Fred Drake8b880931999-03-03 20:24:30 +0000479 "# generated by mkhowto\n"
Fred Drake4437fdf1999-05-03 14:29:07 +0000480 "$NO_AUTO_LINK = 1;\n"
Fred Drake8b880931999-03-03 20:24:30 +0000481 )
Fred Drakecf1b06e1999-09-23 16:55:09 +0000482 l2hoption(fp, "ABOUT_FILE", options.about_file)
Fred Drake8b880931999-03-03 20:24:30 +0000483 l2hoption(fp, "ICONSERVER", options.icon_server)
Fred Drake52ea0ce1999-09-22 19:55:35 +0000484 l2hoption(fp, "IMAGE_TYPE", options.image_type)
Fred Drake8b880931999-03-03 20:24:30 +0000485 l2hoption(fp, "ADDRESS", options.address)
486 l2hoption(fp, "MAX_LINK_DEPTH", options.max_link_depth)
487 l2hoption(fp, "MAX_SPLIT_DEPTH", options.max_split_depth)
Fred Drakedfa539d2000-08-31 06:58:34 +0000488 l2hoption(fp, "EXTERNAL_UP_LINK", options.up_link)
489 l2hoption(fp, "EXTERNAL_UP_TITLE", options.up_title)
Fred Drake56c8c272002-10-30 17:02:21 +0000490 l2hoption(fp, "FAVORITES_ICON", options.favicon)
Fred Drake42181db2001-01-09 22:02:10 +0000491 l2hoption(fp, "GLOBAL_MODULE_INDEX", options.global_module_index)
Fred Drake3ce28e42001-10-30 16:09:51 +0000492 l2hoption(fp, "DVIPS_SAFE", options.dvips_safe)
Fred Drake8b880931999-03-03 20:24:30 +0000493 fp.write("1;\n")
494 fp.close()
495
496 def cleanup(self):
497 self.__have_temps = 0
498 for pattern in ("%s.aux", "%s.log", "%s.out", "%s.toc", "%s.bkm",
Fred Drakea871c2e1999-05-06 19:37:38 +0000499 "%s.idx", "%s.ilg", "%s.ind", "%s.pla",
Fred Drake8b880931999-03-03 20:24:30 +0000500 "%s.bbl", "%s.blg",
501 "mod%s.idx", "mod%s.ind", "mod%s.ilg",
502 ):
503 safe_unlink(pattern % self.doc)
Fred Drakea871c2e1999-05-06 19:37:38 +0000504 map(safe_unlink, glob.glob(self.doc + "*.syn"))
Fred Drake8b880931999-03-03 20:24:30 +0000505 for spec in ("IMG*", "*.pl", "WARNINGS", "index.dat", "modindex.dat"):
506 pattern = os.path.join(self.doc, spec)
507 map(safe_unlink, glob.glob(pattern))
508 if "dvi" not in self.options.formats:
509 safe_unlink(self.doc + ".dvi")
510 if os.path.isdir(self.doc + "-temp-html"):
511 shutil.rmtree(self.doc + "-temp-html", ignore_errors=1)
512 if not self.options.logging:
513 os.unlink(self.log_filename)
514 if not self.options.debugging:
515 os.unlink(self.l2h_aux_init_file)
516
517 def run(self, command):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000518 self.message(command)
Fred Drake1cb560a2001-08-10 20:17:09 +0000519 if sys.platform.startswith("win"):
520 rc = os.system(command)
521 else:
522 rc = os.system("(%s) </dev/null >>%s 2>&1"
523 % (command, self.log_filename))
Fred Drake8b880931999-03-03 20:24:30 +0000524 if rc:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000525 self.warning(
526 "Session transcript and error messages are in %s."
Fred Drake8b880931999-03-03 20:24:30 +0000527 % self.log_filename)
Fred Drake1cb560a2001-08-10 20:17:09 +0000528 if hasattr(os, "WIFEXITED"):
529 if os.WIFEXITED(rc):
530 self.warning("Exited with status %s." % os.WEXITSTATUS(rc))
531 else:
532 self.warning("Killed by signal %s." % os.WSTOPSIG(rc))
533 else:
534 self.warning("Return code: %s" % rc)
Fred Drake4e3f2752001-02-04 15:20:26 +0000535 sys.stderr.write("The relevant lines from the transcript are:\n")
536 sys.stderr.write("-" * 72 + "\n")
537 sys.stderr.writelines(get_run_transcript(self.log_filename))
Fred Drake8b880931999-03-03 20:24:30 +0000538 sys.exit(rc)
539
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000540 def message(self, msg):
541 msg = "+++ " + msg
542 if not self.options.quiet:
543 print msg
Fred Drake52ea0ce1999-09-22 19:55:35 +0000544 self.log(msg + "\n")
545
546 def warning(self, msg):
547 msg = "*** %s\n" % msg
548 sys.stderr.write(msg)
549 self.log(msg)
550
551 def log(self, msg):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000552 fp = open(self.log_filename, "a")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000553 fp.write(msg)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000554 fp.close()
555
Fred Drake8b880931999-03-03 20:24:30 +0000556
Fred Drake4e3f2752001-02-04 15:20:26 +0000557def get_run_transcript(filename):
558 """Return lines from the transcript file for the most recent run() call."""
559 fp = open(filename)
560 lines = fp.readlines()
561 fp.close()
562 lines.reverse()
563 L = []
564 for line in lines:
565 L.append(line)
566 if line[:4] == "+++ ":
567 break
568 L.reverse()
569 return L
570
571
Fred Drake8b880931999-03-03 20:24:30 +0000572def safe_unlink(path):
Fred Drake4e3f2752001-02-04 15:20:26 +0000573 """Unlink a file without raising an error if it doesn't exist."""
Fred Drake8b880931999-03-03 20:24:30 +0000574 try:
575 os.unlink(path)
576 except os.error:
577 pass
578
579
Fred Drakea871c2e1999-05-06 19:37:38 +0000580def split_pathname(path):
Fred Drakebfd80dd2001-06-23 03:06:01 +0000581 path = os.path.abspath(path)
Fred Drakea871c2e1999-05-06 19:37:38 +0000582 dirname, basename = os.path.split(path)
Fred Drake8b880931999-03-03 20:24:30 +0000583 if basename[-4:] == ".tex":
584 basename = basename[:-4]
585 return dirname, basename
586
587
Fred Drakea871c2e1999-05-06 19:37:38 +0000588_doctype_rx = re.compile(r"\\documentclass(?:\[[^]]*\])?{([a-zA-Z]*)}")
589def get_doctype(path):
590 fp = open(path)
591 doctype = None
592 while 1:
593 line = fp.readline()
594 if not line:
595 break
596 m = _doctype_rx.match(line)
597 if m:
598 doctype = m.group(1)
599 break
600 fp.close()
601 return doctype
602
603
Fred Drake8b880931999-03-03 20:24:30 +0000604def main():
605 options = Options()
606 try:
607 args = options.parse(sys.argv[1:])
608 except getopt.error, msg:
609 error(options, msg)
610 if not args:
611 # attempt to locate single .tex file in current directory:
612 args = glob.glob("*.tex")
613 if not args:
614 error(options, "No file to process.")
615 if len(args) > 1:
616 error(options, "Could not deduce which files should be processed.")
617 #
618 # parameters are processed, let's go!
619 #
620 for path in args:
621 Job(options, path).build()
622
623
624def l2hoption(fp, option, value):
625 if value:
626 fp.write('$%s = "%s";\n' % (option, string_to_perl(str(value))))
627
628
629_to_perl = {}
630for c in map(chr, range(1, 256)):
631 _to_perl[c] = c
632_to_perl["@"] = "\\@"
633_to_perl["$"] = "\\$"
634_to_perl['"'] = '\\"'
635
636def string_to_perl(s):
637 return string.join(map(_to_perl.get, s), '')
638
639
640def check_for_bibtex(filename):
641 fp = open(filename)
642 pos = string.find(fp.read(), r"\bibdata{")
643 fp.close()
644 return pos >= 0
645
646def uniqify_module_table(filename):
647 lines = open(filename).readlines()
648 if len(lines) > 1:
649 if lines[-1] == lines[-2]:
650 del lines[-1]
651 open(filename, "w").writelines(lines)
652
653
654def new_index(filename, label="genindex"):
655 fp = open(filename, "w")
656 fp.write(r"""\
657\begin{theindex}
658\label{%s}
659\end{theindex}
660""" % label)
661 fp.close()
662
663
664if __name__ == "__main__":
665 main()