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 'exposed': exposed_if, | |
243 'per_context_enabled': per_context_enabled_if, | |
244 'runtime_enabled': runtime_enabled_if, | 242 'runtime_enabled': runtime_enabled_if, |
245 }) | 243 }) |
246 return jinja_env | 244 return jinja_env |
247 | 245 |
248 | 246 |
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 | |
257 # [Conditional] | 247 # [Conditional] |
258 def conditional_if_endif(code, conditional_string): | 248 def conditional_if_endif(code, conditional_string): |
259 # Jinja2 filter to generate if/endif directive blocks | 249 # Jinja2 filter to generate if/endif directive blocks |
260 if not conditional_string: | 250 if not conditional_string: |
261 return code | 251 return code |
262 return ('#if %s\n' % conditional_string + | 252 return ('#if %s\n' % conditional_string + |
263 code + | 253 code + |
264 '#endif // %s\n' % conditional_string) | 254 '#endif // %s\n' % conditional_string) |
265 | 255 |
266 | 256 |
267 # [Exposed] | |
268 def exposed_if(code, exposed_test): | |
269 if not exposed_test: | |
270 return code | |
271 return generate_indented_conditional(code, 'context && (%s)' % exposed_test) | |
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, 'context && context->isDocument()
&& %s(toDocument(context))' % per_context_enabled_function) | |
279 | |
280 | |
281 # [RuntimeEnabled] | 257 # [RuntimeEnabled] |
282 def runtime_enabled_if(code, runtime_enabled_function_name): | 258 def runtime_enabled_if(code, runtime_enabled_function_name): |
283 if not runtime_enabled_function_name: | 259 if not runtime_enabled_function_name: |
284 return code | 260 return code |
285 return generate_indented_conditional(code, '%s()' % runtime_enabled_function
_name) | 261 # Indent if statement to level of original code |
| 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) |
286 | 266 |
287 | 267 |
288 ################################################################################ | 268 ################################################################################ |
289 | 269 |
290 def main(argv): | 270 def main(argv): |
291 # If file itself executed, cache templates | 271 # If file itself executed, cache templates |
292 try: | 272 try: |
293 cache_dir = argv[1] | 273 cache_dir = argv[1] |
294 dummy_filename = argv[2] | 274 dummy_filename = argv[2] |
295 except IndexError as err: | 275 except IndexError as err: |
(...skipping 10 matching lines...) Expand all Loading... |
306 | 286 |
307 # Create a dummy file as output for the build system, | 287 # Create a dummy file as output for the build system, |
308 # since filenames of individual cache files are unpredictable and opaque | 288 # since filenames of individual cache files are unpredictable and opaque |
309 # (they are hashes of the template path, which varies based on environment) | 289 # (they are hashes of the template path, which varies based on environment) |
310 with open(dummy_filename, 'w') as dummy_file: | 290 with open(dummy_filename, 'w') as dummy_file: |
311 pass # |open| creates or touches the file | 291 pass # |open| creates or touches the file |
312 | 292 |
313 | 293 |
314 if __name__ == '__main__': | 294 if __name__ == '__main__': |
315 sys.exit(main(sys.argv)) | 295 sys.exit(main(sys.argv)) |
OLD | NEW |