1 #!/usr/local/bin/python
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.
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 ###############################################################################
19 ###############################################################################
21 ###############################################################################
23 # Regexs for <a name="..."></a>
24 alink = re.compile(r"<a *name *= *\"(.*)\"></a>", re.IGNORECASE)
25 heading = re.compile(r"(_nn\d)", re.IGNORECASE)
27 def getheadingname(m):
28 autogeneratedheading = True;
29 if m.group(1) != None:
30 amatch = alink.match(m.group(1))
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)
41 ###############################################################################
43 ###############################################################################
45 if len(sys.argv) != 3:
46 print "usage: makechap.py filename num"
49 filename = sys.argv[1]
50 filenamebase = string.split(filename,".")[0]
51 num = int(sys.argv[2])
60 # Regexs for <h1>,... <h5> sections
62 h1 = re.compile(r".*?<H1>(<a.*a>)*[\d\.\s]*(.*?)</H1>", re.IGNORECASE)
63 h2 = re.compile(r".*?<H2>(<a.*a>)*[\d\.\s]*(.*?)</H2>", re.IGNORECASE)
64 h3 = re.compile(r".*?<H3>(<a.*a>)*[\d\.\s]*(.*?)</H3>", re.IGNORECASE)
65 h4 = re.compile(r".*?<H4>(<a.*a>)*[\d\.\s]*(.*?)</H4>", re.IGNORECASE)
66 h5 = re.compile(r".*?<H5>(<a.*a>)*[\d\.\s]*(.*?)</H5>", re.IGNORECASE)
68 data = open(filename).read() # Read data
69 open(filename+".bak","w").write(data) # Make backup
71 lines = data.splitlines()
72 result = [ ] # This is the result of postprocessing the file
73 index = "<!-- INDEX -->\n<div class=\"sectiontoc\">\n" # index contains the index for adding at the top of the file. Also printed to stdout.
79 if s == "<!-- INDEX -->":
88 if not s and skipspace:
98 prevheadingtext = m.group(2)
100 headingname = getheadingname(m)
101 result.append("""<H1><a name="%s"></a>%d %s</H1>""" % (headingname,num,prevheadingtext))
102 result.append("@INDEX@")
107 name = prevheadingtext
112 prevheadingtext = m.group(2)
115 headingname = getheadingname(m)
116 result.append("""<H2><a name="%s"></a>%d.%d %s</H2>""" % (headingname,num,section, prevheadingtext))
127 index += """<li><a href="#%s">%s</a>\n""" % (headingname,prevheadingtext)
135 prevheadingtext = m.group(2)
138 headingname = getheadingname(m)
139 result.append("""<H3><a name="%s"></a>%d.%d.%d %s</H3>""" % (headingname,num,section, subsection, prevheadingtext))
148 index += """<li><a href="#%s">%s</a>\n""" % (headingname,prevheadingtext)
154 prevheadingtext = m.group(2)
158 headingname = getheadingname(m)
159 result.append("""<H4><a name="%s"></a>%d.%d.%d.%d %s</H4>""" % (headingname,num,section, subsection, subsubsection, prevheadingtext))
163 if subsubsection == 1:
166 index += """<li><a href="#%s">%s</a>\n""" % (headingname,prevheadingtext)
172 prevheadingtext = m.group(2)
174 subsubsubsection += 1
175 headingname = getheadingname(m)
176 result.append("""<H5><a name="%s"></a>%d.%d.%d.%d.%d %s</H5>""" % (headingname,num,section, subsection, subsubsection, subsubsubsection, prevheadingtext))
178 if subsubsubsection == 1:
181 index += """<li><a href="#%s">%s</a>\n""" % (headingname,prevheadingtext)
199 index += "</div>\n<!-- INDEX -->\n"
201 data = "\n".join(result)
203 data = data.replace("@INDEX@",index) + "\n";
205 # Write the file back out
206 open(filename,"w").write(data)
208 # Print the TOC data to stdout correcting the anchor links for external referencing
210 index = index.replace("<li><a href=\"#","<li><a href=\"%s#" % filename)
211 print """<h3><a href="%s#%s">%d %s</a></h3>\n""" % (filename,filenamebase,num,name)