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) != 2:
46 print "usage
: makedoc
.py filename
"
49 filename = sys.argv[1]
50 filenamebase = string.split(filename,".")[0]
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
-->":
81 result.append("@INDEX@")
89 if not s and skipspace:
99 prevheadingtext = m.group(2)
102 headingname = getheadingname(m)
103 result.append("""<H2><a name="%s"></a>%d. %s</H2>""" % (headingname,section, prevheadingtext))
114 index += """<li><a href="#%s">%s</a>\n""" % (headingname,prevheadingtext)
122 prevheadingtext
= m
.group(2)
125 headingname
= getheadingname(m
)
126 result
.append("""<H3><a name="%s"></a>%d.%d %s</H3>""" % (headingname
,section
, subsection
, prevheadingtext
))
135 index
+= """<li><a href="#%s">%s</a>\n""" % (headingname
,prevheadingtext
)
141 prevheadingtext
= m
.group(2)
145 headingname
= getheadingname(m
)
146 result
.append("""<H4><a name="%s"></a>%d.%d.%d %s</H4>""" % (headingname
,section
, subsection
, subsubsection
, prevheadingtext
))
150 if subsubsection
== 1:
153 index
+= """<li><a href="#%s">%s</a>\n""" % (headingname
,prevheadingtext
)
158 prevheadingtext
= m
.group(2)
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
))
164 if subsubsubsection
== 1:
167 index
+= """<li><a href="#%s">%s</a>\n""" % (headingname
,prevheadingtext
)
185 index
+= "</div>\n<!-- INDEX -->\n"
187 data
= "\n".join(result
)
189 data
= data
.replace("@INDEX@",index
) + "\n";
191 # Write the file back out
192 open(filename
,"w").write(data
)