#!python import os import shutil import pdfkit from markdown2 import markdown input_ = 'input_files' output = 'Files' templates = 'templates' def render_template(template, **kwargs): expanded = template[:] for var, val in kwargs.items(): expanded = expanded.replace('${%s}' % var, val) return expanded with open(templates + '/file.html', 'r') as template: file_template = template.read() with open(templates + '/dir.html', 'r') as template: dir_template = template.read() 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') outfiles = [] for file in files: if file.endswith('.md'): basename = file[:-3] outfile = outroot + '/' + basename + '.html' outfiles.append(basename + '.html') infile = root + '/' + file with open(infile, 'r') as f: content = f.read() with open(outfile, 'w') as f: content = markdown(content) pdf = basename + '.pdf' pretty_name = basename.replace('_', ' ') new_file = render_template(file_template, title=pretty_name, path=outroot.replace('_', ' ') + '/' + pretty_name, pdf=pdf, content=content) content += '' pdfkit.from_string(content, outroot + '/' + pdf) f.write(new_file) index_html = '
' with open(outroot + '/index.html', 'w') as f: pretty_outroot = outroot.replace('_', ' ') f.write(render_template(dir_template, title=pretty_outroot, path=pretty_outroot, content=index_html))