1 # Langs (Safe Embedded Languages)
3 # `lang` defines custom text types with automatic escaping.
6 # Custom escaping rules can be created with `convert`
7 convert(t:Text -> HTML)
8 t = t.translate({"&": "&", "<": "<", ">": ">"})
9 return HTML.from_text(t)
11 func paragraph(content:HTML -> HTML)
12 return $HTML"<p>$content</p>"
15 # Type safety prevents injection:
16 func greet(name:HTML -> HTML)
17 return $HTML"Hello $name!"
21 malicious_input := "<b>hello</b>"
23 safe := $HTML"User said: $malicious_input"
27 assert safe.paragraph() == ???
29 greeting := greet(malicious_input) # This won't compile