Current section

Files

Jump to
chilp src chilp@internal.erl
Raw

src/chilp@internal.erl

-module(chilp@internal).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/chilp/internal.gleam").
-export([sanitise/1, sanitise_ls/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(false).
-file("src/chilp/internal.gleam", 24).
?DOC(false).
-spec sanitise_reconstruct(html_parser:element()) -> binary().
sanitise_reconstruct(El) ->
case El of
empty_element ->
<<""/utf8>>;
{start_element, Name, Attributes, Children} ->
Opener = case Name of
<<"b"/utf8>> ->
<<"<b"/utf8>>;
<<"i"/utf8>> ->
<<"<i"/utf8>>;
<<"em"/utf8>> ->
<<"<em"/utf8>>;
<<"strong"/utf8>> ->
<<"<strong"/utf8>>;
<<"a"/utf8>> ->
<<"<a"/utf8>>;
<<"p"/utf8>> ->
<<"<p"/utf8>>;
<<"br"/utf8>> ->
<<"<br"/utf8>>;
<<"span"/utf8>> ->
<<"<span"/utf8>>;
_ ->
<<""/utf8>>
end,
<<<<<<<<Opener/binary,
(begin
_pipe = gleam@list:map(
Attributes,
fun(Attrib) -> case Attrib of
_ when Opener =:= <<""/utf8>> ->
<<""/utf8>>;
{attribute, <<"href"/utf8>>, Link} ->
<<<<" href=\""/utf8,
Link/binary>>/binary,
"\" "/utf8>>;
{attribute,
<<"class"/utf8>>,
Classes} ->
<<<<" class=\""/utf8,
Classes/binary>>/binary,
"\" "/utf8>>;
{attribute,
<<"target"/utf8>>,
Target} ->
<<<<" target=\""/utf8,
Target/binary>>/binary,
"\" "/utf8>>;
{attribute, _, _} ->
<<""/utf8>>
end end
),
erlang:list_to_binary(_pipe)
end)/binary>>/binary,
(case Opener =:= <<""/utf8>> of
false ->
<<">"/utf8>>;
true ->
<<""/utf8>>
end)/binary>>/binary,
(begin
_pipe@1 = gleam@list:map(
Children,
fun sanitise_reconstruct/1
),
erlang:list_to_binary(_pipe@1)
end)/binary>>/binary,
(case Name of
<<"b"/utf8>> ->
<<"</b>"/utf8>>;
<<"i"/utf8>> ->
<<"</i>"/utf8>>;
<<"em"/utf8>> ->
<<"</em>"/utf8>>;
<<"strong"/utf8>> ->
<<"</strong>"/utf8>>;
<<"a"/utf8>> ->
<<"</a>"/utf8>>;
<<"p"/utf8>> ->
<<"</p>"/utf8>>;
<<"br"/utf8>> ->
<<"</br>"/utf8>>;
<<"span"/utf8>> ->
<<"</span>"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>;
{end_element, _} ->
<<"ERROR: Did not expect an element end here!"/utf8>>;
{content, Cnt} ->
Cnt
end.
-file("src/chilp/internal.gleam", 9).
?DOC(false).
-spec sanitise(binary()) -> binary().
sanitise(Html) ->
_pipe = html_parser:as_tree(Html),
sanitise_reconstruct(_pipe).
-file("src/chilp/internal.gleam", 80).
?DOC(false).
-spec sanitise_reconstruct_ls(html_parser:element()) -> lustre@vdom@vnode:element(any()).
sanitise_reconstruct_ls(El) ->
case El of
empty_element ->
lustre@element:none();
{start_element, Name, Attributes, Children} ->
Attribs = gleam@list:map(Attributes, fun(Attrib) -> case Attrib of
{attribute, <<"href"/utf8>>, Link} ->
lustre@attribute:href(Link);
{attribute, <<"class"/utf8>>, Classes} ->
lustre@attribute:class(Classes);
{attribute, <<"target"/utf8>>, Target} ->
lustre@attribute:target(Target);
{attribute, _, _} ->
lustre@attribute:none()
end end),
_pipe@1 = [begin
_pipe = gleam@list:map(
Children,
fun sanitise_reconstruct_ls/1
),
case Name of
<<"b"/utf8>> ->
fun(_capture) ->
lustre@element@html:b(Attribs, _capture)
end;
<<"i"/utf8>> ->
fun(_capture@1) ->
lustre@element@html:i(Attribs, _capture@1)
end;
<<"em"/utf8>> ->
fun(_capture@2) ->
lustre@element@html:em(Attribs, _capture@2)
end;
<<"strong"/utf8>> ->
fun(_capture@3) ->
lustre@element@html:strong(Attribs, _capture@3)
end;
<<"a"/utf8>> ->
fun(_capture@4) ->
lustre@element@html:a(
[lustre@attribute:class(
<<"link link-secondary-content"/utf8>>
) |
Attribs],
_capture@4
)
end;
<<"p"/utf8>> ->
fun(_capture@5) ->
lustre@element@html:p(Attribs, _capture@5)
end;
<<"br"/utf8>> ->
fun(_) -> lustre@element@html:br(Attribs) end;
<<"span"/utf8>> ->
fun(_capture@6) ->
lustre@element@html:span(Attribs, _capture@6)
end;
_ ->
fun lustre@element:fragment/1
end(_pipe)
end,
lustre@element:text(<<" "/utf8>>)],
lustre@element:fragment(_pipe@1);
{end_element, _} ->
lustre@element:text(
<<"ERROR: Did not expect an element end here!"/utf8>>
);
{content, Cnt} ->
lustre@element:text(Cnt)
end.
-file("src/chilp/internal.gleam", 17).
?DOC(false).
-spec sanitise_ls(binary()) -> lustre@vdom@vnode:element(any()).
sanitise_ls(Html) ->
_pipe = html_parser:as_tree(Html),
sanitise_reconstruct_ls(_pipe).