makechm.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. ########################################################################
  2. ##
  3. ## Win32 Usage: makepanda\makechm.bat
  4. ## Linux Usage: makepanda/makechm.py
  5. ##
  6. ## To use this script, you will need to have hhc.exe on your system.
  7. ## For verbose output, run with -v or --verbose option.
  8. ## To keep the temporary .hhc, .hhk, .hhp, .chw files use -k or --keep.
  9. ##
  10. ## You can also import this file as a python module. You will then have
  11. ## access to three functions: makeCHM, makeManualCHM, makeReferenceCHM.
  12. ## This is how you call them:
  13. ## makeCHM(outputfile, dirname, title)
  14. ## where outputfile is the filename where the .chm file will be written,
  15. ## and dirname is the directory containing the html files to include.
  16. ## Title will be the title of the CHM file.
  17. ## The functions makeManualCHM and makeReferenceCHM work exactly the
  18. ## same, except that they work with a structure resembling that of the
  19. ## Panda3D manual and reference, respectively.
  20. ## Note: outputfile should not contain spaces.
  21. ##
  22. ########################################################################
  23. __all__ = ["makeCHM", "makeManualCHM", "makeReferenceCHM"]
  24. import os, re
  25. from sys import exit
  26. import xml.dom.minidom
  27. from xml.dom.minidom import Node
  28. VERBOSE = False
  29. KEEPTEMP = False
  30. if __name__ == "__main__":
  31. from sys import argv
  32. VERBOSE = ("-v" in argv) or ("-vk" in argv) or ("-kv" in argv) or ("--verbose" in argv)
  33. KEEPTEMP = ("-k" in argv) or ("-kv" in argv) or ("-vk" in argv) or ("--keep" in argv)
  34. OPTIONBLOCK = """
  35. Binary TOC=Yes
  36. Compatibility=1.1 or later
  37. Compiled file=%s
  38. Contents file=%s.hhc
  39. Default Font=Arial,10,0
  40. Default topic=%s
  41. Display compile progress=VERBOSE
  42. Full-text search=Yes
  43. Index file=%s.hhk
  44. Language=0x409 English (United States)
  45. Title=%s""".replace("VERBOSE", VERBOSE and "Yes" or "No")
  46. HTMLBLOCK = """<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
  47. <html>
  48. <head>
  49. <meta name="generator" content="Panda3D - makechm.py">
  50. </head>
  51. <body>
  52. <object type="text/site properties">
  53. <param name="Window Styles" value="0x800025">
  54. <param name="ImageType" value="Folder">
  55. <param name="Font" value="Arial,8,0">
  56. </object>
  57. <ul>\n"""
  58. REFERENCEITEMS = [
  59. ("index.html", "Main Page"),
  60. ("methods.html", "Methods"),
  61. ("functions.html", "Global Functions"),
  62. ("classes.html", "Classes"),
  63. ]
  64. def urldecode(url):
  65. regex = re.compile("%([0-9a-hA-H][0-9a-hA-H])", re.M)
  66. return regex.sub(lambda x: chr(int(x.group(1), 16)), url)
  67. def ireplace(string, target, replacement):
  68. """Case-insensitive replace."""
  69. index = string.lower().find(target.lower())
  70. if index >= 0:
  71. result = string[:index] + replacement + string[index + len(target):]
  72. return result
  73. else:
  74. return string
  75. def parseAnchor(node):
  76. """Parses an XML minidom node representing an anchor and returns a tuple
  77. containing the href and the content of the link."""
  78. assert node.nodeType == Node.ELEMENT_NODE
  79. assert node.localName == "a"
  80. href = ""
  81. title = ""
  82. for localName, a in node.attributes.items():
  83. if localName.lower() == "href":
  84. href = a
  85. for e in node.childNodes:
  86. if e.nodeType == Node.TEXT_NODE:
  87. title += e.data
  88. return href, title
  89. def parseManualTree(node):
  90. """Parses a tree of the manual Main_Page and returns it through a list containing tuples:
  91. [(title, href, [(title, href, [...]), ...]), ...]"""
  92. if node.nodeType != Node.ELEMENT_NODE: return []
  93. result = []
  94. lastadded = None
  95. for e in node.childNodes:
  96. if e.nodeType == Node.ELEMENT_NODE:
  97. if e.localName == "ol":
  98. assert lastadded != None
  99. for i in xrange(len(result)):
  100. if result[i][:2] == lastadded:
  101. result[i] = lastadded + (parseManualTree(e),)
  102. elif e.localName == "a":
  103. href, title = parseAnchor(e)
  104. lastadded = title, href
  105. result.append((title, href, None))
  106. return result
  107. def parseManualTOC(filename):
  108. """Reads the manual's Main_Page file and returns a list of all the trees found."""
  109. filename = open(filename)
  110. text = filename.read()
  111. filename.close()
  112. text = text.split("<h2>Table of Contents</h2>")[1].split("</div>")[0]
  113. text = "<root>" + text.replace("<li>", "") + "</root>"
  114. text = re.sub(re.compile("<!--([^>]+)>"), "", text)
  115. result = []
  116. for e in xml.dom.minidom.parseString(text).childNodes[0].childNodes:
  117. if e.nodeType == Node.ELEMENT_NODE:
  118. result.append(parseManualTree(e))
  119. return result
  120. def treeToHTML(tree, dirname, indent = ""):
  121. """Converts a tree into HTML code suitable for .hhc or .hhk files. The tree should be like:
  122. [(title, href, [(title, href, [...]), ...]), ...]"""
  123. html = ""
  124. for title, href, sub in tree:
  125. html += indent + "<li><object type=\"text/sitemap\">\n"
  126. html += indent + " <param name=\"Name\" value=\"%s\">\n" % title.replace("CXX", "C++").replace("\"", "&quot;")
  127. html += indent + " <param name=\"Local\" value=\"%s\">\n" % urldecode(os.path.join(dirname, href))
  128. html += indent + "</object>\n"
  129. if sub != None:
  130. html += indent + "<ul>\n"
  131. html += treeToHTML(sub, dirname, indent + " ")
  132. html += indent + "</ul>\n"
  133. return html
  134. def makeCHM(outputfile, dirname, title, special = None):
  135. """Creates a CHM file based on a directory of HTML files. See the top of this file for more info."""
  136. assert special == None or special in ["manual", "reference"]
  137. reference = (special == "reference")
  138. manual = (special == "manual")
  139. base = ireplace(outputfile, ".chm", "")
  140. if os.path.isfile(base + ".chm"): os.remove(base + ".chm")
  141. # Create the hhp file
  142. hhp = open(base + ".hhp", "w")
  143. hhp.write("[OPTIONS]\n")
  144. hhp.write(OPTIONBLOCK % (base + ".chm", base, urldecode(os.path.join(dirname, "index.html")), base, title))
  145. hhp.write("\n[FILES]\n")
  146. # Create the TOC file and Index file
  147. hhk = open(base + ".hhk", "w")
  148. hhc = open(base + ".hhc", "w")
  149. hhk.write(HTMLBLOCK)
  150. hhc.write(HTMLBLOCK)
  151. # The manual should be treated as a special case.
  152. if manual:
  153. hhc.write(" <li><object type=\"text/sitemap\">\n")
  154. hhc.write(" <param name=\"Name\" value=\"Main Page\">\n")
  155. hhc.write(" <param name=\"Local\" value=\"%s\">\n" % urldecode(os.path.join(dirname, "index.html")))
  156. hhc.write(" </object>\n")
  157. for item in parseManualTOC(dirname + "/index.html"):
  158. hhc.write(treeToHTML(item, dirname, " "))
  159. for i in os.listdir(dirname):
  160. hhp.write(os.path.join(dirname, i) + "\n")
  161. if i != "index.html":
  162. hhk.write(" <li><object type=\"text/sitemap\">\n")
  163. hhk.write(" <param name=\"Name\" value=\"%s\">\n" % ireplace(urldecode(i).replace(".1", "").replace("_", " ").replace("CXX", "C++"), ".html", "").replace("\"", "&quot;"))
  164. hhk.write(" <param name=\"Local\" value=\"%s\">\n" % urldecode(os.path.join(dirname, i)))
  165. hhk.write(" </object>\n")
  166. else:
  167. idt = " "
  168. # If we are writing out the reference, write some extra stuff.
  169. if reference:
  170. idt = " "
  171. for i, desc in REFERENCEITEMS:
  172. hhk.write(" <li><object type=\"text/sitemap\">\n")
  173. hhk.write(" <param name=\"Name\" value=\"%s\">\n" % desc.replace("\"", "&quot;"))
  174. hhk.write(" <param name=\"Local\" value=\"%s\">\n" % urldecode(os.path.join(dirname, i)))
  175. hhk.write(" </object>\n")
  176. hhc.write(" <li><object type=\"text/sitemap\">\n")
  177. hhc.write(" <param name=\"Name\" value=\"%s\">\n" % desc.replace("\"", "&quot;"))
  178. hhc.write(" <param name=\"Local\" value=\"%s\">\n" % urldecode(os.path.join(dirname, i)))
  179. hhc.write(" </object>\n")
  180. hhc.write(" <ul>\n")
  181. # Loop through the directories and write out relevant data.
  182. for i in os.listdir(dirname):
  183. hhp.write(os.path.join(dirname, i) + "\n")
  184. if i != "index.html" and ((not reference) or (not i in ["classes.html", "methods.html", "functions.html"])):
  185. hhk.write(" <li><object type=\"text/sitemap\">\n")
  186. hhk.write(" <param name=\"Name\" value=\"%s\">\n" % ireplace(urldecode(i).replace(".1", ""), ".html", "").replace("\"", "&quot;"))
  187. hhk.write(" <param name=\"Local\" value=\"%s\">\n" % urldecode(os.path.join(dirname, i)))
  188. hhk.write(" </object>\n")
  189. hhc.write(idt + "<li><object type=\"text/sitemap\">\n")
  190. hhc.write(idt + " <param name=\"Name\" value=\"%s\">\n" % ireplace(urldecode(i).replace(".1", ""), ".html", "").replace("\"", "&quot;"))
  191. hhc.write(idt + " <param name=\"Local\" value=\"%s\">\n" % urldecode(os.path.join(dirname, i)))
  192. hhc.write(idt + "</object>\n")
  193. # Close the files.
  194. if reference: hhc.write(" </ul>\n")
  195. hhk.write(" </ul>\n </body>\n</html>")
  196. hhc.write(" </ul>\n </body>\n</html>")
  197. hhk.close()
  198. hhc.close()
  199. hhp.close()
  200. # Now, execute the command to compile the files.
  201. if "PROGRAMFILES" in os.environ and os.path.isdir("%s\\HTML Help Workshop" % os.environ["PROGRAMFILES"]):
  202. cmd = "\"%s\\HTML Help Workshop\\hhc.exe\" %s.hhp" % (os.environ["PROGRAMFILES"], base)
  203. elif os.path.isdir("C:\\Program Files\\HTML Help Workshop"):
  204. cmd = "\"C:\\Program Files\\HTML Help Workshop\\hhc.exe\" %s.hhp" % base
  205. else:
  206. cmd = "hhc \"%s.hhp\"" % base
  207. print(cmd)
  208. os.system(cmd)
  209. if not KEEPTEMP:
  210. if os.path.isfile("%s.hhp" % base): os.remove("%s.hhp" % base)
  211. if os.path.isfile("%s.hhc" % base): os.remove("%s.hhc" % base)
  212. if os.path.isfile("%s.hhk" % base): os.remove("%s.hhk" % base)
  213. if os.path.isfile("%s.chw" % base): os.remove("%s.chw" % base)
  214. if not os.path.isfile(base + ".chm"):
  215. print("An error has occurred!")
  216. if __name__ == "__main__":
  217. exit(1)
  218. else:
  219. return False
  220. if __name__ != "__main__":
  221. return True
  222. def makeManualCHM(outputfile, dirname, title):
  223. """Same as makeCHM, but suitable for converting the Panda3D manual."""
  224. return makeCHM(outputfile, dirname, title, special = "manual")
  225. def makeReferenceCHM(outputfile, dirname, title):
  226. """Same as makeCHM, but converts a structure resembling that of the Panda3D reference."""
  227. return makeCHM(outputfile, dirname, title, special = "reference")
  228. if __name__ == "__main__":
  229. # Extract a version number, if we have one.
  230. VERSION = None
  231. try:
  232. f = file("built/include/pandaVersion.h","r")
  233. pattern = re.compile('^\\s*[#]\\s*define\\s+PANDA_VERSION_STR\\s+["]([0-9.]+)["]')
  234. for line in f:
  235. match = pattern.match(line,0)
  236. if (match):
  237. VERSION = match.group(1)
  238. break
  239. f.close()
  240. except:
  241. # If not, we don't care at all.
  242. pass
  243. # Now, make CHM's for both the manual and reference, if we have them.
  244. for lang in ["python", "cxx"]:
  245. if not os.path.isdir("manual-" + lang):
  246. print("No directory named 'manual-%s' found" % lang)
  247. else:
  248. print("Making CHM file for manual-%s..." % lang)
  249. if VERSION == None:
  250. makeManualCHM("manual-%s.chm" % lang, "manual-" + lang, "Panda3D Manual")
  251. else:
  252. makeManualCHM("manual-%s-%s.chm" % (VERSION, lang), "manual-" + lang, "Panda3D %s Manual" % VERSION)
  253. if not os.path.isdir("reference-" + lang):
  254. print("No directory named 'reference-%s' found" % lang)
  255. else:
  256. print("Making CHM file for reference-%s..." % lang)
  257. if VERSION == None:
  258. makeReferenceCHM("reference-%s.chm" % lang, "reference-" + lang, "Panda3D Reference")
  259. else:
  260. makeReferenceCHM("reference-%s-%s.chm" % (VERSION, lang), "reference-" + lang, "Panda3D %s Reference" % VERSION)
  261. print("Done!")