Current section
Files
Jump to
Current section
Files
src/sprocket@html.erl
-module(sprocket@html).
-compile([no_auto_import, nowarn_unused_vars]).
-export([el/3, dangerous_raw_html/1, text/1, keyed/2, debug/3, html/2, head/2, meta/1, link/1, script/2, body/2, 'div'/2, span/2, i/2, button/2, h1/2, p/2, a/2, input/1, aside/2]).
-spec el(
binary(),
list(sprocket@html@attribute:attribute()),
list(sprocket@element:element())
) -> sprocket@element:element().
el(Tag, Attrs, Children) ->
{element, Tag, Attrs, Children}.
-spec dangerous_raw_html(binary()) -> sprocket@element:element().
dangerous_raw_html(Html) ->
{raw, Html}.
-spec safe_replace_char(binary()) -> binary().
safe_replace_char(Key) ->
case Key of
<<"&"/utf8>> ->
<<"&"/utf8>>;
<<"<"/utf8>> ->
<<"<"/utf8>>;
<<">"/utf8>> ->
<<">"/utf8>>;
<<"\""/utf8>> ->
<<"""/utf8>>;
<<"'"/utf8>> ->
<<"'"/utf8>>;
<<"/"/utf8>> ->
<<"/"/utf8>>;
<<"`"/utf8>> ->
<<"`"/utf8>>;
<<"="/utf8>> ->
<<"="/utf8>>;
_ ->
Key
end.
-spec escape_html(binary()) -> sprocket@element:element().
escape_html(Unsafe) ->
_pipe = gleam@string:to_graphemes(Unsafe),
_pipe@1 = gleam@list:fold(
_pipe,
gleam@string_builder:new(),
fun(Sb, Grapheme) ->
gleam@string_builder:append(Sb, safe_replace_char(Grapheme))
end
),
_pipe@2 = gleam@string_builder:to_string(_pipe@1),
{safe_html, _pipe@2}.
-spec text(binary()) -> sprocket@element:element().
text(Text) ->
escape_html(Text).
-spec keyed(binary(), sprocket@element:element()) -> sprocket@element:element().
keyed(Key, Element) ->
{keyed, Key, Element}.
-spec debug(
binary(),
gleam@option:option(gleam@dynamic:dynamic()),
sprocket@element:element()
) -> sprocket@element:element().
debug(Id, Meta, Element) ->
{debug, Id, Meta, Element}.
-spec html(
list(sprocket@html@attribute:attribute()),
list(sprocket@element:element())
) -> sprocket@element:element().
html(Attrs, Children) ->
el(<<"html"/utf8>>, Attrs, Children).
-spec head(
list(sprocket@html@attribute:attribute()),
list(sprocket@element:element())
) -> sprocket@element:element().
head(Attrs, Children) ->
el(<<"head"/utf8>>, Attrs, Children).
-spec meta(list(sprocket@html@attribute:attribute())) -> sprocket@element:element().
meta(Attrs) ->
el(<<"meta"/utf8>>, Attrs, []).
-spec link(list(sprocket@html@attribute:attribute())) -> sprocket@element:element().
link(Attrs) ->
el(<<"link"/utf8>>, Attrs, []).
-spec script(
list(sprocket@html@attribute:attribute()),
gleam@option:option(binary())
) -> sprocket@element:element().
script(Attrs, Body) ->
case Body of
{some, Body@1} ->
el(<<"script"/utf8>>, Attrs, [text(Body@1)]);
none ->
el(<<"script"/utf8>>, Attrs, [])
end.
-spec body(
list(sprocket@html@attribute:attribute()),
list(sprocket@element:element())
) -> sprocket@element:element().
body(Attrs, Children) ->
el(<<"body"/utf8>>, Attrs, Children).
-spec 'div'(
list(sprocket@html@attribute:attribute()),
list(sprocket@element:element())
) -> sprocket@element:element().
'div'(Attrs, Children) ->
el(<<"div"/utf8>>, Attrs, Children).
-spec span(
list(sprocket@html@attribute:attribute()),
list(sprocket@element:element())
) -> sprocket@element:element().
span(Attrs, Children) ->
el(<<"span"/utf8>>, Attrs, Children).
-spec i(
list(sprocket@html@attribute:attribute()),
list(sprocket@element:element())
) -> sprocket@element:element().
i(Attrs, Children) ->
el(<<"i"/utf8>>, Attrs, Children).
-spec button(
list(sprocket@html@attribute:attribute()),
list(sprocket@element:element())
) -> sprocket@element:element().
button(Attrs, Children) ->
el(<<"button"/utf8>>, Attrs, Children).
-spec h1(
list(sprocket@html@attribute:attribute()),
list(sprocket@element:element())
) -> sprocket@element:element().
h1(Attrs, Children) ->
el(<<"h1"/utf8>>, Attrs, Children).
-spec p(
list(sprocket@html@attribute:attribute()),
list(sprocket@element:element())
) -> sprocket@element:element().
p(Attrs, Children) ->
el(<<"p"/utf8>>, Attrs, Children).
-spec a(
list(sprocket@html@attribute:attribute()),
list(sprocket@element:element())
) -> sprocket@element:element().
a(Attrs, Children) ->
el(<<"a"/utf8>>, Attrs, Children).
-spec input(list(sprocket@html@attribute:attribute())) -> sprocket@element:element().
input(Attrs) ->
el(<<"input"/utf8>>, Attrs, []).
-spec aside(
list(sprocket@html@attribute:attribute()),
list(sprocket@element:element())
) -> sprocket@element:element().
aside(Attrs, Children) ->
el(<<"aside"/utf8>>, Attrs, Children).