Mercurial > libervia-backend
comparison libervia/backend/tools/common/template.py @ 4100:810921c33a47
tools (common/template): add filter to get media types:
Add 2 filters to get main type and subtype of media type. Jinja2 and Nunjucks don't handle
slices in the same way (Python way for Jinja2, JS way for Nunjucks), making it difficult
to retrieve main type of a media from media type. Thoses filters work in both cases.
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 22 Jun 2023 15:49:06 +0200 |
parents | e7ee611fc860 |
children | 81faa85c9cfa |
comparison
equal
deleted
inserted
replaced
4099:11e802089b19 | 4100:810921c33a47 |
---|---|
480 self.env.filters["adv_format"] = self._adv_format | 480 self.env.filters["adv_format"] = self._adv_format |
481 self.env.filters["dict_ext"] = self._dict_ext | 481 self.env.filters["dict_ext"] = self._dict_ext |
482 self.env.filters["highlight"] = self.highlight | 482 self.env.filters["highlight"] = self.highlight |
483 self.env.filters["front_url"] = (self._front_url if front_url_filter is None | 483 self.env.filters["front_url"] = (self._front_url if front_url_filter is None |
484 else front_url_filter) | 484 else front_url_filter) |
485 self.env.filters["media_type_main"] = self.media_type_main | |
486 self.env.filters["media_type_sub"] = self.media_type_sub | |
485 # custom tests | 487 # custom tests |
486 self.env.tests["in_the_past"] = self._in_the_past | 488 self.env.tests["in_the_past"] = self._in_the_past |
487 self.icons_path = os.path.join(host.media_dir, "fonts/fontello/svg") | 489 self.icons_path = os.path.join(host.media_dir, "fonts/fontello/svg") |
488 | 490 |
489 # policies | 491 # policies |
922 else: | 924 else: |
923 lexer = lexers.get_lexer_by_name(lexer_name, **lexer_opts) | 925 lexer = lexers.get_lexer_by_name(lexer_name, **lexer_opts) |
924 formatter = formatters.HtmlFormatter(**html_fmt_opts) | 926 formatter = formatters.HtmlFormatter(**html_fmt_opts) |
925 return safe(pygments.highlight(code, lexer, formatter)) | 927 return safe(pygments.highlight(code, lexer, formatter)) |
926 | 928 |
929 def media_type_main(self, value: Optional[str]) -> Optional[str]: | |
930 """Return main type of a media type""" | |
931 if not value: | |
932 return None | |
933 return value.partition("/")[0] | |
934 | |
935 def media_type_sub(self, value: Optional[str]) -> Optional[str]: | |
936 """Return main type of a media type""" | |
937 if not value: | |
938 return None | |
939 return value.partition("/")[1] | |
940 | |
927 ## custom tests ## | 941 ## custom tests ## |
928 | 942 |
929 def _in_the_past(self, timestamp): | 943 def _in_the_past(self, timestamp): |
930 """check if a date is in the past | 944 """check if a date is in the past |
931 | 945 |