Pyjo.String.HTML - String of markup

from Pyjo.String.HTML import html

# Markup that is not escaped again
link = html('<a href="/">Home</a>')

# What is added to markup is escaped
name = '<script>'
print(html('<p>') + name + html('</p>'))

# And what goes into it
print(html('<p>{}</p>').format(name))
print(html('<br>').join(['Hello', name]))

Pyjo.String.HTML is a Pyjo.String.Unicode that holds markup, which escaping leaves as it is: Pyjo.Util.xml_escape() and anything else that knows the __html__ protocol of markupsafe, Jinja and Django. A string that is not marked is escaped, a Pyjo.String.Unicode too, so that only an explicit html() turns a string into markup.

What is added to markup, with + on either side, %, format(), format_map() or join(), is escaped, unless it is markup itself, and the result is markup. The methods of Pyjo.String.Unicode return markup too, as the string has been marked explicitly; those of str return a str.

# "&lt;p&gt;"
html('<p>').xml_escape()

See also

Pyjoyment, https://mojolicious.org.

Inspired by Markup of markupsafe.

Functions

class Pyjo.String.HTML.Pyjo_String_HTML(*parts: Anything)[source]

Pyjo.String.HTML inherits all methods from Pyjo.String.Unicode and implements the following new ones.

format(*args: Anything, **kwargs: Anything) → Self[source]
string = string.format(value, name=value)

Format with str.format(), escaping the fields unless they are markup.

format_map(mapping: Any) → Self[source]
string = string.format_map({'name': value})

Format with str.format_map(), escaping the fields unless they are markup.

join(iterable: Iterable[Anything]) → Self[source]
string = string.join(['Hello', 'World'])

Join the strings with the markup between them, escaping those that are not markup.

Pyjo.String.HTML.html(*parts: Anything) → Pyjo_String_HTML[source]
string = html('<p>Hello</p>')

Construct a new Pyjo.String.HTML object, marking the string as markup.