summaryrefslogtreecommitdiff
path: root/docs/jinja2
blob: a1c89e2badcc8c11e885aba7a1526f81f5cbd0bb (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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'))