Skip to content

Commit

Permalink
Strip single trailing newlines (#44)
Browse files Browse the repository at this point in the history
* Strip single trailing newlines

* Fix build. Change from checkout@v2 to v3

* The latest version of rebar3 no longer supports OTP 23, moved to 24,25,26

* Added missing applications to the app.src file
  • Loading branch information
mmzeeman authored May 24, 2023
1 parent 5c21876 commit 2bda250
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/hex-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Check out
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Publish to Hex.pm
uses: erlangpack/github-action@v3
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ jobs:

strategy:
matrix:
otp_version: [23,24,25]
otp_version: [24,25,26]
os: [ubuntu-latest]

container:
image: erlang:${{ matrix.otp_version }}

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Compile
run: make
- name: Test
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,11 @@ when you are still writing the template:
The expression value will be printed using `io_lib:format("~p", [ SomExpr ])`, then
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.

License
=======
Expand Down
5 changes: 4 additions & 1 deletion src/template_compiler.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
{applications, [
kernel,
stdlib,
crypto
crypto,
compiler,
syntax_tools,
zotonic_stdlib
]},
{env,[
{template_dir, {template_compiler, "templates"}}
Expand Down
12 changes: 7 additions & 5 deletions src/template_compiler_scanner.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@

-export([scan/1, scan/2]).

-define(IS_EOF(S),
(S =:= <<>> orelse S =:= <<"\n">> orelse S =:= <<"\r\n">>)).

%%====================================================================
%% API
Expand Down Expand Up @@ -138,20 +140,20 @@ identifier_to_keyword({Type, Pos, String}, {_PrevToken, Acc}) ->
{Type, [{Type, Pos, String}|Acc]}.


scan(<<>>, Scanned, _, in_text) ->
scan(Eof, Scanned, _, in_text) when ?IS_EOF(Eof) ->
{_Token, ScannedKeyword} = lists:foldr(fun identifier_to_keyword/2, {'$eof', []}, Scanned),
{ok, lists:reverse(ScannedKeyword)};

scan(<<>>, _Scanned, {SourceRef, _, _}, {in_comment, _}) ->
scan(Eof, _Scanned, {SourceRef, _, _}, {in_comment, _}) when ?IS_EOF(Eof) ->
{error, io_lib:format("Reached end of ~p inside a comment.", [SourceRef])};

scan(<<>>, _Scanned, {SourceRef, _, _}, {in_trans, _}) ->
scan(Eof, _Scanned, {SourceRef, _, _}, {in_trans, _}) when ?IS_EOF(Eof) ->
{error, io_lib:format("Reached end of ~p inside a trans block.", [SourceRef])};

scan(<<>>, _Scanned, {SourceRef, _, _}, {in_raw, _}) ->
scan(Eof, _Scanned, {SourceRef, _, _}, {in_raw, _}) when ?IS_EOF(Eof) ->
{error, io_lib:format("Reached end of ~p inside a raw block.", [SourceRef])};

scan(<<>>, _Scanned, {SourceRef, _, _}, _) ->
scan(Eof, _Scanned, {SourceRef, _, _}, _) when ?IS_EOF(Eof) ->
{error, io_lib:format("Reached end of ~p inside a code block.", [SourceRef])};

%%we just capture the {% raw %} {% endraw %} tags and pass on string between the tags as is
Expand Down
2 changes: 1 addition & 1 deletion test/template_compiler_expr_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ undefined
<<104,101,108,108,111,32,119,111,114,108,100>>
atom
[a,b,c]
#{<<97>> => 1,<<98>> => 2}\n">> = iolist_to_binary(Bin1),
#{<<97>> => 1,<<98>> => 2}">> = iolist_to_binary(Bin1),
ok.

expr_nested(_Config) ->
Expand Down
7 changes: 7 additions & 0 deletions test/template_compiler_include_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ include_test(_Config) ->
include_dynamic_test(_Config) ->
{ok, Bin1} = template_compiler:render("include_dynamic.tpl", #{ t => "include_b.tpl" }, [], undefined),
<<"abc">> = iolist_to_binary(Bin1),

{ok, Bin2} = template_compiler:render("include_dynamic.tpl", #{ t => "include_b_no_trailing_newline.tpl" }, [], undefined),
<<"abc">> = iolist_to_binary(Bin2),

{ok, Bin3} = template_compiler:render("include_dynamic.tpl", #{ t => "include_b_trailing_dos_newline.tpl" }, [], undefined),
<<"abc">> = iolist_to_binary(Bin3),

ok.

include_args_test(_Config) ->
Expand Down
2 changes: 1 addition & 1 deletion test/test-data/include_b.tpl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b
b
1 change: 1 addition & 0 deletions test/test-data/include_b_no_trailing_newline.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b
1 change: 1 addition & 0 deletions test/test-data/include_b_trailing_dos_newline.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b

0 comments on commit 2bda250

Please sign in to comment.