Mercurial > hg > kdocs
annotate make/indexsections.py @ 104:7bcba6e2acc3
OLS papers are up to 2009 now.
author | Rob Landley <rob@landley.net> |
---|---|
date | Fri, 01 Jan 2010 04:54:13 -0600 |
parents | 010777961045 |
children | 88f40fbf8e8b |
rev | line source |
---|---|
35
6f2dd07a6016
Python preprocessor for master index. Uses <span id="name"> tags in html to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
1 #!/usr/bin/python |
6f2dd07a6016
Python preprocessor for master index. Uses <span id="name"> tags in html to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
2 |
6f2dd07a6016
Python preprocessor for master index. Uses <span id="name"> tags in html to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
3 import os,sys |
6f2dd07a6016
Python preprocessor for master index. Uses <span id="name"> tags in html to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
4 |
76
75251bfa6b33
Teach indexsections.py to produce debugging output when necessary.
Rob Landley <rob@landley.net>
parents:
65
diff
changeset
|
5 debug = 0 |
75251bfa6b33
Teach indexsections.py to produce debugging output when necessary.
Rob Landley <rob@landley.net>
parents:
65
diff
changeset
|
6 |
47
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
7 # Print appropriate number of <ul> or </ul> tags, with indentation. |
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
8 def adjust_depth(newdepth, depth): |
35
6f2dd07a6016
Python preprocessor for master index. Uses <span id="name"> tags in html to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
9 while newdepth > depth: |
41
4480365c7aef
Add section numbers, and cosmetically indent <ul></ul> in "view source".
Rob Landley <rob@landley.net>
parents:
40
diff
changeset
|
10 sys.stdout.write("%s<ul>\n" % (" "*depth)) |
35
6f2dd07a6016
Python preprocessor for master index. Uses <span id="name"> tags in html to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
11 depth += 1 |
6f2dd07a6016
Python preprocessor for master index. Uses <span id="name"> tags in html to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
12 while newdepth < depth: |
41
4480365c7aef
Add section numbers, and cosmetically indent <ul></ul> in "view source".
Rob Landley <rob@landley.net>
parents:
40
diff
changeset
|
13 sys.stdout.write("%s</ul>\n" % (" "*depth)) |
35
6f2dd07a6016
Python preprocessor for master index. Uses <span id="name"> tags in html to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
14 depth -= 1 |
60
db0a5f758831
Make the index output have more distinct major sections.
Rob Landley <rob@landley.net>
parents:
47
diff
changeset
|
15 return depth |
35
6f2dd07a6016
Python preprocessor for master index. Uses <span id="name"> tags in html to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
16 |
65
503142fc4d43
Make index/section links bidirectional, so you can jump back to the index
Rob Landley <rob@landley.net>
parents:
60
diff
changeset
|
17 # Read through HTML code to find and process some extra tags, describing |
503142fc4d43
Make index/section links bidirectional, so you can jump back to the index
Rob Landley <rob@landley.net>
parents:
60
diff
changeset
|
18 # content sections to be numbered and indexed. |
503142fc4d43
Make index/section links bidirectional, so you can jump back to the index
Rob Landley <rob@landley.net>
parents:
60
diff
changeset
|
19 |
47
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
20 def process(data, idx): |
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
21 line = data[0].count("\n") |
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
22 depth = 0 |
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
23 spans = [] |
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
24 secnum = [0] |
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
25 |
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
26 if not idx: sys.stdout.write(data[0]) |
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
27 for i in data[1:]: |
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
28 i = i.split(">",1) |
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
29 tag = i[0].split(None,1) |
65
503142fc4d43
Make index/section links bidirectional, so you can jump back to the index
Rob Landley <rob@landley.net>
parents:
60
diff
changeset
|
30 |
503142fc4d43
Make index/section links bidirectional, so you can jump back to the index
Rob Landley <rob@landley.net>
parents:
60
diff
changeset
|
31 # The magic tag to output the condensed list of all numbered sections. |
503142fc4d43
Make index/section links bidirectional, so you can jump back to the index
Rob Landley <rob@landley.net>
parents:
60
diff
changeset
|
32 # This recurses to call this function again, printing out different data. |
503142fc4d43
Make index/section links bidirectional, so you can jump back to the index
Rob Landley <rob@landley.net>
parents:
60
diff
changeset
|
33 |
47
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
34 if tag[0] == "put_index_here": |
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
35 if not idx: process(data, 1) |
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
36 continue |
65
503142fc4d43
Make index/section links bidirectional, so you can jump back to the index
Rob Landley <rob@landley.net>
parents:
60
diff
changeset
|
37 |
503142fc4d43
Make index/section links bidirectional, so you can jump back to the index
Rob Landley <rob@landley.net>
parents:
60
diff
changeset
|
38 # Parse one of our magic "span" tags describing a section. |
503142fc4d43
Make index/section links bidirectional, so you can jump back to the index
Rob Landley <rob@landley.net>
parents:
60
diff
changeset
|
39 |
47
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
40 if tag[0] == "span": |
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
41 if len(tag) == 0 or not tag[1].startswith("id="): |
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
42 sys.stderr.write("Bad span at line %s: %s" % (line,i[0])) |
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
43 sys.exit(1) |
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
44 tag = tag[1][3:] |
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
45 if tag[0]=='"' and tag[-1]=='"': tag=tag[1:-1] |
76
75251bfa6b33
Teach indexsections.py to produce debugging output when necessary.
Rob Landley <rob@landley.net>
parents:
65
diff
changeset
|
46 if debug: sys.stderr.write("tag %s\n" % tag) |
47
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
47 spans.append("_".join(tag.split())) |
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
48 secnum[-1] += 1 |
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
49 secstr = ".".join(map(lambda a: str(a), secnum)) |
65
503142fc4d43
Make index/section links bidirectional, so you can jump back to the index
Rob Landley <rob@landley.net>
parents:
60
diff
changeset
|
50 |
503142fc4d43
Make index/section links bidirectional, so you can jump back to the index
Rob Landley <rob@landley.net>
parents:
60
diff
changeset
|
51 # If we're writing out the index... |
47
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
52 if idx: |
60
db0a5f758831
Make the index output have more distinct major sections.
Rob Landley <rob@landley.net>
parents:
47
diff
changeset
|
53 depth = adjust_depth(len(spans)-1, depth) |
db0a5f758831
Make the index output have more distinct major sections.
Rob Landley <rob@landley.net>
parents:
47
diff
changeset
|
54 if depth: style="li" |
db0a5f758831
Make the index output have more distinct major sections.
Rob Landley <rob@landley.net>
parents:
47
diff
changeset
|
55 else: style="h2" |
99 | 56 if idx != 1: linkout=idx |
57 else: linkout="" | |
58 sys.stdout.write('%s<%s><a href="%s#%s" name="%s">%s %s</a></%s>\n' % (" "*len(spans),style,linkout,spans[-1],secstr,secstr,tag,style)) | |
65
503142fc4d43
Make index/section links bidirectional, so you can jump back to the index
Rob Landley <rob@landley.net>
parents:
60
diff
changeset
|
59 |
503142fc4d43
Make index/section links bidirectional, so you can jump back to the index
Rob Landley <rob@landley.net>
parents:
60
diff
changeset
|
60 # If we're writing out the contents... |
47
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
61 else: |
65
503142fc4d43
Make index/section links bidirectional, so you can jump back to the index
Rob Landley <rob@landley.net>
parents:
60
diff
changeset
|
62 sys.stdout.write('<h2><a href="#%s" name="%s">%s %s</a></h2>\n' % (secstr,spans[-1],secstr,tag)) |
47
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
63 sys.stdout.write('<%s>%s' % (i[0], i[1])) |
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
64 secnum.append(0) |
65
503142fc4d43
Make index/section links bidirectional, so you can jump back to the index
Rob Landley <rob@landley.net>
parents:
60
diff
changeset
|
65 |
503142fc4d43
Make index/section links bidirectional, so you can jump back to the index
Rob Landley <rob@landley.net>
parents:
60
diff
changeset
|
66 # End of a section, pop it off the stack. |
503142fc4d43
Make index/section links bidirectional, so you can jump back to the index
Rob Landley <rob@landley.net>
parents:
60
diff
changeset
|
67 |
47
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
68 elif tag[0] == "/span": |
76
75251bfa6b33
Teach indexsections.py to produce debugging output when necessary.
Rob Landley <rob@landley.net>
parents:
65
diff
changeset
|
69 if debug: sys.stderr.write("pop %s\n" % spans.pop()) |
75251bfa6b33
Teach indexsections.py to produce debugging output when necessary.
Rob Landley <rob@landley.net>
parents:
65
diff
changeset
|
70 else: spans.pop() |
47
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
71 secnum.pop() |
65
503142fc4d43
Make index/section links bidirectional, so you can jump back to the index
Rob Landley <rob@landley.net>
parents:
60
diff
changeset
|
72 |
503142fc4d43
Make index/section links bidirectional, so you can jump back to the index
Rob Landley <rob@landley.net>
parents:
60
diff
changeset
|
73 # This tag wasn't one of ours, so output it verbatim. (It's HTML.) |
503142fc4d43
Make index/section links bidirectional, so you can jump back to the index
Rob Landley <rob@landley.net>
parents:
60
diff
changeset
|
74 |
35
6f2dd07a6016
Python preprocessor for master index. Uses <span id="name"> tags in html to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
75 else: |
47
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
76 if not idx: sys.stdout.write('<%s>%s' % (i[0], i[1])); |
76
75251bfa6b33
Teach indexsections.py to produce debugging output when necessary.
Rob Landley <rob@landley.net>
parents:
65
diff
changeset
|
77 if debug: sys.stderr.write("i=%s\n" % i) |
87 | 78 if len(i)==1: |
79 sys.stderr.write("Unterminated tag, line %s: %s\n" % (line,i)) | |
80 sys.exit(1) | |
81 if len(i): line += i[1].count("\n") | |
47
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
82 if idx: adjust_depth(0, depth) |
35
6f2dd07a6016
Python preprocessor for master index. Uses <span id="name"> tags in html to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
83 |
99 | 84 baseurl=None |
85 if len(sys.argv) == 4 and sys.argv[1] == '-b': | |
86 del sys.argv[1] | |
87 baseurl=sys.argv[1] | |
88 del sys.argv[1] | |
89 | |
47
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
90 if len(sys.argv) != 2: |
99 | 91 sys.stderr.write("Usage: indexsections.py [-b http://baseurl] filename\n"); |
47
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
92 sys.exit(1) |
0df6348fc276
Teach indexsections to output index and contents in one pass, and put index
Rob Landley <rob@landley.net>
parents:
41
diff
changeset
|
93 |
99 | 94 process(open(sys.argv[1]).read().split("<"), baseurl) |