JargonFile/makeJargon.py

259 lines
7.1 KiB
Python
Raw Normal View History

2016-05-17 14:00:27 -04:00
import operator
2014-04-26 12:52:25 -04:00
import os
2016-05-17 14:00:27 -04:00
import datetime
2014-04-27 11:18:46 -04:00
import time
2014-04-26 12:52:25 -04:00
2016-05-17 14:06:30 -04:00
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:
2016-05-17 14:06:30 -04:00
entry.append(line[i].replace('\n', '').strip())
2014-04-26 12:52:25 -04:00
if i >= 2:
text = text + line[i]
2016-05-17 14:06:30 -04:00
text = text.replace('\n', ' ')
2014-04-26 17:47:37 -04:00
# remove any gaps
pos = text.find(' ')
while pos != -1:
2016-05-17 14:06:30 -04:00
text = text.replace(' ', ' ')
2014-04-26 17:47:37 -04:00
pos = text.find(' ')
2014-04-26 12:52:25 -04:00
entry.append(text.strip())
return entry
2016-05-17 14:06:30 -04:00
2014-04-26 16:24:44 -04:00
def jargonSubdefinitions(text):
2016-05-17 14:06:30 -04:00
"""returns the number of sub-definitions within a description"""
2014-04-26 16:24:44 -04:00
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
2014-05-26 12:03:58 -04:00
if definitions > 8:
2016-05-17 14:06:30 -04:00
definitions = 0
2014-04-26 16:24:44 -04:00
return definitions
2016-05-17 14:06:30 -04:00
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
2016-05-17 14:06:30 -04:00
2014-04-27 11:18:46 -04:00
def saveLicense(fp, year, publishername):
2016-05-17 14:06:30 -04:00
"""saves the license details"""
2014-04-27 11:18:46 -04:00
fp.write("Copyright (c) " + str(year) + " " + publishername + "\n")
2016-05-17 14:06:30 -04:00
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")
2014-04-27 11:18:46 -04:00
fp.write("Free Documentation License\".\n\n")
2016-05-17 14:00:27 -04:00
def saveGenerated(fp):
"""saves the last-generated timestamp"""
utcnow = datetime.datetime.utcnow().strftime("%A, %d %B %Y %I:%M%p UTC")
fp.write("This file last generated " + utcnow + "\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
2016-05-17 14:06:30 -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")
2016-05-17 14:06:30 -04:00
fp = open(manpageFilename, 'w')
2014-04-26 13:49:51 -04:00
2016-05-17 14:06:30 -04:00
fp.write(".TH \"The Jargon File\" 1 \"" + time.strftime("%x") +
2014-04-27 11:18:46 -04:00
"\" \"\" \"" + version + "\"\n\n")
fp.write(".SH LICENSE\n\n")
saveLicense(fp, year, publishername)
2014-04-26 13:49:51 -04:00
2016-05-17 14:00:27 -04:00
fp.write(".SH GENERATED\n\n")
saveGenerated(fp)
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:"
2016-05-17 14:06:30 -04:00
print("sudo install -m 644 " + manpageFilename +
".gz /usr/local/share/man/man1")
2014-04-26 13:49:51 -04:00
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)
2016-05-17 14:06:30 -04:00
fp = open(orgFilename, 'w')
2014-04-27 11:18:46 -04:00
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")
2016-05-17 14:06:30 -04:00
fp.write('#+STYLE: <link rel="stylesheet" type="text/css" '
'href="index.css" />\n\n')
2014-04-27 11:18:46 -04:00
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)
2016-05-17 14:00:27 -04:00
fp.write("* Generated\n\n")
saveGenerated(fp)
2014-04-27 11:18:46 -04:00
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)
2016-05-17 14:06:30 -04:00
fp = open(htmlFilename, 'w')
2014-04-27 15:47:08 -04:00
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")
2016-05-17 14:00:27 -04:00
fp.write(" <H2>Generated</H2>\n")
fp.write(" <p>\n")
saveGenerated(fp)
fp.write(" </p>\n")
2014-04-27 15:47:08 -04:00
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")
2016-05-17 14:06:30 -04:00
fp.write(" </body>\n")
2014-04-27 15:47:08 -04:00
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)