Fred Drake | 361ee65 | 1998-03-06 21:29:00 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
| 3 | """Convert a LaTeX .toc file to some PDFTeX magic to create that neat outline. |
| 4 | |
| 5 | The output file has an extension of '.bkm' instead of '.out', since hyperref |
Fred Drake | 8c1e114 | 1998-10-07 14:12:20 +0000 | [diff] [blame] | 6 | already uses that extension. |
Fred Drake | 361ee65 | 1998-03-06 21:29:00 +0000 | [diff] [blame] | 7 | """ |
| 8 | |
Fred Drake | 473a90e | 1998-03-07 15:34:50 +0000 | [diff] [blame] | 9 | import getopt |
Fred Drake | 361ee65 | 1998-03-06 21:29:00 +0000 | [diff] [blame] | 10 | import os |
| 11 | import re |
| 12 | import string |
| 13 | import sys |
| 14 | |
| 15 | |
| 16 | # Ench item in an entry is a tuple of: |
| 17 | # |
| 18 | # Section #, Title String, Page #, List of Sub-entries |
Fred Drake | 25d4e28 | 1998-05-14 20:07:10 +0000 | [diff] [blame] | 19 | # |
| 20 | # The return value of parse_toc() is such a tuple. |
Fred Drake | 361ee65 | 1998-03-06 21:29:00 +0000 | [diff] [blame] | 21 | |
| 22 | cline_re = r"""^ |
| 23 | \\contentsline\ \{([a-z]*)} # type of section in $1 |
| 24 | \{(?:\\numberline\ \{([0-9.A-Z]+)})? # section number |
| 25 | (.*)} # title string |
Fred Drake | 7787841 | 2000-10-07 12:50:05 +0000 | [diff] [blame] | 26 | \{(\d+)}$""" # page number |
Fred Drake | 361ee65 | 1998-03-06 21:29:00 +0000 | [diff] [blame] | 27 | |
| 28 | cline_rx = re.compile(cline_re, re.VERBOSE) |
| 29 | |
| 30 | OUTER_TO_INNER = -1 |
| 31 | |
| 32 | _transition_map = { |
| 33 | ('chapter', 'section'): OUTER_TO_INNER, |
| 34 | ('section', 'subsection'): OUTER_TO_INNER, |
| 35 | ('subsection', 'subsubsection'): OUTER_TO_INNER, |
| 36 | ('subsubsection', 'subsection'): 1, |
| 37 | ('subsection', 'section'): 1, |
| 38 | ('section', 'chapter'): 1, |
| 39 | ('subsection', 'chapter'): 2, |
| 40 | ('subsubsection', 'section'): 2, |
| 41 | ('subsubsection', 'chapter'): 3, |
| 42 | } |
| 43 | |
Fred Drake | 8c1e114 | 1998-10-07 14:12:20 +0000 | [diff] [blame] | 44 | INCLUDED_LEVELS = ("chapter", "section", "subsection", "subsubsection") |
| 45 | |
| 46 | |
Fred Drake | 473a90e | 1998-03-07 15:34:50 +0000 | [diff] [blame] | 47 | def parse_toc(fp, bigpart=None): |
Fred Drake | 361ee65 | 1998-03-06 21:29:00 +0000 | [diff] [blame] | 48 | toc = top = [] |
| 49 | stack = [toc] |
Fred Drake | 473a90e | 1998-03-07 15:34:50 +0000 | [diff] [blame] | 50 | level = bigpart or 'chapter' |
Fred Drake | 361ee65 | 1998-03-06 21:29:00 +0000 | [diff] [blame] | 51 | lineno = 0 |
| 52 | while 1: |
Fred Drake | 7787841 | 2000-10-07 12:50:05 +0000 | [diff] [blame] | 53 | line = fp.readline() |
| 54 | if not line: |
| 55 | break |
| 56 | lineno = lineno + 1 |
| 57 | m = cline_rx.match(line) |
| 58 | if m: |
| 59 | stype, snum, title, pageno = m.group(1, 2, 3, 4) |
| 60 | title = clean_title(title) |
Fred Drake | 071972e | 2002-10-16 15:30:17 +0000 | [diff] [blame] | 61 | entry = (stype, snum, title, int(pageno), []) |
Fred Drake | 7787841 | 2000-10-07 12:50:05 +0000 | [diff] [blame] | 62 | if stype == level: |
| 63 | toc.append(entry) |
| 64 | else: |
Fred Drake | 8c1e114 | 1998-10-07 14:12:20 +0000 | [diff] [blame] | 65 | if stype not in INCLUDED_LEVELS: |
| 66 | # we don't want paragraphs & subparagraphs |
| 67 | continue |
Fred Drake | 7787841 | 2000-10-07 12:50:05 +0000 | [diff] [blame] | 68 | direction = _transition_map[(level, stype)] |
| 69 | if direction == OUTER_TO_INNER: |
| 70 | toc = toc[-1][-1] |
| 71 | stack.insert(0, toc) |
| 72 | toc.append(entry) |
| 73 | else: |
| 74 | for i in range(direction): |
| 75 | del stack[0] |
| 76 | toc = stack[0] |
| 77 | toc.append(entry) |
| 78 | level = stype |
| 79 | else: |
| 80 | sys.stderr.write("l.%s: " + line) |
Fred Drake | 361ee65 | 1998-03-06 21:29:00 +0000 | [diff] [blame] | 81 | return top |
| 82 | |
| 83 | |
Fred Drake | ac77b79 | 1998-03-10 14:02:35 +0000 | [diff] [blame] | 84 | hackscore_rx = re.compile(r"\\hackscore\s*{[^}]*}") |
| 85 | raisebox_rx = re.compile(r"\\raisebox\s*{[^}]*}") |
| 86 | title_rx = re.compile(r"\\([a-zA-Z])+\s+") |
Fred Drake | 361ee65 | 1998-03-06 21:29:00 +0000 | [diff] [blame] | 87 | title_trans = string.maketrans("", "") |
| 88 | |
| 89 | def clean_title(title): |
Fred Drake | ac77b79 | 1998-03-10 14:02:35 +0000 | [diff] [blame] | 90 | title = raisebox_rx.sub("", title) |
| 91 | title = hackscore_rx.sub(r"\\_", title) |
| 92 | pos = 0 |
Fred Drake | 361ee65 | 1998-03-06 21:29:00 +0000 | [diff] [blame] | 93 | while 1: |
Fred Drake | 7787841 | 2000-10-07 12:50:05 +0000 | [diff] [blame] | 94 | m = title_rx.search(title, pos) |
| 95 | if m: |
| 96 | start = m.start() |
| 97 | if title[start:start+15] != "\\textunderscore": |
| 98 | title = title[:start] + title[m.end():] |
| 99 | pos = start + 1 |
| 100 | else: |
| 101 | break |
Fred Drake | 071972e | 2002-10-16 15:30:17 +0000 | [diff] [blame] | 102 | title = title.translate(title_trans, "{}") |
Fred Drake | ac77b79 | 1998-03-10 14:02:35 +0000 | [diff] [blame] | 103 | return title |
Fred Drake | 361ee65 | 1998-03-06 21:29:00 +0000 | [diff] [blame] | 104 | |
| 105 | |
| 106 | def write_toc(toc, fp): |
| 107 | for entry in toc: |
Fred Drake | 7787841 | 2000-10-07 12:50:05 +0000 | [diff] [blame] | 108 | write_toc_entry(entry, fp, 0) |
Fred Drake | 361ee65 | 1998-03-06 21:29:00 +0000 | [diff] [blame] | 109 | |
| 110 | def write_toc_entry(entry, fp, layer): |
| 111 | stype, snum, title, pageno, toc = entry |
Fred Drake | bf88c38 | 1998-04-15 17:50:22 +0000 | [diff] [blame] | 112 | s = "\\pdfoutline goto name{page%03d}" % pageno |
Fred Drake | 361ee65 | 1998-03-06 21:29:00 +0000 | [diff] [blame] | 113 | if toc: |
Fred Drake | 7787841 | 2000-10-07 12:50:05 +0000 | [diff] [blame] | 114 | s = "%s count -%d" % (s, len(toc)) |
Fred Drake | 361ee65 | 1998-03-06 21:29:00 +0000 | [diff] [blame] | 115 | if snum: |
Fred Drake | 7787841 | 2000-10-07 12:50:05 +0000 | [diff] [blame] | 116 | title = "%s %s" % (snum, title) |
Fred Drake | 361ee65 | 1998-03-06 21:29:00 +0000 | [diff] [blame] | 117 | s = "%s {%s}\n" % (s, title) |
| 118 | fp.write(s) |
| 119 | for entry in toc: |
Fred Drake | 7787841 | 2000-10-07 12:50:05 +0000 | [diff] [blame] | 120 | write_toc_entry(entry, fp, layer + 1) |
Fred Drake | 361ee65 | 1998-03-06 21:29:00 +0000 | [diff] [blame] | 121 | |
| 122 | |
Fred Drake | 7c8754f | 1999-03-03 19:25:56 +0000 | [diff] [blame] | 123 | def process(ifn, ofn, bigpart=None): |
| 124 | toc = parse_toc(open(ifn), bigpart) |
| 125 | write_toc(toc, open(ofn, "w")) |
| 126 | |
| 127 | |
Fred Drake | 361ee65 | 1998-03-06 21:29:00 +0000 | [diff] [blame] | 128 | def main(): |
Fred Drake | 473a90e | 1998-03-07 15:34:50 +0000 | [diff] [blame] | 129 | bigpart = None |
| 130 | opts, args = getopt.getopt(sys.argv[1:], "c:") |
| 131 | if opts: |
Fred Drake | 7787841 | 2000-10-07 12:50:05 +0000 | [diff] [blame] | 132 | bigpart = opts[0][1] |
Fred Drake | 473a90e | 1998-03-07 15:34:50 +0000 | [diff] [blame] | 133 | if not args: |
Fred Drake | 7787841 | 2000-10-07 12:50:05 +0000 | [diff] [blame] | 134 | usage() |
| 135 | sys.exit(2) |
Fred Drake | 473a90e | 1998-03-07 15:34:50 +0000 | [diff] [blame] | 136 | for filename in args: |
Fred Drake | 7787841 | 2000-10-07 12:50:05 +0000 | [diff] [blame] | 137 | base, ext = os.path.splitext(filename) |
| 138 | ext = ext or ".toc" |
Fred Drake | 7c8754f | 1999-03-03 19:25:56 +0000 | [diff] [blame] | 139 | process(base + ext, base + ".bkm", bigpart) |
Fred Drake | 361ee65 | 1998-03-06 21:29:00 +0000 | [diff] [blame] | 140 | |
| 141 | |
| 142 | if __name__ == "__main__": |
| 143 | main() |