Mercurial > libervia-backend
comparison src/tools/common/template.py @ 2454:06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 30 Nov 2017 21:00:24 +0100 |
parents | 84e84a46b014 |
children | bb0bbcc80fc8 |
comparison
equal
deleted
inserted
replaced
2453:84e84a46b014 | 2454:06ff33052354 |
---|---|
30 import re | 30 import re |
31 from babel import support | 31 from babel import support |
32 from babel import Locale | 32 from babel import Locale |
33 from babel.core import UnknownLocaleError | 33 from babel.core import UnknownLocaleError |
34 from babel import dates | 34 from babel import dates |
35 import pygments | |
36 from pygments import lexers | |
37 from pygments import formatters | |
35 try: | 38 try: |
36 import sat_templates | 39 import sat_templates |
37 except ImportError: | 40 except ImportError: |
38 raise exceptions.MissingModule(u'sat_templates module is not available, please install it or check your path to use template engine') | 41 raise exceptions.MissingModule(u'sat_templates module is not available, please install it or check your path to use template engine') |
39 else: | 42 else: |
206 self.env.filters['xmlui_class'] = self._xmlui_class | 209 self.env.filters['xmlui_class'] = self._xmlui_class |
207 self.env.filters['attr_escape'] = self.attr_escape | 210 self.env.filters['attr_escape'] = self.attr_escape |
208 self.env.filters['item_filter'] = self._item_filter | 211 self.env.filters['item_filter'] = self._item_filter |
209 self.env.filters['adv_format'] = self._adv_format | 212 self.env.filters['adv_format'] = self._adv_format |
210 self.env.filters['dict_ext'] = self._dict_ext | 213 self.env.filters['dict_ext'] = self._dict_ext |
214 self.env.filters['highlight'] = self.highlight | |
211 # custom tests | 215 # custom tests |
212 self.env.tests['in_the_past'] = self._in_the_past | 216 self.env.tests['in_the_past'] = self._in_the_past |
213 | 217 |
214 def installTranslations(self): | 218 def installTranslations(self): |
215 i18n_dir = os.path.join(self.base_dir, 'i18n') | 219 i18n_dir = os.path.join(self.base_dir, 'i18n') |
509 extra_dict = extra_dict.get(key, {}) | 513 extra_dict = extra_dict.get(key, {}) |
510 ret = source_dict.copy() | 514 ret = source_dict.copy() |
511 ret.update(extra_dict) | 515 ret.update(extra_dict) |
512 return ret | 516 return ret |
513 | 517 |
518 def highlight(self, code, lexer_name=None, lexer_opts=None, html_fmt_opts=None): | |
519 """Do syntax highlighting on code | |
520 | |
521 under the hood, pygments is used, check its documentation for options possible values | |
522 @param code(unicode): code or markup to highlight | |
523 @param lexer_name(unicode, None): name of the lexer to use | |
524 None to autodetect it | |
525 @param html_fmt_opts(dict, None): kword arguments to use for HtmlFormatter | |
526 @return (unicode): HTML markup with highlight classes | |
527 """ | |
528 if lexer_opts is None: | |
529 lexer_opts = {} | |
530 if html_fmt_opts is None: | |
531 html_fmt_opts = {} | |
532 if lexer_name is None: | |
533 lexer = lexers.guess_lexer(code, **lexer_opts) | |
534 else: | |
535 lexer = lexers.get_lexer_by_name(lexer_name, **lexer_opts) | |
536 formatter = formatters.HtmlFormatter(**html_fmt_opts) | |
537 return safe(pygments.highlight(code, lexer, formatter)) | |
538 | |
514 ## custom tests ## | 539 ## custom tests ## |
515 | 540 |
516 def _in_the_past(self, timestamp): | 541 def _in_the_past(self, timestamp): |
517 """check if a date is in the past | 542 """check if a date is in the past |
518 | 543 |