Current section

Files

Jump to
tale src tale@generators.erl
Raw

src/tale@generators.erl

-module(tale@generators).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/tale/generators.gleam").
-export([new_theme_gen/1, new_post_gen/1, new_site_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", 124).
-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", 195).
-spec matches_key(binary(), binary()) -> boolean().
matches_key(Line, Key) ->
gleam_stdlib:string_starts_with(Line, <<Key/binary, " "/utf8>>) orelse gleam_stdlib:string_starts_with(
Line,
<<Key/binary, "="/utf8>>
).
-file("src/tale/generators.gleam", 167).
-spec replace_property(binary(), binary(), binary()) -> 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 ->
<<<<Line/binary, "\n"/utf8>>/binary, Rebuilt/binary>>
end.
-file("src/tale/generators.gleam", 199).
-spec quoted(binary()) -> binary().
quoted(Value) ->
Escaped = gleam@string:replace(Value, <<"\""/utf8>>, <<"\\\""/utf8>>),
<<<<"\""/utf8, Escaped/binary>>/binary, "\""/utf8>>.
-file("src/tale/generators.gleam", 135).
-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", 151).
-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", 77).
-spec scaffold_theme(binary(), binary()) -> {ok, binary()} | {error, binary()}.
scaffold_theme(Path, Name) ->
gleam@result:'try'(
tale@templates:write_default_theme(Path),
fun(_) ->
gleam@result:'try'(
update_theme_name(<<Path/binary, "/theme.toml"/utf8>>, Name),
fun(_) ->
{ok,
<<<<<<<<"Theme "/utf8, Name/binary>>/binary,
" created at "/utf8>>/binary,
Path/binary>>/binary,
"."/utf8>>}
end
)
end
).
-file("src/tale/generators.gleam", 59).
-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", 50).
?DOC(
" Creates a new theme `<name>` 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.
-file("src/tale/generators.gleam", 205).
?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} ->
{ok, tale@templates:default_post_archetype()};
{error, Err} ->
{error,
<<"Unable to read archetype at archetypes/default.md: "/utf8,
(simplifile:describe_error(Err))/binary>>}
end.
-file("src/tale/generators.gleam", 235).
-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", 217).
-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", 244).
-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", 296).
?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", 258).
?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", 315).
?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", 305).
?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", 252).
?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", 105).
?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", 94).
?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", 85).
?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", 67).
-spec scaffold_site(binary()) -> {ok, binary()} | {error, binary()}.
scaffold_site(Name) ->
gleam@result:'try'(
tale@templates:write_site(Name),
fun(_) ->
gleam@result:'try'(
tale@templates:write_default_theme(
<<<<Name/binary, "/themes/"/utf8>>/binary, "default"/utf8>>
),
fun(_) ->
gleam@result:'try'(
update_config_title(
<<Name/binary, "/config.toml"/utf8>>,
Name
),
fun(_) ->
{ok,
<<<<"Site "/utf8, Name/binary>>/binary,
" created with the Tale default theme."/utf8>>}
end
)
end
)
end
).
-file("src/tale/generators.gleam", 37).
-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", 28).
?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.