Current section
Files
Jump to
Current section
Files
src/ghtml@cache.erl
-module(ghtml@cache).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/ghtml/cache.gleam").
-export([hash_content/1, extract_hash/1, needs_regeneration/2, generate_header/2, is_generated/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.
?MODULEDOC(false).
-file("src/ghtml/cache.gleam", 13).
?DOC(false).
-spec hash_content(binary()) -> binary().
hash_content(Content) ->
_pipe = Content,
_pipe@1 = gleam_stdlib:identity(_pipe),
_pipe@2 = gleam@crypto:hash(sha256, _pipe@1),
_pipe@3 = gleam_stdlib:base16_encode(_pipe@2),
string:lowercase(_pipe@3).
-file("src/ghtml/cache.gleam", 23).
?DOC(false).
-spec extract_hash(binary()) -> {ok, binary()} | {error, nil}.
extract_hash(Generated_content) ->
_pipe = Generated_content,
_pipe@1 = gleam@string:split(_pipe, <<"\n"/utf8>>),
gleam@list:find_map(
_pipe@1,
fun(Line) ->
case gleam_stdlib:string_starts_with(Line, <<"// @hash "/utf8>>) of
true ->
{ok,
begin
_pipe@2 = gleam@string:drop_start(Line, 9),
gleam@string:trim(_pipe@2)
end};
false ->
{error, nil}
end
end
).
-file("src/ghtml/cache.gleam", 36).
?DOC(false).
-spec needs_regeneration(binary(), binary()) -> boolean().
needs_regeneration(Source_path, Output_path) ->
case {simplifile:read(Source_path), simplifile:read(Output_path)} of
{{ok, Source}, {ok, Existing}} ->
Current_hash = hash_content(Source),
case extract_hash(Existing) of
{ok, Stored_hash} ->
Current_hash /= Stored_hash;
{error, _} ->
true
end;
{{ok, _}, {error, _}} ->
true;
{{error, _}, _} ->
false
end.
-file("src/ghtml/cache.gleam", 54).
?DOC(false).
-spec generate_header(binary(), binary()) -> binary().
generate_header(Source_filename, Hash) ->
<<<<<<<<<<<<"// @generated from "/utf8, Source_filename/binary>>/binary,
"\n"/utf8>>/binary,
"// @hash "/utf8>>/binary,
Hash/binary>>/binary,
"\n"/utf8>>/binary,
"// DO NOT EDIT - regenerate with: gleam run -m ghtml\n"/utf8>>.
-file("src/ghtml/cache.gleam", 65).
?DOC(false).
-spec is_generated(binary()) -> boolean().
is_generated(Content) ->
gleam_stdlib:string_starts_with(Content, <<"// @generated from "/utf8>>).