blob: 322b6ba6b1875d0752f817efaae4fc825e65dea6 [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.
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 Drakefcb87252000-08-29 18:15:05 +000023 --style Specify the CSS file to use for the output (filename,
24 not a URL).
Fred Drakedfa539d2000-08-31 06:58:34 +000025 --up-link URL to a parent document.
26 --up-title Title of a parent document.
Fred Drake8b880931999-03-03 20:24:30 +000027
28Other options:
29 --a4 Format for A4 paper.
30 --letter Format for US letter paper (the default).
31 --help, -H Show this text.
32 --logging, -l Log stdout and stderr to a file (*.how).
33 --debugging, -D Echo commands as they are executed.
34 --keep, -k Keep temporary files around.
35 --quiet, -q Do not print command output to stdout.
36 (stderr is also lost, sorry; see *.how for errors)
37"""
38
39import getopt
40import glob
41import os
Fred Drakea871c2e1999-05-06 19:37:38 +000042import re
Fred Drake8b880931999-03-03 20:24:30 +000043import shutil
44import string
45import sys
Fred Drake8b880931999-03-03 20:24:30 +000046
47
Fred Drake964c0742001-05-29 16:10:07 +000048if not hasattr(os.path, "abspath"):
Fred Drakebfd80dd2001-06-23 03:06:01 +000049 # Python 1.5.1 or earlier
Fred Drake964c0742001-05-29 16:10:07 +000050 def abspath(path):
51 """Return an absolute path."""
52 if not os.path.isabs(path):
53 path = os.path.join(os.getcwd(), path)
54 return os.path.normpath(path)
55
56 os.path.abspath = abspath
57
58
Fred Drakefcb87252000-08-29 18:15:05 +000059MYDIR = os.path.abspath(sys.path[0])
60TOPDIR = os.path.dirname(MYDIR)
Fred Drake8b880931999-03-03 20:24:30 +000061
62ISTFILE = os.path.join(TOPDIR, "texinputs", "python.ist")
63NODE2LABEL_SCRIPT = os.path.join(MYDIR, "node2label.pl")
64L2H_INIT_FILE = os.path.join(TOPDIR, "perl", "l2hinit.perl")
65
66BIBTEX_BINARY = "bibtex"
67DVIPS_BINARY = "dvips"
68LATEX_BINARY = "latex"
69LATEX2HTML_BINARY = "latex2html"
70LYNX_BINARY = "lynx"
71MAKEINDEX_BINARY = "makeindex"
72PDFLATEX_BINARY = "pdflatex"
73PERL_BINARY = "perl"
74PYTHON_BINARY = "python"
75
76
77def usage(options):
78 print __doc__ % options
79
80def error(options, message, err=2):
81 sys.stdout = sys.stderr
82 print message
83 print
84 usage(options)
85 sys.exit(2)
86
87
88class Options:
89 program = os.path.basename(sys.argv[0])
90 #
91 address = ''
Fred Drake50d1fcf2001-02-19 19:18:09 +000092 builddir = None
Fred Drake8b880931999-03-03 20:24:30 +000093 debugging = 0
94 discard_temps = 1
95 have_temps = 0
96 icon_server = None
Fred Drake52ea0ce1999-09-22 19:55:35 +000097 image_type = "gif"
Fred Drake8b880931999-03-03 20:24:30 +000098 logging = 0
99 max_link_depth = 3
100 max_split_depth = 6
101 paper = "letter"
102 quiet = 0
Fred Drake52ea0ce1999-09-22 19:55:35 +0000103 runs = 0
Fred Drake9a257b42000-03-31 20:27:36 +0000104 numeric = 0
Fred Drake42181db2001-01-09 22:02:10 +0000105 global_module_index = None
Fred Drake8b880931999-03-03 20:24:30 +0000106 style_file = os.path.join(TOPDIR, "html", "style.css")
Fred Drakecf1b06e1999-09-23 16:55:09 +0000107 about_file = os.path.join(TOPDIR, "html", "about.dat")
Fred Drakedfa539d2000-08-31 06:58:34 +0000108 up_link = None
109 up_title = None
Fred Drake8b880931999-03-03 20:24:30 +0000110 #
Fred Drake3ce28e42001-10-30 16:09:51 +0000111 # 'dvips_safe' is a weird option. It is used mostly to make
112 # LaTeX2HTML not try to be too smart about protecting the user
113 # from a bad version of dvips -- some versions would core dump if
114 # the path to the source DVI contained a dot, and it's appearantly
115 # difficult to determine if the version available has that bug.
116 # This option gets set when PostScript output is requested
117 # (because we're going to run dvips regardless, and we'll either
118 # know it succeeds before LaTeX2HTML is run, or we'll have
119 # detected the failure and bailed), or the user asserts that it's
120 # safe from the command line.
121 #
122 # So, why does LaTeX2HTML think it appropriate to protect the user
123 # from a dvips that's only potentially going to core dump? Only
124 # because they want to avoid doing a lot of work just to have to
125 # bail later with no useful intermediates. Unfortunately, they
126 # bail *before* they know whether dvips will be needed at all.
127 # I've gone around the bush a few times with the LaTeX2HTML
128 # developers over whether this is appropriate behavior, and they
129 # don't seem interested in changing their position.
130 #
131 dvips_safe = 0
132 #
Fred Drake55994412001-01-30 22:30:01 +0000133 DEFAULT_FORMATS = ("html",)
Fred Drake8b880931999-03-03 20:24:30 +0000134 ALL_FORMATS = ("dvi", "html", "pdf", "ps", "text")
135
136 def __init__(self):
Fred Drake8b880931999-03-03 20:24:30 +0000137 self.formats = []
Fred Drake8bc627a2000-08-31 06:14:38 +0000138 self.l2h_init_files = []
Fred Drake8b880931999-03-03 20:24:30 +0000139
140 def __getitem__(self, key):
141 # This is used when formatting the usage message.
142 try:
143 return getattr(self, key)
144 except AttributeError:
145 raise KeyError, key
146
147 def parse(self, args):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000148 opts, args = getopt.getopt(args, "Hi:a:s:lDkqr:",
Fred Drake8b880931999-03-03 20:24:30 +0000149 ["all", "postscript", "help", "iconserver=",
Fred Drake8bc627a2000-08-31 06:14:38 +0000150 "address=", "a4", "letter", "l2h-init=",
Fred Drake8b880931999-03-03 20:24:30 +0000151 "link=", "split=", "logging", "debugging",
Fred Drakecf1b06e1999-09-23 16:55:09 +0000152 "keep", "quiet", "runs=", "image-type=",
Fred Drake50d1fcf2001-02-19 19:18:09 +0000153 "about=", "numeric", "style=", "paper=",
154 "up-link=", "up-title=", "dir=",
Fred Drake3ce28e42001-10-30 16:09:51 +0000155 "global-module-index=", "dvips-safe"]
Fred Drake52ea0ce1999-09-22 19:55:35 +0000156 + list(self.ALL_FORMATS))
Fred Drake8b880931999-03-03 20:24:30 +0000157 for opt, arg in opts:
158 if opt == "--all":
159 self.formats = list(self.ALL_FORMATS)
Fred Drake3ce28e42001-10-30 16:09:51 +0000160 self.dvips_safe = "ps" in self.formats
Fred Drake8b880931999-03-03 20:24:30 +0000161 elif opt in ("-H", "--help"):
162 usage(self)
163 sys.exit()
164 elif opt == "--iconserver":
165 self.icon_server = arg
166 elif opt in ("-a", "--address"):
167 self.address = arg
168 elif opt == "--a4":
169 self.paper = "a4"
170 elif opt == "--letter":
171 self.paper = "letter"
Fred Drake8b880931999-03-03 20:24:30 +0000172 elif opt == "--link":
173 self.max_link_depth = int(arg)
174 elif opt in ("-s", "--split"):
175 self.max_split_depth = int(arg)
176 elif opt in ("-l", "--logging"):
177 self.logging = self.logging + 1
178 elif opt in ("-D", "--debugging"):
179 self.debugging = self.debugging + 1
180 elif opt in ("-k", "--keep"):
181 self.discard_temps = 0
182 elif opt in ("-q", "--quiet"):
183 self.quiet = 1
Fred Drake52ea0ce1999-09-22 19:55:35 +0000184 elif opt in ("-r", "--runs"):
185 self.runs = int(arg)
186 elif opt == "--image-type":
187 self.image_type = arg
Fred Drakecf1b06e1999-09-23 16:55:09 +0000188 elif opt == "--about":
189 # always make this absolute:
190 self.about_file = os.path.normpath(
Fred Drakefcb87252000-08-29 18:15:05 +0000191 os.path.abspath(arg))
Fred Drake9a257b42000-03-31 20:27:36 +0000192 elif opt == "--numeric":
193 self.numeric = 1
Fred Drakefcb87252000-08-29 18:15:05 +0000194 elif opt == "--style":
195 self.style_file = os.path.abspath(arg)
Fred Drake8bc627a2000-08-31 06:14:38 +0000196 elif opt == "--l2h-init":
197 self.l2h_init_files.append(os.path.abspath(arg))
Fred Drakedfa539d2000-08-31 06:58:34 +0000198 elif opt == "--up-link":
199 self.up_link = arg
200 elif opt == "--up-title":
201 self.up_title = arg
Fred Drake42181db2001-01-09 22:02:10 +0000202 elif opt == "--global-module-index":
203 self.global_module_index = arg
Fred Drake50d1fcf2001-02-19 19:18:09 +0000204 elif opt == "--dir":
Fred Drake1cb560a2001-08-10 20:17:09 +0000205 if os.sep == "\\":
206 arg = re.sub("/", "\\", arg)
Fred Drake50d1fcf2001-02-19 19:18:09 +0000207 self.builddir = arg
208 elif opt == "--paper":
209 self.paper = arg
Fred Drake3ce28e42001-10-30 16:09:51 +0000210 elif opt == "--dvips-safe":
211 self.dvips_safe = 1
Fred Drake8b880931999-03-03 20:24:30 +0000212 #
213 # Format specifiers:
214 #
215 elif opt[2:] in self.ALL_FORMATS:
216 self.add_format(opt[2:])
217 elif opt == "--postscript":
218 # synonym for --ps
219 self.add_format("ps")
220 self.initialize()
221 #
222 # return the args to allow the caller access:
223 #
224 return args
225
226 def add_format(self, format):
227 """Add a format to the formats list if not present."""
228 if not format in self.formats:
Fred Drake3ce28e42001-10-30 16:09:51 +0000229 if format == "ps":
230 # assume this is safe since we're going to run it anyway
231 self.dvips_safe = 1
Fred Drake8b880931999-03-03 20:24:30 +0000232 self.formats.append(format)
233
234 def initialize(self):
235 """Complete initialization. This is needed if parse() isn't used."""
236 # add the default format if no formats were specified:
237 if not self.formats:
238 self.formats = self.DEFAULT_FORMATS
239 # determine the base set of texinputs directories:
240 texinputs = string.split(os.environ.get("TEXINPUTS", ""), os.pathsep)
241 if not texinputs:
242 texinputs = ['']
243 self.base_texinputs = [
244 os.path.join(TOPDIR, "paper-" + self.paper),
245 os.path.join(TOPDIR, "texinputs"),
246 ] + texinputs
Fred Drakebfd80dd2001-06-23 03:06:01 +0000247 if self.builddir:
248 self.builddir = os.path.abspath(self.builddir)
Fred Drake8b880931999-03-03 20:24:30 +0000249
250
251class Job:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000252 latex_runs = 0
253
Fred Drake8b880931999-03-03 20:24:30 +0000254 def __init__(self, options, path):
255 self.options = options
Fred Drakea871c2e1999-05-06 19:37:38 +0000256 self.doctype = get_doctype(path)
Fred Drake8b880931999-03-03 20:24:30 +0000257 self.filedir, self.doc = split_pathname(path)
Fred Drakebfd80dd2001-06-23 03:06:01 +0000258 self.builddir = os.path.abspath(options.builddir or self.doc)
Fred Drakeaebbca32001-07-17 14:46:09 +0000259 if ("html" in options.formats or "text" in options.formats):
260 if not os.path.exists(self.builddir):
261 os.mkdir(self.builddir)
262 self.log_filename = os.path.join(self.builddir, self.doc + ".how")
263 else:
264 self.log_filename = os.path.abspath(self.doc + ".how")
Fred Drake8b880931999-03-03 20:24:30 +0000265 if os.path.exists(self.log_filename):
266 os.unlink(self.log_filename)
Fred Drake246beb22002-08-27 16:34:54 +0000267 l2hconf = self.doc + ".l2h"
268 if os.path.exists(l2hconf):
269 if os.path.exists(l2hconf + "~"):
270 os.unlink(l2hconf + "~")
271 os.rename(l2hconf, l2hconf + "~")
272 self.l2h_aux_init_file = self.doc + ".l2h"
Fred Drake8b880931999-03-03 20:24:30 +0000273 self.write_l2h_aux_init_file()
274
275 def build(self):
276 self.setup_texinputs()
277 formats = self.options.formats
278 if "dvi" in formats or "ps" in formats:
279 self.build_dvi()
280 if "pdf" in formats:
281 self.build_pdf()
282 if "ps" in formats:
283 self.build_ps()
284 if "html" in formats:
285 self.require_temps()
Fred Drakebfd80dd2001-06-23 03:06:01 +0000286 self.build_html(self.builddir)
Fred Drake8b880931999-03-03 20:24:30 +0000287 if self.options.icon_server == ".":
Fred Drake52ea0ce1999-09-22 19:55:35 +0000288 pattern = os.path.join(TOPDIR, "html", "icons",
289 "*." + self.options.image_type)
290 imgs = glob.glob(pattern)
291 if not imgs:
292 self.warning(
293 "Could not locate support images of type %s."
294 % `self.options.image_type`)
295 for fn in imgs:
Fred Drake8b880931999-03-03 20:24:30 +0000296 new_fn = os.path.join(self.doc, os.path.basename(fn))
297 shutil.copyfile(fn, new_fn)
298 if "text" in formats:
299 self.require_temps()
300 tempdir = self.doc
301 need_html = "html" not in formats
302 if self.options.max_split_depth != 1:
303 fp = open(self.l2h_aux_init_file, "a")
304 fp.write("# re-hack this file for --text:\n")
305 l2hoption(fp, "MAX_SPLIT_DEPTH", "1")
306 fp.write("1;\n")
307 fp.close()
308 tempdir = self.doc + "-temp-html"
309 need_html = 1
310 if need_html:
311 self.build_html(tempdir, max_split_depth=1)
312 self.build_text(tempdir)
313 if self.options.discard_temps:
314 self.cleanup()
315
316 def setup_texinputs(self):
317 texinputs = [self.filedir] + list(self.options.base_texinputs)
318 os.environ["TEXINPUTS"] = string.join(texinputs, os.pathsep)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000319 self.message("TEXINPUTS=" + os.environ["TEXINPUTS"])
Fred Drake8b880931999-03-03 20:24:30 +0000320
Fred Drake8b880931999-03-03 20:24:30 +0000321 def build_aux(self, binary=None):
322 if binary is None:
323 binary = LATEX_BINARY
324 new_index( "%s.ind" % self.doc, "genindex")
325 new_index("mod%s.ind" % self.doc, "modindex")
326 self.run("%s %s" % (binary, self.doc))
327 self.use_bibtex = check_for_bibtex(self.doc + ".aux")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000328 self.latex_runs = 1
Fred Drake8b880931999-03-03 20:24:30 +0000329
330 def build_dvi(self):
331 self.use_latex(LATEX_BINARY)
332
333 def build_pdf(self):
334 self.use_latex(PDFLATEX_BINARY)
335
336 def use_latex(self, binary):
337 self.require_temps(binary=binary)
Fred Drakedf84fac2000-09-20 05:49:09 +0000338 if self.latex_runs < 2:
339 if os.path.isfile("mod%s.idx" % self.doc):
340 self.run("%s mod%s.idx" % (MAKEINDEX_BINARY, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000341 use_indfix = 0
Fred Drakedf84fac2000-09-20 05:49:09 +0000342 if os.path.isfile(self.doc + ".idx"):
Fred Drake9dce7b32000-11-03 02:57:31 +0000343 use_indfix = 1
Fred Drakedf84fac2000-09-20 05:49:09 +0000344 # call to Doc/tools/fix_hack omitted; doesn't appear necessary
345 self.run("%s %s.idx" % (MAKEINDEX_BINARY, self.doc))
346 import indfix
347 indfix.process(self.doc + ".ind")
348 if self.use_bibtex:
349 self.run("%s %s" % (BIBTEX_BINARY, self.doc))
350 self.process_synopsis_files()
Fred Drakedf84fac2000-09-20 05:49:09 +0000351 self.run("%s %s" % (binary, self.doc))
Fred Drakeb258bed2001-02-12 15:30:22 +0000352 self.latex_runs = self.latex_runs + 1
Fred Drakedf84fac2000-09-20 05:49:09 +0000353 if os.path.isfile("mod%s.idx" % self.doc):
354 self.run("%s -s %s mod%s.idx"
355 % (MAKEINDEX_BINARY, ISTFILE, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000356 if use_indfix:
Fred Drakedf84fac2000-09-20 05:49:09 +0000357 self.run("%s -s %s %s.idx"
358 % (MAKEINDEX_BINARY, ISTFILE, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000359 indfix.process(self.doc + ".ind")
Fred Drakedf84fac2000-09-20 05:49:09 +0000360 self.process_synopsis_files()
Fred Drakea871c2e1999-05-06 19:37:38 +0000361 #
362 # and now finish it off:
363 #
364 if os.path.isfile(self.doc + ".toc") and binary == PDFLATEX_BINARY:
365 import toc2bkm
Fred Drake239e1d52000-09-05 21:45:11 +0000366 if self.doctype == "manual":
367 bigpart = "chapter"
368 else:
369 bigpart = "section"
370 toc2bkm.process(self.doc + ".toc", self.doc + ".bkm", bigpart)
Fred Drakea871c2e1999-05-06 19:37:38 +0000371 if self.use_bibtex:
372 self.run("%s %s" % (BIBTEX_BINARY, self.doc))
373 self.run("%s %s" % (binary, self.doc))
Fred Drakeb258bed2001-02-12 15:30:22 +0000374 self.latex_runs = self.latex_runs + 1
Fred Drakea871c2e1999-05-06 19:37:38 +0000375
376 def process_synopsis_files(self):
377 synopsis_files = glob.glob(self.doc + "*.syn")
378 for path in synopsis_files:
379 uniqify_module_table(path)
Fred Drake8b880931999-03-03 20:24:30 +0000380
381 def build_ps(self):
382 self.run("%s -N0 -o %s.ps %s" % (DVIPS_BINARY, self.doc, self.doc))
383
Fred Drakeaebbca32001-07-17 14:46:09 +0000384 def build_html(self, builddir, max_split_depth=None):
Fred Drake8b880931999-03-03 20:24:30 +0000385 if max_split_depth is None:
386 max_split_depth = self.options.max_split_depth
387 texfile = None
388 for p in string.split(os.environ["TEXINPUTS"], os.pathsep):
389 fn = os.path.join(p, self.doc + ".tex")
390 if os.path.isfile(fn):
391 texfile = fn
392 break
393 if not texfile:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000394 self.warning("Could not locate %s.tex; aborting." % self.doc)
Fred Drake8b880931999-03-03 20:24:30 +0000395 sys.exit(1)
396 # remove leading ./ (or equiv.); might avoid problems w/ dvips
397 if texfile[:2] == os.curdir + os.sep:
398 texfile = texfile[2:]
399 # build the command line and run LaTeX2HTML:
Fred Drakeba828782000-04-03 04:19:14 +0000400 if not os.path.isdir(builddir):
401 os.mkdir(builddir)
Fred Drakef3d41272000-09-14 22:25:47 +0000402 else:
403 for fname in glob.glob(os.path.join(builddir, "*.html")):
404 os.unlink(fname)
Fred Drake8b880931999-03-03 20:24:30 +0000405 args = [LATEX2HTML_BINARY,
Fred Drake8b880931999-03-03 20:24:30 +0000406 "-init_file", self.l2h_aux_init_file,
407 "-dir", builddir,
408 texfile
409 ]
410 self.run(string.join(args)) # XXX need quoting!
411 # ... postprocess
412 shutil.copyfile(self.options.style_file,
413 os.path.join(builddir, self.doc + ".css"))
Fred Drake4437fdf1999-05-03 14:29:07 +0000414 shutil.copyfile(os.path.join(builddir, self.doc + ".html"),
415 os.path.join(builddir, "index.html"))
Fred Drakecfef00962001-03-02 16:26:45 +0000416 if max_split_depth != 1:
Fred Drakeaf922182001-05-09 04:03:16 +0000417 label_file = os.path.join(builddir, "labels.pl")
418 fp = open(label_file)
419 about_node = None
420 target = " = q/about/;\n"
421 x = len(target)
422 while 1:
423 line = fp.readline()
424 if not line:
425 break
426 if line[-x:] == target:
Fred Drakecfef00962001-03-02 16:26:45 +0000427 line = fp.readline()
Fred Drakeaf922182001-05-09 04:03:16 +0000428 m = re.search(r"\|(node\d+\.[a-z]+)\|", line)
429 about_node = m.group(1)
430 shutil.copyfile(os.path.join(builddir, about_node),
431 os.path.join(builddir, "about.html"))
432 break
433 if not self.options.numeric:
Fred Drakecfef00962001-03-02 16:26:45 +0000434 pwd = os.getcwd()
435 try:
436 os.chdir(builddir)
437 self.run("%s %s *.html" % (PERL_BINARY, NODE2LABEL_SCRIPT))
438 finally:
439 os.chdir(pwd)
Fred Drake8b880931999-03-03 20:24:30 +0000440
441 def build_text(self, tempdir=None):
442 if tempdir is None:
443 tempdir = self.doc
444 indexfile = os.path.join(tempdir, "index.html")
445 self.run("%s -nolist -dump %s >%s.txt"
446 % (LYNX_BINARY, indexfile, self.doc))
447
448 def require_temps(self, binary=None):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000449 if not self.latex_runs:
Fred Drake8b880931999-03-03 20:24:30 +0000450 self.build_aux(binary=binary)
451
452 def write_l2h_aux_init_file(self):
Fred Drake8bc627a2000-08-31 06:14:38 +0000453 options = self.options
Fred Drake8b880931999-03-03 20:24:30 +0000454 fp = open(self.l2h_aux_init_file, "w")
Fred Drake19157542000-07-31 17:47:49 +0000455 d = string_to_perl(os.path.dirname(L2H_INIT_FILE))
456 fp.write("package main;\n"
457 "push (@INC, '%s');\n"
458 "$mydir = '%s';\n"
459 % (d, d))
Fred Drake498c18f2000-07-24 23:03:32 +0000460 fp.write(open(L2H_INIT_FILE).read())
Fred Drake8bc627a2000-08-31 06:14:38 +0000461 for filename in options.l2h_init_files:
462 fp.write("\n# initialization code incorporated from:\n# ")
463 fp.write(filename)
464 fp.write("\n")
465 fp.write(open(filename).read())
Fred Drake498c18f2000-07-24 23:03:32 +0000466 fp.write("\n"
467 "# auxillary init file for latex2html\n"
Fred Drake8b880931999-03-03 20:24:30 +0000468 "# generated by mkhowto\n"
Fred Drake4437fdf1999-05-03 14:29:07 +0000469 "$NO_AUTO_LINK = 1;\n"
Fred Drake8b880931999-03-03 20:24:30 +0000470 )
Fred Drakecf1b06e1999-09-23 16:55:09 +0000471 l2hoption(fp, "ABOUT_FILE", options.about_file)
Fred Drake8b880931999-03-03 20:24:30 +0000472 l2hoption(fp, "ICONSERVER", options.icon_server)
Fred Drake52ea0ce1999-09-22 19:55:35 +0000473 l2hoption(fp, "IMAGE_TYPE", options.image_type)
Fred Drake8b880931999-03-03 20:24:30 +0000474 l2hoption(fp, "ADDRESS", options.address)
475 l2hoption(fp, "MAX_LINK_DEPTH", options.max_link_depth)
476 l2hoption(fp, "MAX_SPLIT_DEPTH", options.max_split_depth)
Fred Drakedfa539d2000-08-31 06:58:34 +0000477 l2hoption(fp, "EXTERNAL_UP_LINK", options.up_link)
478 l2hoption(fp, "EXTERNAL_UP_TITLE", options.up_title)
Fred Drake42181db2001-01-09 22:02:10 +0000479 l2hoption(fp, "GLOBAL_MODULE_INDEX", options.global_module_index)
Fred Drake3ce28e42001-10-30 16:09:51 +0000480 l2hoption(fp, "DVIPS_SAFE", options.dvips_safe)
Fred Drake8b880931999-03-03 20:24:30 +0000481 fp.write("1;\n")
482 fp.close()
483
484 def cleanup(self):
485 self.__have_temps = 0
486 for pattern in ("%s.aux", "%s.log", "%s.out", "%s.toc", "%s.bkm",
Fred Drakea871c2e1999-05-06 19:37:38 +0000487 "%s.idx", "%s.ilg", "%s.ind", "%s.pla",
Fred Drake8b880931999-03-03 20:24:30 +0000488 "%s.bbl", "%s.blg",
489 "mod%s.idx", "mod%s.ind", "mod%s.ilg",
490 ):
491 safe_unlink(pattern % self.doc)
Fred Drakea871c2e1999-05-06 19:37:38 +0000492 map(safe_unlink, glob.glob(self.doc + "*.syn"))
Fred Drake8b880931999-03-03 20:24:30 +0000493 for spec in ("IMG*", "*.pl", "WARNINGS", "index.dat", "modindex.dat"):
494 pattern = os.path.join(self.doc, spec)
495 map(safe_unlink, glob.glob(pattern))
496 if "dvi" not in self.options.formats:
497 safe_unlink(self.doc + ".dvi")
498 if os.path.isdir(self.doc + "-temp-html"):
499 shutil.rmtree(self.doc + "-temp-html", ignore_errors=1)
500 if not self.options.logging:
501 os.unlink(self.log_filename)
502 if not self.options.debugging:
503 os.unlink(self.l2h_aux_init_file)
504
505 def run(self, command):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000506 self.message(command)
Fred Drake1cb560a2001-08-10 20:17:09 +0000507 if sys.platform.startswith("win"):
508 rc = os.system(command)
509 else:
510 rc = os.system("(%s) </dev/null >>%s 2>&1"
511 % (command, self.log_filename))
Fred Drake8b880931999-03-03 20:24:30 +0000512 if rc:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000513 self.warning(
514 "Session transcript and error messages are in %s."
Fred Drake8b880931999-03-03 20:24:30 +0000515 % self.log_filename)
Fred Drake1cb560a2001-08-10 20:17:09 +0000516 if hasattr(os, "WIFEXITED"):
517 if os.WIFEXITED(rc):
518 self.warning("Exited with status %s." % os.WEXITSTATUS(rc))
519 else:
520 self.warning("Killed by signal %s." % os.WSTOPSIG(rc))
521 else:
522 self.warning("Return code: %s" % rc)
Fred Drake4e3f2752001-02-04 15:20:26 +0000523 sys.stderr.write("The relevant lines from the transcript are:\n")
524 sys.stderr.write("-" * 72 + "\n")
525 sys.stderr.writelines(get_run_transcript(self.log_filename))
Fred Drake8b880931999-03-03 20:24:30 +0000526 sys.exit(rc)
527
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000528 def message(self, msg):
529 msg = "+++ " + msg
530 if not self.options.quiet:
531 print msg
Fred Drake52ea0ce1999-09-22 19:55:35 +0000532 self.log(msg + "\n")
533
534 def warning(self, msg):
535 msg = "*** %s\n" % msg
536 sys.stderr.write(msg)
537 self.log(msg)
538
539 def log(self, msg):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000540 fp = open(self.log_filename, "a")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000541 fp.write(msg)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000542 fp.close()
543
Fred Drake8b880931999-03-03 20:24:30 +0000544
Fred Drake4e3f2752001-02-04 15:20:26 +0000545def get_run_transcript(filename):
546 """Return lines from the transcript file for the most recent run() call."""
547 fp = open(filename)
548 lines = fp.readlines()
549 fp.close()
550 lines.reverse()
551 L = []
552 for line in lines:
553 L.append(line)
554 if line[:4] == "+++ ":
555 break
556 L.reverse()
557 return L
558
559
Fred Drake8b880931999-03-03 20:24:30 +0000560def safe_unlink(path):
Fred Drake4e3f2752001-02-04 15:20:26 +0000561 """Unlink a file without raising an error if it doesn't exist."""
Fred Drake8b880931999-03-03 20:24:30 +0000562 try:
563 os.unlink(path)
564 except os.error:
565 pass
566
567
Fred Drakea871c2e1999-05-06 19:37:38 +0000568def split_pathname(path):
Fred Drakebfd80dd2001-06-23 03:06:01 +0000569 path = os.path.abspath(path)
Fred Drakea871c2e1999-05-06 19:37:38 +0000570 dirname, basename = os.path.split(path)
Fred Drake8b880931999-03-03 20:24:30 +0000571 if basename[-4:] == ".tex":
572 basename = basename[:-4]
573 return dirname, basename
574
575
Fred Drakea871c2e1999-05-06 19:37:38 +0000576_doctype_rx = re.compile(r"\\documentclass(?:\[[^]]*\])?{([a-zA-Z]*)}")
577def get_doctype(path):
578 fp = open(path)
579 doctype = None
580 while 1:
581 line = fp.readline()
582 if not line:
583 break
584 m = _doctype_rx.match(line)
585 if m:
586 doctype = m.group(1)
587 break
588 fp.close()
589 return doctype
590
591
Fred Drake8b880931999-03-03 20:24:30 +0000592def main():
593 options = Options()
594 try:
595 args = options.parse(sys.argv[1:])
596 except getopt.error, msg:
597 error(options, msg)
598 if not args:
599 # attempt to locate single .tex file in current directory:
600 args = glob.glob("*.tex")
601 if not args:
602 error(options, "No file to process.")
603 if len(args) > 1:
604 error(options, "Could not deduce which files should be processed.")
605 #
606 # parameters are processed, let's go!
607 #
608 for path in args:
609 Job(options, path).build()
610
611
612def l2hoption(fp, option, value):
613 if value:
614 fp.write('$%s = "%s";\n' % (option, string_to_perl(str(value))))
615
616
617_to_perl = {}
618for c in map(chr, range(1, 256)):
619 _to_perl[c] = c
620_to_perl["@"] = "\\@"
621_to_perl["$"] = "\\$"
622_to_perl['"'] = '\\"'
623
624def string_to_perl(s):
625 return string.join(map(_to_perl.get, s), '')
626
627
628def check_for_bibtex(filename):
629 fp = open(filename)
630 pos = string.find(fp.read(), r"\bibdata{")
631 fp.close()
632 return pos >= 0
633
634def uniqify_module_table(filename):
635 lines = open(filename).readlines()
636 if len(lines) > 1:
637 if lines[-1] == lines[-2]:
638 del lines[-1]
639 open(filename, "w").writelines(lines)
640
641
642def new_index(filename, label="genindex"):
643 fp = open(filename, "w")
644 fp.write(r"""\
645\begin{theindex}
646\label{%s}
647\end{theindex}
648""" % label)
649 fp.close()
650
651
652if __name__ == "__main__":
653 main()