Add compile_all command

This commit is contained in:
uid 2018-07-21 01:38:42 +02:00
parent 5302b7dc78
commit 4c7839728d
2 changed files with 50 additions and 3 deletions

View File

@ -36,3 +36,41 @@ def index(folder):
def compile(template, data): def compile(template, data):
t = jinja2.Template(template) t = jinja2.Template(template)
return t.render(**data) return t.render(**data)
def compile_all(root_path):
print('Compiling', root_path)
md_path = os.path.join(root_path, 'markdown')
files = [f
for f in os.listdir(md_path)
if f.endswith('.md')]
data = {'pages': {}}
for fname in files:
full_path = os.path.join(md_path, fname)
name, _ = os.path.splitext(fname)
data['pages'][name] = parse_file(full_path)
template_path = os.path.join(root_path, 'templates')
templates = {}
for file_name in os.listdir(template_path):
name, ext = os.path.splitext(file_name)
if ext == '.html':
templates[name] = os.path.join(template_path, file_name)
target_path = os.path.join(root_path, 'target')
for page_name, page in data['pages'].items():
t_name = page.get('template', 'default')
with open(templates[t_name]) as tf:
t = jinja2.Template(tf.read())
out_path = os.path.join(target_path, page_name + '.html')
out = t.render(**data['pages'][page_name])
with open(out_path, 'w') as f:
f.write(out)

View File

@ -4,7 +4,7 @@ import sys
import click import click
import ciclostile import ciclostile
from ciclostile.web import app as webapp # from ciclostile.web import app as webapp
@click.group() @click.group()
@ -33,9 +33,18 @@ def index(template, output, folder):
output.write(html) output.write(html)
@cli.command()
@click.argument("input_dir", default=os.getcwd(),
type=click.Path(exists=True,
file_okay=False))
def compile_all(input_dir):
ciclostile.compile_all(input_dir)
@cli.command() @cli.command()
@click.option("-p", "--port", type=int, default=12345) @click.option("-p", "--port", type=int, default=12345)
@click.argument("input_dir", default=os.getcwd(), type=click.Path(exists=True, @click.argument("input_dir", default=os.getcwd(),
type=click.Path(exists=True,
file_okay=False)) file_okay=False))
def app(port, input_dir): def app(port, input_dir):
print({'port': port, print({'port': port,