Skip to content

Commit

Permalink
Support _("syntax") in custom tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Miller committed Jun 25, 2011
1 parent 62d4f1d commit 13362ee
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Expand Up @@ -54,7 +54,7 @@ will evaluate to `<b>100</b>`. Get it?
* `custom_tags_module` - A module to be used for handling custom tags. Each custom
tag should correspond to an exported function, e.g.:

some_tag(Variables, TranslationFun) -> iolist()
some_tag(Variables, RenderOptions) -> iolist()

* `vars` - Variables (and their values) to evaluate at compile-time rather than
render-time.
Expand Down
4 changes: 2 additions & 2 deletions priv/custom_tags/flashvideo
Expand Up @@ -12,10 +12,10 @@
<h2>To view the Video:</h2>
<p>
<a href="http://www.adobe.com/go/getflashplayer">
<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif"alt="Get Adobe Flash player">
<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="{{ alt }}">
</a>
</p>
<!--[if !IE]>-->
</object>
<!--<![endif]-->
</object>
</object>
20 changes: 13 additions & 7 deletions src/erlydtl_compiler.erl
Expand Up @@ -335,7 +335,7 @@ custom_tags_clauses_ast1([Tag|CustomTags], ExcludeTags, ClauseAcc, InfoAcc, Cont
{ok, DjangoParseTree, CheckSum} ->
{{BodyAst, BodyAstInfo}, TreeWalker1} = with_dependency({CustomTagFile, CheckSum},
body_ast(DjangoParseTree, Context, TreeWalker)),
Clause = erl_syntax:clause([erl_syntax:string(Tag), erl_syntax:variable("Variables"), erl_syntax:variable("TranslationFun")],
Clause = erl_syntax:clause([erl_syntax:string(Tag), erl_syntax:variable("Variables"), options_ast()],
none, [BodyAst]),
custom_tags_clauses_ast1(CustomTags, [Tag|ExcludeTags], [Clause|ClauseAcc], merge_info(BodyAstInfo, InfoAcc),
Context, TreeWalker1);
Expand Down Expand Up @@ -1081,6 +1081,9 @@ tag_ast(Name, Args, Context, TreeWalker) ->
{InterpretedArgs, AstInfo} = lists:mapfoldl(fun
({{identifier, _, Key}, {string_literal, _, Value}}, AstInfoAcc) ->
{erl_syntax:tuple([erl_syntax:string(Key), erl_syntax:string(unescape_string_literal(Value))]), AstInfoAcc};
({{identifier, _, Key}, {trans, StringLiteral}}, AstInfoAcc) ->
{{TransAst, TransAstInfo}, _} = translated_ast(StringLiteral, Context, TreeWalker),
{erl_syntax:tuple([erl_syntax:string(Key), TransAst]), merge_info(TransAstInfo, AstInfoAcc)};
({{identifier, _, Key}, Value}, AstInfoAcc) ->
{AST, VarName} = resolve_variable_ast(Value, Context),
{erl_syntax:tuple([erl_syntax:string(Key), format(AST,Context, TreeWalker)]), merge_info(#ast_info{var_names=[VarName]}, AstInfoAcc)}
Expand All @@ -1089,14 +1092,20 @@ tag_ast(Name, Args, Context, TreeWalker) ->
{RenderAst, RenderInfo} = case Context#dtl_context.custom_tags_module of
none ->
{erl_syntax:application(none, erl_syntax:atom(render_tag),
[erl_syntax:string(Name), erl_syntax:list(InterpretedArgs), erl_syntax:variable("TranslationFun")]),
[erl_syntax:string(Name), erl_syntax:list(InterpretedArgs), options_ast()]),
AstInfo#ast_info{custom_tags = [Name]}};
Module ->
{erl_syntax:application(erl_syntax:atom(Module), erl_syntax:atom(Name),
[erl_syntax:list(InterpretedArgs), erl_syntax:variable("TranslationFun")]), AstInfo}
[erl_syntax:list(InterpretedArgs), options_ast()]), AstInfo}
end,
{{RenderAst, RenderInfo}, TreeWalker}.

options_ast() ->
erl_syntax:list([
erl_syntax:tuple([erl_syntax:atom(translation_fun), erl_syntax:variable("TranslationFun")]),
erl_syntax:tuple([erl_syntax:atom(locale), erl_syntax:variable("CurrentLocale")])
]).

call_ast(Module, TreeWalkerAcc) ->
call_ast(Module, erl_syntax:variable("Variables"), #ast_info{}, TreeWalkerAcc).

Expand All @@ -1108,10 +1117,7 @@ call_ast(Module, Variable, AstInfo, TreeWalker) ->
AppAst = erl_syntax:application(
erl_syntax:atom(Module),
erl_syntax:atom(render),
[Variable, erl_syntax:list([
erl_syntax:tuple([erl_syntax:atom(translation_fun), erl_syntax:variable("TranslationFun")]),
erl_syntax:tuple([erl_syntax:atom(locale), erl_syntax:variable("CurrentLocale")])
])]),
[Variable, options_ast()]),
RenderedAst = erl_syntax:variable("Rendered"),
OkAst = erl_syntax:clause(
[erl_syntax:tuple([erl_syntax:atom(ok), RenderedAst])],
Expand Down
2 changes: 1 addition & 1 deletion tests/input/custom_tag
Expand Up @@ -6,7 +6,7 @@
</head>
<body>
before
{% flashvideo dom_id="myvideo" width="800" height="600" static="/static" path_to_video="/myvid.mp4" path_to_preview_image="/mypic.jpg" %}
{% flashvideo dom_id="myvideo" width="800" height="600" static="/static" path_to_video="/myvid.mp4" path_to_preview_image="/mypic.jpg" alt=_("Get Adobe Flash player") %}
after
</body>
</html>

0 comments on commit 13362ee

Please sign in to comment.