Current section
Files
Jump to
Current section
Files
src/render.erl
-module(render).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([escape/1, element/1, html/1, xml/1]).
-spec attributes_to_list(list(elements:attribute())) -> binary().
attributes_to_list(Attributes) ->
_pipe = Attributes,
_pipe@1 = gleam@list:map(
_pipe,
fun(Attr) ->
<<<<<<<<(erlang:element(2, Attr))/binary, "="/utf8>>/binary,
"\""/utf8>>/binary,
(erlang:element(3, Attr))/binary>>/binary,
"\""/utf8>>
end
),
gleam@string:join(_pipe@1, <<" "/utf8>>).
-spec append_if(binary(), fun(() -> boolean()), fun(() -> binary())) -> binary().
append_if(Text, Predicate, Value_func) ->
case Predicate() of
true ->
gleam@string:append(Text, Value_func());
false ->
Text
end.
-spec escape(binary()) -> binary().
escape(Value) ->
_pipe = Value,
_pipe@1 = gleam@string:replace(_pipe, <<"&"/utf8>>, <<"&"/utf8>>),
_pipe@2 = gleam@string:replace(_pipe@1, <<"<"/utf8>>, <<"<"/utf8>>),
_pipe@3 = gleam@string:replace(_pipe@2, <<">"/utf8>>, <<">"/utf8>>),
_pipe@4 = gleam@string:replace(_pipe@3, <<"\""/utf8>>, <<"""/utf8>>),
_pipe@5 = gleam@string:replace(_pipe@4, <<"'"/utf8>>, <<"'"/utf8>>),
gleam@string:replace(_pipe@5, <<"/"/utf8>>, <<"/"/utf8>>).
-spec element(elements:element()) -> binary().
element(El) ->
case El of
{element, Tag, Attributes, Children} ->
Html = begin
_pipe = <<"<"/utf8>>,
_pipe@1 = gleam@string:append(_pipe, Tag),
append_if(
_pipe@1,
fun() -> erlang:length(Attributes) > 0 end,
fun() ->
<<" "/utf8, (attributes_to_list(Attributes))/binary>>
end
)
end,
case Children of
[] ->
_pipe@2 = Html,
gleam@string:append(_pipe@2, <<" />"/utf8>>);
_ ->
Html_childen = begin
_pipe@3 = Children,
_pipe@4 = gleam@list:map(_pipe@3, fun element/1),
_pipe@5 = gleam@string:join(_pipe@4, <<""/utf8>>),
_pipe@6 = gleam@string:append(_pipe@5, <<"</"/utf8>>),
_pipe@7 = gleam@string:append(_pipe@6, Tag),
gleam@string:append(_pipe@7, <<">"/utf8>>)
end,
_pipe@8 = Html,
_pipe@9 = gleam@string:append(_pipe@8, <<">"/utf8>>),
gleam@string:append(_pipe@9, Html_childen)
end;
{escape_text, Value} ->
escape(Value);
{text, Value@1} ->
Value@1;
{c_data, Value@2} ->
_pipe@10 = <<"<![CDATA["/utf8>>,
_pipe@11 = gleam@string:append(_pipe@10, Value@2),
gleam@string:append(_pipe@11, <<"]]>"/utf8>>)
end.
-spec html(elements:element()) -> binary().
html(El) ->
_pipe = <<"<!DOCTYPE html>"/utf8>>,
gleam@string:append(_pipe, element(El)).
-spec xml(elements:element()) -> binary().
xml(El) ->
_pipe = <<"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"/utf8>>,
gleam@string:append(_pipe, element(El)).