Current section

Files

Jump to
lustre_ssg src lustre@ssg.erl
Raw

src/lustre@ssg.erl

-module(lustre@ssg).
-compile([no_auto_import, nowarn_unused_vars]).
-export([new/1, add_static_route/3, add_dynamic_route/4, add_static_dir/2, build/1]).
-export_type([config/2, no_static_routes/0, no_static_dir/0, has_static_routes/0, has_static_dir/0, route/0]).
-opaque config(HYQ, HYR) :: {config,
binary(),
gleam@option:option(binary()),
list(route())} |
{gleam_phantom, HYQ, HYR}.
-type no_static_routes() :: any().
-type no_static_dir() :: any().
-type has_static_routes() :: any().
-type has_static_dir() :: any().
-type route() :: {static, binary(), lustre@element:element(nil)} |
{dynamic, binary(), gleam@map:map_(binary(), lustre@element:element(nil))}.
-spec new(binary()) -> config(no_static_routes(), no_static_dir()).
new(Out_dir) ->
{config, Out_dir, none, []}.
-spec routify(binary()) -> binary().
routify(Path) ->
_assert_subject = gleam@regex:from_string(<<"\\s+"/utf8>>),
{ok, Whitespace} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"lustre/ssg"/utf8>>,
function => <<"routify"/utf8>>,
line => 208})
end,
_pipe = gleam@regex:split(Whitespace, Path),
_pipe@1 = gleam@string:join(_pipe, <<"-"/utf8>>),
gleam@string:lowercase(_pipe@1).
-spec add_static_route(
config(any(), HYZ),
binary(),
lustre@element:element(any())
) -> config(has_static_routes(), HYZ).
add_static_route(Config, Path, Page) ->
{config, Out_dir, Static_dir, Routes} = Config,
Route = {static, routify(Path), lustre@element:map(Page, fun(_) -> nil end)},
{config, Out_dir, Static_dir, [Route | Routes]}.
-spec add_dynamic_route(
config(HZG, HZH),
binary(),
gleam@map:map_(binary(), HZK),
fun((HZK) -> lustre@element:element(any()))
) -> config(HZG, HZH).
add_dynamic_route(Config, Path, Data, Page) ->
{config, Out_dir, Static_dir, Routes} = Config,
Route = begin
Path@1 = routify(Path),
Pages = gleam@map:map_values(
Data,
fun(_, Data@1) ->
lustre@element:map(Page(Data@1), fun(_) -> nil end)
end
),
{dynamic, Path@1, Pages}
end,
{config, Out_dir, Static_dir, [Route | Routes]}.
-spec add_static_dir(config(HZR, no_static_dir()), binary()) -> config(HZR, has_static_dir()).
add_static_dir(Config, Path) ->
{config, Out_dir, _, Routes} = Config,
Static_dir = routify(Path),
{config, Out_dir, {some, Static_dir}, Routes}.
-spec trim_slash(binary()) -> binary().
trim_slash(Path) ->
case gleam@string:ends_with(Path, <<"/"/utf8>>) of
true ->
gleam@string:drop_right(Path, 1);
false ->
Path
end.
-spec last_segment(binary()) -> {binary(), binary()}.
last_segment(Path) ->
_assert_subject = gleam@regex:from_string(<<"(.*/)+?(.+)"/utf8>>),
{ok, Segments} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"lustre/ssg"/utf8>>,
function => <<"last_segment"/utf8>>,
line => 223})
end,
_assert_subject@1 = gleam@regex:scan(Segments, Path),
[{match, _, [{some, Leading}, {some, Last}]}] = case _assert_subject@1 of
[{match, _, [{some, _}, {some, _}]}] -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@1,
module => <<"lustre/ssg"/utf8>>,
function => <<"last_segment"/utf8>>,
line => 224})
end,
{Leading, Last}.
-spec build(config(has_static_routes(), any())) -> nil.
build(Config) ->
{config, Out_dir, Static_dir, Routes} = Config,
Out_dir@1 = trim_slash(Out_dir),
_ = simplifile:delete(Out_dir@1),
_assert_subject = case Static_dir of
{some, Path} ->
simplifile:copy_directory(Path, Out_dir@1);
none ->
simplifile:create_directory_all(Out_dir@1)
end,
{ok, _} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"lustre/ssg"/utf8>>,
function => <<"build"/utf8>>,
line => 40})
end,
Routes@1 = gleam@list:sort(
Routes,
fun(A, B) ->
gleam@string:compare(erlang:element(2, A), erlang:element(2, B))
end
),
gleam@list:each(Routes@1, fun(Route) -> case Route of
{static, <<"/"/utf8>>, El} ->
Path@1 = <<Out_dir@1/binary, "/index.html"/utf8>>,
Html = lustre@element:to_string(El),
_assert_subject@1 = simplifile:write(Html, Path@1),
{ok, _} = case _assert_subject@1 of
{ok, _} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@1,
module => <<"lustre/ssg"/utf8>>,
function => <<"build"/utf8>>,
line => 51})
end,
nil;
{static, Path@2, El@1} ->
{Path@3, Name} = last_segment(Path@2),
_ = simplifile:create_directory_all(
<<Out_dir@1/binary, Path@3/binary>>
),
Path@4 = <<<<<<<<Out_dir@1/binary,
(trim_slash(Path@3))/binary>>/binary,
"/"/utf8>>/binary,
Name/binary>>/binary,
".html"/utf8>>,
Html@1 = lustre@element:to_string(El@1),
_assert_subject@2 = simplifile:write(Html@1, Path@4),
{ok, _} = case _assert_subject@2 of
{ok, _} -> _assert_subject@2;
_assert_fail@2 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@2,
module => <<"lustre/ssg"/utf8>>,
function => <<"build"/utf8>>,
line => 61})
end,
nil;
{dynamic, Path@5, Pages} ->
_ = simplifile:create_directory_all(
<<Out_dir@1/binary, Path@5/binary>>
),
gleam@list:each(
gleam@map:to_list(Pages),
fun(_use0) ->
{Page, El@2} = _use0,
Path@6 = <<<<<<<<Out_dir@1/binary,
(trim_slash(Path@5))/binary>>/binary,
"/"/utf8>>/binary,
(routify(Page))/binary>>/binary,
".html"/utf8>>,
Html@2 = lustre@element:to_string(El@2),
_assert_subject@3 = simplifile:write(Html@2, Path@6),
{ok, _} = case _assert_subject@3 of
{ok, _} -> _assert_subject@3;
_assert_fail@3 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@3,
module => <<"lustre/ssg"/utf8>>,
function => <<"build"/utf8>>,
line => 71})
end,
nil
end
)
end end).