Total Complexity | 5 |
Total Lines | 51 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | from html import escape as html_escape |
||
2 | |||
3 | import jinja2 |
||
4 | from jinja2 import Environment, Undefined |
||
5 | from jinja2.filters import do_mark_safe |
||
6 | # noinspection PyUnresolvedReferences |
||
7 | from satcfdi.transform.catalog import CATALOGS |
||
8 | from satcfdi.transform.helpers import iterate as h_iterate |
||
9 | |||
10 | |||
11 | class FacturacionEnvironment(Environment): |
||
12 | @property |
||
13 | def glob(self): |
||
14 | def sub(f): |
||
15 | self.globals[f.__name__] = f |
||
16 | return f |
||
17 | |||
18 | return sub |
||
19 | |||
20 | def __init__(self): |
||
21 | super().__init__( |
||
22 | loader=jinja2.FileSystemLoader(searchpath=['templates']), |
||
23 | autoescape=True, |
||
24 | trim_blocks=True, |
||
25 | lstrip_blocks=True, |
||
26 | undefined=jinja2.StrictUndefined, |
||
27 | ) |
||
28 | |||
29 | @self.glob |
||
30 | def iterate(v): |
||
31 | if isinstance(v, Undefined): |
||
32 | return v |
||
33 | return h_iterate(v) |
||
34 | |||
35 | |||
36 | environment_default = FacturacionEnvironment() |
||
37 | |||
38 | |||
39 | def tag(text, tag): |
||
40 | return '<' + tag + '>' + text + '</' + tag + '>' |
||
41 | |||
42 | |||
43 | def finalize_html(val): |
||
44 | return do_mark_safe( |
||
45 | tag(html_escape(val), "b") |
||
46 | ) |
||
47 | |||
48 | |||
49 | environment_bold_escaped = FacturacionEnvironment() |
||
50 | environment_bold_escaped.finalize = finalize_html |
||
51 |