My favorites | Sign in
Project Home
Checkout   Browse   Changes  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
%%%-------------------------------------------------------------------
%%% File: erlydtl_compiler.erl
%%% @author Roberto Saccon <rsaccon@gmail.com> [http://rsaccon.com]
%%% @author Evan Miller <emmiller@gmail.com>
%%% @copyright 2008 Roberto Saccon, Evan Miller
%%% @doc
%%% ErlyDTL template compiler
%%% @end
%%%
%%% The MIT License
%%%
%%% Copyright (c) 2007 Roberto Saccon, Evan Miller
%%%
%%% Permission is hereby granted, free of charge, to any person obtaining a copy
%%% of this software and associated documentation files (the "Software"), to deal
%%% in the Software without restriction, including without limitation the rights
%%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%%% copies of the Software, and to permit persons to whom the Software is
%%% furnished to do so, subject to the following conditions:
%%%
%%% The above copyright notice and this permission notice shall be included in
%%% all copies or substantial portions of the Software.
%%%
%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
%%% THE SOFTWARE.
%%%
%%% @since 2007-12-16 by Roberto Saccon, Evan Miller
%%%-------------------------------------------------------------------
-module(erlydtl_compiler).
-author('rsaccon@gmail.com').
-author('emmiller@gmail.com').

-export([compile/2, compile/3]).

-record(dtl_context, {
local_scopes = [],
block_dict = dict:new(),
auto_escape = off,
doc_root = "",
parse_trail = [],
vars = [],
custom_tags_dir = [],
reader = {file, read_file},
module = [],
force_recompile = false}).

-record(ast_info, {
dependencies = [],
var_names = [],
pre_render_asts = []}).

-record(treewalker, {
counter = 0,
custom_tags = []}).


compile(File, Module) ->
compile(File, Module, []).

compile(File, Module, Options) ->
crypto:start(),
Context = init_dtl_context(File, Module, Options),
case parse(File, Context) of
ok ->
ok;
{ok, DjangoParseTree, CheckSum} ->
try body_ast(DjangoParseTree, Context, #treewalker{}) of
{{Ast, Info}, _} ->
case compile:forms(forms(File, Module, Ast, Info, CheckSum), []) of
{ok, Module1, Bin} ->
OutDir = proplists:get_value(out_dir, Options, "ebin"),
BeamFile = filename:join([OutDir, atom_to_list(Module1) ++ ".beam"]),
case file:write_file(BeamFile, Bin) of
ok ->
code:purge(Module1),
case code:load_binary(Module1, atom_to_list(Module1) ++ ".erl", Bin) of
{module, _} -> ok;
_ -> {error, lists:concat(["code reload failed: ", BeamFile])}
end;
{error, Reason} ->
{error, lists:concat(["beam generation failed (", Reason, "): ", BeamFile])}
end;
error ->
{error, lists:concat(["compilation failed: ", File])};
OtherError ->
OtherError
end
catch
throw:Error -> Error
end;
Err ->
Err
end.


%%====================================================================
%% Internal functions
%%====================================================================

init_dtl_context(File, Module, Options) ->
Ctx = #dtl_context{},
#dtl_context{
parse_trail = [File],
module = list_to_atom(Module),
doc_root = proplists:get_value(doc_root, Options, filename:dirname(File)),
custom_tags_dir = proplists:get_value(custom_tags_dir, Options, Ctx#dtl_context.custom_tags_dir),
vars = proplists:get_value(vars, Options, Ctx#dtl_context.vars),
reader = proplists:get_value(reader, Options, Ctx#dtl_context.reader),
force_recompile = proplists:get_value(force_recompile, Options, Ctx#dtl_context.force_recompile)}.


is_up_to_date(_, #dtl_context{force_recompile = true}) ->
false;
is_up_to_date(CheckSum, Context) ->
Module = Context#dtl_context.module,
{M, F} = Context#dtl_context.reader,
case catch Module:source() of
{_, CheckSum} ->
case catch Module:dependencies() of
L when is_list(L) ->
RecompileList = lists:foldl(fun
({XFile, XCheckSum}, Acc) ->
case catch M:F(XFile) of
{ok, Data} ->
case binary_to_list(crypto:sha(Data)) of
XCheckSum ->
Acc;
_ ->
[recompile | Acc]
end;
_ ->
[recompile | Acc]
end
end, [], L),
case RecompileList of
[] -> true;
_ -> false
end;
_ ->
false
end;
_ ->
false
end.


parse(Data) ->
case erlydtl_scanner:scan(binary_to_list(Data)) of
{ok, Tokens} ->
erlydtl_parser:parse(Tokens);
Err ->
Err
end.


parse(CheckSum, Data, Context) ->
case is_up_to_date(CheckSum, Context) of
true ->
ok;
_ ->
case parse(Data) of
{ok, Val} ->
{ok, Val, CheckSum};
Err ->
Err
end
end.


parse(File, Context) ->
{M, F} = Context#dtl_context.reader,
case catch M:F(File) of
{ok, Data} ->
CheckSum = binary_to_list(crypto:sha(Data)),
parse(CheckSum, Data, Context);
_ ->
{error, "reading " ++ File ++ " failed "}
end.


forms(File, Module, BodyAst, BodyInfo, CheckSum) ->
Render0FunctionAst = erl_syntax:function(erl_syntax:atom(render),
[erl_syntax:clause([], none, [erl_syntax:application(none,
erl_syntax:atom(render), [erl_syntax:list([])])])]),
Function2 = erl_syntax:application(none, erl_syntax:atom(render2),
[erl_syntax:variable("Variables")]),
ClauseOk = erl_syntax:clause([erl_syntax:variable("Val")], none,
[erl_syntax:tuple([erl_syntax:atom(ok), erl_syntax:variable("Val")])]),
ClauseCatch = erl_syntax:clause([erl_syntax:variable("Err")], none,
[erl_syntax:tuple([erl_syntax:atom(error), erl_syntax:variable("Err")])]),
Render1FunctionAst = erl_syntax:function(erl_syntax:atom(render),
[erl_syntax:clause([erl_syntax:variable("Variables")], none,
[erl_syntax:try_expr([Function2], [ClauseOk], [ClauseCatch])])]),

SourceFunctionTuple = erl_syntax:tuple(
[erl_syntax:string(File), erl_syntax:string(CheckSum)]),
SourceFunctionAst = erl_syntax:function(
erl_syntax:atom(source),
[erl_syntax:clause([], none, [SourceFunctionTuple])]),

DependenciesFunctionAst = erl_syntax:function(
erl_syntax:atom(dependencies), [erl_syntax:clause([], none,
[erl_syntax:list(lists:map(fun
({XFile, XCheckSum}) ->
erl_syntax:tuple([erl_syntax:string(XFile), erl_syntax:string(XCheckSum)])
end, BodyInfo#ast_info.dependencies))])]),

RenderInternalFunctionAst = erl_syntax:function(
erl_syntax:atom(render2),
[erl_syntax:clause([erl_syntax:variable("Variables")], none,
[BodyAst])]),

ProplistsClauseErr = erl_syntax:clause([erl_syntax:atom(undefined)], none,
[erl_syntax:application(none, erl_syntax:atom(throw),
[erl_syntax:tuple([erl_syntax:atom(undefined_variable), erl_syntax:variable("Key")])])]),
ProplistsClauseOk = erl_syntax:clause([erl_syntax:variable("Val")], none,
[erl_syntax:variable("Val")]),
ProplistsFunctionAst = erl_syntax:function(erl_syntax:atom(get_value),
[erl_syntax:clause([erl_syntax:variable("Key"), erl_syntax:variable("L")], none,
[erl_syntax:case_expr(erl_syntax:application(erl_syntax:atom(proplists),
erl_syntax:atom(get_value), [erl_syntax:variable("Key"), erl_syntax:variable("L")]),
[ProplistsClauseErr, ProplistsClauseOk])])]),

ModuleAst = erl_syntax:attribute(erl_syntax:atom(module), [erl_syntax:atom(Module)]),

ExportAst = erl_syntax:attribute(erl_syntax:atom(export),
[erl_syntax:list([erl_syntax:arity_qualifier(erl_syntax:atom(render), erl_syntax:integer(0)),
erl_syntax:arity_qualifier(erl_syntax:atom(render), erl_syntax:integer(1)),
erl_syntax:arity_qualifier(erl_syntax:atom(source), erl_syntax:integer(0)),
erl_syntax:arity_qualifier(erl_syntax:atom(dependencies), erl_syntax:integer(0))])]),

[erl_syntax:revert(X) || X <- [ModuleAst, ExportAst, Render0FunctionAst,
Render1FunctionAst, SourceFunctionAst, DependenciesFunctionAst, RenderInternalFunctionAst,
ProplistsFunctionAst | BodyInfo#ast_info.pre_render_asts]].


% child templates should only consist of blocks at the top level
body_ast([{extends, {string_literal, _Pos, String}} | ThisParseTree], Context, TreeWalker) ->
File = full_path(unescape_string_literal(String), Context#dtl_context.doc_root),
case lists:member(File, Context#dtl_context.parse_trail) of
true ->
throw({error, "Circular file inclusion!"});
_ ->
case parse(File, Context) of
{ok, ParentParseTree, CheckSum} ->
BlockDict = lists:foldl(
fun
({block, {identifier, _, Name}, Contents}, Dict) ->
dict:store(Name, Contents, Dict);
(_, Dict) ->
Dict
end, dict:new(), ThisParseTree),
with_dependency({File, CheckSum}, body_ast(ParentParseTree, Context#dtl_context{
block_dict = dict:merge(fun(_Key, _ParentVal, ChildVal) -> ChildVal end,
BlockDict, Context#dtl_context.block_dict),
parse_trail = [File | Context#dtl_context.parse_trail]}, TreeWalker));
Err ->
throw(Err)
end
end;


body_ast(DjangoParseTree, Context, TreeWalker) ->
{AstInfoList, TreeWalker2} = lists:mapfoldl(
fun
({'block', {identifier, _, Name}, Contents}, TreeWalkerAcc) ->
Block = case dict:find(Name, Context#dtl_context.block_dict) of
{ok, ChildBlock} ->
ChildBlock;
_ ->
Contents
end,
body_ast(Block, Context, TreeWalkerAcc);
({'comment', _Contents}, TreeWalkerAcc) ->
empty_ast(TreeWalkerAcc);
({'autoescape', {identifier, _, OnOrOff}, Contents}, TreeWalkerAcc) ->
body_ast(Contents, Context#dtl_context{auto_escape = list_to_atom(OnOrOff)}, TreeWalkerAcc);
({'text', _Pos, String}, TreeWalkerAcc) ->
string_ast(String, TreeWalkerAcc);
({'string_literal', _Pos, String}, TreeWalkerAcc) ->
{{auto_escape(erl_syntax:string(unescape_string_literal(String)), Context), #ast_info{}}, TreeWalkerAcc};
({'number_literal', _Pos, Number}, TreeWalkerAcc) ->
string_ast(Number, TreeWalkerAcc);
({'variable', Variable}, TreeWalkerAcc) ->
{Ast, VarName} = resolve_variable_ast(Variable, Context),
{{format(Ast, Context), #ast_info{var_names = [VarName]}}, TreeWalkerAcc};
({'include', {string_literal, _, File}}, TreeWalkerAcc) ->
include_ast(unescape_string_literal(File), Context, TreeWalkerAcc);
({'if', {variable, Variable}, Contents}, TreeWalkerAcc) ->
{IfAstInfo, TreeWalker1} = body_ast(Contents, Context, TreeWalkerAcc),
{ElseAstInfo, TreeWalker2} = empty_ast(TreeWalker1),
ifelse_ast(Variable, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
({'if', {'not', {variable, Variable}}, Contents}, TreeWalkerAcc) ->
{IfAstInfo, TreeWalker1} = empty_ast(TreeWalkerAcc),
{ElseAstInfo, TreeWalker2} = body_ast(Contents, Context, TreeWalker1),
ifelse_ast(Variable, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
({'ifelse', {variable, Variable}, IfContents, ElseContents}, TreeWalkerAcc) ->
{IfAstInfo, TreeWalker1} = body_ast(IfContents, Context, TreeWalkerAcc),
{ElseAstInfo, TreeWalker2} = body_ast(ElseContents, Context, TreeWalker1),
ifelse_ast(Variable, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
({'ifelse', {'not', {variable, Variable}}, IfContents, ElseContents}, TreeWalkerAcc) ->
{IfAstInfo, TreeWalker1} = body_ast(ElseContents, Context, TreeWalkerAcc),
{ElseAstInfo, TreeWalker2} = body_ast(IfContents, Context, TreeWalker1),
ifelse_ast(Variable, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
({'ifequal', Args, Contents}, TreeWalkerAcc) ->
{IfAstInfo, TreeWalker1} = body_ast(Contents, Context, TreeWalkerAcc),
{ElseAstInfo, TreeWalker2} = empty_ast(TreeWalker1),
ifequalelse_ast(Args, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
({'ifequalelse', Args, IfContents, ElseContents}, TreeWalkerAcc) ->
{IfAstInfo, TreeWalker1} = body_ast(IfContents, Context, TreeWalkerAcc),
{ElseAstInfo, TreeWalker2} = body_ast(ElseContents, Context,TreeWalker1),
ifequalelse_ast(Args, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
({'ifnotequal', Args, Contents}, TreeWalkerAcc) ->
{IfAstInfo, TreeWalker1} = empty_ast(TreeWalkerAcc),
{ElseAstInfo, TreeWalker2} = body_ast(Contents, Context, TreeWalker1),
ifequalelse_ast(Args, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
({'ifnotequalelse', Args, IfContents, ElseContents}, TreeWalkerAcc) ->
{IfAstInfo, TreeWalker1} = body_ast(ElseContents, Context, TreeWalkerAcc),
{ElseAstInfo, TreeWalker2} = body_ast(IfContents, Context, TreeWalker1),
ifequalelse_ast(Args, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
({'apply_filter', Variable, Filter}, TreeWalkerAcc) ->
filter_ast(Variable, Filter, Context, TreeWalkerAcc);
({'for', {'in', IteratorList, Variable}, Contents}, TreeWalkerAcc) ->
for_loop_ast(IteratorList, Variable, Contents, Context, TreeWalkerAcc);
({'load', Names}, TreeWalkerAcc) ->
load_ast(Names, Context, TreeWalkerAcc);
({'tag', {identifier, _, Name}, Args}, TreeWalkerAcc) ->
tag_ast(Name, Args, Context, TreeWalkerAcc);
({'call', {'identifier', _, Name}}, TreeWalkerAcc) ->
call_ast(Name, TreeWalkerAcc);
({'call', {'identifier', _, Name}, {variable, With}}, TreeWalkerAcc) ->
call_with_ast(Name, With, Context, TreeWalkerAcc)
end, TreeWalker, DjangoParseTree),
{AstList, {Info, TreeWalker3}} = lists:mapfoldl(
fun({Ast, Info}, {InfoAcc, TreeWalkerAcc}) ->
PresetVars = lists:foldl(fun
(X, Acc) ->
case proplists:lookup(list_to_atom(X), Context#dtl_context.vars) of
none ->
Acc;
Val ->
[erl_syntax:abstract(Val) | Acc]
end
end, [], Info#ast_info.var_names),
case PresetVars of
[] ->
{Ast, {merge_info(Info, InfoAcc), TreeWalkerAcc}};
_ ->
Counter = TreeWalkerAcc#treewalker.counter,
Name = lists:concat([pre_render, Counter]),
Ast1 = erl_syntax:application(none, erl_syntax:atom(Name),
[erl_syntax:list(PresetVars)]),
PreRenderAst = erl_syntax:function(erl_syntax:atom(Name),
[erl_syntax:clause([erl_syntax:variable("Variables")], none, [Ast])]),
PreRenderAsts = Info#ast_info.pre_render_asts,
Info1 = Info#ast_info{pre_render_asts = [PreRenderAst | PreRenderAsts]},
{Ast1, {merge_info(Info1, InfoAcc), TreeWalkerAcc#treewalker{counter = Counter + 1}}}
end
end, {#ast_info{}, TreeWalker2}, AstInfoList),
{{erl_syntax:list(AstList), Info}, TreeWalker3}.


merge_info(Info1, Info2) ->
#ast_info{dependencies =
lists:merge(
lists:sort(Info1#ast_info.dependencies),
lists:sort(Info2#ast_info.dependencies)),
var_names =
lists:merge(
lists:sort(Info1#ast_info.var_names),
lists:sort(Info2#ast_info.var_names)),
pre_render_asts =
lists:merge(
Info1#ast_info.pre_render_asts,
Info2#ast_info.pre_render_asts)}.


with_dependencies([], Args) ->
Args;
with_dependencies([H, T], Args) ->
with_dependencies(T, with_dependency(H, Args)).

with_dependency(FilePath, {{Ast, Info}, TreeWalker}) ->
{{Ast, Info#ast_info{dependencies = [FilePath | Info#ast_info.dependencies]}}, TreeWalker}.


empty_ast(TreeWalker) ->
{{erl_syntax:list([]), #ast_info{}}, TreeWalker}.


string_ast(String, TreeWalker) ->
{{erl_syntax:string(String), #ast_info{}}, TreeWalker}. %% less verbose AST, better for development and debugging
% {{erl_syntax:binary([erl_syntax:binary_field(erl_syntax:integer(X)) || X <- String]), #ast_info{}}, TreeWalker}.


include_ast(File, Context, TreeWalker) ->
FilePath = full_path(File, Context#dtl_context.doc_root),
case parse(FilePath, Context) of
{ok, InclusionParseTree, CheckSum} ->
with_dependency({FilePath, CheckSum}, body_ast(InclusionParseTree, Context#dtl_context{
parse_trail = [FilePath | Context#dtl_context.parse_trail]}, TreeWalker));
Err ->
throw(Err)
end.


filter_ast(Variable, Filter, Context, TreeWalker) ->
% the escape filter is special; it is always applied last, so we have to go digging for it

% AutoEscape = 'did' means we (will have) decided whether to escape the current variable,
% so don't do any more escaping
{{UnescapedAst, Info}, TreeWalker2} = filter_ast_noescape(Variable, Filter,
Context#dtl_context{auto_escape = did}, TreeWalker),
case search_for_escape_filter(Variable, Filter, Context) of
on ->
{{erl_syntax:application(
erl_syntax:atom(erlydtl_filters),
erl_syntax:atom(force_escape),
[UnescapedAst]),
Info}, TreeWalker2};
_ ->
{{UnescapedAst, Info}, TreeWalker2}
end.

filter_ast_noescape(Variable, [{identifier, _, "escape"}], Context, TreeWalker) ->
body_ast([Variable], Context, TreeWalker);
filter_ast_noescape(Variable, [{identifier, _, Name} | Arg], Context, TreeWalker) ->
{{VariableAst, Info}, TreeWalker2} = body_ast([Variable], Context, TreeWalker),
{{erl_syntax:application(erl_syntax:atom(erlydtl_filters), erl_syntax:atom(Name),
[VariableAst | case Arg of
[{string_literal, _, ArgName}] ->
[erl_syntax:string(unescape_string_literal(ArgName))];
[{number_literal, _, ArgName}] ->
[erl_syntax:integer(list_to_integer(ArgName))];
_ ->
[]
end]), Info}, TreeWalker2}.

search_for_escape_filter(_, _, #dtl_context{auto_escape = on}) ->
on;
search_for_escape_filter(_, _, #dtl_context{auto_escape = did}) ->
off;
search_for_escape_filter(Variable, Filter, _) ->
search_for_escape_filter(Variable, Filter).

search_for_escape_filter(_, [{identifier, _, "escape"}]) ->
on;
search_for_escape_filter({apply_filter, Variable, Filter}, _) ->
search_for_escape_filter(Variable, Filter);
search_for_escape_filter(_Variable, _Filter) ->
off.

resolve_variable_ast(VarTuple, Context) ->
resolve_variable_ast(VarTuple, Context, none).

resolve_ifvariable_ast(VarTuple, Context) ->
resolve_variable_ast(VarTuple, Context, erl_syntax:atom(proplists)).

resolve_variable_ast({{identifier, _, VarName}}, Context, ModuleAst) ->
{resolve_variable_name_ast(VarName, Context, ModuleAst), VarName};

resolve_variable_ast({{identifier, _, VarName}, {identifier, _, AttrName}}, Context, ModuleAst) ->
{erl_syntax:application(ModuleAst, erl_syntax:atom(get_value),
[erl_syntax:atom(AttrName), resolve_variable_name_ast(VarName, Context)]), VarName}.


resolve_variable_name_ast(VarName, Context) ->
resolve_variable_name_ast(VarName, Context, none).


resolve_variable_name_ast(VarName, Context, ModuleAst) ->
VarValue = lists:foldl(fun(Scope, Value) ->
case Value of
undefined ->
proplists:get_value(list_to_atom(VarName), Scope);
_ ->
Value
end
end, undefined, Context#dtl_context.local_scopes),
case VarValue of
undefined ->
erl_syntax:application(ModuleAst, erl_syntax:atom('get_value'),
[erl_syntax:atom(VarName), erl_syntax:variable("Variables")]);
_ ->
VarValue
end.


format(Ast, Context) ->
auto_escape(format_integer_ast(Ast), Context).


format_integer_ast(Ast) ->
erl_syntax:application(erl_syntax:atom(erlydtl_filters), erl_syntax:atom(format_integer),
[Ast]).


auto_escape(Value, Context) ->
case Context#dtl_context.auto_escape of
on ->
erl_syntax:application(erl_syntax:atom(erlydtl_filters), erl_syntax:atom(force_escape),
[Value]);
_ ->
Value
end.


ifelse_ast(Variable, {IfContentsAst, IfContentsInfo}, {ElseContentsAst, ElseContentsInfo}, Context, TreeWalker) ->
Info = merge_info(IfContentsInfo, ElseContentsInfo),
VarNames = Info#ast_info.var_names,
{Ast, VarName} = resolve_ifvariable_ast(Variable, Context),
{{erl_syntax:case_expr(Ast,
[erl_syntax:clause([erl_syntax:string("")], none,
[ElseContentsAst]),
erl_syntax:clause([erl_syntax:atom(undefined)], none,
[ElseContentsAst]),
erl_syntax:clause([erl_syntax:atom(false)], none,
[ElseContentsAst]),
erl_syntax:clause([erl_syntax:string("0")], none,
[ElseContentsAst]),
erl_syntax:clause([erl_syntax:underscore()], none,
[IfContentsAst])
]), Info#ast_info{var_names = [VarName | VarNames]}}, TreeWalker}.


ifequalelse_ast(Args, {IfContentsAst, IfContentsInfo}, {ElseContentsAst, ElseContentsInfo}, Context, TreeWalker) ->
Info = merge_info(IfContentsInfo, ElseContentsInfo),
{[Arg1Ast, Arg2Ast], VarNames} = lists:foldl(fun
(X, {Asts, AccVarNames}) ->
case X of
{variable, Var} ->
{Ast, VarName} = resolve_ifvariable_ast(Var, Context),
{[Ast | Asts], [VarName | AccVarNames]};
{string_literal, _, Literal} ->
{[erl_syntax:string(unescape_string_literal(Literal)) | Asts], AccVarNames};
{number_literal, _, Literal} ->
{[erl_syntax:integer(list_to_integer(Literal)) | Asts], AccVarNames}
end
end,
{[], Info#ast_info.var_names},
Args),
Ast = erl_syntax:application(none, erl_syntax:atom(apply), [erl_syntax:fun_expr(
[erl_syntax:clause([erl_syntax:variable("Arg1"), erl_syntax:variable("Arg2")], none,
[erl_syntax:case_expr(erl_syntax:variable("Arg1"),
[erl_syntax:clause([erl_syntax:variable("Arg2")], none, [IfContentsAst]),
erl_syntax:clause([erl_syntax:underscore()], none, [ElseContentsAst])])])]),
erl_syntax:list([Arg1Ast, Arg2Ast])]),
{{Ast, Info#ast_info{var_names = VarNames}}, TreeWalker}.


for_loop_ast(IteratorList, {variable, Variable}, Contents, Context, TreeWalker) ->
Vars = lists:map(fun({identifier, _, Iterator}) ->
erl_syntax:variable("Var_" ++ Iterator)
end, IteratorList),
CounterVars = erl_syntax:list([
erl_syntax:tuple([erl_syntax:atom('counter'), erl_syntax:variable("Counter")]),
erl_syntax:tuple([erl_syntax:atom('counter0'), erl_syntax:variable("Counter0")])
]),
{{InnerAst, Info}, TreeWalker2} = body_ast(Contents,
Context#dtl_context{local_scopes = [
[{'forloop', CounterVars} | lists:map(
fun({identifier, _, Iterator}) ->
{list_to_atom(Iterator), erl_syntax:variable("Var_" ++ Iterator)}
end, IteratorList)] | Context#dtl_context.local_scopes]}, TreeWalker),
CounterAst = erl_syntax:list([
erl_syntax:tuple([erl_syntax:atom('counter'),
erl_syntax:infix_expr(erl_syntax:variable("Counter"), erl_syntax:operator("+"), erl_syntax:integer(1))]),
erl_syntax:tuple([erl_syntax:atom('counter0'),
erl_syntax:infix_expr(erl_syntax:variable("Counter0"), erl_syntax:operator("+"), erl_syntax:integer(1))])
]),
{ListAst, VarName} = resolve_ifvariable_ast(Variable, Context),
CounterVars0 = erl_syntax:list([
erl_syntax:tuple([erl_syntax:atom('counter'), erl_syntax:integer(1)]),
erl_syntax:tuple([erl_syntax:atom('counter0'), erl_syntax:integer(0)])
]),
{{erl_syntax:application(
erl_syntax:atom('erlang'), erl_syntax:atom('element'),
[erl_syntax:integer(1), erl_syntax:application(
erl_syntax:atom('lists'), erl_syntax:atom('mapfoldl'),
[erl_syntax:fun_expr([
erl_syntax:clause([erl_syntax:tuple(Vars), CounterVars], none,
[erl_syntax:tuple([InnerAst, CounterAst])]),
erl_syntax:clause(case Vars of [H] -> [H, CounterVars];
_ -> [erl_syntax:list(Vars), CounterVars] end, none,
[erl_syntax:tuple([InnerAst, CounterAst])])
]),
CounterVars0, ListAst])]),
Info#ast_info{var_names = [VarName]}}, TreeWalker2}.

load_ast(Names, _Context, TreeWalker) ->
CustomTags = lists:merge([X || {identifier, _ , X} <- Names], TreeWalker#treewalker.custom_tags),
{{erl_syntax:list([]), #ast_info{}}, TreeWalker#treewalker{custom_tags = CustomTags}}.


unescape_string_literal(String) ->
unescape_string_literal(string:strip(String, both, 34), [], noslash).

unescape_string_literal([], Acc, noslash) ->
lists:reverse(Acc);
unescape_string_literal([$\\ | Rest], Acc, noslash) ->
unescape_string_literal(Rest, Acc, slash);
unescape_string_literal([C | Rest], Acc, noslash) ->
unescape_string_literal(Rest, [C | Acc], noslash);
unescape_string_literal("n" ++ Rest, Acc, slash) ->
unescape_string_literal(Rest, ["\n" | Acc], noslash);
unescape_string_literal("r" ++ Rest, Acc, slash) ->
unescape_string_literal(Rest, ["\r" | Acc], noslash);
unescape_string_literal("t" ++ Rest, Acc, slash) ->
unescape_string_literal(Rest, ["\t" | Acc], noslash);
unescape_string_literal([C | Rest], Acc, slash) ->
unescape_string_literal(Rest, [C | Acc], noslash).


full_path(File, DocRoot) ->
filename:join([DocRoot, File]).

%%-------------------------------------------------------------------
%% Custom tags
%%-------------------------------------------------------------------

tag_ast(Name, Args, Context, TreeWalker) ->
case lists:member(Name, TreeWalker#treewalker.custom_tags) of
true ->
InterpretedArgs = lists:map(fun
({{identifier, _, Key}, {string_literal, _, Value}}) ->
{list_to_atom(Key), erl_syntax:string(unescape_string_literal(Value))};
({{identifier, _, Key}, {variable, Value}}) ->
{list_to_atom(Key), format(resolve_variable_ast(Value, Context), Context)}
end, Args),
DefaultFilePath = filename:join([erlydtl_deps:get_base_dir(), "priv", "custom_tags", Name]),
case Context#dtl_context.custom_tags_dir of
[] ->
case parse(DefaultFilePath, Context) of
{ok, TagParseTree, CheckSum} ->
tag_ast2({DefaultFilePath, CheckSum}, TagParseTree, InterpretedArgs, Context, TreeWalker);
_ ->
Reason = lists:concat(["Loading tag source for '", Name, "' failed: ",
DefaultFilePath]),
throw({error, Reason})
end;
_ ->
CustomFilePath = filename:join([Context#dtl_context.custom_tags_dir, Name]),
case parse(CustomFilePath, Context) of
{ok, TagParseTree, CheckSum} ->
tag_ast2({CustomFilePath, CheckSum},TagParseTree, InterpretedArgs, Context, TreeWalker);
_ ->
case parse(DefaultFilePath, Context) of
{ok, TagParseTree, CheckSum} ->
tag_ast2({DefaultFilePath, CheckSum}, TagParseTree, InterpretedArgs, Context, TreeWalker);
_ ->
Reason = lists:concat(["Loading tag source for '", Name, "' failed: ",
CustomFilePath, ", ", DefaultFilePath]),
throw({error, Reason})
end
end
end;
_ ->
throw({error, lists:concat(["Custom tag '", Name, "' not loaded"])})
end.

tag_ast2({Source, _} = Dep, TagParseTree, InterpretedArgs, Context, TreeWalker) ->
with_dependency(Dep, body_ast(TagParseTree, Context#dtl_context{
local_scopes = [ InterpretedArgs | Context#dtl_context.local_scopes ],
parse_trail = [ Source | Context#dtl_context.parse_trail ]}, TreeWalker)).


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

call_with_ast(Module, Variable, Context, TreeWalker) ->
{VarAst, VarName} = resolve_variable_ast(Variable, Context),
call_ast(Module, VarAst, #ast_info{var_names=[VarName]}, TreeWalker).

call_ast(Module, Variable, AstInfo, TreeWalker) ->
AppAst = erl_syntax:application(
erl_syntax:atom(Module),
erl_syntax:atom(render),
[Variable]),
RenderedAst = erl_syntax:variable("Rendered"),
OkAst = erl_syntax:clause(
[erl_syntax:tuple([erl_syntax:atom(ok), RenderedAst])],
none,
[RenderedAst]),
ReasonAst = erl_syntax:variable("Reason"),
ErrStrAst = erl_syntax:application(
erl_syntax:atom(io_lib),
erl_syntax:atom(format),
[erl_syntax:string("error: ~p"), erl_syntax:list([ReasonAst])]),
ErrorAst = erl_syntax:clause(
[erl_syntax:tuple([erl_syntax:atom(error), ReasonAst])],
none,
[ErrStrAst]),
CallAst = erl_syntax:case_expr(AppAst, [OkAst, ErrorAst]),
Module2 = list_to_atom(Module),
with_dependencies(Module2:dependencies(), {{CallAst, AstInfo}, TreeWalker}).

Change log

r118 by rsaccon on Feb 10, 2008   Diff
just a typo and added a comment
Go to: 
Project members, sign in to write a code review

Older revisions

r113 by rsaccon on Feb 5, 2008   Diff
improved error reporting
r112 by rsaccon on Feb 5, 2008   Diff
recompile always if debug compile
custom flag is set
r111 by rsaccon on Feb 5, 2008   Diff
added recompilation-skip if beamfile
exists and checksum of template source
file has not  changed (currently
disabled, but works)
All revisions of this file

File info

Size: 33608 bytes, 700 lines

File properties

svn:executable
*
Powered by Google Project Hosting