-module(arctic). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([build/2]). -export_type([processed_collection/0]). -type processed_collection() :: {processed_collection, common:collection(), list(common:page())}. -spec read_collection(common:collection()) -> {ok, list(common:page())} | {error, snag:snag()}. read_collection(Collection) -> gleam@result:'try'( begin _pipe = simplifile:get_files(erlang:element(2, Collection)), gleam@result:map_error( _pipe, fun(Err) -> snag:new( <<<<<<<<"couldn't get files in `"/utf8, (erlang:element(2, Collection))/binary>>/binary, "` ("/utf8>>/binary, (simplifile:describe_error(Err))/binary>>/binary, ")"/utf8>> ) end ) end, fun(Paths) -> _pipe@1 = gleam@list:try_fold( Paths, [], fun(So_far, Path) -> gleam@result:'try'( gleam@result:map_error( simplifile:read(Path), fun(Err@1) -> snag:new( <<<<<<<<"could not load file `"/utf8, Path/binary>>/binary, "` ("/utf8>>/binary, (simplifile:describe_error(Err@1))/binary>>/binary, ")"/utf8>> ) end ), fun(Content) -> gleam@result:'try'( (erlang:element(3, Collection))(Content), fun(P) -> {ok, [P | So_far]} end ) end ) end ), gleam@result:map(_pipe@1, fun lists:reverse/1) end ). -spec process(list(common:collection())) -> {ok, list(processed_collection())} | {error, snag:snag()}. process(Collections) -> gleam@list:try_fold( Collections, [], fun(Rest, Collection) -> gleam@result:'try'( read_collection(Collection), fun(Pages_unsorted) -> Pages = gleam@list:sort( Pages_unsorted, fun(P, Q) -> (erlang:element(3, P))(Q) end ), {ok, [{processed_collection, Collection, Pages} | Rest]} end ) end ). -spec build(common:config(), list(common:collection())) -> {ok, nil} | {error, snag:snag()}. build(Config, Collections) -> gleam@result:'try'( process(Collections), fun(Processed_collections) -> gleam@result:'try'( begin _pipe = lustre@ssg:new(<<"site"/utf8>>), _pipe@1 = lustre@ssg:use_index_routes(_pipe), _pipe@2 = lustre@ssg:add_static_route( _pipe@1, <<"/"/utf8>>, (erlang:element(2, Config))(Collections) ), _pipe@3 = gleam@list:fold( erlang:element(3, Config), _pipe@2, fun(Ssg_config, Page) -> lustre@ssg:add_static_route( Ssg_config, <<"/"/utf8, (erlang:element(2, Page))/binary>>, erlang:element(3, Page) ) end ), gleam@list:try_fold( Processed_collections, _pipe@3, fun(Ssg_config@1, Processed) -> Ssg_config2 = case erlang:element( 4, erlang:element(2, Processed) ) of {some, Render} -> lustre@ssg:add_static_route( Ssg_config@1, <<"/"/utf8, (erlang:element( 2, erlang:element(2, Processed) ))/binary>>, Render(erlang:element(3, Processed)) ); none -> Ssg_config@1 end, _pipe@4 = gleam@list:fold( erlang:element(3, Processed), Ssg_config2, fun(S, P) -> lustre@ssg:add_static_route( S, <<<<<<"/"/utf8, (erlang:element( 2, erlang:element( 2, Processed ) ))/binary>>/binary, "/"/utf8>>/binary, (erlang:element(2, P))/binary>>, erlang:element(4, P) ) end ), {ok, _pipe@4} end ) end, fun(Ssg_config@2) -> gleam@result:'try'( begin _pipe@5 = lustre@ssg:build(Ssg_config@2), gleam@result:map_error( _pipe@5, fun(Err) -> case Err of {cannot_create_temp_directory, File_err} -> snag:new( <<<<"couldn't create temp directory ("/utf8, (simplifile:describe_error( File_err ))/binary>>/binary, ")"/utf8>> ); {cannot_write_static_asset, File_err@1, Path} -> snag:new( <<<<<<<<"couldn't put asset at `"/utf8, Path/binary>>/binary, "` ("/utf8>>/binary, (simplifile:describe_error( File_err@1 ))/binary>>/binary, ")"/utf8>> ); {cannot_generate_route, File_err@2, Path@1} -> snag:new( <<<<<<<<"couldn't generate `"/utf8, Path@1/binary>>/binary, "` ("/utf8>>/binary, (simplifile:describe_error( File_err@2 ))/binary>>/binary, ")"/utf8>> ); {cannot_write_to_output_dir, File_err@3} -> snag:new( <<<<"couldn't move from temp directory to output directory ("/utf8, (simplifile:describe_error( File_err@3 ))/binary>>/binary, ")"/utf8>> ); {cannot_cleanup_temp_dir, File_err@4} -> snag:new( <<<<"couldn't remove temp directory ("/utf8, (simplifile:describe_error( File_err@4 ))/binary>>/binary, ")"/utf8>> ) end end ) end, fun(_) -> gleam@result:'try'( begin _pipe@6 = simplifile:create_directory( <<"site/public"/utf8>> ), gleam@result:map_error( _pipe@6, fun(Err@1) -> snag:new( <<<<"couldn't create directory `site/public` ("/utf8, (simplifile:describe_error( Err@1 ))/binary>>/binary, ")"/utf8>> ) end ) end, fun(_) -> gleam@result:'try'( begin _pipe@7 = simplifile:copy_directory( <<"public"/utf8>>, <<"site/public"/utf8>> ), gleam@result:map_error( _pipe@7, fun(Err@2) -> snag:new( <<<<"couldn't copy `public` to `site/public` ("/utf8, (simplifile:describe_error( Err@2 ))/binary>>/binary, ")"/utf8>> ) end ) end, fun(_) -> gleam@result:'try'( begin _pipe@8 = simplifile:create_file( <<"site/public/feed.rss"/utf8>> ), gleam@result:map_error( _pipe@8, fun(Err@3) -> snag:new( <<<<"couldn't create file `site/public/feed.rss` ("/utf8, (simplifile:describe_error( Err@3 ))/binary>>/binary, ")"/utf8>> ) end ) end, fun(_) -> gleam@list:try_each( Processed_collections, fun(Processed@1) -> case erlang:element( 5, erlang:element( 2, Processed@1 ) ) of {some, Render@1} -> _pipe@9 = simplifile:write( <<"site/public/feed.rss"/utf8>>, Render@1( erlang:element( 3, Processed@1 ) ) ), gleam@result:map_error( _pipe@9, fun( Err@4 ) -> snag:new( <<<<"couldn't write to `site/public/feed.rss` ("/utf8, (simplifile:describe_error( Err@4 ))/binary>>/binary, ")"/utf8>> ) end ); none -> {ok, nil} end end ) end ) end ) end ) end ) end ) end ).