From c35db75eba1f67c6d6bbca9fefe7aaefb6b6d6e9 Mon Sep 17 00:00:00 2001 From: Peter Ward Date: Sun, 29 Jul 2012 23:43:29 +1000 Subject: Add start of tutorial. --- docs/jinja2 | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100755 docs/jinja2 (limited to 'docs/jinja2') 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')) -- cgit v1.2.3