]> granicus.if.org Git - python/commitdiff
Allow the user to specify the "biggest" section type from the command line;
authorFred Drake <fdrake@acm.org>
Sat, 7 Mar 1998 15:34:50 +0000 (15:34 +0000)
committerFred Drake <fdrake@acm.org>
Sat, 7 Mar 1998 15:34:50 +0000 (15:34 +0000)
default is "chapter".  Use 'python toc2bkm.py -c section' to use with
Python HOWTO documents.

Doc/tools/toc2bkm.py

index dd74fd4f7526b0f904f6e0c72f0c30a5ff9054b2..2b9631248385fb557903eae5383e765182468e99 100755 (executable)
@@ -6,6 +6,7 @@ The output file has an extension of '.bkm' instead of '.out', since hyperref
 already uses that extension.  Let's avoid clashing.
 """
 
+import getopt
 import os
 import re
 import string
@@ -38,10 +39,10 @@ _transition_map = {
     ('subsubsection', 'chapter'): 3,
     }
 
-def parse_toc(fp):
+def parse_toc(fp, bigpart=None):
     toc = top = []
     stack = [toc]
-    level = 'chapter'
+    level = bigpart or 'chapter'
     lineno = 0
     while 1:
        line = fp.readline()
@@ -105,10 +106,18 @@ def write_toc_entry(entry, fp, layer):
 
 
 def main():
-    base, ext = os.path.splitext(sys.argv[1])
-    ext = ext or ".toc"
-    toc = parse_toc(open(base + ext))
-    write_toc(toc, open(base + ".bkm", "w"))
+    bigpart = None
+    opts, args = getopt.getopt(sys.argv[1:], "c:")
+    if opts:
+       bigpart = opts[0][1]
+    if not args:
+       usage()
+       sys.exit(2)
+    for filename in args:
+       base, ext = os.path.splitext(filename)
+       ext = ext or ".toc"
+       toc = parse_toc(open(base + ext), bigpart)
+       write_toc(toc, open(base + ".bkm", "w"))
 
 
 if __name__ == "__main__":