2022-01-14 18:07:31 -05:00
|
|
|
#!python
|
|
|
|
import os
|
|
|
|
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-14 19:32:43 -05:00
|
|
|
input_ = 'input_files'
|
|
|
|
output = 'Files'
|
2022-01-14 18:07:31 -05:00
|
|
|
templates = 'templates'
|
|
|
|
|
|
|
|
|
2022-01-14 19:32:43 -05:00
|
|
|
def render_template(template, **kwargs):
|
2022-01-14 18:07:31 -05:00
|
|
|
expanded = template[:]
|
|
|
|
|
|
|
|
for var, val in kwargs.items():
|
|
|
|
expanded = expanded.replace('${%s}' % var, val)
|
|
|
|
|
|
|
|
return expanded
|
|
|
|
|
|
|
|
|
2022-01-14 19:32:43 -05:00
|
|
|
with open(templates + '/file.html', 'r') as template:
|
|
|
|
file_template = template.read()
|
|
|
|
|
|
|
|
with open(templates + '/dir.html', 'r') as template:
|
|
|
|
dir_template = template.read()
|
2022-01-14 18:07:31 -05:00
|
|
|
|
|
|
|
for root, dirs, files in os.walk(input_, topdown=True):
|
|
|
|
outroot = output + root[len(input_):]
|
|
|
|
|
|
|
|
os.makedirs(outroot, exist_ok=True)
|
|
|
|
|
|
|
|
shutil.copy(templates + '/stylesheet.css', outroot + '/stylesheet.css')
|
|
|
|
|
2022-01-14 18:22:15 -05:00
|
|
|
outfiles = []
|
|
|
|
|
2022-01-14 18:07:31 -05:00
|
|
|
for file in files:
|
|
|
|
if file.endswith('.md'):
|
|
|
|
basename = file[:-3]
|
|
|
|
outfile = outroot + '/' + basename + '.html'
|
2022-01-14 18:22:15 -05:00
|
|
|
outfiles.append(basename + '.html')
|
2022-01-14 18:07:31 -05:00
|
|
|
infile = root + '/' + file
|
|
|
|
|
|
|
|
with open(infile, 'r') as f:
|
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
with open(outfile, 'w') as f:
|
|
|
|
content = markdown(content)
|
2022-01-14 19:49:11 -05:00
|
|
|
pdf = basename + '.pdf'
|
2022-01-14 18:07:31 -05:00
|
|
|
|
2022-01-14 19:32:43 -05:00
|
|
|
pretty_name = basename.replace('_', ' ')
|
|
|
|
|
|
|
|
new_file = render_template(file_template,
|
|
|
|
title=pretty_name,
|
|
|
|
path=outroot.replace('_', ' ') + '/' + pretty_name,
|
2022-01-14 19:49:11 -05:00
|
|
|
pdf=pdf,
|
2022-01-14 18:07:31 -05:00
|
|
|
content=content)
|
|
|
|
|
2022-01-14 19:49:11 -05:00
|
|
|
content += '<style>body { text-align: justify; }</style>'
|
|
|
|
|
|
|
|
pdfkit.from_string(content, outroot + '/' + pdf)
|
|
|
|
|
2022-01-14 18:07:31 -05:00
|
|
|
f.write(new_file)
|
2022-01-14 18:22:15 -05:00
|
|
|
|
|
|
|
index_html = '<ul>'
|
|
|
|
|
|
|
|
for directory in dirs:
|
2022-01-14 19:32:43 -05:00
|
|
|
index_html += '<li><a href="%s">%s/</a></li>' % (directory + '/index.html', directory.replace('_', ' '))
|
2022-01-14 18:22:15 -05:00
|
|
|
|
|
|
|
for file in outfiles:
|
|
|
|
index_html += '<li><a href="%s">%s</a></li>' % (file, file.removesuffix('.html').replace('_', ' '))
|
|
|
|
|
|
|
|
index_html += '</ul>'
|
|
|
|
|
|
|
|
with open(outroot + '/index.html', 'w') as f:
|
2022-01-14 19:32:43 -05:00
|
|
|
pretty_outroot = outroot.replace('_', ' ')
|
|
|
|
|
|
|
|
f.write(render_template(dir_template,
|
|
|
|
title=pretty_outroot,
|
|
|
|
path=pretty_outroot,
|
2022-01-14 18:22:15 -05:00
|
|
|
content=index_html))
|