Skip to content

Commit

Permalink
Add trimmed option to blocktrans tag (#212).
Browse files Browse the repository at this point in the history
still need to write some tests.
  • Loading branch information
kaos committed Oct 17, 2015
1 parent 11f4bf0 commit 27cdc1c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/erlydtl_parser.yrl
Expand Up @@ -211,6 +211,7 @@ Terminals
string_literal
string
templatetag_keyword
trimmed_keyword
openblock_keyword
closeblock_keyword
openvariable_keyword
Expand Down Expand Up @@ -421,6 +422,7 @@ BlockTransArgs -> '$empty' : [].
BlockTransArgs -> count_keyword Arg BlockTransArgs : [{count, '$2'}|'$3'].
BlockTransArgs -> with_keyword Args BlockTransArgs : [{args, '$2'}|'$3'].
BlockTransArgs -> context_keyword string_literal BlockTransArgs : [{context, '$2'}|'$3'].
BlockTransArgs -> trimmed_keyword BlockTransArgs : [trimmed|'$2'].

BlockTransContents -> '$empty' : [].
BlockTransContents -> open_var identifier close_var BlockTransContents : [{variable, '$2'}|'$4'].
Expand Down
3 changes: 2 additions & 1 deletion src/erlydtl_scanner.erl
Expand Up @@ -36,7 +36,7 @@
%%%-------------------------------------------------------------------
-module(erlydtl_scanner).

%% This file was generated 2015-10-16 21:31:55 UTC by slex 0.2.1-2-g7814678.
%% This file was generated 2015-10-17 21:30:29 UTC by slex 0.2.1-2-g7814678.
%% http://github.com/erlydtl/slex
-slex_source(["src/erlydtl_scanner.slex"]).

Expand Down Expand Up @@ -90,6 +90,7 @@ is_keyword(any, "from") -> true;
is_keyword(any, "count") -> true;
is_keyword(any, "context") -> true;
is_keyword(any, "noop") -> true;
is_keyword(any, "trimmed") -> true;
is_keyword(close, "only") -> true;
is_keyword(close, "parsed") -> true;
is_keyword(close, "silent") -> true;
Expand Down
1 change: 1 addition & 0 deletions src/erlydtl_scanner.slex
Expand Up @@ -314,6 +314,7 @@ form \
is_keyword(any, "count") -> true; \
is_keyword(any, "context") -> true; \
is_keyword(any, "noop") -> true; \
is_keyword(any, "trimmed") -> true; \
\
is_keyword(close, "only") -> true; \
is_keyword(close, "parsed") -> true; \
Expand Down
5 changes: 4 additions & 1 deletion src/erlydtl_unparser.erl
Expand Up @@ -15,7 +15,7 @@ unparse([{'block', Identifier, Contents}|Rest], Acc) ->
unparse([{'blocktrans', Args, Contents, undefined}|Rest], Acc) ->
unparse(Rest, [["{% blocktrans ", unparse_blocktrans_args(Args), "%}", unparse(Contents), "{% endblocktrans %}"]|Acc]);
unparse([{'blocktrans', Args, Contents, PluralContents}|Rest], Acc) ->
unparse(Rest, [["{% blocktrans ", unparse_args(Args), " %}",
unparse(Rest, [["{% blocktrans ", unparse_blocktrans_args(Args), " %}",
unparse(Contents),
"{% plural %}",
unparse(PluralContents),
Expand Down Expand Up @@ -212,5 +212,8 @@ unparse_blocktrans_args([{count, Count}|Args], Acc) ->
unparse_blocktrans_args([{context, Context}|Args], Acc) ->
unparse_blocktrans_args(
Args, [["context ", unparse_value(Context)]|Acc]);
unparse_blocktrans_args([trimmed|Args], Acc) ->
unparse_blocktrans_args(
Args, ["trimmed"|Acc]);

This comment has been minimized.

Copy link
@seriyps

seriyps Oct 19, 2015

Member

Looks like there should be additional space like "trimmed "

This comment has been minimized.

Copy link
@kaos

kaos Oct 19, 2015

Author Member

Nice catch, but no ;) The extra trailing space is added here:

unparse(Rest, [["{% blocktrans ", unparse_blocktrans_args(Args), "%}", unparse(Contents), "{% endblocktrans %}"]|Acc]);
(and on L18).

However, I'm inclined to believe that there is indeed a space missing between blocktrans args, in case there are several. So, perhaps there should be a string:join(Acc, " ") at the end...

This comment has been minimized.

Copy link
@seriyps

seriyps Oct 19, 2015

Member

@kaos hmm.. I don't see any trailing space on L16: "{% blocktrans ", unparse_blocktrans_args(Args), "%}" but yes, I mostly talk about situations when there are more than one blocktrans argument, so, if Args = [trimmed, {count, 2}] result will be {%blocktrans trimmedcount=2%}

This comment has been minimized.

Copy link
@kaos

kaos Oct 20, 2015

Author Member

Ha, no, you're right, on line 16, there is a space missing. Will add some more tests to catch this.. 👍 :)

unparse_blocktrans_args([], Acc) ->
lists:reverse(Acc).
16 changes: 14 additions & 2 deletions src/i18n/sources_parser.erl
Expand Up @@ -170,8 +170,9 @@ process_token(Fname,
St#state{acc=[Phrase | Acc], translators_comment=undefined};
process_token(Fname, {blocktrans, Args, Contents, PluralContents}, #state{acc=Acc, translators_comment=Comment}=St) ->
{Fname, Line, Col} = guess_blocktrans_lc(Fname, Args, Contents),
Phrase = #phrase{msgid=unparse(Contents),
msgid_plural=unparse(PluralContents),
Trim = proplists:get_value(trimmed, Args),
Phrase = #phrase{msgid=maybe_trim(unparse(Contents), Trim),
msgid_plural=maybe_trim(unparse(PluralContents), Trim),
context=case proplists:get_value(context, Args) of
{string_literal, _, String} ->
erlydtl_compiler_utils:unescape_string_literal(String);
Expand Down Expand Up @@ -201,6 +202,17 @@ unescape(String) -> string:sub_string(String, 2, string:len(String) -1).
unparse(undefined) -> undefined;
unparse(Contents) -> erlydtl_unparser:unparse(Contents).

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])
)
)
).

%% hack to guess ~position of blocktrans
guess_blocktrans_lc(Fname, [{{identifier, {L, C}, _}, _} | _], _) ->
%% guess by 1'st with
Expand Down

0 comments on commit 27cdc1c

Please sign in to comment.