Otera.Markup (otera v0.2.0)

Copy Markdown View Source

A string that is safe to insert into HTML, like Markup of markupsafe, which Jinja2 uses.

With autoescape, Otera escapes the output of each {{ ... }}, except a Markup. The filters safe, escape, forceescape and tojson give a Markup, and with autoescape, macros, caller(), blocks and the bodies of set and filter blocks give a Markup too.

Templates see a Markup as the Python Markup, a subclass of str. An operation that combines a Markup with a plain string escapes the plain string: +, %, join, format and the replace filter. Most string methods of a Markup give a Markup.

To give a template a string that it does not escape, pass a Markup in the variables:

Otera.render("{{ link }}", %{link: Otera.Markup.new(~s(<a href="/">Home</a>))}, autoescape: true)
#=> {:ok, ~s(<a href="/">Home</a>)}

A custom filter, test or function gets a Markup as this struct, and can return one.

Summary

Functions

Escapes value like markupsafe.escape: a Markup stays as it is, and another value becomes the Markup of its escaped str().

Applies fun to the text of value, and gives a Markup back when value is a Markup.

Marks text as safe, like Markup(text).

Like markupsafe.soft_str: a string or a Markup stays as it is, and another value becomes its str().

Types

t()

@type t() :: %Otera.Markup{text: String.t()}

Functions

escape(markup)

@spec escape(term()) :: t()

Escapes value like markupsafe.escape: a Markup stays as it is, and another value becomes the Markup of its escaped str().

map(text, fun)

@spec map(String.t() | t(), (String.t() -> String.t())) :: String.t() | t()

Applies fun to the text of value, and gives a Markup back when value is a Markup.

new(text)

@spec new(String.t()) :: t()

Marks text as safe, like Markup(text).

soft_str(markup)

@spec soft_str(term()) :: String.t() | t()

Like markupsafe.soft_str: a string or a Markup stays as it is, and another value becomes its str().