Packages
lustre
4.3.2
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, nowarn_unused_function, nowarn_nomatch]).
-export([element/3, keyed/2, namespaced/4, advanced/6, text/1, none/0, fragment/1, map/2, to_string/1, to_document_string/1, to_string_builder/1, to_document_string_builder/1]).
-spec element(
binary(),
list(lustre@internals@vdom:attribute(NXQ)),
list(lustre@internals@vdom:element(NXQ))
) -> lustre@internals@vdom:element(NXQ).
element(Tag, Attrs, Children) ->
case Tag of
<<"area"/utf8>> ->
{element, <<""/utf8>>, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"base"/utf8>> ->
{element, <<""/utf8>>, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"br"/utf8>> ->
{element, <<""/utf8>>, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"col"/utf8>> ->
{element, <<""/utf8>>, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"embed"/utf8>> ->
{element, <<""/utf8>>, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"hr"/utf8>> ->
{element, <<""/utf8>>, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"img"/utf8>> ->
{element, <<""/utf8>>, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"input"/utf8>> ->
{element, <<""/utf8>>, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"link"/utf8>> ->
{element, <<""/utf8>>, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"meta"/utf8>> ->
{element, <<""/utf8>>, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"param"/utf8>> ->
{element, <<""/utf8>>, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"source"/utf8>> ->
{element, <<""/utf8>>, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"track"/utf8>> ->
{element, <<""/utf8>>, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"wbr"/utf8>> ->
{element, <<""/utf8>>, <<""/utf8>>, Tag, Attrs, [], false, true};
_ ->
{element,
<<""/utf8>>,
<<""/utf8>>,
Tag,
Attrs,
Children,
false,
false}
end.
-spec do_keyed(lustre@internals@vdom:element(NYD), binary()) -> lustre@internals@vdom:element(NYD).
do_keyed(El, Key) ->
case El of
{element, _, Namespace, Tag, Attrs, Children, Self_closing, Void} ->
{element, Key, Namespace, Tag, Attrs, Children, Self_closing, Void};
{map, Subtree} ->
{map, fun() -> do_keyed(Subtree(), Key) end};
{fragment, Elements, _} ->
_pipe = Elements,
_pipe@1 = gleam@list:index_map(
_pipe,
fun(Element, Idx) -> case Element of
{element, El_key, _, _, _, _, _, _} ->
New_key = case El_key of
<<""/utf8>> ->
<<<<Key/binary, "-"/utf8>>/binary,
(gleam@int:to_string(Idx))/binary>>;
_ ->
<<<<Key/binary, "-"/utf8>>/binary,
El_key/binary>>
end,
do_keyed(Element, New_key);
_ ->
do_keyed(Element, Key)
end end
),
{fragment, _pipe@1, Key};
_ ->
El
end.
-spec keyed(
fun((list(lustre@internals@vdom:element(NXW))) -> lustre@internals@vdom:element(NXW)),
list({binary(), lustre@internals@vdom:element(NXW)})
) -> lustre@internals@vdom:element(NXW).
keyed(El, Children) ->
El(
(gleam@list:map(
Children,
fun(_use0) ->
{Key, Child} = _use0,
do_keyed(Child, Key)
end
))
).
-spec namespaced(
binary(),
binary(),
list(lustre@internals@vdom:attribute(NYG)),
list(lustre@internals@vdom:element(NYG))
) -> lustre@internals@vdom:element(NYG).
namespaced(Namespace, Tag, Attrs, Children) ->
{element, <<""/utf8>>, Namespace, Tag, Attrs, Children, false, false}.
-spec advanced(
binary(),
binary(),
list(lustre@internals@vdom:attribute(NYM)),
list(lustre@internals@vdom:element(NYM)),
boolean(),
boolean()
) -> lustre@internals@vdom:element(NYM).
advanced(Namespace, Tag, Attrs, Children, Self_closing, Void) ->
{element, <<""/utf8>>, Namespace, Tag, Attrs, Children, Self_closing, Void}.
-spec text(binary()) -> lustre@internals@vdom:element(any()).
text(Content) ->
{text, Content}.
-spec none() -> lustre@internals@vdom:element(any()).
none() ->
{text, <<""/utf8>>}.
-spec flatten_fragment_elements(list(lustre@internals@vdom:element(NZA))) -> list(lustre@internals@vdom:element(NZA)).
flatten_fragment_elements(Elements) ->
gleam@list:fold_right(
Elements,
[],
fun(New_elements, Element) -> case Element of
{fragment, Fr_elements, _} ->
lists:append(Fr_elements, New_elements);
El ->
[El | New_elements]
end end
).
-spec fragment(list(lustre@internals@vdom:element(NYW))) -> lustre@internals@vdom:element(NYW).
fragment(Elements) ->
_pipe = flatten_fragment_elements(Elements),
{fragment, _pipe, <<""/utf8>>}.
-spec map(lustre@internals@vdom:element(NZE), fun((NZE) -> NZG)) -> lustre@internals@vdom:element(NZG).
map(Element, F) ->
case Element of
{text, Content} ->
{text, Content};
{map, Subtree} ->
{map, fun() -> map(Subtree(), F) end};
{element, Key, Namespace, Tag, Attrs, Children, Self_closing, Void} ->
{map,
fun() ->
{element,
Key,
Namespace,
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
),
Self_closing,
Void}
end};
{fragment, Elements, Key@1} ->
{map,
fun() ->
{fragment,
gleam@list:map(
Elements,
fun(_capture@2) -> map(_capture@2, F) end
),
Key@1}
end}
end.
-spec to_string(lustre@internals@vdom:element(any())) -> binary().
to_string(Element) ->
lustre@internals@vdom:element_to_string(Element).
-spec to_document_string(lustre@internals@vdom:element(any())) -> binary().
to_document_string(El) ->
_pipe = lustre@internals@vdom:element_to_string(case El of
{element, _, _, <<"html"/utf8>>, _, _, _, _} ->
El;
{element, _, _, <<"head"/utf8>>, _, _, _, _} ->
element(<<"html"/utf8>>, [], [El]);
{element, _, _, <<"body"/utf8>>, _, _, _, _} ->
element(<<"html"/utf8>>, [], [El]);
{map, Subtree} ->
Subtree();
_ ->
element(
<<"html"/utf8>>,
[],
[element(<<"body"/utf8>>, [], [El])]
)
end),
gleam@string:append(<<"<!doctype html>\n"/utf8>>, _pipe).
-spec to_string_builder(lustre@internals@vdom:element(any())) -> gleam@string_builder:string_builder().
to_string_builder(Element) ->
lustre@internals@vdom:element_to_string_builder(Element).
-spec to_document_string_builder(lustre@internals@vdom:element(any())) -> gleam@string_builder:string_builder().
to_document_string_builder(El) ->
_pipe = lustre@internals@vdom:element_to_string_builder(case El of
{element, _, _, <<"html"/utf8>>, _, _, _, _} ->
El;
{element, _, _, <<"head"/utf8>>, _, _, _, _} ->
element(<<"html"/utf8>>, [], [El]);
{element, _, _, <<"body"/utf8>>, _, _, _, _} ->
element(<<"html"/utf8>>, [], [El]);
{map, Subtree} ->
Subtree();
_ ->
element(
<<"html"/utf8>>,
[],
[element(<<"body"/utf8>>, [], [El])]
)
end),
gleam@string_builder:prepend(_pipe, <<"<!doctype html>\n"/utf8>>).