Current section
Files
Jump to
Current section
Files
src/tale@config.erl
-module(tale@config).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/tale/config.gleam").
-export([public_root/1, load/0, site_paths/1]).
-export_type([site_config/0, site_paths/0, menu_item/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type site_config() :: {site_config,
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
gleam@option:option(integer()),
list(menu_item())}.
-type site_paths() :: {site_paths,
binary(),
binary(),
binary(),
binary(),
binary()}.
-type menu_item() :: {menu_item, binary(), binary(), integer()}.
-file("src/tale/config.gleam", 100).
?DOC(
" Configure public root.\n"
" By default this is `public` but user can change it in the config.toml file.\n"
).
-spec public_root(site_config()) -> binary().
public_root(Config) ->
erlang:element(7, Config).
-file("src/tale/config.gleam", 119).
-spec themed_directory(binary(), binary()) -> binary().
themed_directory(Candidate, Fallback) ->
case simplifile_erl:is_directory(Candidate) of
{ok, true} ->
Candidate;
_ ->
Fallback
end.
-file("src/tale/config.gleam", 178).
-spec page_ref_to_url(binary()) -> binary().
page_ref_to_url(Ref) ->
case Ref of
<<""/utf8>> ->
<<"/"/utf8>>;
<<"/"/utf8>> ->
<<"/"/utf8>>;
Other ->
Cleaned = gleam@string:trim(Other),
With_leading = case gleam_stdlib:string_starts_with(
Cleaned,
<<"/"/utf8>>
) of
true ->
Cleaned;
false ->
<<"/"/utf8, Cleaned/binary>>
end,
case With_leading of
<<""/utf8>> ->
<<"/"/utf8>>;
<<"/"/utf8>> ->
<<"/"/utf8>>;
_ ->
With_leading
end
end.
-file("src/tale/config.gleam", 163).
-spec menu_item_from_table(gleam@dict:dict(binary(), tom:toml())) -> gleam@option:option(menu_item()).
menu_item_from_table(Table) ->
Label = tale@util:optional_string(tom:get_string(Table, [<<"name"/utf8>>])),
Page_ref = tale@util:optional_string(
tom:get_string(Table, [<<"pageRef"/utf8>>])
),
Weight = tale@util:optional_int(tom:get_int(Table, [<<"weight"/utf8>>])),
case {Label, Page_ref} of
{{some, Label@1}, {some, Ref}} ->
{some,
{menu_item,
Label@1,
page_ref_to_url(Ref),
gleam@option:unwrap(Weight, 0)}};
{_, _} ->
none
end.
-file("src/tale/config.gleam", 155).
-spec menu_item_from(tom:toml()) -> gleam@option:option(menu_item()).
menu_item_from(Entry) ->
case Entry of
{table, Table} ->
menu_item_from_table(Table);
{inline_table, Table@1} ->
menu_item_from_table(Table@1);
_ ->
none
end.
-file("src/tale/config.gleam", 139).
-spec parse_menu_entries(list(tom:toml()), list(menu_item())) -> list(menu_item()).
parse_menu_entries(Entries, Acc) ->
case Entries of
[] ->
lists:reverse(Acc);
[Entry | Rest] ->
Acc@1 = case menu_item_from(Entry) of
{some, Item} ->
[Item | Acc];
none ->
Acc
end,
parse_menu_entries(Rest, Acc@1)
end.
-file("src/tale/config.gleam", 198).
?DOC(" Sorting menu items per weight\n").
-spec sort_menu_items(list(menu_item())) -> list(menu_item()).
sort_menu_items(Items) ->
gleam@list:sort(
Items,
fun(A, B) -> case erlang:element(4, A) =:= erlang:element(4, B) of
true ->
gleam@string:compare(
erlang:element(2, A),
erlang:element(2, B)
);
false ->
gleam@int:compare(
erlang:element(4, A),
erlang:element(4, B)
)
end end
).
-file("src/tale/config.gleam", 127).
?DOC(" Parse functions for the menu items\n").
-spec parse_menu(gleam@dict:dict(binary(), tom:toml()), list(binary())) -> list(menu_item()).
parse_menu(Doc, Key) ->
case tom:get_array(Doc, Key) of
{ok, Entries} ->
_pipe = parse_menu_entries(Entries, []),
sort_menu_items(_pipe);
{error, _} ->
[]
end.
-file("src/tale/config.gleam", 58).
?DOC(" Loads the configuration file\n").
-spec load() -> {ok, site_config()} | {error, binary()}.
load() ->
gleam@result:'try'(
begin
_pipe = simplifile:read(<<"config.toml"/utf8>>),
gleam@result:map_error(
_pipe,
fun(Err) ->
<<<<<<"Unable to read config at "/utf8, "config.toml"/utf8>>/binary,
": "/utf8>>/binary,
(simplifile:describe_error(Err))/binary>>
end
)
end,
fun(Contents) ->
gleam@result:'try'(
begin
_pipe@1 = tom:parse(Contents),
gleam@result:map_error(
_pipe@1,
fun(Err@1) ->
<<<<<<"Unable to parse config at "/utf8,
"config.toml"/utf8>>/binary,
": "/utf8>>/binary,
(tale@util:describe_toml_error(Err@1))/binary>>
end
)
end,
fun(Doc) ->
Legacy_base_url = tale@util:get_string_or(
Doc,
[<<"base_url"/utf8>>],
<<"/"/utf8>>
),
Configured_base_url = begin
_pipe@2 = tale@util:get_string_or(
Doc,
[<<"baseUrl"/utf8>>],
Legacy_base_url
),
tale@util:normalize_base_url(_pipe@2)
end,
Menu_main = parse_menu(
Doc,
[<<"menus"/utf8>>, <<"main"/utf8>>]
),
{ok,
{site_config,
tale@util:get_string_or(
Doc,
[<<"title"/utf8>>],
<<"Untitled site"/utf8>>
),
tale@util:get_string_or(
Doc,
[<<"theme"/utf8>>],
<<"default"/utf8>>
),
tale@util:get_string_or(
Doc,
[<<"description"/utf8>>],
<<""/utf8>>
),
tale@util:get_string_or(
Doc,
[<<"author"/utf8>>],
<<"Anonymous"/utf8>>
),
Configured_base_url,
tale@util:get_string_or(
Doc,
[<<"publishDir"/utf8>>],
<<"public"/utf8>>
),
tale@util:get_string_or(
Doc,
[<<"contentDir"/utf8>>],
<<"contents"/utf8>>
),
tale@util:optional_int(
tom:get_int(Doc, [<<"pagination"/utf8>>])
),
Menu_main}}
end
)
end
).
-file("src/tale/config.gleam", 105).
?DOC(" Site path for the themes\n").
-spec site_paths(site_config()) -> site_paths().
site_paths(Config) ->
Theme_root = <<"themes/"/utf8, (erlang:element(3, Config))/binary>>,
{site_paths,
erlang:element(8, Config),
themed_directory(
<<Theme_root/binary, "/layouts"/utf8>>,
<<"layouts"/utf8>>
),
themed_directory(
<<Theme_root/binary, "/layouts/_partials"/utf8>>,
<<"layouts/_partials"/utf8>>
),
themed_directory(
<<Theme_root/binary, "/assets"/utf8>>,
<<"assets"/utf8>>
),
themed_directory(
<<Theme_root/binary, "/static"/utf8>>,
<<"static"/utf8>>
)}.