Current section
Files
Jump to
Current section
Files
src/glugify@internal@entities.erl
-module(glugify@internal@entities).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glugify/internal/entities.gleam").
-export([decode/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/glugify/internal/entities.gleam", 99).
?DOC(false).
-spec codepoint_to_string(integer()) -> {ok, binary()} | {error, nil}.
codepoint_to_string(Code) ->
case gleam@string:utf_codepoint(Code) of
{ok, Codepoint} ->
{ok, gleam_stdlib:utf_codepoint_list_to_string([Codepoint])};
{error, nil} ->
{error, nil}
end.
-file("src/glugify/internal/entities.gleam", 83).
?DOC(false).
-spec decode_numeric_entity(binary()) -> {ok, binary()} | {error, nil}.
decode_numeric_entity(Name) ->
case Name of
<<"#x"/utf8, Hex/binary>> ->
case gleam@int:base_parse(Hex, 16) of
{ok, Code} ->
codepoint_to_string(Code);
{error, nil} ->
{error, nil}
end;
<<"#X"/utf8, Hex/binary>> ->
case gleam@int:base_parse(Hex, 16) of
{ok, Code} ->
codepoint_to_string(Code);
{error, nil} ->
{error, nil}
end;
<<"#"/utf8, Decimal/binary>> ->
case gleam_stdlib:parse_int(Decimal) of
{ok, Code@1} ->
codepoint_to_string(Code@1);
{error, nil} ->
{error, nil}
end;
_ ->
{error, nil}
end.
-file("src/glugify/internal/entities.gleam", 56).
?DOC(false).
-spec decode_entity(binary()) -> {ok, binary()} | {error, nil}.
decode_entity(Name) ->
case Name of
<<"amp"/utf8>> ->
{ok, <<"&"/utf8>>};
<<"lt"/utf8>> ->
{ok, <<"<"/utf8>>};
<<"gt"/utf8>> ->
{ok, <<">"/utf8>>};
<<"quot"/utf8>> ->
{ok, <<"\""/utf8>>};
<<"apos"/utf8>> ->
{ok, <<"'"/utf8>>};
<<"nbsp"/utf8>> ->
{ok, <<"\x{00A0}"/utf8>>};
<<"ndash"/utf8>> ->
{ok, <<"–"/utf8>>};
<<"mdash"/utf8>> ->
{ok, <<"—"/utf8>>};
<<"hellip"/utf8>> ->
{ok, <<"…"/utf8>>};
<<"lsquo"/utf8>> ->
{ok, <<"‘"/utf8>>};
<<"rsquo"/utf8>> ->
{ok, <<"’"/utf8>>};
<<"ldquo"/utf8>> ->
{ok, <<"“"/utf8>>};
<<"rdquo"/utf8>> ->
{ok, <<"”"/utf8>>};
<<"copy"/utf8>> ->
{ok, <<"©"/utf8>>};
<<"reg"/utf8>> ->
{ok, <<"®"/utf8>>};
<<"trade"/utf8>> ->
{ok, <<"™"/utf8>>};
<<"deg"/utf8>> ->
{ok, <<"°"/utf8>>};
<<"euro"/utf8>> ->
{ok, <<"€"/utf8>>};
<<"pound"/utf8>> ->
{ok, <<"£"/utf8>>};
<<"yen"/utf8>> ->
{ok, <<"¥"/utf8>>};
<<"cent"/utf8>> ->
{ok, <<"¢"/utf8>>};
_ ->
decode_numeric_entity(Name)
end.
-file("src/glugify/internal/entities.gleam", 36).
?DOC(false).
-spec take_entity(list(binary()), list(binary()), integer()) -> {ok,
{binary(), list(binary())}} |
{error, nil}.
take_entity(Graphemes, Acc, Length) ->
case Graphemes of
[<<";"/utf8>> | Rest] ->
case Acc of
[] ->
{error, nil};
_ ->
{ok,
{gleam@string:join(lists:reverse(Acc), <<""/utf8>>),
Rest}}
end;
[Grapheme | Rest@1] ->
case Length > 10 of
true ->
{error, nil};
false ->
take_entity(Rest@1, [Grapheme | Acc], Length + 1)
end;
[] ->
{error, nil}
end.
-file("src/glugify/internal/entities.gleam", 16).
?DOC(false).
-spec decode_graphemes(list(binary()), list(binary())) -> list(binary()).
decode_graphemes(Graphemes, Acc) ->
case Graphemes of
[] ->
lists:reverse(Acc);
[<<"&"/utf8>> | Rest] ->
case take_entity(Rest, [], 0) of
{ok, {Name, Remaining}} ->
case decode_entity(Name) of
{ok, Replacement} ->
decode_graphemes(Remaining, [Replacement | Acc]);
{error, nil} ->
decode_graphemes(Rest, [<<"&"/utf8>> | Acc])
end;
{error, nil} ->
decode_graphemes(Rest, [<<"&"/utf8>> | Acc])
end;
[Grapheme | Rest@1] ->
decode_graphemes(Rest@1, [Grapheme | Acc])
end.
-file("src/glugify/internal/entities.gleam", 9).
?DOC(false).
-spec decode(binary()) -> binary().
decode(Text) ->
_pipe = Text,
_pipe@1 = gleam@string:to_graphemes(_pipe),
_pipe@2 = decode_graphemes(_pipe@1, []),
gleam@string:join(_pipe@2, <<""/utf8>>).