blob: 4fe97fb84f3246c0c6bff11c3afd2835e6f3e1f3 [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 Drake8b880931999-03-03 20:24:30 +000028
29Other options:
30 --a4 Format for A4 paper.
31 --letter Format for US letter paper (the default).
32 --help, -H Show this text.
33 --logging, -l Log stdout and stderr to a file (*.how).
34 --debugging, -D Echo commands as they are executed.
35 --keep, -k Keep temporary files around.
36 --quiet, -q Do not print command output to stdout.
37 (stderr is also lost, sorry; see *.how for errors)
38"""
39
40import getopt
41import glob
42import os
Fred Drakea871c2e1999-05-06 19:37:38 +000043import re
Fred Drake8b880931999-03-03 20:24:30 +000044import shutil
45import string
46import sys
Fred Drake8b880931999-03-03 20:24:30 +000047
48
Fred Drake964c0742001-05-29 16:10:07 +000049if not hasattr(os.path, "abspath"):
Fred Drakebfd80dd2001-06-23 03:06:01 +000050 # Python 1.5.1 or earlier
Fred Drake964c0742001-05-29 16:10:07 +000051 def abspath(path):
52 """Return an absolute path."""
53 if not os.path.isabs(path):
54 path = os.path.join(os.getcwd(), path)
55 return os.path.normpath(path)
56
57 os.path.abspath = abspath
58
59
Fred Drakefcb87252000-08-29 18:15:05 +000060MYDIR = os.path.abspath(sys.path[0])
61TOPDIR = os.path.dirname(MYDIR)
Fred Drake8b880931999-03-03 20:24:30 +000062
63ISTFILE = os.path.join(TOPDIR, "texinputs", "python.ist")
64NODE2LABEL_SCRIPT = os.path.join(MYDIR, "node2label.pl")
65L2H_INIT_FILE = os.path.join(TOPDIR, "perl", "l2hinit.perl")
66
67BIBTEX_BINARY = "bibtex"
68DVIPS_BINARY = "dvips"
69LATEX_BINARY = "latex"
70LATEX2HTML_BINARY = "latex2html"
71LYNX_BINARY = "lynx"
72MAKEINDEX_BINARY = "makeindex"
73PDFLATEX_BINARY = "pdflatex"
74PERL_BINARY = "perl"
75PYTHON_BINARY = "python"
76
77
78def usage(options):
79 print __doc__ % options
80
81def error(options, message, err=2):
82 sys.stdout = sys.stderr
83 print message
84 print
85 usage(options)
86 sys.exit(2)
87
88
89class Options:
90 program = os.path.basename(sys.argv[0])
91 #
92 address = ''
Fred Drake50d1fcf2001-02-19 19:18:09 +000093 builddir = None
Fred Drake8b880931999-03-03 20:24:30 +000094 debugging = 0
95 discard_temps = 1
96 have_temps = 0
Fred Drake15a159c2002-10-01 15:20:20 +000097 icon_server = "."
Fred Drake52ea0ce1999-09-22 19:55:35 +000098 image_type = "gif"
Fred Drake8b880931999-03-03 20:24:30 +000099 logging = 0
100 max_link_depth = 3
101 max_split_depth = 6
102 paper = "letter"
103 quiet = 0
Fred Drake52ea0ce1999-09-22 19:55:35 +0000104 runs = 0
Fred Drake9a257b42000-03-31 20:27:36 +0000105 numeric = 0
Fred Drake42181db2001-01-09 22:02:10 +0000106 global_module_index = None
Fred Drake8b880931999-03-03 20:24:30 +0000107 style_file = os.path.join(TOPDIR, "html", "style.css")
Fred Drakecf1b06e1999-09-23 16:55:09 +0000108 about_file = os.path.join(TOPDIR, "html", "about.dat")
Fred Drakedfa539d2000-08-31 06:58:34 +0000109 up_link = None
110 up_title = None
Fred Drake8b880931999-03-03 20:24:30 +0000111 #
Fred Drake3ce28e42001-10-30 16:09:51 +0000112 # 'dvips_safe' is a weird option. It is used mostly to make
113 # LaTeX2HTML not try to be too smart about protecting the user
114 # from a bad version of dvips -- some versions would core dump if
115 # the path to the source DVI contained a dot, and it's appearantly
116 # difficult to determine if the version available has that bug.
117 # This option gets set when PostScript output is requested
118 # (because we're going to run dvips regardless, and we'll either
119 # know it succeeds before LaTeX2HTML is run, or we'll have
120 # detected the failure and bailed), or the user asserts that it's
121 # safe from the command line.
122 #
123 # So, why does LaTeX2HTML think it appropriate to protect the user
124 # from a dvips that's only potentially going to core dump? Only
125 # because they want to avoid doing a lot of work just to have to
126 # bail later with no useful intermediates. Unfortunately, they
127 # bail *before* they know whether dvips will be needed at all.
128 # I've gone around the bush a few times with the LaTeX2HTML
129 # developers over whether this is appropriate behavior, and they
130 # don't seem interested in changing their position.
131 #
132 dvips_safe = 0
133 #
Fred Drake55994412001-01-30 22:30:01 +0000134 DEFAULT_FORMATS = ("html",)
Fred Drake8b880931999-03-03 20:24:30 +0000135 ALL_FORMATS = ("dvi", "html", "pdf", "ps", "text")
136
137 def __init__(self):
Fred Drake8b880931999-03-03 20:24:30 +0000138 self.formats = []
Fred Drake8bc627a2000-08-31 06:14:38 +0000139 self.l2h_init_files = []
Fred Drake8b880931999-03-03 20:24:30 +0000140
141 def __getitem__(self, key):
142 # This is used when formatting the usage message.
143 try:
144 return getattr(self, key)
145 except AttributeError:
146 raise KeyError, key
147
148 def parse(self, args):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000149 opts, args = getopt.getopt(args, "Hi:a:s:lDkqr:",
Fred Drake8b880931999-03-03 20:24:30 +0000150 ["all", "postscript", "help", "iconserver=",
Fred Drake8bc627a2000-08-31 06:14:38 +0000151 "address=", "a4", "letter", "l2h-init=",
Fred Drake8b880931999-03-03 20:24:30 +0000152 "link=", "split=", "logging", "debugging",
Fred Drakecf1b06e1999-09-23 16:55:09 +0000153 "keep", "quiet", "runs=", "image-type=",
Fred Drake50d1fcf2001-02-19 19:18:09 +0000154 "about=", "numeric", "style=", "paper=",
155 "up-link=", "up-title=", "dir=",
Fred Drake3ce28e42001-10-30 16:09:51 +0000156 "global-module-index=", "dvips-safe"]
Fred Drake52ea0ce1999-09-22 19:55:35 +0000157 + list(self.ALL_FORMATS))
Fred Drake8b880931999-03-03 20:24:30 +0000158 for opt, arg in opts:
159 if opt == "--all":
160 self.formats = list(self.ALL_FORMATS)
Fred Drake3ce28e42001-10-30 16:09:51 +0000161 self.dvips_safe = "ps" in self.formats
Fred Drake8b880931999-03-03 20:24:30 +0000162 elif opt in ("-H", "--help"):
163 usage(self)
164 sys.exit()
165 elif opt == "--iconserver":
166 self.icon_server = arg
167 elif opt in ("-a", "--address"):
168 self.address = arg
169 elif opt == "--a4":
170 self.paper = "a4"
171 elif opt == "--letter":
172 self.paper = "letter"
Fred Drake8b880931999-03-03 20:24:30 +0000173 elif opt == "--link":
174 self.max_link_depth = int(arg)
175 elif opt in ("-s", "--split"):
176 self.max_split_depth = int(arg)
177 elif opt in ("-l", "--logging"):
178 self.logging = self.logging + 1
179 elif opt in ("-D", "--debugging"):
180 self.debugging = self.debugging + 1
181 elif opt in ("-k", "--keep"):
182 self.discard_temps = 0
183 elif opt in ("-q", "--quiet"):
184 self.quiet = 1
Fred Drake52ea0ce1999-09-22 19:55:35 +0000185 elif opt in ("-r", "--runs"):
186 self.runs = int(arg)
187 elif opt == "--image-type":
188 self.image_type = arg
Fred Drakecf1b06e1999-09-23 16:55:09 +0000189 elif opt == "--about":
190 # always make this absolute:
191 self.about_file = os.path.normpath(
Fred Drakefcb87252000-08-29 18:15:05 +0000192 os.path.abspath(arg))
Fred Drake9a257b42000-03-31 20:27:36 +0000193 elif opt == "--numeric":
194 self.numeric = 1
Fred Drakefcb87252000-08-29 18:15:05 +0000195 elif opt == "--style":
196 self.style_file = os.path.abspath(arg)
Fred Drake8bc627a2000-08-31 06:14:38 +0000197 elif opt == "--l2h-init":
198 self.l2h_init_files.append(os.path.abspath(arg))
Fred Drakedfa539d2000-08-31 06:58:34 +0000199 elif opt == "--up-link":
200 self.up_link = arg
201 elif opt == "--up-title":
202 self.up_title = arg
Fred Drake42181db2001-01-09 22:02:10 +0000203 elif opt == "--global-module-index":
204 self.global_module_index = arg
Fred Drake50d1fcf2001-02-19 19:18:09 +0000205 elif opt == "--dir":
Fred Drake1cb560a2001-08-10 20:17:09 +0000206 if os.sep == "\\":
207 arg = re.sub("/", "\\", arg)
Fred Drake50d1fcf2001-02-19 19:18:09 +0000208 self.builddir = arg
209 elif opt == "--paper":
210 self.paper = arg
Fred Drake3ce28e42001-10-30 16:09:51 +0000211 elif opt == "--dvips-safe":
212 self.dvips_safe = 1
Fred Drake8b880931999-03-03 20:24:30 +0000213 #
214 # Format specifiers:
215 #
216 elif opt[2:] in self.ALL_FORMATS:
217 self.add_format(opt[2:])
218 elif opt == "--postscript":
219 # synonym for --ps
220 self.add_format("ps")
221 self.initialize()
222 #
223 # return the args to allow the caller access:
224 #
225 return args
226
227 def add_format(self, format):
228 """Add a format to the formats list if not present."""
229 if not format in self.formats:
Fred Drake3ce28e42001-10-30 16:09:51 +0000230 if format == "ps":
231 # assume this is safe since we're going to run it anyway
232 self.dvips_safe = 1
Fred Drake8b880931999-03-03 20:24:30 +0000233 self.formats.append(format)
234
235 def initialize(self):
236 """Complete initialization. This is needed if parse() isn't used."""
237 # add the default format if no formats were specified:
238 if not self.formats:
239 self.formats = self.DEFAULT_FORMATS
240 # determine the base set of texinputs directories:
241 texinputs = string.split(os.environ.get("TEXINPUTS", ""), os.pathsep)
242 if not texinputs:
243 texinputs = ['']
244 self.base_texinputs = [
245 os.path.join(TOPDIR, "paper-" + self.paper),
246 os.path.join(TOPDIR, "texinputs"),
247 ] + texinputs
Fred Drakebfd80dd2001-06-23 03:06:01 +0000248 if self.builddir:
249 self.builddir = os.path.abspath(self.builddir)
Fred Drake8b880931999-03-03 20:24:30 +0000250
251
252class Job:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000253 latex_runs = 0
254
Fred Drake8b880931999-03-03 20:24:30 +0000255 def __init__(self, options, path):
256 self.options = options
Fred Drakea871c2e1999-05-06 19:37:38 +0000257 self.doctype = get_doctype(path)
Fred Drake8b880931999-03-03 20:24:30 +0000258 self.filedir, self.doc = split_pathname(path)
Fred Drakebfd80dd2001-06-23 03:06:01 +0000259 self.builddir = os.path.abspath(options.builddir or self.doc)
Fred Drakeaebbca32001-07-17 14:46:09 +0000260 if ("html" in options.formats or "text" in options.formats):
261 if not os.path.exists(self.builddir):
262 os.mkdir(self.builddir)
263 self.log_filename = os.path.join(self.builddir, self.doc + ".how")
264 else:
265 self.log_filename = os.path.abspath(self.doc + ".how")
Fred Drake8b880931999-03-03 20:24:30 +0000266 if os.path.exists(self.log_filename):
267 os.unlink(self.log_filename)
Fred Drake246beb22002-08-27 16:34:54 +0000268 l2hconf = self.doc + ".l2h"
269 if os.path.exists(l2hconf):
270 if os.path.exists(l2hconf + "~"):
271 os.unlink(l2hconf + "~")
272 os.rename(l2hconf, l2hconf + "~")
273 self.l2h_aux_init_file = self.doc + ".l2h"
Fred Drake8b880931999-03-03 20:24:30 +0000274 self.write_l2h_aux_init_file()
275
276 def build(self):
277 self.setup_texinputs()
278 formats = self.options.formats
279 if "dvi" in formats or "ps" in formats:
280 self.build_dvi()
281 if "pdf" in formats:
282 self.build_pdf()
283 if "ps" in formats:
284 self.build_ps()
285 if "html" in formats:
286 self.require_temps()
Fred Drakebfd80dd2001-06-23 03:06:01 +0000287 self.build_html(self.builddir)
Fred Drake8b880931999-03-03 20:24:30 +0000288 if self.options.icon_server == ".":
Fred Drake52ea0ce1999-09-22 19:55:35 +0000289 pattern = os.path.join(TOPDIR, "html", "icons",
290 "*." + self.options.image_type)
291 imgs = glob.glob(pattern)
292 if not imgs:
293 self.warning(
294 "Could not locate support images of type %s."
295 % `self.options.image_type`)
296 for fn in imgs:
Fred Drake8b880931999-03-03 20:24:30 +0000297 new_fn = os.path.join(self.doc, os.path.basename(fn))
298 shutil.copyfile(fn, new_fn)
299 if "text" in formats:
300 self.require_temps()
301 tempdir = self.doc
302 need_html = "html" not in formats
303 if self.options.max_split_depth != 1:
304 fp = open(self.l2h_aux_init_file, "a")
305 fp.write("# re-hack this file for --text:\n")
306 l2hoption(fp, "MAX_SPLIT_DEPTH", "1")
307 fp.write("1;\n")
308 fp.close()
309 tempdir = self.doc + "-temp-html"
310 need_html = 1
311 if need_html:
312 self.build_html(tempdir, max_split_depth=1)
313 self.build_text(tempdir)
314 if self.options.discard_temps:
315 self.cleanup()
316
317 def setup_texinputs(self):
318 texinputs = [self.filedir] + list(self.options.base_texinputs)
319 os.environ["TEXINPUTS"] = string.join(texinputs, os.pathsep)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000320 self.message("TEXINPUTS=" + os.environ["TEXINPUTS"])
Fred Drake8b880931999-03-03 20:24:30 +0000321
Fred Drake8b880931999-03-03 20:24:30 +0000322 def build_aux(self, binary=None):
323 if binary is None:
324 binary = LATEX_BINARY
325 new_index( "%s.ind" % self.doc, "genindex")
326 new_index("mod%s.ind" % self.doc, "modindex")
327 self.run("%s %s" % (binary, self.doc))
328 self.use_bibtex = check_for_bibtex(self.doc + ".aux")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000329 self.latex_runs = 1
Fred Drake8b880931999-03-03 20:24:30 +0000330
331 def build_dvi(self):
332 self.use_latex(LATEX_BINARY)
333
334 def build_pdf(self):
335 self.use_latex(PDFLATEX_BINARY)
336
337 def use_latex(self, binary):
338 self.require_temps(binary=binary)
Fred Drakedf84fac2000-09-20 05:49:09 +0000339 if self.latex_runs < 2:
340 if os.path.isfile("mod%s.idx" % self.doc):
341 self.run("%s mod%s.idx" % (MAKEINDEX_BINARY, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000342 use_indfix = 0
Fred Drakedf84fac2000-09-20 05:49:09 +0000343 if os.path.isfile(self.doc + ".idx"):
Fred Drake9dce7b32000-11-03 02:57:31 +0000344 use_indfix = 1
Fred Drakedf84fac2000-09-20 05:49:09 +0000345 # call to Doc/tools/fix_hack omitted; doesn't appear necessary
346 self.run("%s %s.idx" % (MAKEINDEX_BINARY, self.doc))
347 import indfix
348 indfix.process(self.doc + ".ind")
349 if self.use_bibtex:
350 self.run("%s %s" % (BIBTEX_BINARY, self.doc))
351 self.process_synopsis_files()
Fred Drakedf84fac2000-09-20 05:49:09 +0000352 self.run("%s %s" % (binary, self.doc))
Fred Drakeb258bed2001-02-12 15:30:22 +0000353 self.latex_runs = self.latex_runs + 1
Fred Drakedf84fac2000-09-20 05:49:09 +0000354 if os.path.isfile("mod%s.idx" % self.doc):
355 self.run("%s -s %s mod%s.idx"
356 % (MAKEINDEX_BINARY, ISTFILE, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000357 if use_indfix:
Fred Drakedf84fac2000-09-20 05:49:09 +0000358 self.run("%s -s %s %s.idx"
359 % (MAKEINDEX_BINARY, ISTFILE, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000360 indfix.process(self.doc + ".ind")
Fred Drakedf84fac2000-09-20 05:49:09 +0000361 self.process_synopsis_files()
Fred Drakea871c2e1999-05-06 19:37:38 +0000362 #
363 # and now finish it off:
364 #
365 if os.path.isfile(self.doc + ".toc") and binary == PDFLATEX_BINARY:
366 import toc2bkm
Fred Drake239e1d52000-09-05 21:45:11 +0000367 if self.doctype == "manual":
368 bigpart = "chapter"
369 else:
370 bigpart = "section"
371 toc2bkm.process(self.doc + ".toc", self.doc + ".bkm", bigpart)
Fred Drakea871c2e1999-05-06 19:37:38 +0000372 if self.use_bibtex:
373 self.run("%s %s" % (BIBTEX_BINARY, self.doc))
374 self.run("%s %s" % (binary, self.doc))
Fred Drakeb258bed2001-02-12 15:30:22 +0000375 self.latex_runs = self.latex_runs + 1
Fred Drakea871c2e1999-05-06 19:37:38 +0000376
377 def process_synopsis_files(self):
378 synopsis_files = glob.glob(self.doc + "*.syn")
379 for path in synopsis_files:
380 uniqify_module_table(path)
Fred Drake8b880931999-03-03 20:24:30 +0000381
382 def build_ps(self):
383 self.run("%s -N0 -o %s.ps %s" % (DVIPS_BINARY, self.doc, self.doc))
384
Fred Drakeaebbca32001-07-17 14:46:09 +0000385 def build_html(self, builddir, max_split_depth=None):
Fred Drake8b880931999-03-03 20:24:30 +0000386 if max_split_depth is None:
387 max_split_depth = self.options.max_split_depth
388 texfile = None
389 for p in string.split(os.environ["TEXINPUTS"], os.pathsep):
390 fn = os.path.join(p, self.doc + ".tex")
391 if os.path.isfile(fn):
392 texfile = fn
393 break
394 if not texfile:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000395 self.warning("Could not locate %s.tex; aborting." % self.doc)
Fred Drake8b880931999-03-03 20:24:30 +0000396 sys.exit(1)
397 # remove leading ./ (or equiv.); might avoid problems w/ dvips
398 if texfile[:2] == os.curdir + os.sep:
399 texfile = texfile[2:]
400 # build the command line and run LaTeX2HTML:
Fred Drakeba828782000-04-03 04:19:14 +0000401 if not os.path.isdir(builddir):
402 os.mkdir(builddir)
Fred Drakef3d41272000-09-14 22:25:47 +0000403 else:
404 for fname in glob.glob(os.path.join(builddir, "*.html")):
405 os.unlink(fname)
Fred Drake8b880931999-03-03 20:24:30 +0000406 args = [LATEX2HTML_BINARY,
Fred Drake8b880931999-03-03 20:24:30 +0000407 "-init_file", self.l2h_aux_init_file,
408 "-dir", builddir,
409 texfile
410 ]
411 self.run(string.join(args)) # XXX need quoting!
412 # ... postprocess
413 shutil.copyfile(self.options.style_file,
414 os.path.join(builddir, self.doc + ".css"))
Fred Drake4437fdf1999-05-03 14:29:07 +0000415 shutil.copyfile(os.path.join(builddir, self.doc + ".html"),
416 os.path.join(builddir, "index.html"))
Fred Drakecfef00962001-03-02 16:26:45 +0000417 if max_split_depth != 1:
Fred Drakeaf922182001-05-09 04:03:16 +0000418 label_file = os.path.join(builddir, "labels.pl")
419 fp = open(label_file)
420 about_node = None
421 target = " = q/about/;\n"
422 x = len(target)
423 while 1:
424 line = fp.readline()
425 if not line:
426 break
427 if line[-x:] == target:
Fred Drakecfef00962001-03-02 16:26:45 +0000428 line = fp.readline()
Fred Drakeaf922182001-05-09 04:03:16 +0000429 m = re.search(r"\|(node\d+\.[a-z]+)\|", line)
430 about_node = m.group(1)
431 shutil.copyfile(os.path.join(builddir, about_node),
432 os.path.join(builddir, "about.html"))
433 break
434 if not self.options.numeric:
Fred Drakecfef00962001-03-02 16:26:45 +0000435 pwd = os.getcwd()
436 try:
437 os.chdir(builddir)
438 self.run("%s %s *.html" % (PERL_BINARY, NODE2LABEL_SCRIPT))
439 finally:
440 os.chdir(pwd)
Fred Drake8b880931999-03-03 20:24:30 +0000441
442 def build_text(self, tempdir=None):
443 if tempdir is None:
444 tempdir = self.doc
445 indexfile = os.path.join(tempdir, "index.html")
446 self.run("%s -nolist -dump %s >%s.txt"
447 % (LYNX_BINARY, indexfile, self.doc))
448
449 def require_temps(self, binary=None):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000450 if not self.latex_runs:
Fred Drake8b880931999-03-03 20:24:30 +0000451 self.build_aux(binary=binary)
452
453 def write_l2h_aux_init_file(self):
Fred Drake8bc627a2000-08-31 06:14:38 +0000454 options = self.options
Fred Drake8b880931999-03-03 20:24:30 +0000455 fp = open(self.l2h_aux_init_file, "w")
Fred Drake19157542000-07-31 17:47:49 +0000456 d = string_to_perl(os.path.dirname(L2H_INIT_FILE))
457 fp.write("package main;\n"
458 "push (@INC, '%s');\n"
459 "$mydir = '%s';\n"
460 % (d, d))
Fred Drake498c18f2000-07-24 23:03:32 +0000461 fp.write(open(L2H_INIT_FILE).read())
Fred Drake8bc627a2000-08-31 06:14:38 +0000462 for filename in options.l2h_init_files:
463 fp.write("\n# initialization code incorporated from:\n# ")
464 fp.write(filename)
465 fp.write("\n")
466 fp.write(open(filename).read())
Fred Drake498c18f2000-07-24 23:03:32 +0000467 fp.write("\n"
468 "# auxillary init file for latex2html\n"
Fred Drake8b880931999-03-03 20:24:30 +0000469 "# generated by mkhowto\n"
Fred Drake4437fdf1999-05-03 14:29:07 +0000470 "$NO_AUTO_LINK = 1;\n"
Fred Drake8b880931999-03-03 20:24:30 +0000471 )
Fred Drakecf1b06e1999-09-23 16:55:09 +0000472 l2hoption(fp, "ABOUT_FILE", options.about_file)
Fred Drake8b880931999-03-03 20:24:30 +0000473 l2hoption(fp, "ICONSERVER", options.icon_server)
Fred Drake52ea0ce1999-09-22 19:55:35 +0000474 l2hoption(fp, "IMAGE_TYPE", options.image_type)
Fred Drake8b880931999-03-03 20:24:30 +0000475 l2hoption(fp, "ADDRESS", options.address)
476 l2hoption(fp, "MAX_LINK_DEPTH", options.max_link_depth)
477 l2hoption(fp, "MAX_SPLIT_DEPTH", options.max_split_depth)
Fred Drakedfa539d2000-08-31 06:58:34 +0000478 l2hoption(fp, "EXTERNAL_UP_LINK", options.up_link)
479 l2hoption(fp, "EXTERNAL_UP_TITLE", options.up_title)
Fred Drake42181db2001-01-09 22:02:10 +0000480 l2hoption(fp, "GLOBAL_MODULE_INDEX", options.global_module_index)
Fred Drake3ce28e42001-10-30 16:09:51 +0000481 l2hoption(fp, "DVIPS_SAFE", options.dvips_safe)
Fred Drake8b880931999-03-03 20:24:30 +0000482 fp.write("1;\n")
483 fp.close()
484
485 def cleanup(self):
486 self.__have_temps = 0
487 for pattern in ("%s.aux", "%s.log", "%s.out", "%s.toc", "%s.bkm",
Fred Drakea871c2e1999-05-06 19:37:38 +0000488 "%s.idx", "%s.ilg", "%s.ind", "%s.pla",
Fred Drake8b880931999-03-03 20:24:30 +0000489 "%s.bbl", "%s.blg",
490 "mod%s.idx", "mod%s.ind", "mod%s.ilg",
491 ):
492 safe_unlink(pattern % self.doc)
Fred Drakea871c2e1999-05-06 19:37:38 +0000493 map(safe_unlink, glob.glob(self.doc + "*.syn"))
Fred Drake8b880931999-03-03 20:24:30 +0000494 for spec in ("IMG*", "*.pl", "WARNINGS", "index.dat", "modindex.dat"):
495 pattern = os.path.join(self.doc, spec)
496 map(safe_unlink, glob.glob(pattern))
497 if "dvi" not in self.options.formats:
498 safe_unlink(self.doc + ".dvi")
499 if os.path.isdir(self.doc + "-temp-html"):
500 shutil.rmtree(self.doc + "-temp-html", ignore_errors=1)
501 if not self.options.logging:
502 os.unlink(self.log_filename)
503 if not self.options.debugging:
504 os.unlink(self.l2h_aux_init_file)
505
506 def run(self, command):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000507 self.message(command)
Fred Drake1cb560a2001-08-10 20:17:09 +0000508 if sys.platform.startswith("win"):
509 rc = os.system(command)
510 else:
511 rc = os.system("(%s) </dev/null >>%s 2>&1"
512 % (command, self.log_filename))
Fred Drake8b880931999-03-03 20:24:30 +0000513 if rc:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000514 self.warning(
515 "Session transcript and error messages are in %s."
Fred Drake8b880931999-03-03 20:24:30 +0000516 % self.log_filename)
Fred Drake1cb560a2001-08-10 20:17:09 +0000517 if hasattr(os, "WIFEXITED"):
518 if os.WIFEXITED(rc):
519 self.warning("Exited with status %s." % os.WEXITSTATUS(rc))
520 else:
521 self.warning("Killed by signal %s." % os.WSTOPSIG(rc))
522 else:
523 self.warning("Return code: %s" % rc)
Fred Drake4e3f2752001-02-04 15:20:26 +0000524 sys.stderr.write("The relevant lines from the transcript are:\n")
525 sys.stderr.write("-" * 72 + "\n")
526 sys.stderr.writelines(get_run_transcript(self.log_filename))
Fred Drake8b880931999-03-03 20:24:30 +0000527 sys.exit(rc)
528
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000529 def message(self, msg):
530 msg = "+++ " + msg
531 if not self.options.quiet:
532 print msg
Fred Drake52ea0ce1999-09-22 19:55:35 +0000533 self.log(msg + "\n")
534
535 def warning(self, msg):
536 msg = "*** %s\n" % msg
537 sys.stderr.write(msg)
538 self.log(msg)
539
540 def log(self, msg):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000541 fp = open(self.log_filename, "a")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000542 fp.write(msg)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000543 fp.close()
544
Fred Drake8b880931999-03-03 20:24:30 +0000545
Fred Drake4e3f2752001-02-04 15:20:26 +0000546def get_run_transcript(filename):
547 """Return lines from the transcript file for the most recent run() call."""
548 fp = open(filename)
549 lines = fp.readlines()
550 fp.close()
551 lines.reverse()
552 L = []
553 for line in lines:
554 L.append(line)
555 if line[:4] == "+++ ":
556 break
557 L.reverse()
558 return L
559
560
Fred Drake8b880931999-03-03 20:24:30 +0000561def safe_unlink(path):
Fred Drake4e3f2752001-02-04 15:20:26 +0000562 """Unlink a file without raising an error if it doesn't exist."""
Fred Drake8b880931999-03-03 20:24:30 +0000563 try:
564 os.unlink(path)
565 except os.error:
566 pass
567
568
Fred Drakea871c2e1999-05-06 19:37:38 +0000569def split_pathname(path):
Fred Drakebfd80dd2001-06-23 03:06:01 +0000570 path = os.path.abspath(path)
Fred Drakea871c2e1999-05-06 19:37:38 +0000571 dirname, basename = os.path.split(path)
Fred Drake8b880931999-03-03 20:24:30 +0000572 if basename[-4:] == ".tex":
573 basename = basename[:-4]
574 return dirname, basename
575
576
Fred Drakea871c2e1999-05-06 19:37:38 +0000577_doctype_rx = re.compile(r"\\documentclass(?:\[[^]]*\])?{([a-zA-Z]*)}")
578def get_doctype(path):
579 fp = open(path)
580 doctype = None
581 while 1:
582 line = fp.readline()
583 if not line:
584 break
585 m = _doctype_rx.match(line)
586 if m:
587 doctype = m.group(1)
588 break
589 fp.close()
590 return doctype
591
592
Fred Drake8b880931999-03-03 20:24:30 +0000593def main():
594 options = Options()
595 try:
596 args = options.parse(sys.argv[1:])
597 except getopt.error, msg:
598 error(options, msg)
599 if not args:
600 # attempt to locate single .tex file in current directory:
601 args = glob.glob("*.tex")
602 if not args:
603 error(options, "No file to process.")
604 if len(args) > 1:
605 error(options, "Could not deduce which files should be processed.")
606 #
607 # parameters are processed, let's go!
608 #
609 for path in args:
610 Job(options, path).build()
611
612
613def l2hoption(fp, option, value):
614 if value:
615 fp.write('$%s = "%s";\n' % (option, string_to_perl(str(value))))
616
617
618_to_perl = {}
619for c in map(chr, range(1, 256)):
620 _to_perl[c] = c
621_to_perl["@"] = "\\@"
622_to_perl["$"] = "\\$"
623_to_perl['"'] = '\\"'
624
625def string_to_perl(s):
626 return string.join(map(_to_perl.get, s), '')
627
628
629def check_for_bibtex(filename):
630 fp = open(filename)
631 pos = string.find(fp.read(), r"\bibdata{")
632 fp.close()
633 return pos >= 0
634
635def uniqify_module_table(filename):
636 lines = open(filename).readlines()
637 if len(lines) > 1:
638 if lines[-1] == lines[-2]:
639 del lines[-1]
640 open(filename, "w").writelines(lines)
641
642
643def new_index(filename, label="genindex"):
644 fp = open(filename, "w")
645 fp.write(r"""\
646\begin{theindex}
647\label{%s}
648\end{theindex}
649""" % label)
650 fp.close()
651
652
653if __name__ == "__main__":
654 main()