-module(tale@generators). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/tale/generators.gleam"). -export([new_post_gen/1, new_site_gen/1, new_theme_gen/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/generators.gleam", 130). -spec copy_directory(binary(), binary()) -> {ok, nil} | {error, binary()}. copy_directory(Src, Dest) -> _pipe = simplifile:copy_directory(Src, Dest), gleam@result:map_error( _pipe, fun(Err) -> <<<<<<<<<<"Unable to copy directory "/utf8, Src/binary>>/binary, " -> "/utf8>>/binary, Dest/binary>>/binary, ": "/utf8>>/binary, (simplifile:describe_error(Err))/binary>> end ). -file("src/tale/generators.gleam", 142). -spec path_exists(binary()) -> {ok, boolean()} | {error, binary()}. path_exists(Path) -> case simplifile_erl:file_info(Path) of {ok, _} -> {ok, true}; {error, enoent} -> {ok, false}; {error, Err} -> {error, <<<<<<"Unable to inspect "/utf8, Path/binary>>/binary, ": "/utf8>>/binary, (simplifile:describe_error(Err))/binary>>} end. -file("src/tale/generators.gleam", 213). -spec matches_key(binary(), binary()) -> boolean(). matches_key(Line, Key) -> gleam_stdlib:string_starts_with(Line, <>) orelse gleam_stdlib:string_starts_with( Line, < binary(). replace_property(Contents, Key, Line) -> {Replaced, Reversed} = begin _pipe = gleam@string:split(Contents, <<"\n"/utf8>>), gleam@list:fold( _pipe, {false, []}, fun(State, Current) -> {Done, Acc} = State, case Done of true -> {true, [Current | Acc]}; false -> Trimmed = gleam@string:trim(Current), case matches_key(Trimmed, Key) of true -> {true, [Line | Acc]}; false -> {false, [Current | Acc]} end end end ) end, Rebuilt = begin _pipe@1 = Reversed, _pipe@2 = lists:reverse(_pipe@1), gleam@string:join(_pipe@2, <<"\n"/utf8>>) end, case Replaced of true -> Rebuilt; false -> <<<>/binary, Rebuilt/binary>> end. -file("src/tale/generators.gleam", 217). -spec quoted(binary()) -> binary(). quoted(Value) -> Escaped = gleam@string:replace(Value, <<"\""/utf8>>, <<"\\\""/utf8>>), <<<<"\""/utf8, Escaped/binary>>/binary, "\""/utf8>>. -file("src/tale/generators.gleam", 153). -spec update_config_title(binary(), binary()) -> {ok, nil} | {error, binary()}. update_config_title(Path, Title) -> gleam@result:'try'( begin _pipe = simplifile:read(Path), gleam@result:map_error( _pipe, fun(Err) -> <<<<<<"Unable to read "/utf8, Path/binary>>/binary, ": "/utf8>>/binary, (simplifile:describe_error(Err))/binary>> end ) end, fun(Contents) -> Updated = replace_property( Contents, <<"title"/utf8>>, <<"title = "/utf8, (quoted(Title))/binary>> ), _pipe@1 = simplifile:write(Path, Updated), gleam@result:map_error( _pipe@1, fun(Err@1) -> <<<<<<"Unable to write "/utf8, Path/binary>>/binary, ": "/utf8>>/binary, (simplifile:describe_error(Err@1))/binary>> end ) end ). -file("src/tale/generators.gleam", 169). -spec update_theme_name(binary(), binary()) -> {ok, nil} | {error, binary()}. update_theme_name(Path, Name) -> gleam@result:'try'( begin _pipe = simplifile:read(Path), gleam@result:map_error( _pipe, fun(Err) -> <<<<<<"Unable to read "/utf8, Path/binary>>/binary, ": "/utf8>>/binary, (simplifile:describe_error(Err))/binary>> end ) end, fun(Contents) -> Updated = replace_property( Contents, <<"name"/utf8>>, <<"name = "/utf8, (quoted(Name))/binary>> ), _pipe@1 = simplifile:write(Path, Updated), gleam@result:map_error( _pipe@1, fun(Err@1) -> <<<<<<"Unable to write "/utf8, Path/binary>>/binary, ": "/utf8>>/binary, (simplifile:describe_error(Err@1))/binary>> end ) end ). -file("src/tale/generators.gleam", 262). -spec capitalize_word(binary()) -> binary(). capitalize_word(Word) -> case string:length(Word) of 0 -> <<""/utf8>>; _ -> <<(string:uppercase(gleam@string:slice(Word, 0, 1)))/binary, (string:lowercase(gleam@string:drop_start(Word, 1)))/binary>> end. -file("src/tale/generators.gleam", 244). -spec prettify_title(binary()) -> binary(). prettify_title(Input) -> Cleaned = begin _pipe = Input, _pipe@1 = gleam@string:replace(_pipe, <<"-"/utf8>>, <<" "/utf8>>), _pipe@2 = gleam@string:replace(_pipe@1, <<"_"/utf8>>, <<" "/utf8>>), gleam@string:trim(_pipe@2) end, case string:length(Cleaned) of 0 -> <<"New Post"/utf8>>; _ -> _pipe@3 = Cleaned, _pipe@4 = gleam@string:split(_pipe@3, <<" "/utf8>>), _pipe@5 = gleam@list:filter( _pipe@4, fun(Word) -> Word /= <<""/utf8>> end ), _pipe@6 = gleam@list:map(_pipe@5, fun capitalize_word/1), gleam@string:join(_pipe@6, <<" "/utf8>>) end. -file("src/tale/generators.gleam", 271). -spec post_slug(binary()) -> binary(). post_slug(Input) -> case tale@util:slugify(Input) of <<""/utf8>> -> <<"post"/utf8>>; Slug -> Slug end. -file("src/tale/generators.gleam", 323). ?DOC(" Drop last path segments helper.\n"). -spec drop_last_path_segments(list(binary())) -> list(binary()). drop_last_path_segments(List_) -> case List_ of [] -> []; [_] -> []; [First | Rest] -> [First | drop_last_path_segments(Rest)] end. -file("src/tale/generators.gleam", 285). ?DOC(" Post destination info function.\n"). -spec post_destination_info(binary()) -> {binary(), binary()}. post_destination_info(Input) -> Normalised = begin _pipe = Input, _pipe@1 = gleam@string:trim(_pipe), gleam@string:replace(_pipe@1, <<"\\"/utf8>>, <<"/"/utf8>>) end, Segments = begin _pipe@2 = Normalised, _pipe@3 = gleam@string:split(_pipe@2, <<"/"/utf8>>), gleam@list:filter(_pipe@3, fun(Segment) -> Segment /= <<""/utf8>> end) end, Filename_input = begin _pipe@4 = Segments, _pipe@5 = lists:reverse(_pipe@4), _pipe@6 = gleam@list:first(_pipe@5), gleam@result:unwrap(_pipe@6, <<"post"/utf8>>) end, Directories = drop_last_path_segments(Segments), Filename = case gleam_stdlib:string_ends_with( Filename_input, <<".md"/utf8>> ) of true -> Filename_input; false -> <<(post_slug(Filename_input))/binary, ".md"/utf8>> end, Title_source = case gleam_stdlib:string_ends_with( Filename_input, <<".md"/utf8>> ) of true -> gleam@string:drop_end(Filename_input, 3); false -> Filename_input end, Dest = case Directories of [] -> Filename; _ -> <<<<(gleam@string:join(Directories, <<"/"/utf8>>))/binary, "/"/utf8>>/binary, Filename/binary>> end, {Dest, Title_source}. -file("src/tale/generators.gleam", 342). ?DOC(" Pad helper function.\n"). -spec pad(integer()) -> binary(). pad(Value) -> S = erlang:integer_to_binary(Value), case string:length(S) of 1 -> <<"0"/utf8, S/binary>>; _ -> S end. -file("src/tale/generators.gleam", 332). ?DOC(" Format timestamp function.\n"). -spec format_timestamp(gleam@time@timestamp:timestamp()) -> binary(). format_timestamp(Ts) -> {Date, _} = gleam@time@timestamp:to_calendar(Ts, {duration, 0, 0}), <<<<<<<<(erlang:integer_to_binary(erlang:element(2, Date)))/binary, "-"/utf8>>/binary, (pad(gleam@time@calendar:month_to_int(erlang:element(3, Date))))/binary>>/binary, "-"/utf8>>/binary, (pad(erlang:element(4, Date)))/binary>>. -file("src/tale/generators.gleam", 279). ?DOC(" Current timestamp function.\n"). -spec current_timestamp() -> binary(). current_timestamp() -> _pipe = gleam@time@timestamp:system_time(), format_timestamp(_pipe). -file("src/tale/generators.gleam", 223). ?DOC(" Load post template function.\n"). -spec load_post_template() -> {ok, binary()} | {error, binary()}. load_post_template() -> case simplifile:read(<<"archetypes/default.md"/utf8>>) of {ok, Contents} -> {ok, Contents}; {error, enoent} -> Fallback = <<(<<"templates"/utf8, "/site"/utf8>>)/binary, "/archetypes/default.md"/utf8>>, _pipe = simplifile:read(Fallback), gleam@result:map_error( _pipe, fun(Err) -> <<<<<<"Unable to read archetype at "/utf8, Fallback/binary>>/binary, ": "/utf8>>/binary, (simplifile:describe_error(Err))/binary>> end ); {error, Err@1} -> {error, <<"Unable to read archetype at archetypes/default.md: "/utf8, (simplifile:describe_error(Err@1))/binary>>} end. -file("src/tale/generators.gleam", 111). ?DOC(" Scaffold post function.\n"). -spec scaffold_post(binary(), binary()) -> {ok, binary()} | {error, binary()}. scaffold_post(Title_source, Dest) -> gleam@result:'try'( load_post_template(), fun(Template) -> Title = prettify_title(Title_source), Timestamp = current_timestamp(), Updated = begin _pipe = Template, _pipe@1 = replace_property( _pipe, <<"title"/utf8>>, <<"title = "/utf8, (quoted(Title))/binary>> ), replace_property( _pipe@1, <<"date"/utf8>>, <<"date = "/utf8, (quoted(Timestamp))/binary>> ) end, gleam@result:'try'( tale@paths:ensure_parent_dirs(Dest), fun(_) -> _pipe@2 = simplifile:write(Dest, Updated), _pipe@3 = gleam@result:map_error( _pipe@2, fun(Err) -> <<<<<<"Unable to write post at "/utf8, Dest/binary>>/binary, ": "/utf8>>/binary, (simplifile:describe_error(Err))/binary>> end ), gleam@result:map( _pipe@3, fun(_) -> <<<<"Post created at "/utf8, Dest/binary>>/binary, "."/utf8>> end ) end ) end ). -file("src/tale/generators.gleam", 100). ?DOC(" Create post\n"). -spec create_post(binary()) -> {ok, binary()} | {error, binary()}. create_post(Name) -> {Dest, Title_source} = post_destination_info(Name), gleam@result:'try'(path_exists(Dest), fun(Exists) -> case Exists of true -> {error, <<"Post already exists: "/utf8, Dest/binary>>}; false -> scaffold_post(Title_source, Dest) end end). -file("src/tale/generators.gleam", 91). ?DOC(" Creates a new post under `content/posts/` using the archetype template and current timestamp.\n"). -spec new_post_gen(binary()) -> {ok, binary()} | {error, binary()}. new_post_gen(Raw_input) -> Input = gleam@string:trim(Raw_input), case Input of <<""/utf8>> -> {error, <<"Please provide a post name, e.g. `tale new post my-article`."/utf8>>}; _ -> create_post(Input) end. -file("src/tale/generators.gleam", 72). -spec scaffold_site(binary()) -> {ok, binary()} | {error, binary()}. scaffold_site(Name) -> gleam@result:'try'( copy_directory(<<"templates"/utf8, "/site"/utf8>>, Name), fun(_) -> gleam@result:'try'( copy_directory( <<"templates"/utf8, "/theme/"/utf8, "default"/utf8>>, <<<>/binary, "default"/utf8>> ), fun(_) -> gleam@result:'try'( update_config_title( <>, Name ), fun(_) -> {ok, <<<<"Site "/utf8, Name/binary>>/binary, " created with the Tale default theme."/utf8>>} end ) end ) end ). -file("src/tale/generators.gleam", 42). -spec create_site(binary()) -> {ok, binary()} | {error, binary()}. create_site(Name) -> gleam@result:'try'(path_exists(Name), fun(Exists) -> case Exists of true -> {error, <<"Cannot create site, path already exists: "/utf8, Name/binary>>}; false -> scaffold_site(Name) end end). -file("src/tale/generators.gleam", 33). ?DOC( " Creates a new site rooted at the provided path. The generated site copies\n" " Tale's default content and theme, and updates the config title.\n" " New site contains:\n" " - archetypes: Contains the default markdown file\n" " - assets: Empty but user can add files here if they dont want to use or create a theme\n" " - content: Contains index.md, pages markdown files and posts directory who contains posts. Can be modified in config.toml\n" " - layouts: Empty but again user can add templates here.\n" " - static: Empty, user can add static files here.\n" " - `config.toml` file\n" ). -spec new_site_gen(binary()) -> {ok, binary()} | {error, binary()}. new_site_gen(Raw_name) -> Name = gleam@string:trim(Raw_name), case Name of <<""/utf8>> -> {error, <<"Please provide a site name, e.g. `tale new site my-blog`."/utf8>>}; _ -> create_site(Name) end. -file("src/tale/generators.gleam", 83). -spec scaffold_theme(binary(), binary()) -> {ok, binary()} | {error, binary()}. scaffold_theme(Path, Name) -> gleam@result:'try'( copy_directory( <<"templates"/utf8, "/theme/"/utf8, "default"/utf8>>, Path ), fun(_) -> gleam@result:'try'( update_theme_name(<>, Name), fun(_) -> {ok, <<<<<<<<"Theme "/utf8, Name/binary>>/binary, " created at "/utf8>>/binary, Path/binary>>/binary, "."/utf8>>} end ) end ). -file("src/tale/generators.gleam", 64). -spec create_theme(binary()) -> {ok, binary()} | {error, binary()}. create_theme(Name) -> gleam@result:'try'(path_exists(Name), fun(Exists) -> case Exists of true -> {error, <<"Path already exists: "/utf8, Name/binary>>}; false -> scaffold_theme(Name, Name) end end). -file("src/tale/generators.gleam", 55). ?DOC( " Creates a new theme `` based on Tale's default theme.\n" " Theme directory contains:\n" " - assets: Contains css, js and images\n" " - layouts: Contains layout templates\n" " - static: Contains all static files that will be copied in the site/blog like robots.txt, favicon etc.\n" ). -spec new_theme_gen(binary()) -> {ok, binary()} | {error, binary()}. new_theme_gen(Raw_name) -> Name = gleam@string:trim(Raw_name), case Name of <<""/utf8>> -> {error, <<"Please provide a theme name, e.g. `tale new theme docs`."/utf8>>}; _ -> create_theme(Name) end.