1#!/usr/local/bin/python
2
3###############################################################################
4# Takes a chapter as input and adds internal links and numbering to all
5# of the H1, H2, H3, H4 and H5 sections.
6#
7# Every heading HTML tag (H1, H2 etc) is given an autogenerated name to link
8# to. However, if the name is not an autogenerated name from a previous run,
9# it will be kept. If it is autogenerated, it might change on subsequent runs
10# of this program. Thus if you want to create links to one of the headings,
11# then change the heading link name to something that does not look like an
12# autogenerated link name.
13###############################################################################
14
15import sys
16import re
17import string
18
19###############################################################################
20# Functions
21###############################################################################
22
23# Regexs for <a name="..."></a>
24alink = re.compile(r"<a *name *= *\"(.*)\"></a>", re.IGNORECASE)
25heading = re.compile(r"(_nn\d)", re.IGNORECASE)
26
27def getheadingname(m):
28    autogeneratedheading = True;
29    if m.group(1) != None:
30        amatch = alink.match(m.group(1))
31        if amatch:
32            # A non-autogenerated heading - keep it
33            headingname = amatch.group(1)
34            autogeneratedheading = heading.match(headingname)
35    if autogeneratedheading:
36        # The heading name was either non-existent or autogenerated,
37        # We can create a new heading / change the existing heading
38        headingname = "%s_nn%d" % (filenamebase, nameindex)
39    return headingname
40
41###############################################################################
42# Main program
43###############################################################################
44
45if len(sys.argv) != 2:
46    print "usage: makedoc.py filename"
47    sys.exit(1)
48
49filename = sys.argv[1]
50filenamebase = string.split(filename,".")[0]
51
52section = 0
53subsection = 0
54subsubsection = 0
55subsubsubsection = 0
56nameindex = 0
57
58name = ""
59
60# Regexs for <h1>,... <h5> sections
61
62h1 = re.compile(r".*?<H1>(<a.*a>)*[\d\.\s]*(.*?)</H1>", re.IGNORECASE)
63h2 = re.compile(r".*?<H2>(<a.*a>)*[\d\.\s]*(.*?)</H2>", re.IGNORECASE)
64h3 = re.compile(r".*?<H3>(<a.*a>)*[\d\.\s]*(.*?)</H3>", re.IGNORECASE)
65h4 = re.compile(r".*?<H4>(<a.*a>)*[\d\.\s]*(.*?)</H4>", re.IGNORECASE)
66h5 = re.compile(r".*?<H5>(<a.*a>)*[\d\.\s]*(.*?)</H5>", re.IGNORECASE)
67
68data = open(filename).read()            # Read data
69open(filename+".bak","w").write(data)   # Make backup
70
71lines = data.splitlines()
72result = [ ] # This is the result of postprocessing the file
73index = "<!-- INDEX -->\n<div class=\"sectiontoc\">\n" # index contains the index for adding at the top of the file. Also printed to stdout.
74
75skip = 0
76skipspace = 0
77
78for s in lines:
79    if s == "<!-- INDEX -->":
80        if not skip:
81            result.append("@INDEX@")
82            skip = 1
83        else:
84            skip = 0
85        continue;
86    if skip:
87        continue
88
89    if not s and skipspace:
90        continue
91
92    if skipspace:
93        result.append("")
94        result.append("")
95        skipspace = 0
96
97    m = h2.match(s)
98    if m:
99        prevheadingtext = m.group(2)
100        nameindex += 1
101        section += 1
102        headingname = getheadingname(m)
103        result.append("""<H2><a name="%s"></a>%d. %s</H2>""" % (headingname,section, prevheadingtext))
104
105        if subsubsubsection:
106            index += "</ul>\n"
107        if subsubsection:
108            index += "</ul>\n"
109        if subsection:
110            index += "</ul>\n"
111        if section == 1:
112            index += "<ul>\n"
113
114        index += """<li><a href="#%s">%s</a>\n""" % (headingname,prevheadingtext)
115        subsection = 0
116        subsubsection = 0
117        subsubsubsection = 0
118        skipspace = 1
119        continue
120    m = h3.match(s)
121    if m:
122        prevheadingtext = m.group(2)
123        nameindex += 1
124        subsection += 1
125        headingname = getheadingname(m)
126        result.append("""<H3><a name="%s"></a>%d.%d %s</H3>""" % (headingname,section, subsection, prevheadingtext))
127
128        if subsubsubsection:
129            index += "</ul>\n"
130        if subsubsection:
131            index += "</ul>\n"
132        if subsection == 1:
133            index += "<ul>\n"
134
135        index += """<li><a href="#%s">%s</a>\n""" % (headingname,prevheadingtext)
136        subsubsection = 0
137        skipspace = 1
138        continue
139    m = h4.match(s)
140    if m:
141        prevheadingtext = m.group(2)
142        nameindex += 1
143        subsubsection += 1
144        subsubsubsection = 0
145        headingname = getheadingname(m)
146        result.append("""<H4><a name="%s"></a>%d.%d.%d %s</H4>""" % (headingname,section, subsection, subsubsection, prevheadingtext))
147
148        if subsubsubsection:
149            index += "</ul>\n"
150        if subsubsection == 1:
151            index += "<ul>\n"
152
153        index += """<li><a href="#%s">%s</a>\n""" % (headingname,prevheadingtext)
154        skipspace = 1
155        continue
156    m = h5.match(s)
157    if m:
158        prevheadingtext = m.group(2)
159        nameindex += 1
160        subsubsubsection += 1
161        headingname = getheadingname(m)
162        result.append("""<H5><a name="%s"></a>%d.%d.%d.%d %s</H5>""" % (headingname,section, subsection, subsubsection, subsubsubsection, prevheadingtext))
163
164        if subsubsubsection == 1:
165            index += "<ul>\n"
166
167        index += """<li><a href="#%s">%s</a>\n""" % (headingname,prevheadingtext)
168        skipspace = 1
169        continue
170
171    result.append(s)
172
173if subsubsubsection:
174    index += "</ul>\n"
175
176if subsubsection:
177    index += "</ul>\n"
178
179if subsection:
180    index += "</ul>\n"
181
182if section:
183    index += "</ul>\n"
184
185index += "</div>\n<!-- INDEX -->\n"
186
187data = "\n".join(result)
188
189data = data.replace("@INDEX@",index) + "\n";
190
191# Write the file back out
192open(filename,"w").write(data)
193
194
195