cabinet/generate.py

166 lines
4.6 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-16 15:58:46 -05:00
import subprocess
from html.parser import HTMLParser
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
class CodeHighlighter(HTMLParser):
data = ''
reading_code = False
code = ''
lang = ''
def output(self):
data = self.data
self.data = ''
return data
def handle_starttag(self, tag, attrs):
if tag == 'pre':
self.reading_code = True
if tag == 'code':
for attr in attrs:
if attr[0] == 'class':
self.lang = attr[1].split(' ')[1]
if not self.reading_code:
self.attrs = attrs
self.data += '<' + tag
for attr in attrs:
self.data += ' %s="%s"' % (attr[0], attr[1])
self.data += '>'
def handle_data(self, data):
if self.reading_code:
self.code += data
else:
self.data += data
def handle_endtag(self, tag):
if not self.reading_code:
self.data += '</%s>' % tag
if tag == 'pre':
self.reading_code = False
self.data += highlight(self.code,
get_lexer_by_name(self.lang),
HtmlFormatter(linenos=True))
self.code = ''
self.lang = ''
highlighter = CodeHighlighter()
2022-01-14 18:07:31 -05:00
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-16 15:58:46 -05:00
shutil.copy(templates + '/highlight.css', outroot + '/highlight.css')
shutil.copytree(templates + '/mathjax', outroot + '/mathjax')
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
2022-01-16 15:58:46 -05:00
self.basename = name[:-4]
2022-01-15 14:55:28 -05:00
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-16 15:58:46 -05:00
self.input_path = root + '/' + name
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-16 15:58:46 -05:00
# with open(root + '/' + name, 'r') as f:
# markdowner = markdown2.Markdown(extras=['metadata',
# 'fenced-code-blocks',
# 'tables',
# 'code-friendly'])
# self.content = markdowner.convert(f.read())
self.content = subprocess.check_output(['pandoc', '-f', 'latex', '-t', 'html',
'%s/%s' % (root, name)]).decode()
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-16 15:58:46 -05:00
expanded = render_template(file_template,
title=title,
path=self.pretty_path,
root=self.root_reference,
pdf=self.pdf,
content=self.content)
highlighter.feed(expanded)
return highlighter.output()
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):
2022-01-16 15:58:46 -05:00
subprocess.run(['latexmk', '-pdf', '-outdir=%s' % self.outdir, self.input_path])
2022-01-14 18:07:31 -05:00
2022-01-16 15:58:46 -05:00
subprocess.run(['latexmk', '-c', '-outdir=%s' % self.outdir, self.input_path])
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:
2022-01-16 15:58:46 -05:00
if file.endswith('.tex'):
2022-01-15 14:55:28 -05:00
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))