Packages

Handles is a templating language written in pure Gleam. Heavily inspired by Mustache and Handlebars.js

Current section

Files

Jump to
handles src handles@internal@engine.erl
Raw

src/handles@internal@engine.erl

-module(handles@internal@engine).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([run/4]).
-spec run(
list(handles@internal@parser:ast()),
handles@ctx:value(),
gleam@dict:dict(binary(), list(handles@internal@parser:ast())),
gleam@string_builder:string_builder()
) -> {ok, binary()} | {error, handles@error:runtime_error()}.
run(Ast, Ctx, Partials, Builder) ->
case Ast of
[] ->
_pipe = Builder,
_pipe@1 = gleam@string_builder:to_string(_pipe),
{ok, _pipe@1};
[Node | Rest] ->
_pipe@7 = case Node of
{constant, _, Value} ->
{ok, Value};
{property, Index, Path} ->
handles@internal@ctx_utils:get_property(Path, Ctx, Index);
{partial, Index@1, Id, Path@1} ->
case gleam@dict:get(Partials, Id) of
{error, _} ->
{error, {unknown_partial, Index@1, Id}};
{ok, Partial} ->
_pipe@2 = handles@internal@ctx_utils:get(
Path@1,
Ctx,
Index@1
),
gleam@result:'try'(
_pipe@2,
fun(_capture) ->
run(
Partial,
_capture,
Partials,
gleam@string_builder:new()
)
end
)
end;
{if_block, Index@2, Path@2, Children} ->
_pipe@3 = handles@internal@ctx_utils:get_bool(
Path@2,
Ctx,
Index@2
),
gleam@result:'try'(
_pipe@3,
fun(_capture@1) ->
run_if(_capture@1, Children, Ctx, Partials)
end
);
{unless_block, Index@3, Path@3, Children@1} ->
_pipe@4 = handles@internal@ctx_utils:get_bool(
Path@3,
Ctx,
Index@3
),
_pipe@5 = gleam@result:map(_pipe@4, fun gleam@bool:negate/1),
gleam@result:'try'(
_pipe@5,
fun(_capture@2) ->
run_if(_capture@2, Children@1, Ctx, Partials)
end
);
{each_block, Index@4, Path@4, Children@2} ->
_pipe@6 = handles@internal@ctx_utils:get_list(
Path@4,
Ctx,
Index@4
),
gleam@result:'try'(
_pipe@6,
fun(_capture@3) ->
run_each(
_capture@3,
Children@2,
gleam@string_builder:new(),
Partials
)
end
)
end,
gleam@result:'try'(
_pipe@7,
fun(It) ->
run(
Rest,
Ctx,
Partials,
begin
_pipe@8 = Builder,
gleam@string_builder:append(_pipe@8, It)
end
)
end
)
end.
-spec run_if(
boolean(),
list(handles@internal@parser:ast()),
handles@ctx:value(),
gleam@dict:dict(binary(), list(handles@internal@parser:ast()))
) -> {ok, binary()} | {error, handles@error:runtime_error()}.
run_if(Bool, Children, Ctx, Partials) ->
case Bool of
false ->
{ok, <<""/utf8>>};
true ->
run(Children, Ctx, Partials, gleam@string_builder:new())
end.
-spec run_each(
list(handles@ctx:value()),
list(handles@internal@parser:ast()),
gleam@string_builder:string_builder(),
gleam@dict:dict(binary(), list(handles@internal@parser:ast()))
) -> {ok, binary()} | {error, handles@error:runtime_error()}.
run_each(Ctxs, Ast, Builder, Partials) ->
case Ctxs of
[] ->
_pipe = Builder,
_pipe@1 = gleam@string_builder:to_string(_pipe),
{ok, _pipe@1};
[Ctx | Rest] ->
_pipe@2 = run(Ast, Ctx, Partials, gleam@string_builder:new()),
gleam@result:'try'(
_pipe@2,
fun(It) ->
run_each(
Rest,
Ast,
begin
_pipe@3 = Builder,
gleam@string_builder:append(_pipe@3, It)
end,
Partials
)
end
)
end.