44
55register = template .Library ()
66
7+ _TUPLE_RE = re .compile (r"^(.*?)\s*<([^>]+)>\s?$" )
8+
79
810@register .simple_tag (takes_context = True )
911def cms_component (context : template .Context , * args , ** kwargs : dict ) -> str :
@@ -59,17 +61,17 @@ def _to_tuple_if_needed(value: str) -> str | tuple[str, str]:
5961 value (str): The string to be converted.
6062
6163 Returns:
62- tuple[str, str]: A tuple containing the two parts of the string if it contains a delimiter,
63- otherwise returns the original string as a single-element tuple .
64+ str | tuple[str, str]: A tuple containing the two parts of the string if it contains
65+ a delimiter, otherwise returns the original string .
6466 """
65- match = re . match ( r"^(.*?)\s*<([^>]+)>\s?$" , value )
67+ match = _TUPLE_RE . fullmatch ( value )
6668 if match :
6769 return (match .group (2 ).strip (), match .group (1 ).strip ())
6870 return value
6971
7072
7173@register .filter
72- def split (value : str , delimiter : str = "|" ) -> list [str ] | list [ tuple [str , str ]]:
74+ def split (value : str , delimiter : str = "|" ) -> list [str | tuple [str , str ]]:
7375 """
7476 Helper that splits a given string into a list of substrings based on a specified delimiter.
7577 If the substring is of the format "Verbose name <value>" it is turned into a 2-tuple
@@ -80,7 +82,8 @@ def split(value: str, delimiter: str = "|") -> list[str] | list[tuple[str, str]]
8082 delimiter (str, optional): The delimiter to use for splitting the string. Defaults to "|".
8183
8284 Returns:
83- list[str]: A list of substrings obtained by splitting the input string using the delimiter.
85+ list[str | tuple[str, str]: A list of substrings or 2-tuples obtained by splitting the
86+ input string using the delimiter.
8487 """
8588 split_list = value .split (delimiter )
8689 return [_to_tuple_if_needed (item ) for item in split_list ]
0 commit comments