add indexing in compile_all

This commit is contained in:
subnixr 2018-07-21 02:01:36 +02:00
parent 4c7839728d
commit 18c77e71f2
2 changed files with 26 additions and 17 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.egg-info
*.pyc
test_site/

View File

@ -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)