4242 del implementation
4343
4444
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-
6045class Token : # pylint: disable=too-few-public-methods
6146 """Stores a token with its position in a template."""
6247
@@ -201,59 +186,6 @@ def _replace_amp_or_semi(match: re.Match):
201186 )
202187
203188
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-
257189_EXTENDS_PATTERN = re .compile (r"{% extends '.+?' %}|{% extends \".+?\" %}" )
258190_BLOCK_PATTERN = re .compile (r"{% block \w+? %}" )
259191_INCLUDE_PATTERN = re .compile (r"{% include '.+?' %}|{% include \".+?\" %}" )
@@ -515,7 +447,6 @@ def _remove_matched_comment(template: str, comment_match: re.Match):
515447
516448def _create_template_rendering_function ( # pylint: disable=,too-many-locals,too-many-branches,too-many-statements
517449 template : str ,
518- language : str = Language .HTML ,
519450 * ,
520451 trim_blocks : bool = True ,
521452 lstrip_blocks : bool = True ,
@@ -575,11 +506,11 @@ def _create_template_rendering_function( # pylint: disable=,too-many-locals,too
575506 else :
576507 autoescape = True
577508
578- # Expression should be escaped with language-specific function
509+ # Expression should be escaped
579510 if autoescape :
580511 function_string += (
581512 indent * indentation_level
582- + f"yield safe_ { language . lower () } ({ token .content [3 :- 3 ]} )\n "
513+ + f"yield safe_html ({ token .content [3 :- 3 ]} )\n "
583514 )
584515 # Expression should not be escaped
585516 else :
@@ -761,22 +692,13 @@ class Template:
761692
762693 _template_function : "Generator[str]"
763694
764- def __init__ (self , template_string : str , * , language : str = Language . HTML ) -> None :
695+ def __init__ (self , template_string : str ) -> None :
765696 """
766697 Creates a reusable template from the given template string.
767698
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-
774699 :param str template_string: String containing the template to be rendered
775- :param str language: Language for autoescaping. Defaults to HTML
776700 """
777- self ._template_function = _create_template_rendering_function (
778- template_string , language
779- )
701+ self ._template_function = _create_template_rendering_function (template_string )
780702
781703 def render_iter (
782704 self , context : dict = None , * , chunk_size : int = None
@@ -826,26 +748,19 @@ class FileTemplate(Template):
826748 Class that loads a template from a file and allows to rendering it with different contexts.
827749 """
828750
829- def __init__ (self , template_path : str , * , language : str = Language . HTML ) -> None :
751+ def __init__ (self , template_path : str ) -> None :
830752 """
831753 Loads a file and creates a reusable template from its contents.
832754
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-
839755 :param str template_path: Path to a file containing the template to be rendered
840- :param str language: Language for autoescaping. Defaults to HTML
841756 """
842757
843758 if not _exists_and_is_file (template_path ):
844759 raise TemplateNotFoundError (template_path )
845760
846761 with open (template_path , "rt" , encoding = "utf-8" ) as template_file :
847762 template_string = template_file .read ()
848- super ().__init__ (template_string , language = language )
763+ super ().__init__ (template_string )
849764
850765
851766_CACHE : "dict[int, Template| FileTemplate]" = {}
@@ -856,7 +771,6 @@ def render_string_iter(
856771 context : dict = None ,
857772 * ,
858773 chunk_size : int = None ,
859- language : str = Language .HTML ,
860774 cache : bool = True ,
861775):
862776 """
@@ -866,7 +780,6 @@ def render_string_iter(
866780 :param dict context: Dictionary containing the context for the template
867781 :param int chunk_size: Size of the chunks to be yielded. If ``None``, the generator yields
868782 the template in chunks sized specifically for the given template
869- :param str language: Language for autoescaping. Defaults to HTML
870783 :param bool cache: When ``True``, the template is saved and reused on next calls.
871784
872785 Example::
@@ -884,7 +797,7 @@ def render_string_iter(
884797 _CACHE [key ].render_iter (context or {}, chunk_size ), chunk_size
885798 )
886799
887- template = Template (template_string , language = language )
800+ template = Template (template_string )
888801
889802 if cache :
890803 _CACHE [key ] = template
@@ -898,15 +811,13 @@ def render_string(
898811 template_string : str ,
899812 context : dict = None ,
900813 * ,
901- language : str = Language .HTML ,
902814 cache : bool = True ,
903815):
904816 """
905817 Creates a `Template` from the given ``template_string`` and renders it using the provided
906818 ``context``. Returns the rendered output as a string.
907819
908820 :param dict context: Dictionary containing the context for the template
909- :param str language: Language for autoescaping. Defaults to HTML
910821 :param bool cache: When ``True``, the template is saved and reused on next calls.
911822
912823 Example::
@@ -919,7 +830,7 @@ def render_string(
919830 if cache and key in _CACHE :
920831 return _CACHE [key ].render (context or {})
921832
922- template = Template (template_string , language = language )
833+ template = Template (template_string )
923834
924835 if cache :
925836 _CACHE [key ] = template
@@ -932,7 +843,6 @@ def render_template_iter(
932843 context : dict = None ,
933844 * ,
934845 chunk_size : int = None ,
935- language : str = Language .HTML ,
936846 cache : bool = True ,
937847):
938848 """
@@ -942,7 +852,6 @@ def render_template_iter(
942852 :param dict context: Dictionary containing the context for the template
943853 :param int chunk_size: Size of the chunks to be yielded. If ``None``, the generator yields
944854 the template in chunks sized specifically for the given template
945- :param str language: Language for autoescaping. Defaults to HTML
946855 :param bool cache: When ``True``, the template is saved and reused on next calls.
947856
948857 Example::
@@ -960,7 +869,7 @@ def render_template_iter(
960869 _CACHE [key ].render_iter (context or {}, chunk_size ), chunk_size
961870 )
962871
963- template = FileTemplate (template_path , language = language )
872+ template = FileTemplate (template_path )
964873
965874 if cache :
966875 _CACHE [key ] = template
@@ -974,15 +883,13 @@ def render_template(
974883 template_path : str ,
975884 context : dict = None ,
976885 * ,
977- language : str = Language .HTML ,
978886 cache : bool = True ,
979887):
980888 """
981889 Creates a `FileTemplate` from the given ``template_path`` and renders it using the provided
982890 ``context``. Returns the rendered output as a string.
983891
984892 :param dict context: Dictionary containing the context for the template
985- :param str language: Language for autoescaping. Defaults to HTML
986893 :param bool cache: When ``True``, the template is saved and reused on next calls.
987894
988895 Example::
@@ -996,7 +903,7 @@ def render_template(
996903 if cache and key in _CACHE :
997904 return _CACHE [key ].render (context or {})
998905
999- template = FileTemplate (template_path , language = language )
906+ template = FileTemplate (template_path )
1000907
1001908 if cache :
1002909 _CACHE [key ] = template
0 commit comments