Current section

Files

Jump to
tale src tale@util.erl
Raw

src/tale@util.erl

-module(tale@util).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/tale/util.gleam").
-export([normalize_base_url/1, absolute_url/2, get_string_or/3, get_bool_or/3, optional_string/1, optional_int/1, get_string_list_or/3, describe_toml_error/1, describe_tokenizer_error/1, describe_runtime_error/1, slugify/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.
-file("src/tale/util.gleam", 17).
-spec drop_trailing_slashes(binary()) -> binary().
drop_trailing_slashes(Value) ->
case Value of
<<""/utf8>> ->
<<""/utf8>>;
<<"/"/utf8>> ->
<<""/utf8>>;
_ ->
case gleam_stdlib:string_ends_with(Value, <<"/"/utf8>>) of
true ->
drop_trailing_slashes(gleam@string:drop_end(Value, 1));
false ->
Value
end
end.
-file("src/tale/util.gleam", 29).
-spec normalize_root(binary()) -> binary().
normalize_root(Value) ->
case Value of
<<"/"/utf8>> ->
<<""/utf8>>;
Other ->
Other
end.
-file("src/tale/util.gleam", 10).
?DOC(" Normalize the configured base URL so that joining paths is predictable.\n").
-spec normalize_base_url(binary()) -> binary().
normalize_base_url(Value) ->
_pipe = Value,
_pipe@1 = gleam@string:trim(_pipe),
_pipe@2 = drop_trailing_slashes(_pipe@1),
normalize_root(_pipe@2).
-file("src/tale/util.gleam", 57).
-spec base_or_root(binary()) -> binary().
base_or_root(Base_url) ->
case Base_url of
<<""/utf8>> ->
<<"/"/utf8>>;
Other ->
Other
end.
-file("src/tale/util.gleam", 64).
-spec ensure_leading_slash(binary()) -> binary().
ensure_leading_slash(Path) ->
case gleam_stdlib:string_starts_with(Path, <<"/"/utf8>>) of
true ->
Path;
false ->
<<"/"/utf8, Path/binary>>
end.
-file("src/tale/util.gleam", 71).
-spec is_absolute_path(binary()) -> boolean().
is_absolute_path(Value) ->
(gleam_stdlib:string_starts_with(Value, <<"http://"/utf8>>) orelse gleam_stdlib:string_starts_with(
Value,
<<"https://"/utf8>>
))
orelse gleam_stdlib:string_starts_with(Value, <<"//"/utf8>>).
-file("src/tale/util.gleam", 38).
?DOC(
" Build an absolute URL using the configured base URL and a site path.\n"
" Paths starting with a scheme (e.g. https://) are returned untouched.\n"
).
-spec absolute_url(binary(), binary()) -> binary().
absolute_url(Base_url, Path) ->
Cleaned_path = gleam@string:trim(Path),
case Cleaned_path of
<<""/utf8>> ->
base_or_root(Base_url);
Other ->
case is_absolute_path(Other) of
true ->
Other;
false ->
Segment = ensure_leading_slash(Other),
case Base_url of
<<""/utf8>> ->
Segment;
Base ->
<<Base/binary, Segment/binary>>
end
end
end.
-file("src/tale/util.gleam", 78).
?DOC(" get string or\n").
-spec get_string_or(
gleam@dict:dict(binary(), tom:toml()),
list(binary()),
binary()
) -> binary().
get_string_or(Doc, Key, Fallback) ->
case tom:get_string(Doc, Key) of
{ok, Value} ->
Value;
{error, _} ->
Fallback
end.
-file("src/tale/util.gleam", 90).
?DOC(" get bool or\n").
-spec get_bool_or(
gleam@dict:dict(binary(), tom:toml()),
list(binary()),
boolean()
) -> boolean().
get_bool_or(Doc, Key, Fallback) ->
case tom:get_bool(Doc, Key) of
{ok, Value} ->
Value;
{error, _} ->
Fallback
end.
-file("src/tale/util.gleam", 118).
?DOC(" Optional string\n").
-spec optional_string({ok, binary()} | {error, tom:get_error()}) -> gleam@option:option(binary()).
optional_string(Result) ->
case Result of
{ok, Value} ->
{some, Value};
{error, _} ->
none
end.
-file("src/tale/util.gleam", 127).
-spec optional_int({ok, integer()} | {error, tom:get_error()}) -> gleam@option:option(integer()).
optional_int(Result) ->
case Result of
{ok, Value} ->
{some, Value};
{error, _} ->
none
end.
-file("src/tale/util.gleam", 135).
?DOC(" Toml array to strings\n").
-spec toml_array_to_strings(list(tom:toml()), list(binary())) -> gleam@option:option(list(binary())).
toml_array_to_strings(Values, Acc) ->
case Values of
[] ->
{some, lists:reverse(Acc)};
[{string, Value} | Rest] ->
toml_array_to_strings(Rest, [Value | Acc]);
[_ | _] ->
none
end.
-file("src/tale/util.gleam", 102).
?DOC(" Get string list\n").
-spec get_string_list_or(
gleam@dict:dict(binary(), tom:toml()),
list(binary()),
list(binary())
) -> list(binary()).
get_string_list_or(Doc, Key, Fallback) ->
case tom:get_array(Doc, Key) of
{ok, Values} ->
case toml_array_to_strings(Values, []) of
{some, Strings} ->
Strings;
none ->
Fallback
end;
{error, _} ->
Fallback
end.
-file("src/tale/util.gleam", 147).
?DOC(" TOML Errors description\n").
-spec describe_toml_error(tom:parse_error()) -> binary().
describe_toml_error(Err) ->
case Err of
{unexpected, Got, Expected} ->
<<<<<<"Unexpected \""/utf8, Got/binary>>/binary,
"\", expected "/utf8>>/binary,
Expected/binary>>;
{key_already_in_use, Key} ->
<<<<"Duplicate key \""/utf8,
(gleam@string:join(Key, <<"."/utf8>>))/binary>>/binary,
"\""/utf8>>
end.
-file("src/tale/util.gleam", 156).
-spec describe_tokenizer_error(handles@error:tokenizer_error()) -> binary().
describe_tokenizer_error(Err) ->
case Err of
{unbalanced_tag, Index} ->
<<"Unbalanced tag near character "/utf8,
(erlang:integer_to_binary(Index))/binary>>;
{unbalanced_block, Index@1} ->
<<"Unbalanced block near character "/utf8,
(erlang:integer_to_binary(Index@1))/binary>>;
{missing_argument, Index@2} ->
<<"Missing argument near character "/utf8,
(erlang:integer_to_binary(Index@2))/binary>>;
{missing_block_kind, Index@3} ->
<<"Missing block kind near character "/utf8,
(erlang:integer_to_binary(Index@3))/binary>>;
{missing_partial_id, Index@4} ->
<<"Missing partial id near character "/utf8,
(erlang:integer_to_binary(Index@4))/binary>>;
{unexpected_multiple_arguments, Index@5} ->
<<"Too many arguments near character "/utf8,
(erlang:integer_to_binary(Index@5))/binary>>;
{unexpected_argument, Index@6} ->
<<"Unexpected argument near character "/utf8,
(erlang:integer_to_binary(Index@6))/binary>>;
{unexpected_block_kind, Index@7} ->
<<"Unexpected block kind near character "/utf8,
(erlang:integer_to_binary(Index@7))/binary>>;
{unexpected_block_end, Index@8} ->
<<"Unexpected block end near character "/utf8,
(erlang:integer_to_binary(Index@8))/binary>>
end.
-file("src/tale/util.gleam", 199).
-spec describe_path(list(binary())) -> binary().
describe_path(Path) ->
case Path of
[] ->
<<"(root)"/utf8>>;
Props ->
gleam@string:join(Props, <<"."/utf8>>)
end.
-file("src/tale/util.gleam", 179).
-spec describe_runtime_error(handles@error:runtime_error()) -> binary().
describe_runtime_error(Err) ->
case Err of
{unexpected_type, Index, Path, Got, Expected} ->
<<<<<<<<<<<<<<"Unexpected value at "/utf8,
(describe_path(Path))/binary>>/binary,
" (near character "/utf8>>/binary,
(erlang:integer_to_binary(Index))/binary>>/binary,
"): got "/utf8>>/binary,
Got/binary>>/binary,
", expected one of "/utf8>>/binary,
(gleam@string:join(Expected, <<", "/utf8>>))/binary>>;
{unknown_property, Index@1, Path@1} ->
<<<<<<"Unknown property "/utf8, (describe_path(Path@1))/binary>>/binary,
" near character "/utf8>>/binary,
(erlang:integer_to_binary(Index@1))/binary>>;
{unknown_partial, _, Id} ->
<<<<"Unknown partial \""/utf8, Id/binary>>/binary, "\""/utf8>>
end.
-file("src/tale/util.gleam", 233).
-spec is_alphanumeric(binary()) -> boolean().
is_alphanumeric(Char) ->
case gleam@string:to_utf_codepoints(Char) of
[Codepoint | _] ->
Value = gleam_stdlib:identity(Codepoint),
Is_digit = (Value >= 48) andalso (Value =< 57),
Is_lower = (Value >= 97) andalso (Value =< 122),
Is_upper = (Value >= 65) andalso (Value =< 90),
(Is_digit orelse Is_lower) orelse Is_upper;
_ ->
false
end.
-file("src/tale/util.gleam", 226).
-spec is_allowed_slug_char(binary()) -> boolean().
is_allowed_slug_char(Char) ->
case Char of
<<"-"/utf8>> ->
true;
Other ->
is_alphanumeric(Other)
end.
-file("src/tale/util.gleam", 213).
-spec replace_for_slug(binary()) -> binary().
replace_for_slug(Value) ->
_pipe = Value,
_pipe@1 = gleam@string:to_graphemes(_pipe),
_pipe@2 = gleam@list:fold(
_pipe@1,
[],
fun(Acc, Char) -> case is_allowed_slug_char(Char) of
true ->
[Char | Acc];
false ->
[<<"-"/utf8>> | Acc]
end end
),
_pipe@3 = lists:reverse(_pipe@2),
erlang:list_to_binary(_pipe@3).
-file("src/tale/util.gleam", 206).
-spec slugify(binary()) -> binary().
slugify(Value) ->
_pipe = Value,
_pipe@1 = string:lowercase(_pipe),
_pipe@2 = replace_for_slug(_pipe@1),
gleam@string:replace(_pipe@2, <<"--"/utf8>>, <<"-"/utf8>>).