Path: blob/main/third_party/ply/doc/makedoc.py
6162 views
#!/usr/local/bin/python12###############################################################################3# Takes a chapter as input and adds internal links and numbering to all4# of the H1, H2, H3, H4 and H5 sections.5#6# Every heading HTML tag (H1, H2 etc) is given an autogenerated name to link7# to. However, if the name is not an autogenerated name from a previous run,8# it will be kept. If it is autogenerated, it might change on subsequent runs9# of this program. Thus if you want to create links to one of the headings,10# then change the heading link name to something that does not look like an11# autogenerated link name.12###############################################################################1314import sys15import re16import string1718###############################################################################19# Functions20###############################################################################2122# Regexs for <a name="..."></a>23alink = re.compile(r"<a *name *= *\"(.*)\"></a>", re.IGNORECASE)24heading = re.compile(r"(_nn\d)", re.IGNORECASE)2526def getheadingname(m):27autogeneratedheading = True;28if m.group(1) != None:29amatch = alink.match(m.group(1))30if amatch:31# A non-autogenerated heading - keep it32headingname = amatch.group(1)33autogeneratedheading = heading.match(headingname)34if autogeneratedheading:35# The heading name was either non-existent or autogenerated,36# We can create a new heading / change the existing heading37headingname = "%s_nn%d" % (filenamebase, nameindex)38return headingname3940###############################################################################41# Main program42###############################################################################4344if len(sys.argv) != 2:45print "usage: makedoc.py filename"46sys.exit(1)4748filename = sys.argv[1]49filenamebase = string.split(filename,".")[0]5051section = 052subsection = 053subsubsection = 054subsubsubsection = 055nameindex = 05657name = ""5859# Regexs for <h1>,... <h5> sections6061h1 = re.compile(r".*?<H1>(<a.*a>)*[\d\.\s]*(.*?)</H1>", re.IGNORECASE)62h2 = re.compile(r".*?<H2>(<a.*a>)*[\d\.\s]*(.*?)</H2>", re.IGNORECASE)63h3 = re.compile(r".*?<H3>(<a.*a>)*[\d\.\s]*(.*?)</H3>", re.IGNORECASE)64h4 = re.compile(r".*?<H4>(<a.*a>)*[\d\.\s]*(.*?)</H4>", re.IGNORECASE)65h5 = re.compile(r".*?<H5>(<a.*a>)*[\d\.\s]*(.*?)</H5>", re.IGNORECASE)6667data = open(filename).read() # Read data68open(filename+".bak","w").write(data) # Make backup6970lines = data.splitlines()71result = [ ] # This is the result of postprocessing the file72index = "<!-- INDEX -->\n<div class=\"sectiontoc\">\n" # index contains the index for adding at the top of the file. Also printed to stdout.7374skip = 075skipspace = 07677for s in lines:78if s == "<!-- INDEX -->":79if not skip:80result.append("@INDEX@")81skip = 182else:83skip = 084continue;85if skip:86continue8788if not s and skipspace:89continue9091if skipspace:92result.append("")93result.append("")94skipspace = 09596m = h2.match(s)97if m:98prevheadingtext = m.group(2)99nameindex += 1100section += 1101headingname = getheadingname(m)102result.append("""<H2><a name="%s"></a>%d. %s</H2>""" % (headingname,section, prevheadingtext))103104if subsubsubsection:105index += "</ul>\n"106if subsubsection:107index += "</ul>\n"108if subsection:109index += "</ul>\n"110if section == 1:111index += "<ul>\n"112113index += """<li><a href="#%s">%s</a>\n""" % (headingname,prevheadingtext)114subsection = 0115subsubsection = 0116subsubsubsection = 0117skipspace = 1118continue119m = h3.match(s)120if m:121prevheadingtext = m.group(2)122nameindex += 1123subsection += 1124headingname = getheadingname(m)125result.append("""<H3><a name="%s"></a>%d.%d %s</H3>""" % (headingname,section, subsection, prevheadingtext))126127if subsubsubsection:128index += "</ul>\n"129if subsubsection:130index += "</ul>\n"131if subsection == 1:132index += "<ul>\n"133134index += """<li><a href="#%s">%s</a>\n""" % (headingname,prevheadingtext)135subsubsection = 0136skipspace = 1137continue138m = h4.match(s)139if m:140prevheadingtext = m.group(2)141nameindex += 1142subsubsection += 1143subsubsubsection = 0144headingname = getheadingname(m)145result.append("""<H4><a name="%s"></a>%d.%d.%d %s</H4>""" % (headingname,section, subsection, subsubsection, prevheadingtext))146147if subsubsubsection:148index += "</ul>\n"149if subsubsection == 1:150index += "<ul>\n"151152index += """<li><a href="#%s">%s</a>\n""" % (headingname,prevheadingtext)153skipspace = 1154continue155m = h5.match(s)156if m:157prevheadingtext = m.group(2)158nameindex += 1159subsubsubsection += 1160headingname = getheadingname(m)161result.append("""<H5><a name="%s"></a>%d.%d.%d.%d %s</H5>""" % (headingname,section, subsection, subsubsection, subsubsubsection, prevheadingtext))162163if subsubsubsection == 1:164index += "<ul>\n"165166index += """<li><a href="#%s">%s</a>\n""" % (headingname,prevheadingtext)167skipspace = 1168continue169170result.append(s)171172if subsubsubsection:173index += "</ul>\n"174175if subsubsection:176index += "</ul>\n"177178if subsection:179index += "</ul>\n"180181if section:182index += "</ul>\n"183184index += "</div>\n<!-- INDEX -->\n"185186data = "\n".join(result)187188data = data.replace("@INDEX@",index) + "\n";189190# Write the file back out191open(filename,"w").write(data)192193194195196