Mercurial > hg > kdocs
view make/indexsections.py @ 40:17389c66eb02
More work on master index skeleton and infrastructure.
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 11 Sep 2007 17:12:19 -0500 |
parents | 6f2dd07a6016 |
children | 4480365c7aef |
line wrap: on
line source
#!/usr/bin/python import os,sys args = sys.argv[1:] spans = [] depth = 0 if args[0] == '-i': args = args[1:] if not len(args) != 3: sys.stderr.write("Usage: indexsections.py [-i] filename\n"); sys.exit(1) data = open(args[0]).read().split("<") def adjust_depth(newdepth): global depth while newdepth > depth: sys.stdout.write("<ul>\n") depth += 1 while newdepth < depth: sys.stdout.write("</ul>\n") depth -= 1 line = data[0].count("\n") if sys.argv[1] != "-i": sys.stdout.write(data[0]) for i in data[1:]: i = i.split(">",1) tag = i[0].split(None,1) if tag[0] == "span": if len(tag) == 0 or not tag[1].startswith("id="): sys.stderr.write("Bad span at line %s: %s" % (line,i[0])) sys.exit(1) tag = tag[1][3:] if tag[0]=='"' and tag[-1]=='"': tag=tag[1:-1] spans.append("_".join(tag.split())) if sys.argv[1] == "-i": adjust_depth(len(spans)) sys.stdout.write('%s<li><a href="#%s">%s</a></li>\n' % (" "*len(spans),spans[-1],tag)) else: sys.stdout.write('<h2><a name="%s">%s</a></h2>\n' % (spans[-1],tag)) sys.stdout.write('<%s>%s' % (i[0], i[1])) elif tag[0] == "/span": spans.pop() else: if sys.argv[1] != '-i': sys.stdout.write('<%s>%s' % (i[0], i[1])); line += i[1].count("\n") if sys.argv[1] == "-i": adjust_depth(0)