Browse Source

Add tests for blocktrans trimmed (#212).

Andreas Stenius 9 years ago
parent
commit
8123a11cd4
2 changed files with 21 additions and 7 deletions
  1. 13 5
      src/i18n/sources_parser.erl
  2. 8 2
      test/sources_parser_tests.erl

+ 13 - 5
src/i18n/sources_parser.erl

@@ -26,9 +26,12 @@
 
 %% New API
 -export([parse_pattern/1, parse_file/1, parse_content/2, phrase_info/2]).
+
 %% Deprecated API
 -export([parse/0, parse/1, process_content/2]).
+-deprecated([{parse, '_'}, {process_content, 2}]).
 
+%% Type exports
 -export_type([phrase/0, compat_phrase/0, field/0]).
 
 %%
@@ -202,13 +205,18 @@ unescape(String) -> string:sub_string(String, 2, string:len(String) -1).
 unparse(undefined) -> undefined;
 unparse(Contents) -> erlydtl_unparser:unparse(Contents).
 
+maybe_trim(undefined, _) -> undefined;
 maybe_trim(Text, undefined) -> Text;
 maybe_trim(Text, true) ->
-    tl(
-      lists:foldr(
-        fun (L, Ls) -> [" ", L|Ls] end, [],
-        lists:flatten(
-          re:replace(Text, <<"(^\s+)|(\s+$)|\n">>, <<"">>, [global, multiline])
+    binary_to_list(
+      iolist_to_binary(
+        tl(
+          lists:foldr(
+            fun (L, Ls) -> [" ", L|Ls] end, [],
+            lists:flatten(
+              re:replace(Text, <<"(^\\s+)|(\\s+$)|\n">>, <<"">>, [global, multiline])
+             )
+           )
          )
        )
      ).

+ 8 - 2
test/sources_parser_tests.erl

@@ -32,7 +32,13 @@ test_defs() ->
         [{"Hello inside an if inside a for",{"dummy_path",1,73}}]},
        {"if and else both with trans",
         <<"<html>{% block content %}{% if thing %} {% trans \"Hello inside an if\" %} {% else %} {% trans \"Hello inside an else\" %} {% endif %} {% endblock %}</html>">>,
-        [{"Hello inside an else",{"dummy_path",1,94}}, {"Hello inside an if",{"dummy_path",1,50}}]}
+        [{"Hello inside an else",{"dummy_path",1,94}}, {"Hello inside an if",{"dummy_path",1,50}}]},
+       {"blocktrans with pretty format",
+        <<"<html>{% blocktrans %}\n  This is a multiline\n  message... \n{% endblocktrans %}">>,
+        [{"\n  This is a multiline\n  message... \n", {"dummy_path",1,10}}]},
+       {"blocktrans with pretty format, trimmed",
+        <<"<html>{% blocktrans trimmed %}\n  This is a multiline\n  message... \n{% endblocktrans %}">>,
+        [{"This is a multiline message...", {"dummy_path",1,18}}]}
       ]}
     ].
 
@@ -80,7 +86,7 @@ unparser_test_() ->
 test_unparser_fun({Name, Tpl}) ->
     {Name, fun() ->
                    %% take input Tpl value, parse it, "unparse" it, then parse it again.
-                   %% the both parsed values should be equvialent, even if the source versions
+                   %% both parsed values should be equvialent, even if the source versions
                    %% are not an exact match (there can be whitespace differences)
                    {ok, Dpt} = erlydtl_compiler:do_parse_template(
                                  Tpl, #dtl_context{}),