2021-06-28 17:30:52 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2021-06-29 16:13:30 +00:00
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# cd ~/src/www-builder
|
|
|
|
# python3 build.py
|
|
|
|
# cd output
|
|
|
|
# cp -r * ~/src/www-home
|
|
|
|
# cd ~/src/www-home
|
|
|
|
# git status
|
|
|
|
# git add [some stuff]
|
|
|
|
# git commit
|
|
|
|
# git push
|
|
|
|
|
2021-06-28 17:30:52 +00:00
|
|
|
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):
|
2021-06-28 20:16:43 +00:00
|
|
|
print('%-62s -> %s' % (in_file, out_file))
|
2021-06-28 17:30:52 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2021-06-28 20:16:43 +00:00
|
|
|
for (dirpath, _, filenames) in os.walk(input_directory):
|
|
|
|
for filename in filenames:
|
|
|
|
markdown_filename = os.path.join(dirpath, filename)
|
|
|
|
if not markdown_filename.endswith('.md'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
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'
|
|
|
|
|
|
|
|
out_filename = os.path.basename(markdown_filename).replace('.md', '.html')
|
|
|
|
|
2021-06-29 15:55:25 +00:00
|
|
|
out_dirpath = os.path.join(output_directory, dirpath)
|
|
|
|
out_dirpath = out_dirpath.replace('/content', '', 1)
|
|
|
|
out_fullpath = os.path.join(out_dirpath, out_filename)
|
|
|
|
page_url = out_fullpath.replace('output/', '', 1)
|
|
|
|
if page_url.endswith('index.html'): # strip off index.html
|
|
|
|
page_url = page_url[:-len('index.html')]
|
|
|
|
|
2021-06-28 20:16:43 +00:00
|
|
|
html = markdown.markdown(text, extensions=md_extensions, output_format='html5')
|
|
|
|
output = template.replace('__TITLE_GOES_HERE__', title)
|
|
|
|
output = output.replace('__CONTENT_GOES_HERE__', html)
|
|
|
|
output = output.replace('__PAGE_URL_GOES_HERE__', page_url)
|
|
|
|
|
|
|
|
os.makedirs(out_dirpath, exist_ok=True)
|
|
|
|
print_file(markdown_filename, out_fullpath)
|
|
|
|
out_file = open(out_fullpath, 'w')
|
|
|
|
out_file.write(output)
|
|
|
|
out_file.close()
|
2021-06-28 17:30:52 +00:00
|
|
|
|
|
|
|
# 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('<a href="%s">%s</a><br>' % (f, f))
|
|
|
|
#index.close()
|