diff options
author | Peter Ward <peteraward@gmail.com> | 2012-07-29 23:43:29 +1000 |
---|---|---|
committer | Peter Ward <peteraward@gmail.com> | 2012-07-29 23:43:29 +1000 |
commit | c35db75eba1f67c6d6bbca9fefe7aaefb6b6d6e9 (patch) | |
tree | d32b851da74ed9b053248351f308a734b8c2a8b6 /docs/jinja2 | |
parent | b62e033801895ad633f702685e50cf367ae0adee (diff) |
Add start of tutorial.
Diffstat (limited to 'docs/jinja2')
-rwxr-xr-x | docs/jinja2 | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/docs/jinja2 b/docs/jinja2 new file mode 100755 index 0000000..a1c89e2 --- /dev/null +++ b/docs/jinja2 @@ -0,0 +1,51 @@ +#!/usr/bin/env python + +import argparse +import json +from os import path +import sys + +from jinja2 import Environment, FileSystemLoader + +parser = argparse.ArgumentParser() +parser.add_argument('--latex', action='store_true') +parser.add_argument('template', nargs='?') +parser.add_argument('data', nargs='?') + +args = parser.parse_args() + +if args.latex: + env = Environment( + block_start_string='%%', + block_end_string='%%', + variable_start_string='<', + variable_end_string='>', + comment_start_string='###', + comment_end_string='###', + ) +else: + env = Environment() + +if args.template: + dirname, basename = path.split(args.template) + + env.loader = FileSystemLoader(dirname) + template = env.get_template(basename) + + if args.data: + with open(args.data, 'rb') as f: + data = json.load(f) + + else: + data = json.load(sys.stdin) + +else: + source = sys.stdin.read() + template = env.from_string(source.decode('utf-8')) + data = {} + + env.loader = FileSystemLoader('.') + +output = template.render(data) + +sys.stdout.write(output.encode('utf-8')) |