#!/usr/bin/env python3 import glob import markdown import os import re import shutil input_directory = 'content' static_directory = 'static' output_directory = 'output' md_extensions = ['fenced_code', 'codehilite', 'nl2br', 'toc', 'smarty', 'tables', 'linkify'] def print_file(in_file, out_file): print('%-53s -> %s' % (in_file, out_file)) template = open('template.html').read() os.makedirs(output_directory, exist_ok=True) for (dirpath, _, filenames) in os.walk(static_directory): for filename in filenames: source = os.path.join(dirpath, filename) out_path = dirpath.replace(static_directory, '', 1) out_path = out_path.lstrip('/') dest_dir = os.path.join(output_directory, out_path) os.makedirs(dest_dir, exist_ok=True) dest = os.path.join(dest_dir, filename) print_file(source, dest) shutil.copy2(source, dest) out_filenames = [] # TODO: also walk the markdown directory for markdown_filename in glob.glob(os.path.join(input_directory, '*.md')): markdown_file = open(markdown_filename) text = markdown_file.read() markdown_file.close() if not text.startswith('# '): text = '# ' + text match = re.match(r'^(.*?)\n', text) if match: title = match.group(1).lstrip('#') else: title = text title += ' | Colin McMillen' if markdown_filename == os.path.join(input_directory, 'index.md'): title = 'Colin McMillen' html = markdown.markdown(text, extensions=md_extensions, output_format='html5') output = template.replace('__TITLE_GOES_HERE__', title) output = output.replace('__CONTENT_GOES_HERE__', html) out_filename = os.path.basename(markdown_filename).replace('.md', '.html') out_filenames.append(out_filename) out_fullpath = os.path.join(output_directory, out_filename) print_file(markdown_filename, out_fullpath) out_file = open(out_fullpath, 'w') out_file.write(output) out_file.close() # TODO: make a sitemap / RSS? #index_filename = os.path.join(output_directory, 'index.html') #print_file('', index_filename) #index = open(index_filename, 'w') #for f in out_filenames: # index.write('%s
' % (f, f)) #index.close()