Mercurial > hg > kdocs
changeset 35:6f2dd07a6016
Python preprocessor for master index. Uses <span id="name"> tags in html to
make a nested <ul><li></li></ul> index and section headings.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 10 Sep 2007 00:13:33 -0500 |
parents | 00ba11f14914 |
children | 9a5ef2d1264e |
files | make/indexsections.py |
diffstat | 1 files changed, 46 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/make/indexsections.py Mon Sep 10 00:13:33 2007 -0500 @@ -0,0 +1,46 @@ +#!/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('<li><a href="#%s">%s</a></li>\n' % (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])) + if tag[0] == "/span": spans.pop() + line += i[1].count("\n") + +if sys.argv[1] == "-i": adjust_depth(0)