blob: ed78f36c37ff56e0f899d950b46bdfc1d15c799a [file] [log] [blame]
Fred Drake361ee651998-03-06 21:29:00 +00001#! /usr/bin/env python
2
3"""Convert a LaTeX .toc file to some PDFTeX magic to create that neat outline.
4
5The output file has an extension of '.bkm' instead of '.out', since hyperref
6already uses that extension. Let's avoid clashing.
7"""
8
Fred Drake473a90e1998-03-07 15:34:50 +00009import getopt
Fred Drake361ee651998-03-06 21:29:00 +000010import os
11import re
12import string
13import sys
14
15
16# Ench item in an entry is a tuple of:
17#
18# Section #, Title String, Page #, List of Sub-entries
19
20cline_re = r"""^
21\\contentsline\ \{([a-z]*)} # type of section in $1
22\{(?:\\numberline\ \{([0-9.A-Z]+)})? # section number
23(.*)} # title string
24\{(\d+)}$""" # page number
25
26cline_rx = re.compile(cline_re, re.VERBOSE)
27
28OUTER_TO_INNER = -1
29
30_transition_map = {
31 ('chapter', 'section'): OUTER_TO_INNER,
32 ('section', 'subsection'): OUTER_TO_INNER,
33 ('subsection', 'subsubsection'): OUTER_TO_INNER,
34 ('subsubsection', 'subsection'): 1,
35 ('subsection', 'section'): 1,
36 ('section', 'chapter'): 1,
37 ('subsection', 'chapter'): 2,
38 ('subsubsection', 'section'): 2,
39 ('subsubsection', 'chapter'): 3,
40 }
41
Fred Drake473a90e1998-03-07 15:34:50 +000042def parse_toc(fp, bigpart=None):
Fred Drake361ee651998-03-06 21:29:00 +000043 toc = top = []
44 stack = [toc]
Fred Drake473a90e1998-03-07 15:34:50 +000045 level = bigpart or 'chapter'
Fred Drake361ee651998-03-06 21:29:00 +000046 lineno = 0
47 while 1:
48 line = fp.readline()
49 if not line:
50 break
51 lineno = lineno + 1
52 m = cline_rx.match(line)
53 if m:
54 stype, snum, title, pageno = m.group(1, 2, 3, 4)
55 title = clean_title(title)
56 entry = (stype, snum, title, string.atoi(pageno), [])
57 if stype == level:
58 toc.append(entry)
59 else:
60 direction = _transition_map[(level, stype)]
61 if direction == OUTER_TO_INNER:
62 toc = toc[-1][-1]
63 stack.insert(0, toc)
64 toc.append(entry)
65 else:
66 for i in range(direction):
67 del stack[0]
68 toc = stack[0]
69 toc.append(entry)
70 level = stype
71 else:
72 sys.stderr.write("l.%s: " + line)
73 return top
74
75
Fred Drakeac77b791998-03-10 14:02:35 +000076hackscore_rx = re.compile(r"\\hackscore\s*{[^}]*}")
77raisebox_rx = re.compile(r"\\raisebox\s*{[^}]*}")
78title_rx = re.compile(r"\\([a-zA-Z])+\s+")
Fred Drake361ee651998-03-06 21:29:00 +000079title_trans = string.maketrans("", "")
80
81def clean_title(title):
Fred Drakeac77b791998-03-10 14:02:35 +000082 title = raisebox_rx.sub("", title)
83 title = hackscore_rx.sub(r"\\_", title)
84 pos = 0
Fred Drake361ee651998-03-06 21:29:00 +000085 while 1:
Fred Drakeac77b791998-03-10 14:02:35 +000086 m = title_rx.search(title, pos)
Fred Drake361ee651998-03-06 21:29:00 +000087 if m:
Fred Drakeac77b791998-03-10 14:02:35 +000088 start = m.start()
Fred Drakeac77b791998-03-10 14:02:35 +000089 if title[start:start+15] != "\\textunderscore":
90 title = title[:start] + title[m.end():]
91 pos = start + 1
Fred Drake361ee651998-03-06 21:29:00 +000092 else:
93 break
Fred Drakeac77b791998-03-10 14:02:35 +000094 title = string.translate(title, title_trans, "{}")
Fred Drakeac77b791998-03-10 14:02:35 +000095 return title
Fred Drake361ee651998-03-06 21:29:00 +000096
97
98def write_toc(toc, fp):
99 for entry in toc:
100 write_toc_entry(entry, fp, 0)
101
102def write_toc_entry(entry, fp, layer):
103 stype, snum, title, pageno, toc = entry
Fred Drakebf88c381998-04-15 17:50:22 +0000104 s = "\\pdfoutline goto name{page%03d}" % pageno
Fred Drake361ee651998-03-06 21:29:00 +0000105 if toc:
106 s = "%s count -%d" % (s, len(toc))
107 if snum:
108 title = "%s %s" % (snum, title)
109 s = "%s {%s}\n" % (s, title)
110 fp.write(s)
111 for entry in toc:
112 write_toc_entry(entry, fp, layer + 1)
113
114
115def main():
Fred Drake473a90e1998-03-07 15:34:50 +0000116 bigpart = None
117 opts, args = getopt.getopt(sys.argv[1:], "c:")
118 if opts:
119 bigpart = opts[0][1]
120 if not args:
121 usage()
122 sys.exit(2)
123 for filename in args:
124 base, ext = os.path.splitext(filename)
125 ext = ext or ".toc"
126 toc = parse_toc(open(base + ext), bigpart)
127 write_toc(toc, open(base + ".bkm", "w"))
Fred Drake361ee651998-03-06 21:29:00 +0000128
129
130if __name__ == "__main__":
131 main()