JargonFile/makeJargon.py

229 lines
6.5 KiB
Python
Raw Normal View History

2014-04-26 12:52:25 -04:00
import os
import string
2014-04-26 14:16:32 -04:00
import operator
2014-04-27 11:18:46 -04:00
import time
2014-04-26 12:52:25 -04:00
def jargonParseEntry(filename):
if not os.path.isfile(filename):
return []
entry = []
line = []
with open(filename) as fp:
line = fp.readlines()
fp.close()
text = ''
if len(line) > 2:
for i in range(len(line)):
if i == 0:
entry.append(line[i].replace('\n','').strip())
if i >= 2:
text = text + line[i]
text = text.replace('\n',' ')
2014-04-26 17:47:37 -04:00
# remove any gaps
pos = text.find(' ')
while pos != -1:
text = text.replace(' ',' ')
pos = text.find(' ')
2014-04-26 12:52:25 -04:00
entry.append(text.strip())
return entry
2014-04-26 16:24:44 -04:00
# returns the number of sub-definitions within a description
def jargonSubdefinitions(text):
definitions = 0
2014-04-26 16:56:29 -04:00
prevpos = 0
2014-04-26 16:24:44 -04:00
for i in range(10):
definitionStr = str(i+1) + ". "
2014-04-26 16:56:29 -04:00
pos = text.find(definitionStr)
if pos == -1 or pos < prevpos:
2014-04-26 16:24:44 -04:00
break
2014-04-26 16:56:29 -04:00
else:
definitions = definitions + 1
prevpos = pos
2014-04-26 16:24:44 -04:00
if definitions == 0:
definitions = 1
# too many definitions
if definitions > 5:
definitions = 0
return definitions
2014-04-26 12:52:25 -04:00
def jargonGetEntries(entriesDir):
entries = []
for dirName, subdirList, fileList in os.walk(entriesDir):
for filename in fileList:
entry = jargonParseEntry(entriesDir + '/' + filename)
if entry:
entries.append(entry)
2014-04-26 14:16:32 -04:00
entries.sort(key=operator.itemgetter(0))
2014-04-26 12:52:25 -04:00
return entries
2014-04-27 11:18:46 -04:00
# saves the license details
def saveLicense(fp, year, publishername):
fp.write("Copyright (c) " + str(year) + " " + publishername + "\n")
fp.write("Permission is granted to copy, distribute and/or modify this document\n")
fp.write("under the terms of the GNU Free Documentation License, Version 1.3\n")
fp.write("or any later version published by the Free Software Foundation;\n")
fp.write("with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.\n")
fp.write("A copy of the license is included in the section entitled \"GNU\n")
fp.write("Free Documentation License\".\n\n")
2014-04-27 15:47:08 -04:00
def jargonWithDefinitions(text, definitions, isHtml):
2014-04-26 16:24:44 -04:00
result = ''
prevpos = 0
for i in range(definitions):
pos = text.find(str(i+1) + ". ")
if pos > -1 and i > 0:
if result != '':
result = result + "\n\n"
2014-04-27 15:47:08 -04:00
if not isHtml:
result = result + text[prevpos:pos]
else:
result = result + "<p>" + text[prevpos:pos] + "</p>"
2014-04-26 16:24:44 -04:00
prevpos = pos
2014-04-27 15:47:08 -04:00
if not isHtml:
result = result + "\n\n" + text[prevpos:]
else:
result = result + "\n\n" + "<p>" + text[prevpos:] + "</p>"
return result
2014-04-26 16:24:44 -04:00
2014-04-27 11:18:46 -04:00
def jargonToManpage(manpageFilename, entries, version, publishername):
year = int(time.strftime("%Y"))
if not os.path.isdir("docs"):
os.system("mkdir docs")
2014-04-26 13:49:51 -04:00
2014-04-26 14:18:58 -04:00
if os.path.isfile(manpageFilename + ".gz"):
os.system("rm " + manpageFilename + ".gz")
2014-04-26 13:49:51 -04:00
fp = open(manpageFilename,'w')
2014-04-27 11:18:46 -04:00
fp.write(".TH \"The Jargon File\" 1 \"" + time.strftime("%x") + \
"\" \"\" \"" + version + "\"\n\n")
fp.write(".SH LICENSE\n\n")
saveLicense(fp, year, publishername)
2014-04-26 13:49:51 -04:00
for entry in entries:
title = entry[0]
text = entry[1]
2014-04-26 16:24:44 -04:00
definitions = jargonSubdefinitions(entry[1])
if definitions > 1:
2014-04-27 15:47:08 -04:00
text = jargonWithDefinitions(text, definitions, False)
2014-04-26 16:24:44 -04:00
2014-04-26 13:49:51 -04:00
fp.write(".SH " + title + "\n")
fp.write(text + "\n\n")
fp.close()
os.system("gzip " + manpageFilename)
print "manpage can be installed with the command:"
print "sudo install -m 644 " + manpageFilename + ".gz /usr/local/share/man/man1"
2014-04-27 11:18:46 -04:00
def jargonToOrgMode(orgFilename, entries, version, publishername):
year = int(time.strftime("%Y"))
if not os.path.isdir("docs"):
os.system("mkdir docs")
if os.path.isfile(orgFilename):
os.system("rm " + orgFilename)
fp = open(orgFilename,'w')
2014-04-27 15:23:34 -04:00
fp.write("#+TITLE: The Jargon File\n")
2014-04-27 11:18:46 -04:00
fp.write("#+VERSION " + version + "\n")
fp.write("#+OPTIONS: ^:nil\n")
fp.write("#+STYLE: <link rel=\"stylesheet\" type=\"text/css\" href=\"index.css\" />\n\n")
fp.write("#+BEGIN_CENTER\n")
fp.write("*Yet more Jargon*\n")
fp.write("#+END_CENTER\n\n")
fp.write("* License\n\n")
saveLicense(fp, year, publishername)
fp.write("* Glossary\n")
2014-04-27 15:23:09 -04:00
subsection = ''
2014-04-27 11:18:46 -04:00
for entry in entries:
title = entry[0]
text = entry[1]
definitions = jargonSubdefinitions(entry[1])
if definitions > 1:
2014-04-27 15:47:08 -04:00
text = jargonWithDefinitions(text, definitions, False)
2014-04-27 11:18:46 -04:00
2014-04-27 15:23:09 -04:00
if title[0:1] != subsection:
subsection = title[0:1]
fp.write("** " + subsection.upper() + "\n")
fp.write("*** " + title + "\n")
2014-04-27 11:18:46 -04:00
fp.write(text + "\n\n")
fp.close()
2014-04-27 15:47:08 -04:00
def jargonToHTML(htmlFilename, entries, version, publishername):
year = int(time.strftime("%Y"))
if not os.path.isdir("docs"):
os.system("mkdir docs")
if os.path.isfile(htmlFilename):
os.system("rm " + htmlFilename)
fp = open(htmlFilename,'w')
fp.write("<!DOCTYPE html>\n")
fp.write("<html>\n")
fp.write(" <head>\n")
fp.write(" <title>The Jargon File</title>\n")
fp.write(" </head>\n")
fp.write(" <body>\n")
fp.write(" <H1>The Jargon File</H1>\n")
fp.write(" <H2>License</H2>\n")
fp.write(" <p>\n")
saveLicense(fp, year, publishername)
fp.write(" </p>\n")
fp.write(" <H2>Glossary</H2>\n")
subsection = ''
for entry in entries:
title = entry[0]
text = entry[1]
if title[0:1] != subsection:
subsection = title[0:1]
fp.write(" <H3>" + subsection.upper() + "</H3>\n")
fp.write(" <H4>" + title + "</H4>\n")
definitions = jargonSubdefinitions(entry[1])
if definitions > 1:
text = jargonWithDefinitions(text, definitions, True)
fp.write(" " + text + "\n")
else:
fp.write(" <p>\n")
fp.write(" " + text + "\n")
fp.write(" </p>\n")
fp.write(" </body>\n");
fp.write("</html>\n")
fp.close()
2014-04-26 12:52:25 -04:00
if __name__ == "__main__":
2014-04-26 14:16:32 -04:00
version = "x.xx"
2014-04-27 11:18:46 -04:00
publishername = "My Name"
2014-04-26 12:52:25 -04:00
entries = jargonGetEntries('entries')
2014-04-27 11:18:46 -04:00
jargonToManpage("docs/jargon.1", entries, version, publishername)
jargonToOrgMode("docs/jargon-org.txt", entries, version, publishername)
2014-04-27 15:47:08 -04:00
jargonToHTML("docs/jargon.html", entries, version, publishername)