PageRenderTime 308ms CodeModel.GetById 4ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1-3-26/SWIG/Doc/Manual/makechap.py

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