Current section
Files
Jump to
Current section
Files
src/mellie.erl
-module(mellie).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/mellie.gleam").
-export([parse/1, elements_to_string/1, element_to_string/1, to_document_string/1, element/3, text/1, inner_text/1, attribute/2, children/1, get_child_by_tag/2, tag/1, attrs/1, get_children_by_tag/2, find_all/2, find_leaf/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/mellie.gleam", 10).
-spec parse(binary()) -> {ok, presentable_soup:element_tree()} |
{error, binary()}.
parse(Str) ->
_pipe = Str,
mellie@internal@html:parse(_pipe).
-file("src/mellie.gleam", 15).
-spec elements_to_string(list(presentable_soup:element_tree())) -> binary().
elements_to_string(El) ->
presentable_soup:elements_to_string(El).
-file("src/mellie.gleam", 19).
-spec element_to_string(presentable_soup:element_tree()) -> binary().
element_to_string(El) ->
_pipe = El,
_pipe@1 = gleam@list:wrap(_pipe),
presentable_soup:elements_to_string(_pipe@1).
-file("src/mellie.gleam", 25).
-spec to_document_string(presentable_soup:element_tree()) -> binary().
to_document_string(El) ->
<<<<"<!doctype html>"/utf8, "\n"/utf8>>/binary,
(element_to_string(El))/binary>>.
-file("src/mellie.gleam", 29).
-spec element(
binary(),
list({binary(), binary()}),
list(presentable_soup:element_tree())
) -> presentable_soup:element_tree().
element(Tag, Attributes, Children) ->
{element_node, Tag, Attributes, Children}.
-file("src/mellie.gleam", 33).
-spec text(binary()) -> presentable_soup:element_tree().
text(Text) ->
{text_node, Text}.
-file("src/mellie.gleam", 38).
?DOC(" Recursively get all text from given element\n").
-spec inner_text(presentable_soup:element_tree()) -> binary().
inner_text(El) ->
case El of
{element_node, _, _, Children} ->
_pipe = Children,
_pipe@1 = gleam@list:map(_pipe, fun inner_text/1),
gleam@string:join(_pipe@1, <<""/utf8>>);
{text_node, Text} ->
Text
end.
-file("src/mellie.gleam", 46).
-spec attribute(EJU, EJV) -> {EJU, EJV}.
attribute(Name, Value) ->
{Name, Value}.
-file("src/mellie.gleam", 73).
?DOC(" Gets the children of an element. `TextNode`s will return `[]`\n").
-spec children(presentable_soup:element_tree()) -> list(presentable_soup:element_tree()).
children(Tree) ->
case Tree of
{element_node, _, _, Children} ->
Children;
_ ->
[]
end.
-file("src/mellie.gleam", 65).
-spec has_tag(presentable_soup:element_tree(), binary()) -> boolean().
has_tag(Tree, Tag) ->
case Tree of
{element_node, T, _, _} ->
Tag =:= T;
_ ->
false
end.
-file("src/mellie.gleam", 50).
-spec get_child_by_tag(presentable_soup:element_tree(), binary()) -> {ok,
presentable_soup:element_tree()} |
{error, nil}.
get_child_by_tag(Tree, Tag) ->
Probe = fun(_capture) -> has_tag(_capture, Tag) end,
Inner = begin
_pipe = Tree,
children(_pipe)
end,
Found = begin
_pipe@1 = Inner,
gleam@list:find(_pipe@1, Probe)
end,
case Found of
{ok, _} ->
Found;
{error, _} ->
_pipe@2 = Inner,
gleam@list:find_map(
_pipe@2,
fun(_capture@1) -> get_child_by_tag(_capture@1, Tag) end
)
end.
-file("src/mellie.gleam", 81).
?DOC(" Gets tag of the given element. `TextNode`s will return `None`\n").
-spec tag(presentable_soup:element_tree()) -> gleam@option:option(binary()).
tag(Tree) ->
case Tree of
{element_node, Tag, _, _} ->
_pipe = Tag,
{some, _pipe};
{text_node, _} ->
none
end.
-file("src/mellie.gleam", 89).
?DOC(" Gets attributes of the given element. `TextNode`s will return `[]`\n").
-spec attrs(presentable_soup:element_tree()) -> list(presentable_soup:element_tree()).
attrs(Tree) ->
case Tree of
{element_node, _, _, Children} ->
Children;
_ ->
[]
end.
-file("src/mellie.gleam", 97).
?DOC(" Gets children with the given tag up to one level of results. Use with `find_all` to recurse further into returned elements\n").
-spec get_children_by_tag(presentable_soup:element_tree(), binary()) -> list(presentable_soup:element_tree()).
get_children_by_tag(Tree, Tag) ->
_pipe = Tree,
_pipe@1 = children(_pipe),
_pipe@2 = gleam@list:map(_pipe@1, fun(Child) -> case has_tag(Child, Tag) of
true ->
[Child];
false ->
get_children_by_tag(Child, Tag)
end end),
lists:append(_pipe@2).
-file("src/mellie.gleam", 114).
?DOC(
" Runs the given function recursively over the result until it no longer results in items.\n"
" Returns the found nodes from every level\n"
).
-spec find_all(EKE, fun((EKE) -> list(EKE))) -> list(EKE).
find_all(In, Fun) ->
Out = Fun(In),
Next = begin
_pipe = Out,
_pipe@1 = gleam@list:map(
_pipe,
fun(_capture) -> find_all(_capture, Fun) end
),
lists:append(_pipe@1)
end,
lists:append(Out, Next).
-file("src/mellie.gleam", 123).
?DOC(
" Runs the given function recursively over the result until it no longer returns items.\n"
" Returns only the deepest matching nodes\n"
).
-spec find_leaf(EKH, fun((EKH) -> list(EKH))) -> list(EKH).
find_leaf(In, Fun) ->
_pipe = Fun(In),
_pipe@1 = gleam@list:map(_pipe, fun(O) -> case find_all(O, Fun) of
[] ->
[O];
Inner ->
Inner
end end),
lists:append(_pipe@1).