Skip to content
Snippets Groups Projects
Commit 6bddeecf authored by Josh Borrow's avatar Josh Borrow
Browse files

Upated compiler to use new program flow

parent ebb3dcff
No related branches found
No related tags found
No related merge requests found
""" Short compilation script from the website (essentially just stitches together a bunch of jinja templates) """
""" Short compilation script from the website
(essentially just stitches together a bunch of jinja templates)
Created 16-08-2017 by Josh Borrow (joshua.borrow@durham.ac.uk)
"""
import jinja2
import yaml
import os
def get_from_filesystem(template_names, template_dir="."):
""" Takes a list of template names and returns a list of template objects
that are ready for rendering. """
def get_from_filesystem(template_name, template_dir="."):
""" Grab a template from the filesystem and return the template object ready for rendering """
template_loader = jinja2.FileSystemLoader(searchpath=template_dir)
template_env = jinja2.Environment(loader=template_loader)
return template_env.get_template(template_name)
return list(map(template_env.get_template, template_names))
def render_template(template, data_dir="../data", args={}):
""" Args is a dictionary of the local variables required for
rendering the template. """
args = args + global_variables(template, data_dir)
return template.render(**args)
def process_navbar(raw, active):
""" Processes the navbar from our favourite friends over at global.yaml """
output = []
for key, item in raw["navbar"].iteritems():
classes = []
def compile_pubs(template_name="pubs.jinja", template_dir=".", data_dir="../data"):
""" Render the pubs page """
template = get_from_filesystem(template_name, template_dir)
if key != len(raw["navbar"])-1: # Lines between items are handled
classes.append("rightborder") # by borders on the right of items
if item[1] == active: # If current template
classes.append("active")
with open("{}/pubs.yaml".format(data_dir), 'r') as f:
cards = yaml.load(f)
output.append([item[0], item[1], " ".join(classes)])
return template.render(cards=cards)
raw["navbar"] = output
return raw
def compile_home(template_name="index.jinja", template_dir=".", data_dir="../data"):
""" Render the homepage """
template = get_from_filesystem(template_name, template_dir)
def global_variables(active, data_dir="../data"):
""" Grabs the global variables from the data directory and sticks them into
a dicitonary ready for template rendering.
Active is the name of the current page's template and is used in the
production of the processed navbar. """
return template.render()
with open("{}/global.yaml".format(data_dir), "r") as f:
raw = yaml.load(f) # We need to process the navbar.
return process_navbar(raw, active)
def render(render_pages, data_dir, template_dir, output_dir):
""" Main rendering function for all of the pages. """
output = [render_template(page, data_dir) for page in render_pages]
# Save the files into the output_dir
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for page, data in zip(render_pages, output):
with open("{}/{}".format(output_dir, page), "r") as f:
f.write(data)
return
if __name__ == "__main__":
print(compile_home())
render_pages = [
"index.html",
"pubs.html"
]
render(
render_pages,
data_dir="../data",
template_dir=".",
output_dir="./compiled",
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment