OLD | NEW |
1 # Copyright (C) 2013 Google Inc. All rights reserved. | 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
2 # | 2 # |
3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
5 # met: | 5 # met: |
6 # | 6 # |
7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
(...skipping 21 matching lines...) Expand all Loading... |
32 _current_dir = os.path.dirname(os.path.realpath(__file__)) | 32 _current_dir = os.path.dirname(os.path.realpath(__file__)) |
33 # jinja2 is in chromium's third_party directory | 33 # jinja2 is in chromium's third_party directory |
34 # Insert at front to override system libraries, and after path[0] == script dir | 34 # Insert at front to override system libraries, and after path[0] == script dir |
35 sys.path.insert(1, os.path.join(_current_dir, *([os.pardir] * 4))) | 35 sys.path.insert(1, os.path.join(_current_dir, *([os.pardir] * 4))) |
36 import jinja2 | 36 import jinja2 |
37 | 37 |
38 | 38 |
39 def apply_template(path_to_template, params, filters=None): | 39 def apply_template(path_to_template, params, filters=None): |
40 dirname, basename = os.path.split(path_to_template) | 40 dirname, basename = os.path.split(path_to_template) |
41 path_to_templates = os.path.join(_current_dir, 'templates') | 41 path_to_templates = os.path.join(_current_dir, 'templates') |
42 jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader([dirname, path
_to_templates]), keep_trailing_newline=True) | 42 jinja_env = jinja2.Environment( |
| 43 loader=jinja2.FileSystemLoader([dirname, path_to_templates]), |
| 44 keep_trailing_newline=True, # newline-terminate generated files |
| 45 lstrip_blocks=True, # so can indent control flow tags |
| 46 trim_blocks=True) # so don't need {%- -%} everywhere |
43 if filters: | 47 if filters: |
44 jinja_env.filters.update(filters) | 48 jinja_env.filters.update(filters) |
45 template = jinja_env.get_template(basename) | 49 template = jinja_env.get_template(basename) |
46 return template.render(params) | 50 return template.render(params) |
47 | 51 |
48 | 52 |
49 def use_jinja(template_file_name, filters=None): | 53 def use_jinja(template_file_name, filters=None): |
50 def real_decorator(generator): | 54 def real_decorator(generator): |
51 def generator_internal(*args, **kwargs): | 55 def generator_internal(*args, **kwargs): |
52 parameters = generator(*args, **kwargs) | 56 parameters = generator(*args, **kwargs) |
53 return apply_template(template_file_name, parameters, filters=filter
s) | 57 return apply_template(template_file_name, parameters, filters=filter
s) |
54 generator_internal.func_name = generator.func_name | 58 generator_internal.func_name = generator.func_name |
55 return generator_internal | 59 return generator_internal |
56 return real_decorator | 60 return real_decorator |
OLD | NEW |