99 lines
2.1 KiB
Python
99 lines
2.1 KiB
Python
import os
|
|
import sys
|
|
from pathlib import Path
|
|
from copy import deepcopy
|
|
|
|
import markdown
|
|
|
|
import chevron
|
|
|
|
|
|
SOURCE_ROOT = os.path.join(
|
|
os.path.abspath(os.path.join(os.getcwd(), os.pardir)),
|
|
'content')
|
|
|
|
DESTINATION_ROOT = '/var/www/creativespirit.tech/html'
|
|
|
|
TEMPLATE_FILE = '../templates/page_template.mustache'
|
|
|
|
if len(sys.argv) == 4:
|
|
SOURCE_ROOT = sys.argv[1]
|
|
DESTINATION_ROOT = sys.argv[2]
|
|
TEMPLATE_FILE = sys.argv[3]
|
|
|
|
|
|
def process_content_dir(root, nodes):
|
|
# More on listing files in a dir:
|
|
# https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory
|
|
|
|
base_path = os.path.join(root, *nodes)
|
|
|
|
for entry in os.listdir(base_path):
|
|
|
|
if os.path.isfile(os.path.join(base_path, entry)):
|
|
process_single_file(root, nodes, entry)
|
|
|
|
else:
|
|
new_nodes = deepcopy(nodes)
|
|
new_nodes.append(entry)
|
|
process_content_dir(root, new_nodes)
|
|
|
|
|
|
def process_single_file(root, nodes, file_name):
|
|
save_html(
|
|
DESTINATION_ROOT,
|
|
nodes,
|
|
get_html_name(file_name),
|
|
render_template(
|
|
preprocess_markdown(
|
|
get_markdown(
|
|
os.path.join(root, *nodes, file_name)))))
|
|
|
|
|
|
def preprocess_markdown(text):
|
|
# Points links to .html
|
|
return text.replace(
|
|
'.md)', '.html)'
|
|
).replace(
|
|
'.md"', '.html"'
|
|
)
|
|
|
|
|
|
def get_markdown(file_path):
|
|
with open(file_path, 'r') as f:
|
|
text = f.read()
|
|
return markdown.markdown(text)
|
|
|
|
|
|
def render_template(body_text):
|
|
with open(TEMPLATE_FILE, 'r') as f:
|
|
template = f.read()
|
|
|
|
args = {
|
|
'template': template,
|
|
'data': {
|
|
'body_text': body_text,
|
|
},
|
|
}
|
|
|
|
html = chevron.render(**args)
|
|
# print(html)
|
|
return html
|
|
|
|
|
|
def get_html_name(file):
|
|
return f'{Path(file).stem}.html'
|
|
|
|
|
|
def save_html(root, nodes, file_name, html):
|
|
with open(
|
|
os.path.join(
|
|
root,
|
|
*nodes,
|
|
file_name),
|
|
'w') as f:
|
|
f.write(html)
|
|
|
|
|
|
process_content_dir(SOURCE_ROOT, [])
|