opus-nt/docgen/generate-docs.py

163 lines
3.7 KiB
Python
Executable File

#!/usr/bin/env python3
import sys
oldexcepthook = sys.excepthook
def newexcepthook(type,value,traceback):
oldexcepthook(type,value,traceback)
input("Press ENTER to quit.")
sys.excepthook = newexcepthook
import os
p = os.path.join
pUp = os.path.dirname
s = False
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
s = os.path.realpath(sys.executable)
else:
s = os.path.realpath(__file__)
sp = pUp(s)
# script start
import html
import shutil
def getDescription(dir):
desc = "No description"
try:
with open(p(dir,"description.txt")) as dfile:
desc = dfile.read().strip(" \t\r\n")
except:
pass
return desc
def getTags(dir):
tags = ["none"]
try:
with open(p(dir,"tags.txt")) as tfile:
tags = tfile.read().strip(" \t\r\n").split(" ")
except:
pass
return tags
def getModList(dir, modlist = {}, rootdir = False):
if rootdir == False: rootdir = dir
for root,dirs,files in os.walk(dir):
for file in sorted(dirs):
ffile = p(root,file)
lfile = ffile.replace(rootdir + os.path.sep,"",1)
modname = ffile.replace(dir + os.path.sep,"",1)
activated = True
if modname[0] == "-":
modname = modname[1:]
activated = False
if modname[0] == "[" and modname[-1] == "]":
getModList(ffile,modlist,rootdir)
continue
fmodname = p(pUp(lfile),modname)
modlist[ffile] = {
"modname": modname,
"fmodname": fmodname,
"activated": activated
}
break
return modlist
def makeHeading(title,id,size = 1):
return '<h' +str(size)+ ' id="' +id+ '"><a href="#' +id+ '">#</a> ' +html.escape(title)+ '</h' +str(size)+ '>\n'
def readTags():
tags = {}
with open(p(sp,"tags.txt"),encoding="utf-8") as tfile:
for line in tfile:
line = line.strip(" \t\r\n")
if line == "": continue
tagSplit = line.split(" ",1)
tags[tagSplit[0]] = tagSplit[1]
return tags
def main():
opusPath = pUp(sp)
ofileHtml = p(opusPath,"docs","index.html")
ofileMd = p(opusPath,"docs","index.md")
ofile = open(ofileHtml,"w",encoding="utf-8")
ofile.write('''\
<!DOCTYPE HTML>
<html>
<head>
<title>opus-nt - Mod Documentation</title>
<meta charset="UTF-8">
<style>
body {
background-color: ButtonFace;
color: ButtonText;
font: menu;
}
table {
border: 1px outset ButtonShadow;
background-color: Window;
}
th, td {
border: 1px inset ButtonShadow;
}
th, td {
padding-left: 5px;
padding-right: 5px;
}
a {
text-decoration: none;
}
h1, h2, h3, h4, h5 {
color: CaptionText;
}
</style>
</head>
<body>\
''')
tags = readTags()
ofile.write(makeHeading("Tags","tags"))
ofile.write('''\
<table>
<tr><th>Tag</th><th>Description</th></tr>
''')
for tag in tags:
ofile.write(' <tr><td><b>' +html.escape(tag)+ '</b></td><td>' +html.escape(tags[tag])+ '</td></tr>\n')
ofile.write('</table>')
modPaths = []
for root,dirs,files in os.walk(opusPath):
for file in sorted(dirs):
ffile = p(root,file)
lfile = ffile.replace(opusPath + os.path.sep,"",1)
if lfile.startswith("mods"):
modPaths.append(ffile)
break
for modPath in modPaths:
modList = getModList(modPath,{})
lmodPath = modPath.replace(opusPath + os.path.sep,"",1)
ofile.write(makeHeading("Mods: " +lmodPath,lmodPath))
ofile.write(html.escape(getDescription(modPath))+ "<br>\n")
ofile.write("<table>\n")
ofile.write(' <tr><th>Mod</th><th>Tags</th><th>Description</th></tr>\n')
for modk in modList:
mod = modList[modk]
ofile.write(' <tr><td>' +html.escape(pUp(mod["fmodname"]).replace("/","\\"))+ '\\<b>' +html.escape(mod["modname"])+ '</b></td><td>' +html.escape(", ".join(getTags(modk)))+ '</td><td>' +html.escape(getDescription(modk))+ '</td></tr>\n')
ofile.write("</table><br>\n")
ofile.write('</body>\n</html>')
ofile.close()
shutil.copyfile(ofileHtml,ofileMd)
main()