Created
January 20, 2019 21:54
-
-
Save lpenz/c0fa0344f32eea78e6e25c521a6bdf24 to your computer and use it in GitHub Desktop.
SConstruct template with an action that renders jinja2 templates using data from an yaml file, while providing a markdown filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SConstruct template with an action that renders jinja2 templates | |
# using data from an yaml file, while providing a markdown filter. | |
import yaml | |
import jinja2 | |
import markdown | |
if False: | |
Environment = None | |
def markdown2html_filter(md): | |
html = markdown.markdown(md, output_format='html5') | |
return html | |
def render(target, source, env): | |
assert len(target) == 1 | |
data = {} | |
jinjafiles = [] | |
for filename in [str(s) for s in source]: | |
if filename.endswith('.yaml'): | |
with open(filename) as fd: | |
d = yaml.load(fd) | |
data.update(d) | |
elif filename.endswith('.j2'): | |
jinjafiles.append(filename) | |
assert len(jinjafiles) == 1 | |
env = jinja2.Environment(loader=jinja2.FileSystemLoader('.')) | |
env.filters['markdown'] = markdown2html_filter | |
template = env.get_template(jinjafiles[0]) | |
with open(str(target[0]), 'w') as fdout: | |
template.stream(data).dump(fdout) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment