comparison libervia/backend/tools/common/template.py @ 4081:84f6bee6440d

installation: moved from `setup.py` to `pyproject.toml`: - updated installation to use the now standard `pyproject.toml` instead of legacy `setup.py`. `setup.py` and other legacy files have been deleted. - removed outdated README4PACKAGERS - removed pylintrc which has not been correctly reviewed. Linter conf should go to `pyproject.toml` now. - [hatch](https://hatch.pypa.io) is now used as main building tool. However, thanks to the use of standards, other tools can be used too. - update .hgignore - several dependencies version bump, with code update to adapt to changes.
author Goffi <goffi@goffi.org>
date Tue, 06 Jun 2023 16:45:16 +0200
parents 47401850dec6
children f742f28b3934
comparison
equal deleted inserted replaced
4080:0ea6b34f8f18 4081:84f6bee6440d
59 "Missing module jinja2, please install it from http://jinja.pocoo.org or with " 59 "Missing module jinja2, please install it from http://jinja.pocoo.org or with "
60 "pip install jinja2" 60 "pip install jinja2"
61 ) 61 )
62 62
63 from lxml import etree 63 from lxml import etree
64 from jinja2 import Markup as safe 64 from markupsafe import Markup as safe
65 from jinja2 import is_undefined 65 from jinja2 import is_undefined
66 from jinja2 import utils 66 from jinja2 import utils
67 from jinja2 import TemplateNotFound 67 from jinja2 import TemplateNotFound
68 from jinja2 import contextfilter 68 from jinja2 import pass_context
69 from jinja2.loaders import split_template_path 69 from jinja2.loaders import split_template_path
70 70
71 HTML_EXT = ("html", "xhtml") 71 HTML_EXT = ("html", "xhtml")
72 RE_ATTR_ESCAPE = re.compile(r"[^a-z_-]") 72 RE_ATTR_ESCAPE = re.compile(r"[^a-z_-]")
73 SITE_RESERVED_NAMES = ("sat",) 73 SITE_RESERVED_NAMES = ("sat",)
734 734
735 return css_files, css_files_noscript 735 return css_files, css_files_noscript
736 736
737 ## custom filters ## 737 ## custom filters ##
738 738
739 @contextfilter 739 @pass_context
740 def _front_url(self, ctx, relative_url): 740 def _front_url(self, ctx, relative_url):
741 """Get front URL (URL seen by end-user) from a relative URL 741 """Get front URL (URL seen by end-user) from a relative URL
742 742
743 This default method return absolute full path 743 This default method return absolute full path
744 """ 744 """
750 750
751 site_root_dir = self.sites_paths[template_data.site] 751 site_root_dir = self.sites_paths[template_data.site]
752 return os.path.join(site_root_dir, C.TEMPLATE_TPL_DIR, template_data.theme, 752 return os.path.join(site_root_dir, C.TEMPLATE_TPL_DIR, template_data.theme,
753 relative_url) 753 relative_url)
754 754
755 @contextfilter 755 @pass_context
756 def _next_gidx(self, ctx, value): 756 def _next_gidx(self, ctx, value):
757 """Use next current global index as suffix""" 757 """Use next current global index as suffix"""
758 next_ = ctx["gidx"].next(value) 758 next_ = ctx["gidx"].next(value)
759 return value if next_ == 0 else "{}_{}".format(value, next_) 759 return value if next_ == 0 else "{}_{}".format(value, next_)
760 760
761 @contextfilter 761 @pass_context
762 def _cur_gidx(self, ctx, value): 762 def _cur_gidx(self, ctx, value):
763 """Use current current global index as suffix""" 763 """Use current current global index as suffix"""
764 current = ctx["gidx"].current(value) 764 current = ctx["gidx"].current(value)
765 return value if not current else "{}_{}".format(value, current) 765 return value if not current else "{}_{}".format(value, current)
766 766
819 _('ignoring field "{name}": it doesn\'t exists').format(name=name) 819 _('ignoring field "{name}": it doesn\'t exists').format(name=name)
820 ) 820 )
821 continue 821 continue
822 return " ".join(classes) or None 822 return " ".join(classes) or None
823 823
824 @contextfilter 824 @pass_context
825 def _item_filter(self, ctx, item, filters): 825 def _item_filter(self, ctx, item, filters):
826 """return item's value, filtered if suitable 826 """return item's value, filtered if suitable
827 827
828 @param item(object): item to filter 828 @param item(object): item to filter
829 value must have name and value attributes, 829 value must have name and value attributes,
845 filters_args = filter_.get("filters_args") 845 filters_args = filter_.get("filters_args")
846 for idx, f_name in enumerate(filter_.get("filters", [])): 846 for idx, f_name in enumerate(filter_.get("filters", [])):
847 kwargs = filters_args[idx] if filters_args is not None else {} 847 kwargs = filters_args[idx] if filters_args is not None else {}
848 filter_func = self.env.filters[f_name] 848 filter_func = self.env.filters[f_name]
849 try: 849 try:
850 eval_context_filter = filter_func.evalcontextfilter 850 eval_context_filter = filter_func.evalpass_context
851 except AttributeError: 851 except AttributeError:
852 eval_context_filter = False 852 eval_context_filter = False
853 853
854 if eval_context_filter: 854 if eval_context_filter:
855 value = filter_func(ctx.eval_ctx, value, **kwargs) 855 value = filter_func(ctx.eval_ctx, value, **kwargs)