2323\\contentsline\ \{([a-z]*)} # type of section in $1
2424\{(?:\\numberline\ \{([0-9.A-Z]+)})? # section number
2525(.*)} # title string
26- \{(\d+)}$""" # page number
26+ \{(\d+)}$""" # page number
2727
2828cline_rx = re .compile (cline_re , re .VERBOSE )
2929
@@ -50,34 +50,34 @@ def parse_toc(fp, bigpart=None):
5050 level = bigpart or 'chapter'
5151 lineno = 0
5252 while 1 :
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 )
61- entry = (stype , snum , title , string .atoi (pageno ), [])
62- if stype == level :
63- toc .append (entry )
64- else :
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 )
61+ entry = (stype , snum , title , string .atoi (pageno ), [])
62+ if stype == level :
63+ toc .append (entry )
64+ else :
6565 if stype not in INCLUDED_LEVELS :
6666 # we don't want paragraphs & subparagraphs
6767 continue
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 )
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 )
8181 return top
8282
8383
@@ -91,33 +91,33 @@ def clean_title(title):
9191 title = hackscore_rx .sub (r"\\_" , title )
9292 pos = 0
9393 while 1 :
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
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
102102 title = string .translate (title , title_trans , "{}" )
103103 return title
104104
105105
106106def write_toc (toc , fp ):
107107 for entry in toc :
108- write_toc_entry (entry , fp , 0 )
108+ write_toc_entry (entry , fp , 0 )
109109
110110def write_toc_entry (entry , fp , layer ):
111111 stype , snum , title , pageno , toc = entry
112112 s = "\\ pdfoutline goto name{page%03d}" % pageno
113113 if toc :
114- s = "%s count -%d" % (s , len (toc ))
114+ s = "%s count -%d" % (s , len (toc ))
115115 if snum :
116- title = "%s %s" % (snum , title )
116+ title = "%s %s" % (snum , title )
117117 s = "%s {%s}\n " % (s , title )
118118 fp .write (s )
119119 for entry in toc :
120- write_toc_entry (entry , fp , layer + 1 )
120+ write_toc_entry (entry , fp , layer + 1 )
121121
122122
123123def process (ifn , ofn , bigpart = None ):
@@ -129,13 +129,13 @@ def main():
129129 bigpart = None
130130 opts , args = getopt .getopt (sys .argv [1 :], "c:" )
131131 if opts :
132- bigpart = opts [0 ][1 ]
132+ bigpart = opts [0 ][1 ]
133133 if not args :
134- usage ()
135- sys .exit (2 )
134+ usage ()
135+ sys .exit (2 )
136136 for filename in args :
137- base , ext = os .path .splitext (filename )
138- ext = ext or ".toc"
137+ base , ext = os .path .splitext (filename )
138+ ext = ext or ".toc"
139139 process (base + ext , base + ".bkm" , bigpart )
140140
141141
0 commit comments