42
42
del implementation
43
43
44
44
45
- class Language : # pylint: disable=too-few-public-methods
46
- """
47
- Enum-like class that contains languages supported for escaping.
48
- """
49
-
50
- HTML = "html"
51
- """HTML language"""
52
-
53
- XML = "xml"
54
- """XML language"""
55
-
56
- MARKDOWN = "markdown"
57
- """Markdown language"""
58
-
59
-
60
45
class Token : # pylint: disable=too-few-public-methods
61
46
"""Stores a token with its position in a template."""
62
47
@@ -201,59 +186,6 @@ def _replace_amp_or_semi(match: re.Match):
201
186
)
202
187
203
188
204
- def safe_xml (value : Any ) -> str :
205
- """
206
- Encodes unsafe symbols in ``value`` to XML entities and returns the string that can be safely
207
- used in XML.
208
-
209
- Example::
210
-
211
- safe_xml('<a href="https://circuitpython.org/">CircuitPython</a>')
212
- # <a href="https://circuitpython.org/">CircuitPython</a>
213
- """
214
-
215
- return (
216
- str (value )
217
- .replace ("&" , "&" )
218
- .replace ('"' , """ )
219
- .replace ("'" , "'" )
220
- .replace ("<" , "<" )
221
- .replace (">" , ">" )
222
- )
223
-
224
-
225
- def safe_markdown (value : Any ) -> str :
226
- """
227
- Encodes unsafe symbols in ``value`` and returns the string that can be safely used in Markdown.
228
-
229
- Example::
230
-
231
- safe_markdown('[CircuitPython](https://circuitpython.org/)')
232
- # \\ [CircuitPython\\ ]\\ (https://circuitpython.org/\\ )
233
- """
234
-
235
- return (
236
- str (value )
237
- .replace ("_" , "\\ _" )
238
- .replace ("-" , "\\ -" )
239
- .replace ("!" , "\\ !" )
240
- .replace ("(" , "\\ (" )
241
- .replace (")" , "\\ )" )
242
- .replace ("[" , "\\ [" )
243
- .replace ("]" , "\\ ]" )
244
- .replace ("*" , "\\ *" )
245
- .replace ("*" , "\\ *" )
246
- .replace ("&" , "\\ &" )
247
- .replace ("#" , "\\ #" )
248
- .replace ("`" , "\\ `" )
249
- .replace ("+" , "\\ +" )
250
- .replace ("<" , "\\ <" )
251
- .replace (">" , "\\ >" )
252
- .replace ("|" , "\\ |" )
253
- .replace ("~" , "\\ ~" )
254
- )
255
-
256
-
257
189
_EXTENDS_PATTERN = re .compile (r"{% extends '.+?' %}|{% extends \".+?\" %}" )
258
190
_BLOCK_PATTERN = re .compile (r"{% block \w+? %}" )
259
191
_INCLUDE_PATTERN = re .compile (r"{% include '.+?' %}|{% include \".+?\" %}" )
@@ -515,7 +447,6 @@ def _remove_matched_comment(template: str, comment_match: re.Match):
515
447
516
448
def _create_template_rendering_function ( # pylint: disable=,too-many-locals,too-many-branches,too-many-statements
517
449
template : str ,
518
- language : str = Language .HTML ,
519
450
* ,
520
451
trim_blocks : bool = True ,
521
452
lstrip_blocks : bool = True ,
@@ -575,11 +506,11 @@ def _create_template_rendering_function( # pylint: disable=,too-many-locals,too
575
506
else :
576
507
autoescape = True
577
508
578
- # Expression should be escaped with language-specific function
509
+ # Expression should be escaped
579
510
if autoescape :
580
511
function_string += (
581
512
indent * indentation_level
582
- + f"yield safe_ { language . lower () } ({ token .content [3 :- 3 ]} )\n "
513
+ + f"yield safe_html ({ token .content [3 :- 3 ]} )\n "
583
514
)
584
515
# Expression should not be escaped
585
516
else :
@@ -761,22 +692,13 @@ class Template:
761
692
762
693
_template_function : "Generator[str]"
763
694
764
- def __init__ (self , template_string : str , * , language : str = Language . HTML ) -> None :
695
+ def __init__ (self , template_string : str ) -> None :
765
696
"""
766
697
Creates a reusable template from the given template string.
767
698
768
- For better performance, instantiate the template in global scope and reuse it as many times.
769
- If memory is a concern, instantiate the template in a function or method that uses it.
770
-
771
- By default, the template is rendered as HTML. To render it as XML or Markdown, use the
772
- ``language`` parameter.
773
-
774
699
:param str template_string: String containing the template to be rendered
775
- :param str language: Language for autoescaping. Defaults to HTML
776
700
"""
777
- self ._template_function = _create_template_rendering_function (
778
- template_string , language
779
- )
701
+ self ._template_function = _create_template_rendering_function (template_string )
780
702
781
703
def render_iter (
782
704
self , context : dict = None , * , chunk_size : int = None
@@ -826,26 +748,19 @@ class FileTemplate(Template):
826
748
Class that loads a template from a file and allows to rendering it with different contexts.
827
749
"""
828
750
829
- def __init__ (self , template_path : str , * , language : str = Language . HTML ) -> None :
751
+ def __init__ (self , template_path : str ) -> None :
830
752
"""
831
753
Loads a file and creates a reusable template from its contents.
832
754
833
- For better performance, instantiate the template in global scope and reuse it as many times.
834
- If memory is a concern, instantiate the template in a function or method that uses it.
835
-
836
- By default, the template is rendered as HTML. To render it as XML or Markdown, use the
837
- ``language`` parameter.
838
-
839
755
:param str template_path: Path to a file containing the template to be rendered
840
- :param str language: Language for autoescaping. Defaults to HTML
841
756
"""
842
757
843
758
if not _exists_and_is_file (template_path ):
844
759
raise TemplateNotFoundError (template_path )
845
760
846
761
with open (template_path , "rt" , encoding = "utf-8" ) as template_file :
847
762
template_string = template_file .read ()
848
- super ().__init__ (template_string , language = language )
763
+ super ().__init__ (template_string )
849
764
850
765
851
766
_CACHE : "dict[int, Template| FileTemplate]" = {}
@@ -856,7 +771,6 @@ def render_string_iter(
856
771
context : dict = None ,
857
772
* ,
858
773
chunk_size : int = None ,
859
- language : str = Language .HTML ,
860
774
cache : bool = True ,
861
775
):
862
776
"""
@@ -866,7 +780,6 @@ def render_string_iter(
866
780
:param dict context: Dictionary containing the context for the template
867
781
:param int chunk_size: Size of the chunks to be yielded. If ``None``, the generator yields
868
782
the template in chunks sized specifically for the given template
869
- :param str language: Language for autoescaping. Defaults to HTML
870
783
:param bool cache: When ``True``, the template is saved and reused on next calls.
871
784
872
785
Example::
@@ -884,7 +797,7 @@ def render_string_iter(
884
797
_CACHE [key ].render_iter (context or {}, chunk_size ), chunk_size
885
798
)
886
799
887
- template = Template (template_string , language = language )
800
+ template = Template (template_string )
888
801
889
802
if cache :
890
803
_CACHE [key ] = template
@@ -898,15 +811,13 @@ def render_string(
898
811
template_string : str ,
899
812
context : dict = None ,
900
813
* ,
901
- language : str = Language .HTML ,
902
814
cache : bool = True ,
903
815
):
904
816
"""
905
817
Creates a `Template` from the given ``template_string`` and renders it using the provided
906
818
``context``. Returns the rendered output as a string.
907
819
908
820
:param dict context: Dictionary containing the context for the template
909
- :param str language: Language for autoescaping. Defaults to HTML
910
821
:param bool cache: When ``True``, the template is saved and reused on next calls.
911
822
912
823
Example::
@@ -919,7 +830,7 @@ def render_string(
919
830
if cache and key in _CACHE :
920
831
return _CACHE [key ].render (context or {})
921
832
922
- template = Template (template_string , language = language )
833
+ template = Template (template_string )
923
834
924
835
if cache :
925
836
_CACHE [key ] = template
@@ -932,7 +843,6 @@ def render_template_iter(
932
843
context : dict = None ,
933
844
* ,
934
845
chunk_size : int = None ,
935
- language : str = Language .HTML ,
936
846
cache : bool = True ,
937
847
):
938
848
"""
@@ -942,7 +852,6 @@ def render_template_iter(
942
852
:param dict context: Dictionary containing the context for the template
943
853
:param int chunk_size: Size of the chunks to be yielded. If ``None``, the generator yields
944
854
the template in chunks sized specifically for the given template
945
- :param str language: Language for autoescaping. Defaults to HTML
946
855
:param bool cache: When ``True``, the template is saved and reused on next calls.
947
856
948
857
Example::
@@ -960,7 +869,7 @@ def render_template_iter(
960
869
_CACHE [key ].render_iter (context or {}, chunk_size ), chunk_size
961
870
)
962
871
963
- template = FileTemplate (template_path , language = language )
872
+ template = FileTemplate (template_path )
964
873
965
874
if cache :
966
875
_CACHE [key ] = template
@@ -974,15 +883,13 @@ def render_template(
974
883
template_path : str ,
975
884
context : dict = None ,
976
885
* ,
977
- language : str = Language .HTML ,
978
886
cache : bool = True ,
979
887
):
980
888
"""
981
889
Creates a `FileTemplate` from the given ``template_path`` and renders it using the provided
982
890
``context``. Returns the rendered output as a string.
983
891
984
892
:param dict context: Dictionary containing the context for the template
985
- :param str language: Language for autoescaping. Defaults to HTML
986
893
:param bool cache: When ``True``, the template is saved and reused on next calls.
987
894
988
895
Example::
@@ -996,7 +903,7 @@ def render_template(
996
903
if cache and key in _CACHE :
997
904
return _CACHE [key ].render (context or {})
998
905
999
- template = FileTemplate (template_path , language = language )
906
+ template = FileTemplate (template_path )
1000
907
1001
908
if cache :
1002
909
_CACHE [key ] = template
0 commit comments