# HG changeset patch # User Goffi # Date 1687441746 -7200 # Node ID 810921c33a47411fb267d21951e6a00bd55611b0 # Parent 11e802089b19c34ec5a370b9c11e3d244c66af8d 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. diff -r 11e802089b19 -r 810921c33a47 libervia/backend/tools/common/template.py --- a/libervia/backend/tools/common/template.py Thu Jun 22 15:47:17 2023 +0200 +++ b/libervia/backend/tools/common/template.py Thu Jun 22 15:49:06 2023 +0200 @@ -482,6 +482,8 @@ self.env.filters["highlight"] = self.highlight self.env.filters["front_url"] = (self._front_url if front_url_filter is None else front_url_filter) + self.env.filters["media_type_main"] = self.media_type_main + self.env.filters["media_type_sub"] = self.media_type_sub # custom tests self.env.tests["in_the_past"] = self._in_the_past self.icons_path = os.path.join(host.media_dir, "fonts/fontello/svg") @@ -924,6 +926,18 @@ formatter = formatters.HtmlFormatter(**html_fmt_opts) return safe(pygments.highlight(code, lexer, formatter)) + def media_type_main(self, value: Optional[str]) -> Optional[str]: + """Return main type of a media type""" + if not value: + return None + return value.partition("/")[0] + + def media_type_sub(self, value: Optional[str]) -> Optional[str]: + """Return main type of a media type""" + if not value: + return None + return value.partition("/")[1] + ## custom tests ## def _in_the_past(self, timestamp):