Skip to content

Instantly share code, notes, and snippets.

@lpenz lpenz/SConstruct
Created Jan 20, 2019

Embed
What would you like to do?
SConstruct template with an action that renders jinja2 templates using data from an yaml file, while providing a markdown filter
# 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
You can’t perform that action at this time.