cabinet/generate.py

106 lines
2.7 KiB
Python
Raw Normal View History

2022-01-14 18:07:31 -05:00
#!python
import os
2022-01-15 14:55:28 -05:00
import re
2022-01-14 18:07:31 -05:00
import shutil
2022-01-14 19:49:11 -05:00
import pdfkit
2022-01-14 18:07:31 -05:00
from markdown2 import markdown
2022-01-15 14:55:28 -05:00
input_ = 'input'
outroot = 'out'
2022-01-14 19:32:43 -05:00
output = 'Files'
2022-01-14 18:07:31 -05:00
templates = 'templates'
2022-01-15 14:55:28 -05:00
shutil.rmtree(outroot, ignore_errors=True)
os.mkdir(outroot)
shutil.copy(templates + '/stylesheet.css', outroot + '/stylesheet.css')
2022-01-14 18:07:31 -05:00
2022-01-15 14:55:28 -05:00
with open(templates + '/file.html', 'r') as template:
file_template = template.read()
with open(templates + '/index.html', 'r') as template:
index_template = template.read()
def render_template(template, **kwargs):
2022-01-14 18:07:31 -05:00
for var, val in kwargs.items():
2022-01-15 14:55:28 -05:00
template = template.replace('${%s}' % var, val)
2022-01-14 18:07:31 -05:00
2022-01-15 14:55:28 -05:00
return template
2022-01-14 18:07:31 -05:00
2022-01-15 14:55:28 -05:00
class File:
def __init__(self, root, outdir, name):
self.outdir = outdir
self.basename = name[:-3]
self.pdf = self.basename + '.pdf'
self.html = self.basename + '.html'
self.path = self.outdir.removeprefix(outroot + '/') + '/' + self.html
self.pretty_path = self.path.replace('_', ' ').removesuffix('.html')
2022-01-14 19:32:43 -05:00
2022-01-15 14:55:28 -05:00
self.root_reference = re.sub(r'.+?/', '../', outdir)
self.root_reference = re.sub(r'/[^\.]+$', '/', self.root_reference)
2022-01-14 18:07:31 -05:00
2022-01-15 14:55:28 -05:00
with open(root + '/' + name, 'r') as f:
self.content = markdown(f.read())
2022-01-14 18:07:31 -05:00
2022-01-15 14:55:28 -05:00
def expand_html(self):
title = self.basename.replace('_', ' ')
2022-01-14 18:07:31 -05:00
2022-01-15 14:55:28 -05:00
return render_template(file_template,
title=title,
path=self.pretty_path,
root=self.root_reference,
pdf=self.pdf,
content=self.content)
2022-01-14 18:07:31 -05:00
2022-01-15 14:55:28 -05:00
def write_html(self):
html_content = self.expand_html()
2022-01-14 18:22:15 -05:00
2022-01-15 14:55:28 -05:00
with open(self.outdir + '/' + self.html, 'w') as f:
f.write(html_content)
2022-01-14 18:07:31 -05:00
2022-01-15 14:55:28 -05:00
def write_pdf(self):
content = self.content
2022-01-14 18:07:31 -05:00
2022-01-15 14:55:28 -05:00
# Extra style for PDF
content += """
<style>
body {
text-align: justify;
}
</style>
"""
2022-01-14 18:07:31 -05:00
2022-01-15 14:55:28 -05:00
pdfkit.from_string(content, self.outdir + '/' + self.pdf)
2022-01-14 19:32:43 -05:00
2022-01-15 14:55:28 -05:00
def write(self):
self.write_html()
self.write_pdf()
2022-01-14 18:07:31 -05:00
2022-01-14 19:49:11 -05:00
2022-01-15 14:55:28 -05:00
toc = '<ul>'
2022-01-14 19:49:11 -05:00
2022-01-15 14:55:28 -05:00
for root, dirs, files in os.walk(input_, topdown=True):
outdir = outroot + '/' + output + root[len(input_):]
os.makedirs(outdir, exist_ok=True)
outfiles = []
2022-01-14 18:22:15 -05:00
2022-01-15 14:55:28 -05:00
if len(files) or len(dirs):
for file in files:
if file.endswith('.md'):
f = File(root, outdir, file)
2022-01-14 18:22:15 -05:00
2022-01-15 14:55:28 -05:00
f.write()
2022-01-14 18:22:15 -05:00
2022-01-15 14:55:28 -05:00
toc += '<li><a href="%s">%s</a></li>' % (f.path, f.pretty_path)
2022-01-14 18:22:15 -05:00
2022-01-15 14:55:28 -05:00
toc += '</ul>'
2022-01-14 18:22:15 -05:00
2022-01-14 19:32:43 -05:00
2022-01-15 14:55:28 -05:00
with open(outroot + '/index.html', 'w') as f:
f.write(render_template(index_template,
toc=toc))