Skip to content

Commit

Permalink
Cleanup and small fixes (#49)
Browse files Browse the repository at this point in the history
* Use attributes to define type and spec

* Set locale as UTF-8 via ELIXIR_ERL_OPTIONS env

* Remove extra lines and ws

* Remove unused headers

* Fix no longer match on float

* Also match on -0.0

---------

Co-authored-by: Marc Worrell <marc@worrell.nl>
  • Loading branch information
williamthome and mworrell committed Apr 26, 2024
1 parent 5ba9f43 commit 845bd16
Show file tree
Hide file tree
Showing 21 changed files with 101 additions and 108 deletions.
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ test: $(REBAR)
$(REBAR) $(REBAR_OPTS) ct

edoc: $(REBAR)
$(REBAR) ex_doc
ELIXIR_ERL_OPTIONS="+fnu" $(REBAR) ex_doc

# Cleaning
.PHONY: clean_logs
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ extends() -> undefined.

%% The templates that this template includes.
%% Includes which use variable names for the template name are not listed.
includes() -> [
includes() -> [
#{
template => <<"foo.tpl">>,
line => 5,
Expand Down Expand Up @@ -515,7 +515,7 @@ escaped and surrounded with `<pre>...</pre>`.

Whitespace Handling
-------------------

- A single trailing newline is stripped, from the template, if present.
- Other whitespace, like tabs, newlines and spaces are returned unchanged.

Expand Down
4 changes: 2 additions & 2 deletions include/template_compiler.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down
4 changes: 2 additions & 2 deletions src/filter_template_compiler_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down
4 changes: 2 additions & 2 deletions src/filter_template_compiler_test_set.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down
11 changes: 4 additions & 7 deletions src/template_compiler_admin.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -40,8 +40,6 @@
]).

-include_lib("kernel/include/logger.hrl").
-include("template_compiler.hrl").
-include("template_compiler_internal.hrl").

-type gen_server_from() :: {pid(),term()}.

Expand Down Expand Up @@ -142,7 +140,7 @@ handle_call({compile_request, TplKey, Filename}, From, State) ->
false ->
FTpl = {Filename, TplKey},
State1 = case lists:member(FTpl, State#state.filename_keys) of
false ->
false ->
State#state{
filename_keys=[ FTpl | State#state.filename_keys ]
};
Expand Down Expand Up @@ -210,7 +208,7 @@ handle_cast({flush_context_name, ContextName}, State) ->
end,
Matched),
FilenameKeys = lists:filter(
fun({_Fn, K}) ->
fun({_Fn, K}) ->
K =/= ContextName
end,
State#state.filename_keys),
Expand Down Expand Up @@ -271,4 +269,3 @@ split_waiters(TplKey, State) ->
end,
State#state.waiting),
{Ready, State#state{waiting=Waiting}}.

4 changes: 2 additions & 2 deletions src/template_compiler_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down
8 changes: 4 additions & 4 deletions src/template_compiler_element.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -150,8 +150,8 @@ compile({block, {identifier, SrcPos, Name}, _Elts}, CState, Ws) ->
erl_syntax:atom(CState#cs.runtime),
erl_syntax:variable(CState#cs.context_var)
]),
{value, {BlockName, _Tree, BlockWs}} = lists:keysearch(BlockName, 1, CState#cs.blocks),
Ws1 = Ws#ws{is_forloop_var = Ws#ws.is_forloop_var or BlockWs#ws.is_forloop_var},
{value, {BlockName, _Tree, BlockWs}} = lists:keysearch(BlockName, 1, CState#cs.blocks),
Ws1 = Ws#ws{is_forloop_var = Ws#ws.is_forloop_var or BlockWs#ws.is_forloop_var},
{Ws1, template_compiler_utils:set_pos(SrcPos, Ast)};
compile({inherit, {_, _SrcPos, _}}, #cs{block=undefined}, Ws) ->
{Ws, erl_syntax:abstract(<<>>)};
Expand Down
4 changes: 2 additions & 2 deletions src/template_compiler_expr.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down
5 changes: 2 additions & 3 deletions src/template_compiler_internal.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -62,4 +62,3 @@
context_vars = [] :: list(binary()),
is_autoescape = false :: boolean()
}).

6 changes: 2 additions & 4 deletions src/template_compiler_module.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -25,8 +25,6 @@
]).

-include_lib("syntax_tools/include/merl.hrl").
-include("template_compiler.hrl").
-include("template_compiler_internal.hrl").

compile(Module, Filename, Mtime, IsAutoid, Runtime, Extends, Includes, BlockAsts, undefined) ->
compile(Module, Filename, Mtime, IsAutoid, Runtime, Extends, Includes, BlockAsts, erl_syntax:abstract(<<>>));
Expand Down
40 changes: 21 additions & 19 deletions src/template_compiler_operators.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -128,7 +128,8 @@ divide(A, B, Runtime, Context) ->
{undefined, _} -> undefined;
{_, undefined} -> undefined;
{_, 0} -> undefined;
{_, 0.0} -> undefined;
{_, +0.0} -> undefined;
{_, -0.0} -> undefined;
{A1, B1} -> A1 / B1
end.

Expand All @@ -144,7 +145,8 @@ modulo(A, B, Runtime, Context) ->
{undefined, _} -> undefined;
{_, undefined} -> undefined;
{_, 0} -> undefined;
{_, 0.0} -> undefined;
{_, +0.0} -> undefined;
{_, -0.0} -> undefined;
{A1, B1} -> A1 rem B1
end.

Expand Down Expand Up @@ -216,31 +218,31 @@ to_values({trans, _} = Tr, B, Runtime, Context) ->
to_values(Runtime:to_simple_value(Tr, Context), B, Runtime, Context);
to_values(A, {trans, _} = Tr, Runtime, Context) ->
to_values(A, Runtime:to_simple_value(Tr, Context), Runtime, Context);
to_values(A, B, _Runtime, _Context) when is_number(A), is_number(B) ->
to_values(A, B, _Runtime, _Context) when is_number(A), is_number(B) ->
{A,B};
to_values(A, B, Runtime, Context) when is_boolean(A); is_boolean(B) ->
{z_convert:to_bool(Runtime:to_simple_value(A, Context)),
to_values(A, B, Runtime, Context) when is_boolean(A); is_boolean(B) ->
{z_convert:to_bool(Runtime:to_simple_value(A, Context)),
z_convert:to_bool(Runtime:to_simple_value(B, Context))};
to_values(A, B, Runtime, Context) when is_integer(A); is_integer(B) ->
to_values(A, B, Runtime, Context) when is_integer(A); is_integer(B) ->
{to_maybe_integer(A, Runtime, Context),
to_maybe_integer(B, Runtime, Context)};
to_values(A, B, Runtime, Context) when is_float(A); is_float(B) ->
to_values(A, B, Runtime, Context) when is_float(A); is_float(B) ->
{to_maybe_float(A, Runtime, Context),
to_maybe_float(B, Runtime, Context)};
to_values(A, B, _Runtime, _Context) when is_binary(A), is_binary(B) ->
to_values(A, B, _Runtime, _Context) when is_binary(A), is_binary(B) ->
{A,B};
to_values(A, B, _Runtime, _Context) when is_list(A), is_list(B) ->
to_values(A, B, _Runtime, _Context) when is_list(A), is_list(B) ->
{A,B};
to_values(A, B, Runtime, Context) when is_binary(A); is_binary(B) ->
to_values(A, B, Runtime, Context) when is_binary(A); is_binary(B) ->
{to_maybe_binary(A, Runtime, Context),
to_maybe_binary(B, Runtime, Context)};
to_values(A, B, Runtime, Context) when is_tuple(A), is_tuple(B) ->
{Runtime:to_simple_value(A, Context),
to_values(A, B, Runtime, Context) when is_tuple(A), is_tuple(B) ->
{Runtime:to_simple_value(A, Context),
Runtime:to_simple_value(B, Context)};
to_values(A, B, Runtime, Context) when is_binary(A); is_binary(B) ->
to_values(A, B, Runtime, Context) when is_binary(A); is_binary(B) ->
{to_maybe_binary(A, Runtime, Context),
to_maybe_binary(B, Runtime, Context)};
to_values(A, B, Runtime, Context) ->
to_values(A, B, Runtime, Context) ->
{to_maybe_list(A, Runtime, Context),
to_maybe_list(B, Runtime, Context)}.

Expand All @@ -250,11 +252,11 @@ to_numbers(undefined, B, _Runtime, _Context) ->
{undefined, B};
to_numbers(A, undefined, _Runtime, _Context) ->
{A, undefined};
to_numbers(A, B, _Runtime, _Context) when is_number(A), is_number(B) ->
to_numbers(A, B, _Runtime, _Context) when is_number(A), is_number(B) ->
{A,B};
to_numbers(A, B, Runtime, Context) when is_float(A); is_float(B) ->
to_numbers(A, B, Runtime, Context) when is_float(A); is_float(B) ->
{to_maybe_float(A, Runtime, Context), to_maybe_float(B, Runtime, Context)};
to_numbers(A, B, Runtime, Context) ->
to_numbers(A, B, Runtime, Context) ->
{to_maybe_integer(A, Runtime, Context), to_maybe_integer(B, Runtime, Context)}.

to_maybe_integer(A, _Runtime, _Context) when is_number(A) ->
Expand Down
20 changes: 10 additions & 10 deletions src/template_compiler_parser.yrl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
%%% @copyright 2009-2016 Marc Worrell
%%% @doc Template language grammar
%%% @changes Marc Worrell - added print/image/scomp, more args options etc.
%%% @end
%%% @end
%%%
%%% The MIT License
%%%
Expand Down Expand Up @@ -50,7 +50,7 @@ Nonterminals
ExtendsTag
OverrulesTag
InheritTag

IncludeTag
CatIncludeTag
NowTag
Expand All @@ -71,7 +71,7 @@ Nonterminals
FilterBraced
EndFilterBraced
Filters

ScriptBlock
ScriptBraced
EndScriptBraced
Expand All @@ -94,12 +94,12 @@ Nonterminals
IfEqualBlock
IfEqualBraced
IfEqualExpression
EndIfEqualBraced
EndIfEqualBraced

IfNotEqualBlock
IfNotEqualBraced
IfNotEqualExpression
EndIfNotEqualBraced
EndIfNotEqualBraced

AutoEscapeBlock
AutoEscapeBraced
Expand All @@ -108,7 +108,7 @@ Nonterminals
WithBlock
WithBraced
EndWithBraced

Value
TermValue
Variable
Expand All @@ -118,14 +118,14 @@ Nonterminals

ModelCall
OptModelArg

LibTag
LibUrlTag
LibList

LoadTag
LoadNames

CustomTag
WithArgs
Args
Expand Down
Loading

0 comments on commit 845bd16

Please sign in to comment.