From 18c77e71f26241a3f9919cbb0be839cf938d9a36 Mon Sep 17 00:00:00 2001 From: subnixr Date: Sat, 21 Jul 2018 02:01:36 +0200 Subject: [PATCH] add indexing in compile_all --- .gitignore | 1 + src/ciclostile/__init__.py | 42 +++++++++++++++++++++++--------------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 66dbf51..3752773 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.egg-info *.pyc +test_site/ diff --git a/src/ciclostile/__init__.py b/src/ciclostile/__init__.py index 73a9aec..9094c75 100644 --- a/src/ciclostile/__init__.py +++ b/src/ciclostile/__init__.py @@ -39,29 +39,30 @@ def compile(template, 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')] + template_path = os.path.join(root_path, 'templates') + target_path = os.path.join(root_path, 'target') data = {'pages': {}} - for fname in files: - full_path = os.path.join(md_path, fname) - name, _ = os.path.splitext(fname) + templates = {} + + md_files = [f + for f in os.listdir(md_path) + if f.endswith('.md')] + template_files = [f + for f in os.listdir(template_path) + if f.endswith('.html')] + + for md_fname in md_files: + full_path = os.path.join(md_path, md_fname) + name, _ = os.path.splitext(md_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 file_name in template_files: + full_path = os.path.join(template_path, file_name) + name, _ = os.path.splitext(file_name) + templates[name] = full_path for page_name, page in data['pages'].items(): t_name = page.get('template', 'default') @@ -74,3 +75,10 @@ def compile_all(root_path): with open(out_path, 'w') as f: f.write(out) + + with open(templates['index']) as fh: + index_template = jinja2.Template(fh.read()) + out = index_template.render(**data) + out_path = os.path.join(target_path, 'index.html') + with open(out_path, "w") as f: + f.write(out)