forked from unit/ciclostile
Add compile_all command
This commit is contained in:
parent
5302b7dc78
commit
4c7839728d
|
@ -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)
|
||||
|
|
15
src/cli.py
15
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})
|
||||
|
|
Loading…
Reference in New Issue
Block a user