HypertextLiteral Overview
This package provides a Julia macro, @htl
, that constructs an object which could be rendered to MIME"text/html"
displays. This macro supports string interpolation sensible to the needs of HTML generation.
using HypertextLiteral
v = "<1 Brown \"M&M's\"!";
@htl "<span>$v</span>"
#-> <span><1 Brown "M&M's"!</span>
An equivalent non-standard string literal, htl
, is also provided.
v = "<1 Brown \"M&M's\"!";
htl"<span>$v</span>"
#-> <span><1 Brown "M&M's"!</span>
Interpolation can use the full expressive power of Julia.
books = ["Who Gets What & Why", "Switch", "Governing The Commons"]
@htl "<ul>$(map(books) do b @htl("<li>$b") end)</ul>"
#=>
<ul><li>Who Gets What & Why<li>Switch<li>Governing The Commons</ul>
=#
Translation Contexts
How a Julia expression is translated depends upon where it is used.
Native Julia | Translation | |
---|---|---|
Element Content | "\"M&M\"'s" | M&M's |
:name | name | |
[1, 2] or (1, 2) | 12 | |
nothing | omitted | |
missing | <span class="Base-Missing">missing</span> | |
(a = 1, b = 2) | <span class="Core-NamedTuple">(a = 1, b = 2)</span> | |
Dict(:a => 1, :b => 2) | <span class="Base-Dict">Dict(:a => 1, :b => 2)</span> | |
Attribute Value | "\"M&M\"'s" | M&M's |
:name | name | |
[1, 2] or (1, 2) | 1 2 | |
nothing | omitted | |
missing | missing | |
(a = 1, b = 2) | a: 1; b: 2; | |
Dict(:a => 1, :b => 2) | a: 1; b: 2; | |
Script Tag | "\"M&M\"'s" | "\"M&M\"'s" |
:name | name | |
[1, 2] or (1, 2) | [1, 2] | |
nothing | undefined | |
missing | null | |
Inf | Infinity | |
NaN | NaN | |
(a = 1, b = 2) | {"a": 1, "b": 2} | |
Dict(:a => 1, :b => 2) | {"a": 1, "b": 2} |
If any of these translations are inconvenient:
coalesce()
can be used to provide an alternative formissing
;something()
provides a substitution fornothing
;string()
will use the string translation instead; andHTML()
can be used to bypass escaping within element content.