Current section
Files
Jump to
Current section
Files
src/internal@html.erl
-module(internal@html).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/internal/html.gleam").
-export([element/3, text/1, parse_/1, parse/1]).
-file("src/internal/html.gleam", 6).
-spec element(
binary(),
list({binary(), binary()}),
list(presentable_soup:element_tree())
) -> presentable_soup:element_tree().
element(Tag, Attrs, Children) ->
{element_node, Tag, Attrs, Children}.
-file("src/internal/html.gleam", 10).
-spec text(binary()) -> presentable_soup:element_tree().
text(Text) ->
{text_node, Text}.
-file("src/internal/html.gleam", 14).
-spec is_tag(presentable_soup:element_tree(), binary()) -> boolean().
is_tag(El, Tag) ->
case El of
{element_node, T, _, _} ->
T =:= Tag;
{text_node, _} ->
false
end.
-file("src/internal/html.gleam", 21).
-spec with_body(list(presentable_soup:element_tree())) -> list(presentable_soup:element_tree()).
with_body(Children) ->
_pipe = Children,
lists:append(_pipe, [{element_node, <<"body"/utf8>>, [], []}]).
-file("src/internal/html.gleam", 25).
-spec in_body(list(presentable_soup:element_tree())) -> list(presentable_soup:element_tree()).
in_body(Children) ->
[{element_node, <<"body"/utf8>>, [], Children}].
-file("src/internal/html.gleam", 29).
-spec with_head(list(presentable_soup:element_tree())) -> list(presentable_soup:element_tree()).
with_head(Children) ->
[{element_node, <<"head"/utf8>>, [], []} | Children].
-file("src/internal/html.gleam", 33).
-spec ensure_root(presentable_soup:element_tree()) -> presentable_soup:element_tree().
ensure_root(Root) ->
case Root of
{text_node, _} ->
{element_node,
<<"html"/utf8>>,
[],
begin
_pipe = [Root],
_pipe@1 = in_body(_pipe),
with_head(_pipe@1)
end};
{element_node, Tag, _, Children} ->
Head = begin
_pipe@2 = Children,
gleam@list:find(
_pipe@2,
fun(_capture) -> is_tag(_capture, <<"head"/utf8>>) end
)
end,
Body = begin
_pipe@3 = Children,
gleam@list:find(
_pipe@3,
fun(_capture@1) -> is_tag(_capture@1, <<"body"/utf8>>) end
)
end,
case Tag of
<<"body"/utf8>> ->
{element_node,
<<"html"/utf8>>,
[],
begin
_pipe@4 = [Root],
with_head(_pipe@4)
end};
<<"head"/utf8>> ->
{element_node,
<<"html"/utf8>>,
[],
begin
_pipe@5 = [Root],
with_body(_pipe@5)
end};
<<"html"/utf8>> ->
case {Head, Body} of
{{ok, _}, {ok, _}} ->
Root;
{{ok, _}, {error, _}} ->
{element_node,
erlang:element(2, Root),
erlang:element(3, Root),
begin
_pipe@6 = Children,
with_body(_pipe@6)
end};
{{error, _}, {ok, _}} ->
{element_node,
erlang:element(2, Root),
erlang:element(3, Root),
begin
_pipe@7 = Children,
with_head(_pipe@7)
end};
{{error, _}, {error, _}} ->
{element_node,
erlang:element(2, Root),
erlang:element(3, Root),
begin
_pipe@8 = erlang:element(4, Root),
_pipe@9 = in_body(_pipe@8),
with_head(_pipe@9)
end}
end;
_ ->
{element_node,
<<"html"/utf8>>,
[],
[{element_node, <<"head"/utf8>>, [], []},
{element_node, <<"body"/utf8>>, [], [Root]}]}
end
end.
-file("src/internal/html.gleam", 76).
-spec parse_(binary()) -> {ok, presentable_soup:element_tree()} |
{error, binary()}.
parse_(Html) ->
_pipe = presentable_soup:element(
[presentable_soup:with_tag(<<"html"/utf8>>)]
),
_pipe@1 = presentable_soup:return(_pipe, presentable_soup:element_tree()),
_pipe@2 = presentable_soup:scrape(_pipe@1, Html),
gleam@result:replace_error(_pipe@2, <<"presentable_soup error"/utf8>>).
-file("src/internal/html.gleam", 83).
-spec parse(binary()) -> {ok, presentable_soup:element_tree()} |
{error, binary()}.
parse(Html) ->
_pipe = Html,
_pipe@1 = gleam@string:trim(_pipe),
_pipe@2 = parse_(_pipe@1),
gleam@result:map(_pipe@2, fun ensure_root/1).