Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 473a90e

Browse files
committed
Allow the user to specify the "biggest" section type from the command line;
default is "chapter". Use 'python toc2bkm.py -c section' to use with Python HOWTO documents.
1 parent cd05ca9 commit 473a90e

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

Doc/tools/toc2bkm.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
already uses that extension. Let's avoid clashing.
77
"""
88

9+
import getopt
910
import os
1011
import re
1112
import string
@@ -38,10 +39,10 @@
3839
('subsubsection', 'chapter'): 3,
3940
}
4041

41-
def parse_toc(fp):
42+
def parse_toc(fp, bigpart=None):
4243
toc = top = []
4344
stack = [toc]
44-
level = 'chapter'
45+
level = bigpart or 'chapter'
4546
lineno = 0
4647
while 1:
4748
line = fp.readline()
@@ -105,10 +106,18 @@ def write_toc_entry(entry, fp, layer):
105106

106107

107108
def main():
108-
base, ext = os.path.splitext(sys.argv[1])
109-
ext = ext or ".toc"
110-
toc = parse_toc(open(base + ext))
111-
write_toc(toc, open(base + ".bkm", "w"))
109+
bigpart = None
110+
opts, args = getopt.getopt(sys.argv[1:], "c:")
111+
if opts:
112+
bigpart = opts[0][1]
113+
if not args:
114+
usage()
115+
sys.exit(2)
116+
for filename in args:
117+
base, ext = os.path.splitext(filename)
118+
ext = ext or ".toc"
119+
toc = parse_toc(open(base + ext), bigpart)
120+
write_toc(toc, open(base + ".bkm", "w"))
112121

113122

114123
if __name__ == "__main__":

0 commit comments

Comments
 (0)