from datetime import datetime as DateTime
from string.templatelib import Template, Interpolation
from html import escape as html_escape
def html(template: Template) -> str:
if not isinstance(template, Template):
raise TypeError("Normal Strings are not allowed. Use a template string")
parts = []
for item in template:
match item:
case Interpolation():
parts.append(html_escape(str(item.value)))
case str():
parts.append(item)
return "".join(parts)
world = "<b>world</b>"
dt = DateTime.now()
print(html(t"Hello {world}. {dt}"))
try:
html(f"Wrong {world}")
except TypeError as e:
print(e)
Comments
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎