blob: 65640568f7db4b7dae14b1049ae7e38a25ee66b8 [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 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 Drake56c8c272002-10-30 17:02:21 +0000111 favicon = None
Fred Drake8b880931999-03-03 20:24:30 +0000112 #
Fred Drake3ce28e42001-10-30 16:09:51 +0000113 # 'dvips_safe' is a weird option. It is used mostly to make
114 # LaTeX2HTML not try to be too smart about protecting the user
115 # from a bad version of dvips -- some versions would core dump if
116 # the path to the source DVI contained a dot, and it's appearantly
117 # difficult to determine if the version available has that bug.
118 # This option gets set when PostScript output is requested
119 # (because we're going to run dvips regardless, and we'll either
120 # know it succeeds before LaTeX2HTML is run, or we'll have
121 # detected the failure and bailed), or the user asserts that it's
122 # safe from the command line.
123 #
124 # So, why does LaTeX2HTML think it appropriate to protect the user
125 # from a dvips that's only potentially going to core dump? Only
126 # because they want to avoid doing a lot of work just to have to
127 # bail later with no useful intermediates. Unfortunately, they
128 # bail *before* they know whether dvips will be needed at all.
129 # I've gone around the bush a few times with the LaTeX2HTML
130 # developers over whether this is appropriate behavior, and they
131 # don't seem interested in changing their position.
132 #
133 dvips_safe = 0
134 #
Fred Drake55994412001-01-30 22:30:01 +0000135 DEFAULT_FORMATS = ("html",)
Fred Drake8b880931999-03-03 20:24:30 +0000136 ALL_FORMATS = ("dvi", "html", "pdf", "ps", "text")
137
138 def __init__(self):
Fred Drake8b880931999-03-03 20:24:30 +0000139 self.formats = []
Fred Drake8bc627a2000-08-31 06:14:38 +0000140 self.l2h_init_files = []
Fred Drake8b880931999-03-03 20:24:30 +0000141
142 def __getitem__(self, key):
143 # This is used when formatting the usage message.
144 try:
145 return getattr(self, key)
146 except AttributeError:
147 raise KeyError, key
148
149 def parse(self, args):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000150 opts, args = getopt.getopt(args, "Hi:a:s:lDkqr:",
Fred Drake8b880931999-03-03 20:24:30 +0000151 ["all", "postscript", "help", "iconserver=",
Fred Drake8bc627a2000-08-31 06:14:38 +0000152 "address=", "a4", "letter", "l2h-init=",
Fred Drake8b880931999-03-03 20:24:30 +0000153 "link=", "split=", "logging", "debugging",
Fred Drakecf1b06e1999-09-23 16:55:09 +0000154 "keep", "quiet", "runs=", "image-type=",
Fred Drake50d1fcf2001-02-19 19:18:09 +0000155 "about=", "numeric", "style=", "paper=",
156 "up-link=", "up-title=", "dir=",
Fred Drake56c8c272002-10-30 17:02:21 +0000157 "global-module-index=", "dvips-safe",
158 "favicon="]
Fred Drake52ea0ce1999-09-22 19:55:35 +0000159 + list(self.ALL_FORMATS))
Fred Drake8b880931999-03-03 20:24:30 +0000160 for opt, arg in opts:
161 if opt == "--all":
162 self.formats = list(self.ALL_FORMATS)
Fred Drake3ce28e42001-10-30 16:09:51 +0000163 self.dvips_safe = "ps" in self.formats
Fred Drake8b880931999-03-03 20:24:30 +0000164 elif opt in ("-H", "--help"):
165 usage(self)
166 sys.exit()
167 elif opt == "--iconserver":
168 self.icon_server = arg
169 elif opt in ("-a", "--address"):
170 self.address = arg
171 elif opt == "--a4":
172 self.paper = "a4"
173 elif opt == "--letter":
174 self.paper = "letter"
Fred Drake8b880931999-03-03 20:24:30 +0000175 elif opt == "--link":
176 self.max_link_depth = int(arg)
177 elif opt in ("-s", "--split"):
178 self.max_split_depth = int(arg)
179 elif opt in ("-l", "--logging"):
180 self.logging = self.logging + 1
181 elif opt in ("-D", "--debugging"):
182 self.debugging = self.debugging + 1
183 elif opt in ("-k", "--keep"):
184 self.discard_temps = 0
185 elif opt in ("-q", "--quiet"):
186 self.quiet = 1
Fred Drake52ea0ce1999-09-22 19:55:35 +0000187 elif opt in ("-r", "--runs"):
188 self.runs = int(arg)
189 elif opt == "--image-type":
190 self.image_type = arg
Fred Drakecf1b06e1999-09-23 16:55:09 +0000191 elif opt == "--about":
192 # always make this absolute:
193 self.about_file = os.path.normpath(
Fred Drakefcb87252000-08-29 18:15:05 +0000194 os.path.abspath(arg))
Fred Drake9a257b42000-03-31 20:27:36 +0000195 elif opt == "--numeric":
196 self.numeric = 1
Fred Drakefcb87252000-08-29 18:15:05 +0000197 elif opt == "--style":
198 self.style_file = os.path.abspath(arg)
Fred Drake8bc627a2000-08-31 06:14:38 +0000199 elif opt == "--l2h-init":
200 self.l2h_init_files.append(os.path.abspath(arg))
Fred Drake56c8c272002-10-30 17:02:21 +0000201 elif opt == "--favicon":
202 self.favicon = arg
Fred Drakedfa539d2000-08-31 06:58:34 +0000203 elif opt == "--up-link":
204 self.up_link = arg
205 elif opt == "--up-title":
206 self.up_title = arg
Fred Drake42181db2001-01-09 22:02:10 +0000207 elif opt == "--global-module-index":
208 self.global_module_index = arg
Fred Drake50d1fcf2001-02-19 19:18:09 +0000209 elif opt == "--dir":
Fred Drake1cb560a2001-08-10 20:17:09 +0000210 if os.sep == "\\":
211 arg = re.sub("/", "\\", arg)
Fred Drakec0e066a2002-10-01 15:30:56 +0000212 self.builddir = os.path.expanduser(arg)
Fred Drake50d1fcf2001-02-19 19:18:09 +0000213 elif opt == "--paper":
214 self.paper = arg
Fred Drake3ce28e42001-10-30 16:09:51 +0000215 elif opt == "--dvips-safe":
216 self.dvips_safe = 1
Fred Drake8b880931999-03-03 20:24:30 +0000217 #
218 # Format specifiers:
219 #
220 elif opt[2:] in self.ALL_FORMATS:
221 self.add_format(opt[2:])
222 elif opt == "--postscript":
223 # synonym for --ps
224 self.add_format("ps")
225 self.initialize()
226 #
227 # return the args to allow the caller access:
228 #
229 return args
230
231 def add_format(self, format):
232 """Add a format to the formats list if not present."""
233 if not format in self.formats:
Fred Drake3ce28e42001-10-30 16:09:51 +0000234 if format == "ps":
235 # assume this is safe since we're going to run it anyway
236 self.dvips_safe = 1
Fred Drake8b880931999-03-03 20:24:30 +0000237 self.formats.append(format)
238
239 def initialize(self):
240 """Complete initialization. This is needed if parse() isn't used."""
241 # add the default format if no formats were specified:
242 if not self.formats:
243 self.formats = self.DEFAULT_FORMATS
244 # determine the base set of texinputs directories:
Fred Drake2ee37ff2003-09-27 07:05:12 +0000245 texinputs = os.environ.get("TEXINPUTS", "").split(os.pathsep)
Fred Drake8b880931999-03-03 20:24:30 +0000246 if not texinputs:
247 texinputs = ['']
Fred Drake2ee37ff2003-09-27 07:05:12 +0000248 mydirs = [os.path.join(TOPDIR, "paper-" + self.paper),
249 os.path.join(TOPDIR, "texinputs"),
250 ]
251 if '' in texinputs:
252 i = texinputs.index('')
253 texinputs[i:i] = mydirs
254 else:
255 texinputs += mydirs
256 self.base_texinputs = texinputs
Fred Drakebfd80dd2001-06-23 03:06:01 +0000257 if self.builddir:
258 self.builddir = os.path.abspath(self.builddir)
Fred Drake8b880931999-03-03 20:24:30 +0000259
260
261class Job:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000262 latex_runs = 0
263
Fred Drake8b880931999-03-03 20:24:30 +0000264 def __init__(self, options, path):
265 self.options = options
Fred Drakea871c2e1999-05-06 19:37:38 +0000266 self.doctype = get_doctype(path)
Fred Drake8b880931999-03-03 20:24:30 +0000267 self.filedir, self.doc = split_pathname(path)
Fred Drakebfd80dd2001-06-23 03:06:01 +0000268 self.builddir = os.path.abspath(options.builddir or self.doc)
Fred Drakeaebbca32001-07-17 14:46:09 +0000269 if ("html" in options.formats or "text" in options.formats):
270 if not os.path.exists(self.builddir):
271 os.mkdir(self.builddir)
272 self.log_filename = os.path.join(self.builddir, self.doc + ".how")
273 else:
274 self.log_filename = os.path.abspath(self.doc + ".how")
Fred Drake8b880931999-03-03 20:24:30 +0000275 if os.path.exists(self.log_filename):
276 os.unlink(self.log_filename)
Fred Drake246beb22002-08-27 16:34:54 +0000277 l2hconf = self.doc + ".l2h"
278 if os.path.exists(l2hconf):
279 if os.path.exists(l2hconf + "~"):
280 os.unlink(l2hconf + "~")
281 os.rename(l2hconf, l2hconf + "~")
282 self.l2h_aux_init_file = self.doc + ".l2h"
Fred Drake8b880931999-03-03 20:24:30 +0000283 self.write_l2h_aux_init_file()
284
285 def build(self):
286 self.setup_texinputs()
287 formats = self.options.formats
288 if "dvi" in formats or "ps" in formats:
289 self.build_dvi()
290 if "pdf" in formats:
291 self.build_pdf()
292 if "ps" in formats:
293 self.build_ps()
294 if "html" in formats:
295 self.require_temps()
Fred Drakebfd80dd2001-06-23 03:06:01 +0000296 self.build_html(self.builddir)
Fred Drake8b880931999-03-03 20:24:30 +0000297 if self.options.icon_server == ".":
Fred Drake52ea0ce1999-09-22 19:55:35 +0000298 pattern = os.path.join(TOPDIR, "html", "icons",
299 "*." + self.options.image_type)
300 imgs = glob.glob(pattern)
301 if not imgs:
302 self.warning(
303 "Could not locate support images of type %s."
304 % `self.options.image_type`)
305 for fn in imgs:
Barry Warsawcfb30e22002-10-01 15:38:01 +0000306 new_fn = os.path.join(self.builddir, os.path.basename(fn))
Fred Drake8b880931999-03-03 20:24:30 +0000307 shutil.copyfile(fn, new_fn)
308 if "text" in formats:
309 self.require_temps()
310 tempdir = self.doc
311 need_html = "html" not in formats
312 if self.options.max_split_depth != 1:
313 fp = open(self.l2h_aux_init_file, "a")
314 fp.write("# re-hack this file for --text:\n")
315 l2hoption(fp, "MAX_SPLIT_DEPTH", "1")
316 fp.write("1;\n")
317 fp.close()
318 tempdir = self.doc + "-temp-html"
319 need_html = 1
320 if need_html:
321 self.build_html(tempdir, max_split_depth=1)
322 self.build_text(tempdir)
323 if self.options.discard_temps:
324 self.cleanup()
325
326 def setup_texinputs(self):
Fred Drake2ee37ff2003-09-27 07:05:12 +0000327 texinputs = [self.filedir] + self.options.base_texinputs
328 os.environ["TEXINPUTS"] = os.pathsep.join(texinputs)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000329 self.message("TEXINPUTS=" + os.environ["TEXINPUTS"])
Fred Drake8b880931999-03-03 20:24:30 +0000330
Fred Drake8b880931999-03-03 20:24:30 +0000331 def build_aux(self, binary=None):
332 if binary is None:
333 binary = LATEX_BINARY
334 new_index( "%s.ind" % self.doc, "genindex")
335 new_index("mod%s.ind" % self.doc, "modindex")
336 self.run("%s %s" % (binary, self.doc))
337 self.use_bibtex = check_for_bibtex(self.doc + ".aux")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000338 self.latex_runs = 1
Fred Drake8b880931999-03-03 20:24:30 +0000339
340 def build_dvi(self):
341 self.use_latex(LATEX_BINARY)
342
343 def build_pdf(self):
344 self.use_latex(PDFLATEX_BINARY)
345
346 def use_latex(self, binary):
347 self.require_temps(binary=binary)
Fred Drakedf84fac2000-09-20 05:49:09 +0000348 if self.latex_runs < 2:
349 if os.path.isfile("mod%s.idx" % self.doc):
350 self.run("%s mod%s.idx" % (MAKEINDEX_BINARY, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000351 use_indfix = 0
Fred Drakedf84fac2000-09-20 05:49:09 +0000352 if os.path.isfile(self.doc + ".idx"):
Fred Drake9dce7b32000-11-03 02:57:31 +0000353 use_indfix = 1
Fred Drakedf84fac2000-09-20 05:49:09 +0000354 # call to Doc/tools/fix_hack omitted; doesn't appear necessary
355 self.run("%s %s.idx" % (MAKEINDEX_BINARY, self.doc))
356 import indfix
357 indfix.process(self.doc + ".ind")
358 if self.use_bibtex:
359 self.run("%s %s" % (BIBTEX_BINARY, self.doc))
360 self.process_synopsis_files()
Fred Drakedf84fac2000-09-20 05:49:09 +0000361 self.run("%s %s" % (binary, self.doc))
Fred Drakeb258bed2001-02-12 15:30:22 +0000362 self.latex_runs = self.latex_runs + 1
Fred Drakedf84fac2000-09-20 05:49:09 +0000363 if os.path.isfile("mod%s.idx" % self.doc):
364 self.run("%s -s %s mod%s.idx"
365 % (MAKEINDEX_BINARY, ISTFILE, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000366 if use_indfix:
Fred Drakedf84fac2000-09-20 05:49:09 +0000367 self.run("%s -s %s %s.idx"
368 % (MAKEINDEX_BINARY, ISTFILE, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000369 indfix.process(self.doc + ".ind")
Fred Drakedf84fac2000-09-20 05:49:09 +0000370 self.process_synopsis_files()
Fred Drakea871c2e1999-05-06 19:37:38 +0000371 #
372 # and now finish it off:
373 #
374 if os.path.isfile(self.doc + ".toc") and binary == PDFLATEX_BINARY:
375 import toc2bkm
Fred Drake239e1d52000-09-05 21:45:11 +0000376 if self.doctype == "manual":
377 bigpart = "chapter"
378 else:
379 bigpart = "section"
380 toc2bkm.process(self.doc + ".toc", self.doc + ".bkm", bigpart)
Fred Drakea871c2e1999-05-06 19:37:38 +0000381 if self.use_bibtex:
382 self.run("%s %s" % (BIBTEX_BINARY, self.doc))
383 self.run("%s %s" % (binary, self.doc))
Fred Drakeb258bed2001-02-12 15:30:22 +0000384 self.latex_runs = self.latex_runs + 1
Fred Drakea871c2e1999-05-06 19:37:38 +0000385
386 def process_synopsis_files(self):
387 synopsis_files = glob.glob(self.doc + "*.syn")
388 for path in synopsis_files:
389 uniqify_module_table(path)
Fred Drake8b880931999-03-03 20:24:30 +0000390
391 def build_ps(self):
392 self.run("%s -N0 -o %s.ps %s" % (DVIPS_BINARY, self.doc, self.doc))
393
Fred Drakeaebbca32001-07-17 14:46:09 +0000394 def build_html(self, builddir, max_split_depth=None):
Fred Drake8b880931999-03-03 20:24:30 +0000395 if max_split_depth is None:
396 max_split_depth = self.options.max_split_depth
397 texfile = None
Fred Drake2ee37ff2003-09-27 07:05:12 +0000398 for p in os.environ["TEXINPUTS"].split(os.pathsep):
Fred Drake8b880931999-03-03 20:24:30 +0000399 fn = os.path.join(p, self.doc + ".tex")
400 if os.path.isfile(fn):
401 texfile = fn
402 break
403 if not texfile:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000404 self.warning("Could not locate %s.tex; aborting." % self.doc)
Fred Drake8b880931999-03-03 20:24:30 +0000405 sys.exit(1)
406 # remove leading ./ (or equiv.); might avoid problems w/ dvips
407 if texfile[:2] == os.curdir + os.sep:
408 texfile = texfile[2:]
409 # build the command line and run LaTeX2HTML:
Fred Drakeba828782000-04-03 04:19:14 +0000410 if not os.path.isdir(builddir):
411 os.mkdir(builddir)
Fred Drakef3d41272000-09-14 22:25:47 +0000412 else:
413 for fname in glob.glob(os.path.join(builddir, "*.html")):
414 os.unlink(fname)
Fred Drake8b880931999-03-03 20:24:30 +0000415 args = [LATEX2HTML_BINARY,
Fred Drake8b880931999-03-03 20:24:30 +0000416 "-init_file", self.l2h_aux_init_file,
417 "-dir", builddir,
418 texfile
419 ]
Fred Drake2ee37ff2003-09-27 07:05:12 +0000420 self.run(" ".join(args)) # XXX need quoting!
Fred Drake8b880931999-03-03 20:24:30 +0000421 # ... postprocess
422 shutil.copyfile(self.options.style_file,
423 os.path.join(builddir, self.doc + ".css"))
Fred Drake4437fdf1999-05-03 14:29:07 +0000424 shutil.copyfile(os.path.join(builddir, self.doc + ".html"),
425 os.path.join(builddir, "index.html"))
Fred Drakecfef0092001-03-02 16:26:45 +0000426 if max_split_depth != 1:
Fred Drakeaf922182001-05-09 04:03:16 +0000427 label_file = os.path.join(builddir, "labels.pl")
428 fp = open(label_file)
429 about_node = None
430 target = " = q/about/;\n"
431 x = len(target)
432 while 1:
433 line = fp.readline()
434 if not line:
435 break
436 if line[-x:] == target:
Fred Drakecfef0092001-03-02 16:26:45 +0000437 line = fp.readline()
Fred Drakeaf922182001-05-09 04:03:16 +0000438 m = re.search(r"\|(node\d+\.[a-z]+)\|", line)
439 about_node = m.group(1)
440 shutil.copyfile(os.path.join(builddir, about_node),
441 os.path.join(builddir, "about.html"))
442 break
443 if not self.options.numeric:
Fred Drakecfef0092001-03-02 16:26:45 +0000444 pwd = os.getcwd()
445 try:
446 os.chdir(builddir)
447 self.run("%s %s *.html" % (PERL_BINARY, NODE2LABEL_SCRIPT))
448 finally:
449 os.chdir(pwd)
Fred Drake376f0ef2003-01-17 21:25:04 +0000450 # These files need to be cleaned up here since builddir there
451 # can be more than one, so we clean each of them.
452 if self.options.discard_temps:
453 for fn in ("images.tex", "images.log", "images.aux"):
454 safe_unlink(os.path.join(builddir, fn))
Fred Drake8b880931999-03-03 20:24:30 +0000455
456 def build_text(self, tempdir=None):
457 if tempdir is None:
458 tempdir = self.doc
459 indexfile = os.path.join(tempdir, "index.html")
460 self.run("%s -nolist -dump %s >%s.txt"
461 % (LYNX_BINARY, indexfile, self.doc))
462
463 def require_temps(self, binary=None):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000464 if not self.latex_runs:
Fred Drake8b880931999-03-03 20:24:30 +0000465 self.build_aux(binary=binary)
466
467 def write_l2h_aux_init_file(self):
Fred Drake8bc627a2000-08-31 06:14:38 +0000468 options = self.options
Fred Drake8b880931999-03-03 20:24:30 +0000469 fp = open(self.l2h_aux_init_file, "w")
Fred Drake19157542000-07-31 17:47:49 +0000470 d = string_to_perl(os.path.dirname(L2H_INIT_FILE))
471 fp.write("package main;\n"
472 "push (@INC, '%s');\n"
473 "$mydir = '%s';\n"
474 % (d, d))
Fred Drake498c18f2000-07-24 23:03:32 +0000475 fp.write(open(L2H_INIT_FILE).read())
Fred Drake8bc627a2000-08-31 06:14:38 +0000476 for filename in options.l2h_init_files:
477 fp.write("\n# initialization code incorporated from:\n# ")
478 fp.write(filename)
479 fp.write("\n")
480 fp.write(open(filename).read())
Fred Drake498c18f2000-07-24 23:03:32 +0000481 fp.write("\n"
482 "# auxillary init file for latex2html\n"
Fred Drake8b880931999-03-03 20:24:30 +0000483 "# generated by mkhowto\n"
Fred Drake4437fdf1999-05-03 14:29:07 +0000484 "$NO_AUTO_LINK = 1;\n"
Fred Drake8b880931999-03-03 20:24:30 +0000485 )
Fred Drakecf1b06e1999-09-23 16:55:09 +0000486 l2hoption(fp, "ABOUT_FILE", options.about_file)
Fred Drake8b880931999-03-03 20:24:30 +0000487 l2hoption(fp, "ICONSERVER", options.icon_server)
Fred Drake52ea0ce1999-09-22 19:55:35 +0000488 l2hoption(fp, "IMAGE_TYPE", options.image_type)
Fred Drake8b880931999-03-03 20:24:30 +0000489 l2hoption(fp, "ADDRESS", options.address)
490 l2hoption(fp, "MAX_LINK_DEPTH", options.max_link_depth)
491 l2hoption(fp, "MAX_SPLIT_DEPTH", options.max_split_depth)
Fred Drakedfa539d2000-08-31 06:58:34 +0000492 l2hoption(fp, "EXTERNAL_UP_LINK", options.up_link)
493 l2hoption(fp, "EXTERNAL_UP_TITLE", options.up_title)
Fred Drake56c8c272002-10-30 17:02:21 +0000494 l2hoption(fp, "FAVORITES_ICON", options.favicon)
Fred Drake42181db2001-01-09 22:02:10 +0000495 l2hoption(fp, "GLOBAL_MODULE_INDEX", options.global_module_index)
Fred Drake3ce28e42001-10-30 16:09:51 +0000496 l2hoption(fp, "DVIPS_SAFE", options.dvips_safe)
Fred Drake8b880931999-03-03 20:24:30 +0000497 fp.write("1;\n")
498 fp.close()
499
500 def cleanup(self):
501 self.__have_temps = 0
502 for pattern in ("%s.aux", "%s.log", "%s.out", "%s.toc", "%s.bkm",
Fred Drakea871c2e1999-05-06 19:37:38 +0000503 "%s.idx", "%s.ilg", "%s.ind", "%s.pla",
Fred Drake8b880931999-03-03 20:24:30 +0000504 "%s.bbl", "%s.blg",
505 "mod%s.idx", "mod%s.ind", "mod%s.ilg",
506 ):
507 safe_unlink(pattern % self.doc)
Fred Drakea871c2e1999-05-06 19:37:38 +0000508 map(safe_unlink, glob.glob(self.doc + "*.syn"))
Fred Drake8b880931999-03-03 20:24:30 +0000509 for spec in ("IMG*", "*.pl", "WARNINGS", "index.dat", "modindex.dat"):
510 pattern = os.path.join(self.doc, spec)
511 map(safe_unlink, glob.glob(pattern))
512 if "dvi" not in self.options.formats:
513 safe_unlink(self.doc + ".dvi")
514 if os.path.isdir(self.doc + "-temp-html"):
515 shutil.rmtree(self.doc + "-temp-html", ignore_errors=1)
516 if not self.options.logging:
517 os.unlink(self.log_filename)
518 if not self.options.debugging:
519 os.unlink(self.l2h_aux_init_file)
520
521 def run(self, command):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000522 self.message(command)
Fred Drake1cb560a2001-08-10 20:17:09 +0000523 if sys.platform.startswith("win"):
524 rc = os.system(command)
525 else:
526 rc = os.system("(%s) </dev/null >>%s 2>&1"
527 % (command, self.log_filename))
Fred Drake8b880931999-03-03 20:24:30 +0000528 if rc:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000529 self.warning(
530 "Session transcript and error messages are in %s."
Fred Drake8b880931999-03-03 20:24:30 +0000531 % self.log_filename)
Fred Drakec868d162003-05-14 04:16:14 +0000532 result = 1
Fred Drake1cb560a2001-08-10 20:17:09 +0000533 if hasattr(os, "WIFEXITED"):
534 if os.WIFEXITED(rc):
Fred Drakec868d162003-05-14 04:16:14 +0000535 result = os.WEXITSTATUS(rc)
536 self.warning("Exited with status %s." % result)
Fred Drake1cb560a2001-08-10 20:17:09 +0000537 else:
538 self.warning("Killed by signal %s." % os.WSTOPSIG(rc))
539 else:
540 self.warning("Return code: %s" % rc)
Fred Drake4e3f2752001-02-04 15:20:26 +0000541 sys.stderr.write("The relevant lines from the transcript are:\n")
542 sys.stderr.write("-" * 72 + "\n")
543 sys.stderr.writelines(get_run_transcript(self.log_filename))
Fred Drakec868d162003-05-14 04:16:14 +0000544 sys.exit(result)
Fred Drake8b880931999-03-03 20:24:30 +0000545
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000546 def message(self, msg):
547 msg = "+++ " + msg
548 if not self.options.quiet:
549 print msg
Fred Drake52ea0ce1999-09-22 19:55:35 +0000550 self.log(msg + "\n")
551
552 def warning(self, msg):
553 msg = "*** %s\n" % msg
554 sys.stderr.write(msg)
555 self.log(msg)
556
557 def log(self, msg):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000558 fp = open(self.log_filename, "a")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000559 fp.write(msg)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000560 fp.close()
561
Fred Drake8b880931999-03-03 20:24:30 +0000562
Fred Drake4e3f2752001-02-04 15:20:26 +0000563def get_run_transcript(filename):
564 """Return lines from the transcript file for the most recent run() call."""
565 fp = open(filename)
566 lines = fp.readlines()
567 fp.close()
568 lines.reverse()
569 L = []
570 for line in lines:
571 L.append(line)
572 if line[:4] == "+++ ":
573 break
574 L.reverse()
575 return L
576
577
Fred Drake8b880931999-03-03 20:24:30 +0000578def safe_unlink(path):
Fred Drake4e3f2752001-02-04 15:20:26 +0000579 """Unlink a file without raising an error if it doesn't exist."""
Fred Drake8b880931999-03-03 20:24:30 +0000580 try:
581 os.unlink(path)
582 except os.error:
583 pass
584
585
Fred Drakea871c2e1999-05-06 19:37:38 +0000586def split_pathname(path):
Fred Drakebfd80dd2001-06-23 03:06:01 +0000587 path = os.path.abspath(path)
Fred Drakea871c2e1999-05-06 19:37:38 +0000588 dirname, basename = os.path.split(path)
Fred Drake8b880931999-03-03 20:24:30 +0000589 if basename[-4:] == ".tex":
590 basename = basename[:-4]
591 return dirname, basename
592
593
Fred Drakea871c2e1999-05-06 19:37:38 +0000594_doctype_rx = re.compile(r"\\documentclass(?:\[[^]]*\])?{([a-zA-Z]*)}")
595def get_doctype(path):
596 fp = open(path)
597 doctype = None
598 while 1:
599 line = fp.readline()
600 if not line:
601 break
602 m = _doctype_rx.match(line)
603 if m:
604 doctype = m.group(1)
605 break
606 fp.close()
607 return doctype
608
609
Fred Drake8b880931999-03-03 20:24:30 +0000610def main():
611 options = Options()
612 try:
613 args = options.parse(sys.argv[1:])
614 except getopt.error, msg:
615 error(options, msg)
616 if not args:
617 # attempt to locate single .tex file in current directory:
618 args = glob.glob("*.tex")
619 if not args:
620 error(options, "No file to process.")
621 if len(args) > 1:
622 error(options, "Could not deduce which files should be processed.")
623 #
624 # parameters are processed, let's go!
625 #
626 for path in args:
627 Job(options, path).build()
628
629
630def l2hoption(fp, option, value):
631 if value:
632 fp.write('$%s = "%s";\n' % (option, string_to_perl(str(value))))
633
634
635_to_perl = {}
636for c in map(chr, range(1, 256)):
637 _to_perl[c] = c
638_to_perl["@"] = "\\@"
639_to_perl["$"] = "\\$"
640_to_perl['"'] = '\\"'
641
642def string_to_perl(s):
Fred Drake2ee37ff2003-09-27 07:05:12 +0000643 return ''.join(map(_to_perl.get, s))
Fred Drake8b880931999-03-03 20:24:30 +0000644
645
646def check_for_bibtex(filename):
647 fp = open(filename)
Fred Drake2ee37ff2003-09-27 07:05:12 +0000648 pos = fp.read().find(r"\bibdata{")
Fred Drake8b880931999-03-03 20:24:30 +0000649 fp.close()
650 return pos >= 0
651
652def uniqify_module_table(filename):
653 lines = open(filename).readlines()
654 if len(lines) > 1:
655 if lines[-1] == lines[-2]:
656 del lines[-1]
657 open(filename, "w").writelines(lines)
658
659
660def new_index(filename, label="genindex"):
661 fp = open(filename, "w")
662 fp.write(r"""\
663\begin{theindex}
664\label{%s}
665\end{theindex}
666""" % label)
667 fp.close()
668
669
670if __name__ == "__main__":
671 main()