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 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 # Bytecode cache is not concurrency-safe unless pre-cached: | 380 # Bytecode cache is not concurrency-safe unless pre-cached: |
381 # if pre-cached this is read-only, but writing creates a race condition. | 381 # if pre-cached this is read-only, but writing creates a race condition. |
382 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), | 382 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), |
383 keep_trailing_newline=True, # newline-terminate generated files | 383 keep_trailing_newline=True, # newline-terminate generated files |
384 lstrip_blocks=True, # so can indent control flow tags | 384 lstrip_blocks=True, # so can indent control flow tags |
385 trim_blocks=True) | 385 trim_blocks=True) |
386 jinja_env.filters.update({ | 386 jinja_env.filters.update({ |
387 'blink_capitalize': capitalize, | 387 'blink_capitalize': capitalize, |
388 'conditional': conditional_if_endif, | 388 'conditional': conditional_if_endif, |
389 'exposed': exposed_if, | 389 'exposed': exposed_if, |
390 'per_context_enabled': per_context_enabled_if, | |
391 'runtime_enabled': runtime_enabled_if, | 390 'runtime_enabled': runtime_enabled_if, |
392 }) | 391 }) |
393 return jinja_env | 392 return jinja_env |
394 | 393 |
395 | 394 |
396 def generate_indented_conditional(code, conditional): | 395 def generate_indented_conditional(code, conditional): |
397 # Indent if statement to level of original code | 396 # Indent if statement to level of original code |
398 indent = re.match(' *', code).group(0) | 397 indent = re.match(' *', code).group(0) |
399 return ('%sif (%s) {\n' % (indent, conditional) + | 398 return ('%sif (%s) {\n' % (indent, conditional) + |
400 ' %s\n' % '\n '.join(code.splitlines()) + | 399 ' %s\n' % '\n '.join(code.splitlines()) + |
(...skipping 10 matching lines...) Expand all Loading... |
411 '#endif // %s\n' % conditional_string) | 410 '#endif // %s\n' % conditional_string) |
412 | 411 |
413 | 412 |
414 # [Exposed] | 413 # [Exposed] |
415 def exposed_if(code, exposed_test): | 414 def exposed_if(code, exposed_test): |
416 if not exposed_test: | 415 if not exposed_test: |
417 return code | 416 return code |
418 return generate_indented_conditional(code, 'context && (%s)' % exposed_test) | 417 return generate_indented_conditional(code, 'context && (%s)' % exposed_test) |
419 | 418 |
420 | 419 |
421 # [PerContextEnabled] | |
422 def per_context_enabled_if(code, per_context_enabled_function): | |
423 if not per_context_enabled_function: | |
424 return code | |
425 return generate_indented_conditional(code, 'context && context->isDocument()
&& %s(toDocument(context))' % per_context_enabled_function) | |
426 | |
427 | |
428 # [RuntimeEnabled] | 420 # [RuntimeEnabled] |
429 def runtime_enabled_if(code, runtime_enabled_function_name): | 421 def runtime_enabled_if(code, runtime_enabled_function_name): |
430 if not runtime_enabled_function_name: | 422 if not runtime_enabled_function_name: |
431 return code | 423 return code |
432 return generate_indented_conditional(code, '%s()' % runtime_enabled_function
_name) | 424 return generate_indented_conditional(code, '%s()' % runtime_enabled_function
_name) |
433 | 425 |
434 | 426 |
435 ################################################################################ | 427 ################################################################################ |
436 | 428 |
437 def main(argv): | 429 def main(argv): |
(...skipping 15 matching lines...) Expand all Loading... |
453 | 445 |
454 # Create a dummy file as output for the build system, | 446 # Create a dummy file as output for the build system, |
455 # since filenames of individual cache files are unpredictable and opaque | 447 # since filenames of individual cache files are unpredictable and opaque |
456 # (they are hashes of the template path, which varies based on environment) | 448 # (they are hashes of the template path, which varies based on environment) |
457 with open(dummy_filename, 'w') as dummy_file: | 449 with open(dummy_filename, 'w') as dummy_file: |
458 pass # |open| creates or touches the file | 450 pass # |open| creates or touches the file |
459 | 451 |
460 | 452 |
461 if __name__ == '__main__': | 453 if __name__ == '__main__': |
462 sys.exit(main(sys.argv)) | 454 sys.exit(main(sys.argv)) |
OLD | NEW |