summarybugs & patchesrefslogtreecommitdiffstats
path: root/tools/gen.py
blob: e2f65e11dc402b2f7f856c9779064c91d99b1863 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os, shutil
from discord_md import md
import json
from collections import UserDict

class IgnoreUnfoundPlaceholders(UserDict):
	"""Wrapper around dicts for using in str.format()
	It basically tries to lookup the key it gets, and if the key doesn't exist,
	it returns ``{keyname}`` where ``keyname`` is the key it got
	"""
	def __missing__(self, key):
		return "{%s}"%str(key)

# with open("conf.json", "r") as fd:
# 	conf=json.load(fd)

with open("html/footer.html","r") as fd:
	footer=fd.read()

with open("html/header.html","r") as fd:
	header=fd.read()

with open("html/pagetemplate.html","r") as fd:
	template=fd.read()

template=template.format_map(
	IgnoreUnfoundPlaceholders(header=header, footer=footer)
)

if os.path.isdir("out"):
	shutil.rmtree("out")

out=os.path.abspath('out')
os.makedirs(out)
os.symlink(os.path.abspath("static"), os.path.join(out,"static"), True)
#os.chdir("content")

def processdir(path):
	print("processing",path)
	os.chdir(path)
	parent=os.path.dirname(path)
	for file in os.listdir():
		if file.endswith(".md"):

			with open(file, "r") as fd:
				print("| reading",file)
				text=md.convert(fd.read())
			
			meta:dict=md.Meta
			print("Meta:",meta)
			section=meta.get("section",[""])[0]

			html=template.format(
				title=meta.get("title",["<No title>"])[0] + (" - "+section if section else ''),
				section=section,
				content=text,
				toc=md.toc if not meta.get("notoc") else ""
			)

			newname=file.removesuffix(".md")+".html"
			newname=newname.replace("README","index")
			
			target=os.path.join(out, section)
			if not os.path.isdir(target):
				os.makedirs(target)

			with open(os.path.join(out, section, newname), "x") as fd:
				print("| writing")
				fd.write(html)

		elif os.path.isdir(file):
			processdir(os.path.join(path,file))
		
		else:
			shutil.copy2(file, os.path.join(out, os.path.basename(path)))
	
	os.chdir(parent)

processdir("./content")