Packages
lustre
3.0.3
5.7.1
5.7.0
5.6.0
5.5.2
5.5.1
5.5.0
5.4.0
5.3.5
5.3.4
5.3.3
5.3.2
5.3.1
5.3.0
5.2.1
5.2.0
5.1.1
5.1.0
5.0.3
5.0.2
5.0.1
5.0.0
4.6.4
4.6.3
4.6.2
4.6.1
4.6.0
4.5.1
4.5.0
4.4.4
4.4.3
4.4.1
4.4.0
4.3.6
4.3.5
4.3.4
4.3.3
4.3.2
4.3.1
4.3.0
4.2.6
4.2.5
4.2.4
4.2.3
4.2.2
4.2.1
4.2.0
4.1.8
4.1.7
4.1.6
4.1.5
4.1.4
4.1.3
4.1.2
4.1.1
4.1.0
4.0.0
4.0.0-rc1
4.0.0-rc.2
3.1.4
3.1.3
3.1.2
3.1.1
3.1.0
3.0.12
3.0.11
3.0.10
3.0.9
3.0.8
3.0.7
3.0.6
3.0.5
3.0.4
3.0.3
3.0.2
3.0.1
3.0.0
3.0.0-rc.8
3.0.0-rc.7
3.0.0-rc.6
3.0.0-rc.5
3.0.0-rc.4
3.0.0-rc.3
3.0.0-rc.2
3.0.0-rc.1
2.0.1
2.0.0
1.3.0
1.2.0
1.1.0
1.0.0
Create HTML templates, single page applications, Web Components, and real-time server components in Gleam!
Current section
Files
Jump to
Current section
Files
src/lustre@element.erl
-module(lustre@element).
-compile([no_auto_import, nowarn_unused_vars]).
-export([element/3, namespaced/4, text/1, map/2, to_string_builder/1, to_string/1]).
-export_type([element/1]).
-opaque element(FGN) :: {text, binary()} |
{element,
binary(),
list(lustre@attribute:attribute(FGN)),
list(element(FGN))} |
{element_ns,
binary(),
list(lustre@attribute:attribute(FGN)),
list(element(FGN)),
binary()}.
-spec element(
binary(),
list(lustre@attribute:attribute(FGO)),
list(element(FGO))
) -> element(FGO).
element(Tag, Attrs, Children) ->
{element, Tag, Attrs, Children}.
-spec namespaced(
binary(),
binary(),
list(lustre@attribute:attribute(FGU)),
list(element(FGU))
) -> element(FGU).
namespaced(Namespace, Tag, Attrs, Children) ->
{element_ns, Tag, Attrs, Children, Namespace}.
-spec text(binary()) -> element(any()).
text(Content) ->
{text, Content}.
-spec escape(binary(), binary()) -> binary().
escape(Escaped, Content) ->
case gleam@string:pop_grapheme(Content) of
{ok, {<<"<"/utf8>>, Xs}} ->
escape(<<Escaped/binary, "<"/utf8>>, Xs);
{ok, {<<">"/utf8>>, Xs@1}} ->
escape(<<Escaped/binary, ">"/utf8>>, Xs@1);
{ok, {<<"&"/utf8>>, Xs@2}} ->
escape(<<Escaped/binary, "&"/utf8>>, Xs@2);
{ok, {<<"\""/utf8>>, Xs@3}} ->
escape(<<Escaped/binary, """/utf8>>, Xs@3);
{ok, {<<"'"/utf8>>, Xs@4}} ->
escape(<<Escaped/binary, "'"/utf8>>, Xs@4);
{ok, {X, Xs@5}} ->
escape(<<Escaped/binary, X/binary>>, Xs@5);
{error, _} ->
<<Escaped/binary, Content/binary>>
end.
-spec map(element(FHC), fun((FHC) -> FHE)) -> element(FHE).
map(Element, F) ->
case Element of
{text, Content} ->
{text, Content};
{element, Tag, Attrs, Children} ->
{element,
Tag,
gleam@list:map(
Attrs,
fun(_capture) -> lustre@attribute:map(_capture, F) end
),
gleam@list:map(
Children,
fun(_capture@1) -> map(_capture@1, F) end
)};
{element_ns, Tag@1, Attrs@1, Children@1, Namespace} ->
{element_ns,
Tag@1,
gleam@list:map(
Attrs@1,
fun(_capture@2) -> lustre@attribute:map(_capture@2, F) end
),
gleam@list:map(
Children@1,
fun(_capture@3) -> map(_capture@3, F) end
),
Namespace}
end.
-spec attrs_to_string_builder(
gleam@string_builder:string_builder(),
list(lustre@attribute:attribute(any()))
) -> gleam@string_builder:string_builder().
attrs_to_string_builder(Html, Attrs) ->
gleam@bool:guard(
gleam@list:is_empty(Attrs),
Html,
fun() ->
gleam@list:fold(Attrs, Html, fun(Html@1, Attr) -> _pipe = Html@1,
_pipe@1 = gleam@string_builder:append(_pipe, <<" "/utf8>>),
gleam@string_builder:append_builder(
_pipe@1,
lustre@attribute:to_string_builder(Attr)
) end)
end
).
-spec children_to_string_builder(
gleam@string_builder:string_builder(),
list(element(any()))
) -> gleam@string_builder:string_builder().
children_to_string_builder(Html, Children) ->
gleam@list:fold(
Children,
Html,
fun(Html@1, Child) ->
gleam@string_builder:append_builder(
Html@1,
to_string_builder(Child)
)
end
).
-spec to_string_builder(element(any())) -> gleam@string_builder:string_builder().
to_string_builder(Element) ->
case Element of
{text, Content} ->
gleam@string_builder:from_string(escape(<<""/utf8>>, Content));
{element, <<"area"/utf8>> = Tag, Attrs, _} ->
_pipe = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
_pipe@1 = attrs_to_string_builder(_pipe, Attrs),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"base"/utf8>> = Tag, Attrs, _} ->
_pipe = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
_pipe@1 = attrs_to_string_builder(_pipe, Attrs),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"br"/utf8>> = Tag, Attrs, _} ->
_pipe = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
_pipe@1 = attrs_to_string_builder(_pipe, Attrs),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"col"/utf8>> = Tag, Attrs, _} ->
_pipe = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
_pipe@1 = attrs_to_string_builder(_pipe, Attrs),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"embed"/utf8>> = Tag, Attrs, _} ->
_pipe = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
_pipe@1 = attrs_to_string_builder(_pipe, Attrs),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"hr"/utf8>> = Tag, Attrs, _} ->
_pipe = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
_pipe@1 = attrs_to_string_builder(_pipe, Attrs),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"img"/utf8>> = Tag, Attrs, _} ->
_pipe = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
_pipe@1 = attrs_to_string_builder(_pipe, Attrs),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"input"/utf8>> = Tag, Attrs, _} ->
_pipe = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
_pipe@1 = attrs_to_string_builder(_pipe, Attrs),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"link"/utf8>> = Tag, Attrs, _} ->
_pipe = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
_pipe@1 = attrs_to_string_builder(_pipe, Attrs),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"meta"/utf8>> = Tag, Attrs, _} ->
_pipe = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
_pipe@1 = attrs_to_string_builder(_pipe, Attrs),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"param"/utf8>> = Tag, Attrs, _} ->
_pipe = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
_pipe@1 = attrs_to_string_builder(_pipe, Attrs),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"source"/utf8>> = Tag, Attrs, _} ->
_pipe = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
_pipe@1 = attrs_to_string_builder(_pipe, Attrs),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"track"/utf8>> = Tag, Attrs, _} ->
_pipe = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
_pipe@1 = attrs_to_string_builder(_pipe, Attrs),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"wbr"/utf8>> = Tag, Attrs, _} ->
_pipe = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
_pipe@1 = attrs_to_string_builder(_pipe, Attrs),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, Tag@1, Attrs@1, Children} ->
_pipe@2 = gleam@string_builder:from_string(
<<"<"/utf8, Tag@1/binary>>
),
_pipe@3 = attrs_to_string_builder(_pipe@2, Attrs@1),
_pipe@4 = gleam@string_builder:append(_pipe@3, <<">"/utf8>>),
_pipe@5 = children_to_string_builder(_pipe@4, Children),
gleam@string_builder:append(
_pipe@5,
<<<<"</"/utf8, Tag@1/binary>>/binary, ">"/utf8>>
);
{element_ns, Tag@2, Attrs@2, Children@1, Namespace} ->
_pipe@6 = gleam@string_builder:from_string(
<<"<"/utf8, Tag@2/binary>>
),
_pipe@7 = attrs_to_string_builder(_pipe@6, Attrs@2),
_pipe@8 = gleam@string_builder:append(
_pipe@7,
<<<<" xmlns=\""/utf8, Namespace/binary>>/binary, "\""/utf8>>
),
_pipe@9 = gleam@string_builder:append(_pipe@8, <<">"/utf8>>),
_pipe@10 = children_to_string_builder(_pipe@9, Children@1),
gleam@string_builder:append(
_pipe@10,
<<<<"</"/utf8, Tag@2/binary>>/binary, ">"/utf8>>
)
end.
-spec to_string(element(any())) -> binary().
to_string(Element) ->
_pipe = to_string_builder(Element),
gleam@string_builder:to_string(_pipe).