Current section
Files
Jump to
Current section
Files
src/glentities@html_encoder.erl
-module(glentities@html_encoder).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([encode/1]).
-spec do_encode(binary(), gleam@string_builder:string_builder()) -> binary().
do_encode(Text, Acc) ->
case Text of
<<""/utf8>> ->
gleam@string_builder:to_string(Acc);
<<"&"/utf8, Rest/binary>> ->
do_encode(Rest, gleam@string_builder:append(Acc, <<"&"/utf8>>));
<<"<"/utf8, Rest@1/binary>> ->
do_encode(Rest@1, gleam@string_builder:append(Acc, <<"<"/utf8>>));
<<">"/utf8, Rest@2/binary>> ->
do_encode(Rest@2, gleam@string_builder:append(Acc, <<">"/utf8>>));
<<"\""/utf8, Rest@3/binary>> ->
do_encode(
Rest@3,
gleam@string_builder:append(Acc, <<"""/utf8>>)
);
<<"'"/utf8, Rest@4/binary>> ->
do_encode(
Rest@4,
gleam@string_builder:append(Acc, <<"'"/utf8>>)
);
Other ->
Maybe_grapheme = gleam@string:pop_grapheme(Other),
case Maybe_grapheme of
{ok, {Grapheme, Rest@5}} ->
do_encode(
Rest@5,
gleam@string_builder:append(Acc, Grapheme)
);
{error, nil} ->
gleam@string_builder:to_string(Acc)
end
end.
-spec encode(binary()) -> binary().
encode(Text) ->
_pipe = Text,
_pipe@1 = unicode:characters_to_nfc_binary(_pipe),
do_encode(_pipe@1, gleam@string_builder:new()).