Current section

Files

Jump to
sprocket src sprocket@renderers@html.erl
Raw

src/sprocket@renderers@html.erl

-module(sprocket@renderers@html).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([html_renderer/0]).
-export_type([raw_html/0]).
-type raw_html() :: {raw_html, binary(), binary()}.
-file("/home/runner/work/sprocket/sprocket/src/sprocket/renderers/html.gleam", 41).
-spec el(
binary(),
gleam@string_tree:string_tree(),
gleam@string_tree:string_tree()
) -> gleam@string_tree:string_tree().
el(Tag, Attrs, Inner_html) ->
gleam_stdlib:identity(
[gleam_stdlib:identity(<<"<"/utf8>>),
gleam_stdlib:identity(Tag),
Attrs,
gleam_stdlib:identity(<<">"/utf8>>),
Inner_html,
gleam_stdlib:identity(<<"</"/utf8>>),
gleam_stdlib:identity(Tag),
gleam_stdlib:identity(<<">"/utf8>>)]
).
-file("/home/runner/work/sprocket/sprocket/src/sprocket/renderers/html.gleam", 104).
-spec safe_replace_char(binary()) -> binary().
safe_replace_char(Key) ->
case Key of
<<"&"/utf8>> ->
<<"&amp;"/utf8>>;
<<"<"/utf8>> ->
<<"&lt;"/utf8>>;
<<">"/utf8>> ->
<<"&gt;"/utf8>>;
<<"\""/utf8>> ->
<<"&quot;"/utf8>>;
<<"'"/utf8>> ->
<<"&#39;"/utf8>>;
<<"/"/utf8>> ->
<<"&#x2F;"/utf8>>;
<<"`"/utf8>> ->
<<"&#x60;"/utf8>>;
<<"="/utf8>> ->
<<"&#x3D;"/utf8>>;
_ ->
Key
end.
-file("/home/runner/work/sprocket/sprocket/src/sprocket/renderers/html.gleam", 118).
-spec escape_html(binary()) -> binary().
escape_html(Unsafe) ->
_pipe = gleam@string:to_graphemes(Unsafe),
_pipe@1 = gleam@list:fold(
_pipe,
gleam@string_tree:new(),
fun(Sb, Grapheme) ->
gleam@string_tree:append(Sb, safe_replace_char(Grapheme))
end
),
unicode:characters_to_binary(_pipe@1).
-file("/home/runner/work/sprocket/sprocket/src/sprocket/renderers/html.gleam", 126).
-spec text(binary()) -> gleam@string_tree:string_tree().
text(T) ->
_pipe = escape_html(T),
gleam_stdlib:identity(_pipe).
-file("/home/runner/work/sprocket/sprocket/src/sprocket/renderers/html.gleam", 152).
-spec decode_raw(gleam@dynamic:dynamic_()) -> {ok, raw_html()} |
{error, list(gleam@dynamic:decode_error())}.
decode_raw(Data) ->
_pipe = Data,
(gleam@dynamic:decode2(
fun(Field@0, Field@1) -> {raw_html, Field@0, Field@1} end,
gleam@dynamic:field(<<"tag"/utf8>>, fun gleam@dynamic:string/1),
gleam@dynamic:field(<<"innerHtml"/utf8>>, fun gleam@dynamic:string/1)
))(_pipe).
-file("/home/runner/work/sprocket/sprocket/src/sprocket/renderers/html.gleam", 131).
-spec custom(binary(), binary()) -> gleam@string_tree:string_tree().
custom(Kind, Data) ->
case Kind of
<<"raw"/utf8>> ->
case gleam@json:decode(Data, fun decode_raw/1) of
{ok, {raw_html, Tag, Raw_html}} ->
el(
Tag,
gleam_stdlib:identity(<<""/utf8>>),
gleam_stdlib:identity(Raw_html)
);
{error, _} ->
gleam_stdlib:identity(<<""/utf8>>)
end;
_ ->
gleam_stdlib:identity(<<""/utf8>>)
end.
-file("/home/runner/work/sprocket/sprocket/src/sprocket/renderers/html.gleam", 54).
-spec element(
sprocket@internal@utils@unique:unique(sprocket@context:element_id()),
binary(),
gleam@option:option(binary()),
list(sprocket@internal@reconcile:reconciled_attribute()),
list(sprocket@internal@reconcile:reconciled_element())
) -> gleam@string_tree:string_tree().
element(_, Tag, Key, Attrs, Children) ->
Rendered_attrs = begin
_pipe = Attrs,
gleam@list:fold(
_pipe,
gleam@string_tree:new(),
fun(Acc, Attr) -> case Attr of
{reconciled_attribute, Name, Value} ->
gleam_stdlib:iodata_append(
Acc,
gleam_stdlib:identity(
[<<" "/utf8>>,
Name,
<<"=\""/utf8>>,
Value,
<<"\""/utf8>>]
)
);
_ ->
Acc
end end
)
end,
Rendered_attrs@1 = case Key of
{some, K} ->
gleam_stdlib:iodata_append(
Rendered_attrs,
gleam_stdlib:identity(
[<<" "/utf8>>,
<<"spkt-key"/utf8>>,
<<"=\""/utf8>>,
K,
<<"\""/utf8>>]
)
);
none ->
Rendered_attrs
end,
Inner_html = begin
_pipe@1 = Children,
gleam@list:fold(
_pipe@1,
gleam@string_tree:new(),
fun(Acc@1, Child) ->
gleam_stdlib:iodata_append(Acc@1, render(Child))
end
)
end,
el(Tag, Rendered_attrs@1, Inner_html).
-file("/home/runner/work/sprocket/sprocket/src/sprocket/renderers/html.gleam", 24).
-spec render(sprocket@internal@reconcile:reconciled_element()) -> gleam@string_tree:string_tree().
render(El) ->
case El of
{reconciled_element, Id, Tag, Key, Attrs, Children} ->
element(Id, Tag, Key, Attrs, Children);
{reconciled_component, _, _, _, _, El@1} ->
component(El@1);
{reconciled_fragment, _, Children@1} ->
fragment(Children@1);
{reconciled_ignore_update, El@2} ->
render(El@2);
{reconciled_text, T} ->
text(T);
{reconciled_custom, Kind, Data} ->
custom(Kind, Data)
end.
-file("/home/runner/work/sprocket/sprocket/src/sprocket/renderers/html.gleam", 17).
-spec html_renderer() -> sprocket@render:renderer(binary()).
html_renderer() ->
{renderer, fun(El) -> _pipe = render(El),
unicode:characters_to_binary(_pipe) end}.
-file("/home/runner/work/sprocket/sprocket/src/sprocket/renderers/html.gleam", 93).
-spec component(sprocket@internal@reconcile:reconciled_element()) -> gleam@string_tree:string_tree().
component(El) ->
render(El).
-file("/home/runner/work/sprocket/sprocket/src/sprocket/renderers/html.gleam", 97).
-spec fragment(list(sprocket@internal@reconcile:reconciled_element())) -> gleam@string_tree:string_tree().
fragment(Children) ->
_pipe = Children,
gleam@list:fold(
_pipe,
gleam@string_tree:new(),
fun(Acc, Child) -> gleam_stdlib:iodata_append(Acc, render(Child)) end
).