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 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
232 loader=jinja2.FileSystemLoader(templates_dir), | 232 loader=jinja2.FileSystemLoader(templates_dir), |
233 # Bytecode cache is not concurrency-safe unless pre-cached: | 233 # Bytecode cache is not concurrency-safe unless pre-cached: |
234 # if pre-cached this is read-only, but writing creates a race condition. | 234 # if pre-cached this is read-only, but writing creates a race condition. |
235 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), | 235 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), |
236 keep_trailing_newline=True, # newline-terminate generated files | 236 keep_trailing_newline=True, # newline-terminate generated files |
237 lstrip_blocks=True, # so can indent control flow tags | 237 lstrip_blocks=True, # so can indent control flow tags |
238 trim_blocks=True) | 238 trim_blocks=True) |
239 jinja_env.filters.update({ | 239 jinja_env.filters.update({ |
240 'blink_capitalize': capitalize, | 240 'blink_capitalize': capitalize, |
241 'conditional': conditional_if_endif, | 241 'conditional': conditional_if_endif, |
242 'custom_exposed': custom_exposed_if, | |
haraken
2014/07/30 16:32:03
"custom" is a bit confusing since "custom" implies
Peter Beverloo
2014/07/31 19:02:21
Done.
| |
243 'per_context_enabled': per_context_enabled_if, | |
242 'runtime_enabled': runtime_enabled_if, | 244 'runtime_enabled': runtime_enabled_if, |
243 }) | 245 }) |
244 return jinja_env | 246 return jinja_env |
245 | 247 |
246 | 248 |
249 def generate_indented_conditional(code, conditional): | |
250 # Indent if statement to level of original code | |
251 indent = re.match(' *', code).group(0) | |
252 return ('%sif (%s) {\n' % (indent, conditional) + | |
253 ' %s\n' % '\n '.join(code.splitlines()) + | |
254 '%s}\n' % indent) | |
255 | |
256 | |
247 # [Conditional] | 257 # [Conditional] |
248 def conditional_if_endif(code, conditional_string): | 258 def conditional_if_endif(code, conditional_string): |
249 # Jinja2 filter to generate if/endif directive blocks | 259 # Jinja2 filter to generate if/endif directive blocks |
250 if not conditional_string: | 260 if not conditional_string: |
251 return code | 261 return code |
252 return ('#if %s\n' % conditional_string + | 262 return ('#if %s\n' % conditional_string + |
253 code + | 263 code + |
254 '#endif // %s\n' % conditional_string) | 264 '#endif // %s\n' % conditional_string) |
255 | 265 |
256 | 266 |
267 # [Exposed] | |
268 def custom_exposed_if(code, custom_exposed_rules): | |
269 if not custom_exposed_rules: | |
270 return code | |
271 return generate_indented_conditional(code, 'context && (%s)' % custom_expose d_rules) | |
Jens Widell
2014/07/30 16:48:45
Can 'context' really be null? When does that happe
Peter Beverloo
2014/07/31 19:02:21
I'm not sure. The check existed in installPerConte
| |
272 | |
273 | |
274 # [PerContextEnabled] | |
275 def per_context_enabled_if(code, per_context_enabled_function): | |
276 if not per_context_enabled_function: | |
277 return code | |
278 return generate_indented_conditional(code, '%s(impl->document())' % per_cont ext_enabled_function) | |
279 | |
280 | |
257 # [RuntimeEnabled] | 281 # [RuntimeEnabled] |
258 def runtime_enabled_if(code, runtime_enabled_function_name): | 282 def runtime_enabled_if(code, runtime_enabled_function_name): |
259 if not runtime_enabled_function_name: | 283 if not runtime_enabled_function_name: |
260 return code | 284 return code |
261 # Indent if statement to level of original code | 285 return generate_indented_conditional(code, '%s()' % runtime_enabled_function _name) |
262 indent = re.match(' *', code).group(0) | |
263 return ('%sif (%s()) {\n' % (indent, runtime_enabled_function_name) + | |
264 ' %s\n' % '\n '.join(code.splitlines()) + | |
265 '%s}\n' % indent) | |
266 | 286 |
267 | 287 |
268 ################################################################################ | 288 ################################################################################ |
269 | 289 |
270 def main(argv): | 290 def main(argv): |
271 # If file itself executed, cache templates | 291 # If file itself executed, cache templates |
272 try: | 292 try: |
273 cache_dir = argv[1] | 293 cache_dir = argv[1] |
274 dummy_filename = argv[2] | 294 dummy_filename = argv[2] |
275 except IndexError as err: | 295 except IndexError as err: |
(...skipping 10 matching lines...) Expand all Loading... | |
286 | 306 |
287 # Create a dummy file as output for the build system, | 307 # Create a dummy file as output for the build system, |
288 # since filenames of individual cache files are unpredictable and opaque | 308 # since filenames of individual cache files are unpredictable and opaque |
289 # (they are hashes of the template path, which varies based on environment) | 309 # (they are hashes of the template path, which varies based on environment) |
290 with open(dummy_filename, 'w') as dummy_file: | 310 with open(dummy_filename, 'w') as dummy_file: |
291 pass # |open| creates or touches the file | 311 pass # |open| creates or touches the file |
292 | 312 |
293 | 313 |
294 if __name__ == '__main__': | 314 if __name__ == '__main__': |
295 sys.exit(main(sys.argv)) | 315 sys.exit(main(sys.argv)) |
OLD | NEW |