From: William F. <wsf...@us...> - 2004-08-04 21:24:44
|
Update of /cvsroot/swig/SWIG/Doc/Manual In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14397/Doc/Manual Modified Files: makechap.py maketoc.py Log Message: Update to generate valid html for the anchor names Auto generated anchor names won't override a non auto generated anchor name, thereby making it possible to have anchor names that won't change should an extra heading be added earlier in the file Index: maketoc.py =================================================================== RCS file: /cvsroot/swig/SWIG/Doc/Manual/maketoc.py,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** maketoc.py 30 Nov 2002 21:58:09 -0000 1.2 --- maketoc.py 4 Aug 2004 21:24:32 -0000 1.3 *************** *** 19,23 **** f.close() ! num = 0 for c in chs: --- 19,23 ---- f.close() ! num = 1 for c in chs: Index: makechap.py =================================================================== RCS file: /cvsroot/swig/SWIG/Doc/Manual/makechap.py,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** makechap.py 12 Jul 2004 19:45:19 -0000 1.4 --- makechap.py 4 Aug 2004 21:24:32 -0000 1.5 *************** *** 1,10 **** #!/usr/local/bin/python # Takes a chapter as input and adds internal links and numbering to all ! # of the h1, h2, h3, h4 sections and so forth. # import sys import re if len(sys.argv) != 3: --- 1,45 ---- #!/usr/local/bin/python + ############################################################################### # Takes a chapter as input and adds internal links and numbering to all ! # of the H1, H2, H3, H4 and H5 sections. # + # Every heading HTML tag (H1, H2 etc) is given an autogenerated name to link + # to. However, if the name is not an autogenerated name from a previous run, + # it will be kept. If it is autogenerated, it might change on subsequent runs + # of this program. Thus if you want to create links to one of the headings, + # then change the heading link name to something that does not look like an + # autogenerated link name. + ############################################################################### import sys import re + import string + + ############################################################################### + # Functions + ############################################################################### + + # Regexs for <a name="..."></a> + alink = re.compile(r"<a *name *= *\"(.*)\"></a>", re.IGNORECASE) + heading = re.compile(r"(_nn\d)", re.IGNORECASE) + + def getheadingname(m): + autogeneratedheading = True; + if m.group(1) != None: + amatch = alink.match(m.group(1)) + if amatch: + # A non-autogenerated heading - keep it + headingname = amatch.group(1) + autogeneratedheading = heading.match(headingname) + if autogeneratedheading: + # The heading name was either non-existent or autogenerated, + # We can create a new heading / change the existing heading + headingname = "%s_nn%d" % (filenamebase, nameindex) + return headingname + + ############################################################################### + # Main program + ############################################################################### if len(sys.argv) != 3: *************** *** 13,16 **** --- 48,52 ---- filename = sys.argv[1] + filenamebase = string.split(filename,".")[0] num = int(sys.argv[2]) *************** *** 22,32 **** name = "" ! # Regexs for <h1>,... <h4> sections ! h1 = re.compile(r".*?<H1>[\d\.\s]*(.*?)</H1>", re.IGNORECASE) ! h2 = re.compile(r".*?<H2>[\d\.\s]*(.*?)</H2>", re.IGNORECASE) ! h3 = re.compile(r".*?<H3>[\d\.\s]*(.*?)</H3>", re.IGNORECASE) ! h4 = re.compile(r".*?<H4>[\d\.\s]*(.*?)</H4>", re.IGNORECASE) ! h5 = re.compile(r".*?<H5>[\d\.\s]*(.*?)</H5>", re.IGNORECASE) data = open(filename).read() # Read data --- 58,68 ---- name = "" ! # Regexs for <h1>,... <h5> sections ! h1 = re.compile(r".*?<H1>(<a.*a>)*[\d\.\s]*(.*?)</H1>", re.IGNORECASE) ! h2 = re.compile(r".*?<H2>(<a.*a>)*[\d\.\s]*(.*?)</H2>", re.IGNORECASE) ! h3 = re.compile(r".*?<H3>(<a.*a>)*[\d\.\s]*(.*?)</H3>", re.IGNORECASE) ! h4 = re.compile(r".*?<H4>(<a.*a>)*[\d\.\s]*(.*?)</H4>", re.IGNORECASE) ! h5 = re.compile(r".*?<H5>(<a.*a>)*[\d\.\s]*(.*?)</H5>", re.IGNORECASE) data = open(filename).read() # Read data *************** *** 34,39 **** lines = data.splitlines() ! result = [ ] ! index = "<!-- INDEX -->\n<ul>\n" skip = 0 --- 70,75 ---- lines = data.splitlines() ! result = [ ] # This is the result of postprocessing the file ! index = "<!-- INDEX -->\n" # index contains the index for adding at the top of the file. Also printed to stdout. skip = 0 *************** *** 60,65 **** m = h1.match(s) if m: nameindex += 1 ! result.append("""<a name="n%d"></a><H1>%d %s</H1>""" % (nameindex,num,m.group(1))) result.append("@INDEX@") section = 0 --- 96,103 ---- m = h1.match(s) if m: + prevheadingtext = m.group(2) nameindex += 1 ! headingname = getheadingname(m) ! result.append("""<H1><a name="%s"></a>%d %s</H1>""" % (headingname,num,prevheadingtext)) result.append("@INDEX@") section = 0 *************** *** 67,83 **** subsubsection = 0 subsubsubsection = 0 ! name = m.group(1) skipspace = 1 continue m = h2.match(s) if m: nameindex += 1 section += 1 ! result.append("""<a name="n%d"></a><H2>%d.%d %s</H2>""" % (nameindex,num,section, m.group(1))) if subsubsection: index += "</ul>\n" if subsection: index += "</ul>\n" ! index += """<li><a href="#n%d">%s</a>\n""" % (nameindex,m.group(1)) subsection = 0 subsubsection = 0 --- 105,129 ---- subsubsection = 0 subsubsubsection = 0 ! name = prevheadingtext skipspace = 1 continue m = h2.match(s) if m: + prevheadingtext = m.group(2) nameindex += 1 section += 1 ! headingname = getheadingname(m) ! result.append("""<H2><a name="%s"></a>%d.%d %s</H2>""" % (headingname,num,section, prevheadingtext)) ! ! if subsubsubsection: ! index += "</ul>\n" if subsubsection: index += "</ul>\n" if subsection: index += "</ul>\n" ! if section == 1: ! index += "<ul>\n" ! ! index += """<li><a href="#%s">%s</a>\n""" % (headingname,prevheadingtext) subsection = 0 subsubsection = 0 *************** *** 87,101 **** m = h3.match(s) if m: nameindex += 1 subsection += 1 ! result.append("""<a name="n%d"></a><H3>%d.%d.%d %s</H3>""" % (nameindex,num,section, subsection, m.group(1))) if subsubsection: index += "</ul>\n" - if subsection == 1: index += "<ul>\n" - index += """<li><a href="#n%d">%s</a>\n""" % (nameindex,m.group(1)) subsubsection = 0 skipspace = 1 --- 133,150 ---- m = h3.match(s) if m: + prevheadingtext = m.group(2) nameindex += 1 subsection += 1 ! headingname = getheadingname(m) ! result.append("""<H3><a name="%s"></a>%d.%d.%d %s</H3>""" % (headingname,num,section, subsection, prevheadingtext)) + if subsubsubsection: + index += "</ul>\n" if subsubsection: index += "</ul>\n" if subsection == 1: index += "<ul>\n" + index += """<li><a href="#%s">%s</a>\n""" % (headingname,prevheadingtext) subsubsection = 0 skipspace = 1 *************** *** 103,120 **** m = h4.match(s) if m: nameindex += 1 subsubsection += 1 subsubsubsection = 0 ! result.append("""<a name="n%d"></a><H4>%d.%d.%d.%d %s</H4>""" % (nameindex,num,section, subsection, subsubsection, m.group(1))) if subsubsection == 1: index += "<ul>\n" ! index += """<li><a href="#n%d">%s</a>\n""" % (nameindex,m.group(1)) skipspace = 1 continue m = h5.match(s) if m: nameindex += 1 subsubsubsection += 1 ! result.append("""<a name="n%d"></a><H5>%d.%d.%d.%d.%d %s</H5>""" % (nameindex,num,second,subsection,subsubsection,subsubsubsection, m.group(1))) skipspace = 1 continue --- 152,182 ---- m = h4.match(s) if m: + prevheadingtext = m.group(2) nameindex += 1 subsubsection += 1 subsubsubsection = 0 ! headingname = getheadingname(m) ! result.append("""<H4><a name="%s"></a>%d.%d.%d.%d %s</H4>""" % (headingname,num,section, subsection, subsubsection, prevheadingtext)) ! ! if subsubsubsection: ! index += "</ul>\n" if subsubsection == 1: index += "<ul>\n" ! ! index += """<li><a href="#%s">%s</a>\n""" % (headingname,prevheadingtext) skipspace = 1 continue m = h5.match(s) if m: + prevheadingtext = m.group(2) nameindex += 1 subsubsubsection += 1 ! headingname = getheadingname(m) ! result.append("""<H5><a name="%s"></a>%d.%d.%d.%d.%d %s</H5>""" % (headingname,num,section, subsection, subsubsection, subsubsubsection, prevheadingtext)) ! ! if subsubsubsection == 1: ! index += "<ul>\n" ! ! index += """<li><a href="#%s">%s</a>\n""" % (headingname,prevheadingtext) skipspace = 1 continue *************** *** 122,125 **** --- 184,190 ---- result.append(s) + if subsubsubsection: + index += "</ul>\n" + if subsubsection: index += "</ul>\n" *************** *** 140,150 **** open(filename,"w").write(data) ! # Print the TOC data ! index = index.replace("#n","%s#n" % filename) ! print """<h3><a href="%s">%d %s</a></h3>\n""" % (filename,num,name) print index - - - --- 205,212 ---- open(filename,"w").write(data) ! # Print the TOC data to stdout correcting the anchor links for external referencing ! index = index.replace("<li><a href=\"#","<li><a href=\"%s#" % filename) ! print """<h3><a href="%s#%s">%d %s</a></h3>\n""" % (filename,filenamebase,num,name) print index |