makeJargon.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import operator
  2. import os
  3. import datetime
  4. import time
  5. def jargonParseEntry(filename):
  6. if not os.path.isfile(filename):
  7. return []
  8. entry = []
  9. line = []
  10. with open(filename) as fp:
  11. line = fp.readlines()
  12. fp.close()
  13. text = ''
  14. if len(line) > 2:
  15. for i in range(len(line)):
  16. if i == 0:
  17. entry.append(line[i].replace('\n', '').strip())
  18. if i >= 2:
  19. text = text + line[i]
  20. text = text.replace('\n', ' ')
  21. # remove any gaps
  22. pos = text.find(' ')
  23. while pos != -1:
  24. text = text.replace(' ', ' ')
  25. pos = text.find(' ')
  26. entry.append(text.strip())
  27. return entry
  28. def jargonSubdefinitions(text):
  29. """returns the number of sub-definitions within a description"""
  30. definitions = 0
  31. prevpos = 0
  32. for i in range(10):
  33. definitionStr = str(i+1) + ". "
  34. pos = text.find(definitionStr)
  35. if pos == -1 or pos < prevpos:
  36. break
  37. else:
  38. definitions = definitions + 1
  39. prevpos = pos
  40. if definitions == 0:
  41. definitions = 1
  42. # too many definitions
  43. if definitions > 8:
  44. definitions = 0
  45. return definitions
  46. def jargonGetEntries(entriesDir):
  47. entries = []
  48. for dirName, subdirList, fileList in os.walk(entriesDir):
  49. for filename in fileList:
  50. entry = jargonParseEntry(entriesDir + '/' + filename)
  51. if entry:
  52. entries.append(entry)
  53. entries.sort(key=operator.itemgetter(0))
  54. return entries
  55. def saveLicense(fp, year, publishername):
  56. """saves the license details"""
  57. fp.write("Copyright (c) " + str(year) + " " + publishername + "\n")
  58. fp.write("Permission is granted to copy, distribute and/or modify this "
  59. "document\n")
  60. fp.write("under the terms of the GNU Free Documentation License, Version "
  61. "1.3\n")
  62. fp.write("or any later version published by the Free Software "
  63. "Foundation;\n")
  64. fp.write("with no Invariant Sections, no Front-Cover Texts, and no "
  65. "Back-Cover Texts.\n")
  66. fp.write("A copy of the license is included in the section entitled "
  67. "\"GNU\n")
  68. fp.write("Free Documentation License\".\n\n")
  69. def saveGenerated(fp):
  70. """saves the last-generated timestamp"""
  71. utcnow = datetime.datetime.utcnow().strftime("%A, %d %B %Y %I:%M%p UTC")
  72. fp.write("This file last generated " + utcnow + "\n\n")
  73. def jargonWithDefinitions(text, definitions, isHtml):
  74. result = ''
  75. prevpos = 0
  76. for i in range(definitions):
  77. pos = text.find(str(i+1) + ". ")
  78. if pos > -1 and i > 0:
  79. if result != '':
  80. result = result + "\n\n"
  81. if not isHtml:
  82. result = result + text[prevpos:pos]
  83. else:
  84. result = result + "<p>" + text[prevpos:pos] + "</p>"
  85. prevpos = pos
  86. if not isHtml:
  87. result = result + "\n\n" + text[prevpos:]
  88. else:
  89. result = result + "\n\n" + "<p>" + text[prevpos:] + "</p>"
  90. return result
  91. def jargonToManpage(manpageFilename, entries, version, publishername):
  92. year = int(time.strftime("%Y"))
  93. if not os.path.isdir("docs"):
  94. os.system("mkdir docs")
  95. if os.path.isfile(manpageFilename + ".gz"):
  96. os.system("rm " + manpageFilename + ".gz")
  97. fp = open(manpageFilename, 'w')
  98. fp.write(".TH \"The Jargon File\" 1 \"" + time.strftime("%x") +
  99. "\" \"\" \"" + version + "\"\n\n")
  100. fp.write(".SH LICENSE\n\n")
  101. saveLicense(fp, year, publishername)
  102. fp.write(".SH GENERATED\n\n")
  103. saveGenerated(fp)
  104. for entry in entries:
  105. title = entry[0]
  106. text = entry[1]
  107. definitions = jargonSubdefinitions(entry[1])
  108. if definitions > 1:
  109. text = jargonWithDefinitions(text, definitions, False)
  110. fp.write(".SH " + title + "\n")
  111. fp.write(text + "\n\n")
  112. fp.close()
  113. os.system("gzip " + manpageFilename)
  114. print("manpage can be installed with the command:")
  115. print("sudo install -m 644 " + manpageFilename +
  116. ".gz /usr/local/share/man/man1")
  117. def jargonToOrgMode(orgFilename, entries, version, publishername):
  118. year = int(time.strftime("%Y"))
  119. if not os.path.isdir("docs"):
  120. os.system("mkdir docs")
  121. if os.path.isfile(orgFilename):
  122. os.system("rm " + orgFilename)
  123. fp = open(orgFilename, 'w')
  124. fp.write("#+TITLE: The Jargon File\n")
  125. fp.write("#+VERSION " + version + "\n")
  126. fp.write("#+OPTIONS: ^:nil\n")
  127. fp.write('#+STYLE: <link rel="stylesheet" type="text/css" '
  128. 'href="index.css" />\n\n')
  129. fp.write("#+BEGIN_CENTER\n")
  130. fp.write("*Yet more Jargon*\n")
  131. fp.write("#+END_CENTER\n\n")
  132. fp.write("* License\n\n")
  133. saveLicense(fp, year, publishername)
  134. fp.write("* Generated\n\n")
  135. saveGenerated(fp)
  136. fp.write("* Glossary\n")
  137. subsection = ''
  138. for entry in entries:
  139. title = entry[0]
  140. text = entry[1]
  141. definitions = jargonSubdefinitions(entry[1])
  142. if definitions > 1:
  143. text = jargonWithDefinitions(text, definitions, False)
  144. if title[0:1] != subsection:
  145. subsection = title[0:1]
  146. fp.write("** " + subsection.upper() + "\n")
  147. fp.write("*** " + title + "\n")
  148. fp.write(text + "\n\n")
  149. fp.close()
  150. def jargonToHTML(htmlFilename, entries, version, publishername):
  151. year = int(time.strftime("%Y"))
  152. if not os.path.isdir("docs"):
  153. os.system("mkdir docs")
  154. if os.path.isfile(htmlFilename):
  155. os.system("rm " + htmlFilename)
  156. fp = open(htmlFilename, 'w')
  157. fp.write("<!DOCTYPE html>\n")
  158. fp.write("<html>\n")
  159. fp.write(" <head>\n")
  160. fp.write(" <title>The Jargon File</title>\n")
  161. fp.write(" </head>\n")
  162. fp.write(" <body>\n")
  163. fp.write(" <H1>The Jargon File</H1>\n")
  164. fp.write(" <H2>License</H2>\n")
  165. fp.write(" <p>\n")
  166. saveLicense(fp, year, publishername)
  167. fp.write(" </p>\n")
  168. fp.write(" <H2>Generated</H2>\n")
  169. fp.write(" <p>\n")
  170. saveGenerated(fp)
  171. fp.write(" </p>\n")
  172. fp.write(" <H2>Glossary</H2>\n")
  173. subsection = ''
  174. for entry in entries:
  175. title = entry[0]
  176. text = entry[1]
  177. if title[0:1] != subsection:
  178. subsection = title[0:1]
  179. fp.write(" <H3>" + subsection.upper() + "</H3>\n")
  180. fp.write(" <H4>" + title + "</H4>\n")
  181. definitions = jargonSubdefinitions(entry[1])
  182. if definitions > 1:
  183. text = jargonWithDefinitions(text, definitions, True)
  184. fp.write(" " + text + "\n")
  185. else:
  186. fp.write(" <p>\n")
  187. fp.write(" " + text + "\n")
  188. fp.write(" </p>\n")
  189. fp.write(" </body>\n")
  190. fp.write("</html>\n")
  191. fp.close()
  192. if __name__ == "__main__":
  193. version = "x.xx"
  194. publishername = "My Name"
  195. entries = jargonGetEntries('entries')
  196. jargonToManpage("docs/jargon.1", entries, version, publishername)
  197. jargonToOrgMode("docs/jargon.org", entries, version, publishername)
  198. jargonToHTML("docs/jargon.html", entries, version, publishername)