Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: cms/converters.py

Issue 29469568: Issue 5331 - Adds has_string() global (Closed)
Patch Set: remove de/global.json change string for en/global.json Created June 21, 2017, 10:25 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | tests/expected_output/global » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # This file is part of the Adblock Plus web scripts, 1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-2017 eyeo GmbH 2 # Copyright (C) 2006-2017 eyeo GmbH
3 # 3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify 4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as 5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation. 6 # published by the Free Software Foundation.
7 # 7 #
8 # Adblock Plus is distributed in the hope that it will be useful, 8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 Converter.__init__(self, *args, **kwargs) 374 Converter.__init__(self, *args, **kwargs)
375 375
376 filters = { 376 filters = {
377 'translate': self.translate, 377 'translate': self.translate,
378 'linkify': self.linkify, 378 'linkify': self.linkify,
379 'toclist': self.toclist, 379 'toclist': self.toclist,
380 } 380 }
381 381
382 globals = { 382 globals = {
383 'get_string': self.get_string, 383 'get_string': self.get_string,
384 'has_string': self.has_string,
384 'get_page_content': self.get_page_content, 385 'get_page_content': self.get_page_content,
385 } 386 }
386 387
387 for dirname, dictionary in [('filters', filters), 388 for dirname, dictionary in [('filters', filters),
388 ('globals', globals)]: 389 ('globals', globals)]:
389 for filename in self._params['source'].list_files(dirname): 390 for filename in self._params['source'].list_files(dirname):
390 root, ext = os.path.splitext(filename) 391 root, ext = os.path.splitext(filename)
391 if ext.lower() != '.py': 392 if ext.lower() != '.py':
392 continue 393 continue
393 394
394 path = os.path.join(dirname, filename) 395 path = os.path.join(dirname, filename)
395 namespace = self._params['source'].exec_file(path) 396 namespace = self._params['source'].exec_file(path)
396 397
397 name = os.path.basename(root) 398 name = os.path.basename(root)
398 try: 399 try:
399 dictionary[name] = namespace[name] 400 dictionary[name] = namespace[name]
400 except KeyError: 401 except KeyError:
401 raise Exception('Expected symbol {} not found' 402 raise Exception('Expected symbol {} not found'
402 ' in {}'.format(name, path)) 403 ' in {}'.format(name, path))
403 404
404 self._env = jinja2.Environment( 405 self._env = jinja2.Environment(
405 loader=SourceTemplateLoader(self._params['source']), 406 loader=SourceTemplateLoader(self._params['source']),
406 autoescape=True) 407 autoescape=True)
407 self._env.filters.update(filters) 408 self._env.filters.update(filters)
408 self._env.globals.update(globals) 409 self._env.globals.update(globals)
409 410
411 def _get_locale_data(self, page):
412 return self._params['source'].read_locale(self._params['locale'], page)
413
410 def get_html(self, source, filename): 414 def get_html(self, source, filename):
411 env = self._env 415 env = self._env
412 code = env.compile(source, None, filename) 416 code = env.compile(source, None, filename)
413 template = jinja2.Template.from_code(env, code, env.globals) 417 template = jinja2.Template.from_code(env, code, env.globals)
414 418
415 try: 419 try:
416 module = template.make_module(self._params) 420 module = template.make_module(self._params)
417 except Exception: 421 except Exception:
418 env.handle_exception() 422 env.handle_exception()
419 423
420 for key, value in module.__dict__.iteritems(): 424 for key, value in module.__dict__.iteritems():
421 if not key.startswith('_'): 425 if not key.startswith('_'):
422 self._params[key] = value 426 self._params[key] = value
423 427
424 result = unicode(module) 428 result = unicode(module)
425 result = self.process_links(result) 429 result = self.process_links(result)
426 return result 430 return result
427 431
428 def translate(self, default, name, comment=None): 432 def translate(self, default, name, comment=None):
429 return jinja2.Markup(self.localize_string( 433 return jinja2.Markup(self.localize_string(
430 self._params['page'], name, default, comment, 434 self._params['page'], name, default, comment,
431 self._params['localedata'], html_escapes 435 self._params['localedata'], html_escapes
432 )) 436 ))
433 437
434 def get_string(self, name, page=None): 438 def get_string(self, name, page=None):
435 if page is None: 439 if page is None:
436 page = self._params['page'] 440 page = self._params['page']
437 441
438 localedata = self._params['source'].read_locale(self._params['locale'], 442 localedata = self._get_locale_data(page)
439 page)
440 default = localedata[name] 443 default = localedata[name]
441 return jinja2.Markup(self.localize_string( 444 return jinja2.Markup(self.localize_string(
442 page, name, default, '', localedata, html_escapes 445 page, name, default, '', localedata, html_escapes
443 )) 446 ))
444 447
448 def has_string(self, name, page=None):
449 if page is None:
450 page = self._params['page']
451
452 localedata = self._get_locale_data(page)
453 return name in localedata
454
445 def get_page_content(self, page, locale=None): 455 def get_page_content(self, page, locale=None):
446 from cms.utils import get_page_params 456 from cms.utils import get_page_params
447 457
448 if locale is None: 458 if locale is None:
449 locale = self._params['locale'] 459 locale = self._params['locale']
450 return get_page_params(self._params['source'], locale, page) 460 return get_page_params(self._params['source'], locale, page)
451 461
452 def linkify(self, page, locale=None, **attrs): 462 def linkify(self, page, locale=None, **attrs):
453 if locale is None: 463 if locale is None:
454 locale = self._params['locale'] 464 locale = self._params['locale']
(...skipping 24 matching lines...) Expand all
479 stack.pop() 489 stack.pop()
480 stack[-1]['subitems'].append(item) 490 stack[-1]['subitems'].append(item)
481 stack.append(item) 491 stack.append(item)
482 return structured 492 return structured
483 493
484 converters = { 494 converters = {
485 'html': RawConverter, 495 'html': RawConverter,
486 'md': MarkdownConverter, 496 'md': MarkdownConverter,
487 'tmpl': TemplateConverter, 497 'tmpl': TemplateConverter,
488 } 498 }
OLDNEW
« no previous file with comments | « no previous file | tests/expected_output/global » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld