Browse files

Support _("syntax") in custom tags

  • Loading branch information...
1 parent 62d4f1d commit 13362eeefe5ce4d672d33ebd831bb95e235503f5 Evan Miller committed Jun 25, 2011
Showing with 17 additions and 11 deletions.
  1. +1 −1 README.markdown
  2. +2 −2 priv/custom_tags/flashvideo
  3. +13 −7 src/erlydtl_compiler.erl
  4. +1 −1 tests/input/custom_tag
View
2 README.markdown
@@ -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.
View
4 priv/custom_tags/flashvideo
@@ -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>
View
20 src/erlydtl_compiler.erl
@@ -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);
@@ -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)}
@@ -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).
@@ -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])],
View
2 tests/input/custom_tag
@@ -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.