PageRenderTime 84ms CodeModel.GetById 40ms RepoModel.GetById 1ms app.codeStats 0ms

/ext/ply/doc/makedoc.py

https://bitbucket.org/musleh123/gem5_cetus
Python | 194 lines | 150 code | 21 blank | 23 comment | 11 complexity | 25f67fd4fc4d9feab13e0cba89390bc6 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  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) != 2:
  39. print "usage: makedoc.py filename"
  40. sys.exit(1)
  41. filename = sys.argv[1]
  42. filenamebase = string.split(filename,".")[0]
  43. section = 0
  44. subsection = 0
  45. subsubsection = 0
  46. subsubsubsection = 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. result.append("@INDEX@")
  66. skip = 1
  67. else:
  68. skip = 0
  69. continue;
  70. if skip:
  71. continue
  72. if not s and skipspace:
  73. continue
  74. if skipspace:
  75. result.append("")
  76. result.append("")
  77. skipspace = 0
  78. m = h2.match(s)
  79. if m:
  80. prevheadingtext = m.group(2)
  81. nameindex += 1
  82. section += 1
  83. headingname = getheadingname(m)
  84. result.append("""<H2><a name="%s"></a>%d. %s</H2>""" % (headingname,section, prevheadingtext))
  85. if subsubsubsection:
  86. index += "</ul>\n"
  87. if subsubsection:
  88. index += "</ul>\n"
  89. if subsection:
  90. index += "</ul>\n"
  91. if section == 1:
  92. index += "<ul>\n"
  93. index += """<li><a href="#%s">%s</a>\n""" % (headingname,prevheadingtext)
  94. subsection = 0
  95. subsubsection = 0
  96. subsubsubsection = 0
  97. skipspace = 1
  98. continue
  99. m = h3.match(s)
  100. if m:
  101. prevheadingtext = m.group(2)
  102. nameindex += 1
  103. subsection += 1
  104. headingname = getheadingname(m)
  105. result.append("""<H3><a name="%s"></a>%d.%d %s</H3>""" % (headingname,section, subsection, prevheadingtext))
  106. if subsubsubsection:
  107. index += "</ul>\n"
  108. if subsubsection:
  109. index += "</ul>\n"
  110. if subsection == 1:
  111. index += "<ul>\n"
  112. index += """<li><a href="#%s">%s</a>\n""" % (headingname,prevheadingtext)
  113. subsubsection = 0
  114. skipspace = 1
  115. continue
  116. m = h4.match(s)
  117. if m:
  118. prevheadingtext = m.group(2)
  119. nameindex += 1
  120. subsubsection += 1
  121. subsubsubsection = 0
  122. headingname = getheadingname(m)
  123. result.append("""<H4><a name="%s"></a>%d.%d.%d %s</H4>""" % (headingname,section, subsection, subsubsection, prevheadingtext))
  124. if subsubsubsection:
  125. index += "</ul>\n"
  126. if subsubsection == 1:
  127. index += "<ul>\n"
  128. index += """<li><a href="#%s">%s</a>\n""" % (headingname,prevheadingtext)
  129. skipspace = 1
  130. continue
  131. m = h5.match(s)
  132. if m:
  133. prevheadingtext = m.group(2)
  134. nameindex += 1
  135. subsubsubsection += 1
  136. headingname = getheadingname(m)
  137. result.append("""<H5><a name="%s"></a>%d.%d.%d.%d %s</H5>""" % (headingname,section, subsection, subsubsection, subsubsubsection, prevheadingtext))
  138. if subsubsubsection == 1:
  139. index += "<ul>\n"
  140. index += """<li><a href="#%s">%s</a>\n""" % (headingname,prevheadingtext)
  141. skipspace = 1
  142. continue
  143. result.append(s)
  144. if subsubsubsection:
  145. index += "</ul>\n"
  146. if subsubsection:
  147. index += "</ul>\n"
  148. if subsection:
  149. index += "</ul>\n"
  150. if section:
  151. index += "</ul>\n"
  152. index += "</div>\n<!-- INDEX -->\n"
  153. data = "\n".join(result)
  154. data = data.replace("@INDEX@",index) + "\n";
  155. # Write the file back out
  156. open(filename,"w").write(data)