Package Reference

HypertextLiteral.HypertextLiteralModule
HypertextLiteral

This library provides a @htl macro which implements interopolation that is aware of hypertext escape context.

HypertextLiteral.EscapeProxyType
EscapeProxy(io) - wrap an `io` to perform HTML escaping

This is a transparent proxy that performs HTML escaping so that objects that are printed are properly converted into valid HTML values. As a special case, objects wrapped with Bypass are not escaped, and bypass the proxy.

Examples

julia> ep = EscapeProxy(stdout);
julia> print(ep, "A&B")
A&B
julia> print(ep, Bypass("<tag/>"))
<tag/>
HypertextLiteral.ResultType
Result(fn)

This object wraps a function produced by the @htl macro. This function prints a the evaluated to the given io. This object is also showable via "text/html" so it may be used in an HTML display context.

HypertextLiteral.ScriptType
Script(data)

This object prints data unescaped within a <script> tag, wrapped in a ScriptProxy that guards against invalid script content.

HypertextLiteral.ScriptProxyType
ScriptProxy(io)

This is a transparent proxy that ensures neither <!-- nor </script> occur in the output stream.

Examples

julia> gp = ScriptProxy(stdout);
julia> print(gp, "valid");
valid
julia> print(gp, "</script>")
ERROR: "Content within a script tag must not contain `</script>`"
HypertextLiteral.StyleType
Style(data)

This object prints data unescaped within a <script> tag, wrapped in a StyleProxy that guards against invalid script content.

HypertextLiteral.StyleProxyType
StyleProxy(io)

This is a transparent proxy that ensures neither <!-- nor </style> occur in the output stream.

Examples

julia> gp = StyleProxy(stdout);
julia> print(gp, "valid");
valid
julia> print(gp, "</style>")
ERROR: "Content within a style tag must not contain `</style>`"
HypertextLiteral.attribute_valueMethod
attribute_value(x)

This method may be implemented to specify a printed representation suitable for use within a quoted attribute value.

HypertextLiteral.contentMethod
content(x)

This method may be implemented to specify a printed representation suitable for text/html output. AbstractString, Symbol and Number (including Bool) types are printed, with proper escaping.

A default implementation first looks to see if typeof(x) has implemented a way to show themselves as text/html, if so, this is used. Otherwise, the result is printed within a <span> tag, using a class that includes the module and type name. Hence, missing is serialized as: <span class="Base-Missing">missing</span>.

HypertextLiteral.interpolateMethod
interpolate(args)::Expr

Take an interweaved set of Julia expressions and strings, tokenize the strings according to the HTML specification [1], wrapping the expressions with wrappers based upon the escaping context, and returning an expression that combines the result with an Result wrapper.

For these purposes, a Symbol is treated as an expression to be resolved; while a String is treated as a literal string that won't be escaped. Critically, interpolated strings to be escaped are represented as an Expr with head of :string.

There are tags, "script" and "style" which are rawtext, in these cases there is no escaping, and instead raise an exception if the appropriate ending tag is in substituted content.

[1] https://html.spec.whatwg.org/multipage/parsing.html#tokenization

HypertextLiteral.normalize_attribute_nameMethod
normalize_attribute_name(name)

For String names, this simply verifies that they pass the attribute name production, but are otherwise untouched.

For Symbol names, this converts snake_case Symbol objects to their kebab-case equivalent. So that keywords, such as for could be used, we strip leading underscores.

HypertextLiteral.print_scriptMethod
print_script(io, value)

Show value as "text/javascript" to the given io, this provides some baseline functionality for built-in data types.

- `nothing` becomes `undefined`
- `missing` becomes `null`
- `Bool` values are printed as `true` or `false`
- `AbstractString` and `Symbol` become a double-quoted string
- `AbstractVector` and `Tuple` become an array
- `Dict` and `NamedTuple` become a Javascript object, with
   keys converted to string values
- `AbstractFloat` and `Integer` are printed directly, where
  `NaN` remains `NaN` but `Inf` is printed as `Infinity`

The fallback behavior of print_script is to show the object as "text/javascript". The Javascript wrapper will take any string and let it be printed in this way.

HypertextLiteral.print_script_lowerMethod
print_script_lower(io, value)

Provides a hook to override print_script for custom Javascript runtimes, such as Pluto.jl, to provide their own value marshalling.

HypertextLiteral.print_valueMethod
print_value(io, value)

This is the default translation of interpolated values within rawtext tags, such as <style> and attribute values.

  • The elements of a Tuple or AbstractArray object are printed, with a space between each item.

  • The Pair, NamedTuple, and Dict objects are treated as if they are CSS style elements, with a colon between key and value, each pair delimited by a semi-colon.

  • The Nothing object is treated as an empty string.

Otherwise, this method simply uses the standard print representation for the given object.

HypertextLiteral.rewrite_inside_tagMethod
rewrite_inside_tag(expr)

Attempt to speed up serialization of inside_tag by exploring the expression tree at macro expansion time.

HypertextLiteral.script_attribute_valueMethod
script_attribute_value(x)

This method may be implemented to specify a printed representation suitable for use within a quoted attribute value starting with on.

HypertextLiteral.@htlMacro
@htl string-expression

Create a Result object with string interpolation ($) that uses context-sensitive hypertext escaping. Before Julia 1.6, interpolated string literals, e.g. $("Strunk & White"), are treated as errors since they cannot be reliably detected (see Julia issue #38501).

HypertextLiteral.@htl_strMacro
@htl_str -> Result

Create a Result object with string interpolation ($) that uses context-sensitive hypertext escaping. Unlike the @htl macro, this string literal does not include escaping feature [1]. To include $ within user content one must write &#36;. Observe that &quot; and any other HTML ampersand escape sequence can be used as appropriate.

In this syntax, interpolation is extended beyond regular Julia strings to handle three additional cases: tuples, named tuples (for attributes), and generators. See Julia #38734 for the feature request so that this could also work within the @htl macro syntax.

[1] There are also a few edge cases, see @raw_str documentation and Julia #22926 for more detail.