From 4c7839728d8c3825a95e950922d775faaec57ab3 Mon Sep 17 00:00:00 2001 From: User Identifier Date: Sat, 21 Jul 2018 01:38:42 +0200 Subject: [PATCH] Add compile_all command --- src/ciclostile/__init__.py | 38 ++++++++++++++++++++++++++++++++++++++ src/cli.py | 15 ++++++++++++--- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/ciclostile/__init__.py b/src/ciclostile/__init__.py index 4be690c..73a9aec 100644 --- a/src/ciclostile/__init__.py +++ b/src/ciclostile/__init__.py @@ -36,3 +36,41 @@ def index(folder): def compile(template, data): t = jinja2.Template(template) 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) diff --git a/src/cli.py b/src/cli.py index 8e96cdc..b488a9f 100755 --- a/src/cli.py +++ b/src/cli.py @@ -4,7 +4,7 @@ import sys import click import ciclostile -from ciclostile.web import app as webapp +# from ciclostile.web import app as webapp @click.group() @@ -33,10 +33,19 @@ def index(template, output, folder): 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() @click.option("-p", "--port", type=int, default=12345) -@click.argument("input_dir", default=os.getcwd(), type=click.Path(exists=True, - file_okay=False)) +@click.argument("input_dir", default=os.getcwd(), + type=click.Path(exists=True, + file_okay=False)) def app(port, input_dir): print({'port': port, 'input': input_dir})