Browse Source

Support _("syntax") in custom tags

Evan Miller 14 years ago
parent
commit
13362eeefe
4 changed files 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

+ 1 - 1
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
 * `custom_tags_module` - A module to be used for handling custom tags. Each custom
 tag should correspond to an exported function, e.g.: 
 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
 * `vars` - Variables (and their values) to evaluate at compile-time rather than
 render-time. 
 render-time. 

+ 2 - 2
priv/custom_tags/flashvideo

@@ -12,10 +12,10 @@
     <h2>To view the Video:</h2>
     <h2>To view the Video:</h2>
     <p>
     <p>
     	<a href="http://www.adobe.com/go/getflashplayer">
     	<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>
     	</a>
     </p>
     </p>
     <!--[if !IE]>-->
     <!--[if !IE]>-->
     </object>
     </object>
     <!--<![endif]-->
     <!--<![endif]-->
-</object>
+</object>

+ 13 - 7
src/erlydtl_compiler.erl

@@ -335,7 +335,7 @@ custom_tags_clauses_ast1([Tag|CustomTags], ExcludeTags, ClauseAcc, InfoAcc, Cont
                 {ok, DjangoParseTree, CheckSum} ->
                 {ok, DjangoParseTree, CheckSum} ->
                     {{BodyAst, BodyAstInfo}, TreeWalker1} = with_dependency({CustomTagFile, CheckSum}, 
                     {{BodyAst, BodyAstInfo}, TreeWalker1} = with_dependency({CustomTagFile, CheckSum}, 
                         body_ast(DjangoParseTree, Context, TreeWalker)),
                         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]),
                                 none, [BodyAst]),
                     custom_tags_clauses_ast1(CustomTags, [Tag|ExcludeTags], [Clause|ClauseAcc], merge_info(BodyAstInfo, InfoAcc), 
                     custom_tags_clauses_ast1(CustomTags, [Tag|ExcludeTags], [Clause|ClauseAcc], merge_info(BodyAstInfo, InfoAcc), 
                         Context, TreeWalker1);
                         Context, TreeWalker1);
@@ -1081,6 +1081,9 @@ tag_ast(Name, Args, Context, TreeWalker) ->
     {InterpretedArgs, AstInfo} = lists:mapfoldl(fun
     {InterpretedArgs, AstInfo} = lists:mapfoldl(fun
             ({{identifier, _, Key}, {string_literal, _, Value}}, AstInfoAcc) ->
             ({{identifier, _, Key}, {string_literal, _, Value}}, AstInfoAcc) ->
                 {erl_syntax:tuple([erl_syntax:string(Key), erl_syntax:string(unescape_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) ->
             ({{identifier, _, Key}, Value}, AstInfoAcc) ->
                 {AST, VarName} = resolve_variable_ast(Value, Context),
                 {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)}
                 {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
     {RenderAst, RenderInfo} = case Context#dtl_context.custom_tags_module of
         none ->
         none ->
             {erl_syntax:application(none, erl_syntax:atom(render_tag),
             {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]}};
             AstInfo#ast_info{custom_tags = [Name]}};
         Module ->
         Module ->
             {erl_syntax:application(erl_syntax:atom(Module), erl_syntax:atom(Name),
             {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,
     end,
     {{RenderAst, RenderInfo}, TreeWalker}.
     {{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, TreeWalkerAcc) ->
     call_ast(Module, erl_syntax:variable("Variables"), #ast_info{}, 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(
      AppAst = erl_syntax:application(
 		erl_syntax:atom(Module),
 		erl_syntax:atom(Module),
 		erl_syntax:atom(render),
 		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"),
     RenderedAst = erl_syntax:variable("Rendered"),
     OkAst = erl_syntax:clause(
     OkAst = erl_syntax:clause(
 	      [erl_syntax:tuple([erl_syntax:atom(ok), RenderedAst])], 
 	      [erl_syntax:tuple([erl_syntax:atom(ok), RenderedAst])], 

+ 1 - 1
tests/input/custom_tag

@@ -6,7 +6,7 @@
   </head>
   </head>
   <body>
   <body>
 	before
 	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
 	after
   </body>
   </body>
 </html>
 </html>