Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 217 lines (201 sloc) 11.035 kB
dc377d7 {% blocktrans %} no longer uses identifiers
Evan Miller authored
1 -module(erlydtl_unparser).
2 -export([unparse/1]).
3
4 unparse(DjangoParseTree) ->
5 unparse(DjangoParseTree, []).
6
7 unparse([], Acc) ->
1ece0a6 @kaos fix #153: parse (and unparse) comment tags {# .. #}
kaos authored
8 lists:flatten(lists:reverse(Acc));
dc377d7 {% blocktrans %} no longer uses identifiers
Evan Miller authored
9 unparse([{'extends', Value}|Rest], Acc) ->
10 unparse(Rest, [["{% extends ", unparse_value(Value), " %}"]|Acc]);
11 unparse([{'autoescape', OnOrOff, Contents}|Rest], Acc) ->
12 unparse(Rest, [["{% autoescape ", unparse_identifier(OnOrOff), " %}", unparse(Contents), "{% endautoescape %}"]|Acc]);
13 unparse([{'block', Identifier, Contents}|Rest], Acc) ->
14 unparse(Rest, [["{% block ", unparse_identifier(Identifier), " %}", unparse(Contents), "{% endblock %}"]|Acc]);
cc35ef8 @kaos support count and plural forms in blocktrans blocks (#131)
kaos authored
15 unparse([{'blocktrans', Args, Contents, undefined}|Rest], Acc) ->
16 unparse(Rest, [["{% blocktrans ", unparse_blocktrans_args(Args), "%}", unparse(Contents), "{% endblocktrans %}"]|Acc]);
17 unparse([{'blocktrans', Args, Contents, PluralContents}|Rest], Acc) ->
18 unparse(Rest, [["{% blocktrans ", unparse_args(Args), " %}",
19 unparse(Contents),
20 "{% plural %}",
21 unparse(PluralContents),
22 "{% endblocktrans %}"]|Acc]);
dc377d7 {% blocktrans %} no longer uses identifiers
Evan Miller authored
23 unparse([{'call', Identifier}|Rest], Acc) ->
24 unparse(Rest, [["{% call ", unparse_identifier(Identifier), " %}"]|Acc]);
25 unparse([{'call', Identifier, With}|Rest], Acc) ->
26 unparse(Rest, [["{% call ", unparse_identifier(Identifier), " with ", unparse_args(With), " %}"]|Acc]);
27 unparse([{'comment', Contents}|Rest], Acc) ->
28 unparse(Rest, [["{% comment %}", unparse(Contents), "{% endcomment %}"]|Acc]);
1ece0a6 @kaos fix #153: parse (and unparse) comment tags {# .. #}
kaos authored
29 unparse([{'comment_tag', _Pos, Text}|Rest], Acc) ->
30 unparse(Rest, [["{#", Text, "#}"]|Acc]);
dc377d7 {% blocktrans %} no longer uses identifiers
Evan Miller authored
31 unparse([{'cycle', Names}|Rest], Acc) ->
32 unparse(Rest, [["{% cycle ", unparse(Names), " %}"]|Acc]);
33 unparse([{'cycle_compat', Names}|Rest], Acc) ->
34 unparse(Rest, [["{% cycle ", unparse_cycle_compat_names(Names), " %}"]|Acc]);
35 unparse([{'date', 'now', Value}|Rest], Acc) ->
36 unparse(Rest, [["{% now ", unparse_value(Value), " %}"]|Acc]);
37 unparse([{'filter', FilterList, Contents}|Rest], Acc) ->
38 unparse(Rest, [["{% filter ", unparse_filters(FilterList), " %}", unparse(Contents), "{% endfilter %}"]|Acc]);
39 unparse([{'firstof', Vars}|Rest], Acc) ->
40 unparse(Rest, [["{% firstof ", unparse(Vars), " %}"]|Acc]);
41 unparse([{'for', {'in', IteratorList, Identifier}, Contents}|Rest], Acc) ->
c484084 @kaos Fix indentation according to erlang-mode.
kaos authored
42 unparse(Rest, [["{% for ", unparse_identifier(Identifier), " in ", unparse(IteratorList), " %}",
43 unparse(Contents),
44 "{% endfor %}"]|Acc]);
dc377d7 {% blocktrans %} no longer uses identifiers
Evan Miller authored
45 unparse([{'for', {'in', IteratorList, Identifier}, Contents, EmptyPartsContents}|Rest], Acc) ->
46 unparse(Rest, [["{% for ", unparse_identifier(Identifier), " in ", unparse(IteratorList), " %}",
c484084 @kaos Fix indentation according to erlang-mode.
kaos authored
47 unparse(Contents),
48 "{% empty %}",
49 unparse(EmptyPartsContents),
50 "{% endfor %}"]|Acc]);
dc377d7 {% blocktrans %} no longer uses identifiers
Evan Miller authored
51 unparse([{'if', Expression, Contents}|Rest], Acc) ->
52 unparse(Rest, [["{% if ", unparse_expression(Expression), " %}",
c484084 @kaos Fix indentation according to erlang-mode.
kaos authored
53 unparse(Contents),
54 "{% endif %}"]|Acc]);
11b4380 @thge Support {% ifchanged Expression(s) %} like Django does
thge authored
55 unparse([{'ifchanged', Expression, IfContents}|Rest], Acc) ->
56 unparse(Rest, [["{% ifchanged ", unparse_expression(Expression), " %}",
c484084 @kaos Fix indentation according to erlang-mode.
kaos authored
57 unparse(IfContents),
58 "{% endifchanged %}"]|Acc]);
11b4380 @thge Support {% ifchanged Expression(s) %} like Django does
thge authored
59 unparse([{'ifchangedelse', Expression, IfContents, ElseContents}|Rest], Acc) ->
60 unparse(Rest, [["{% ifchanged ", unparse_expression(Expression), " %}",
c484084 @kaos Fix indentation according to erlang-mode.
kaos authored
61 unparse(IfContents),
62 "{% else %}",
63 unparse(ElseContents),
64 "{% endifchanged %}"]|Acc]);
dc377d7 {% blocktrans %} no longer uses identifiers
Evan Miller authored
65 unparse([{'ifelse', Expression, IfContents, ElseContents}|Rest], Acc) ->
66 unparse(Rest, [["{% if ", unparse_expression(Expression), " %}",
c484084 @kaos Fix indentation according to erlang-mode.
kaos authored
67 unparse(IfContents),
68 "{% else %}",
69 unparse(ElseContents),
70 "{% endif %}"]|Acc]);
dc377d7 {% blocktrans %} no longer uses identifiers
Evan Miller authored
71 unparse([{'ifequal', [Arg1, Arg2], Contents}|Rest], Acc) ->
72 unparse(Rest, [["{% ifequal ", unparse_value(Arg1), " ", unparse_value(Arg2), " %}",
c484084 @kaos Fix indentation according to erlang-mode.
kaos authored
73 unparse(Contents),
74 "{% endifequal %}"]|Acc]);
dc377d7 {% blocktrans %} no longer uses identifiers
Evan Miller authored
75 unparse([{'ifequalelse', [Arg1, Arg2], IfContents, ElseContents}|Rest], Acc) ->
76 unparse(Rest, [["{% ifequal ", unparse_value(Arg1), " ", unparse_value(Arg2), " %}",
c484084 @kaos Fix indentation according to erlang-mode.
kaos authored
77 unparse(IfContents),
78 "{% else %}",
79 unparse(ElseContents),
80 "{% endifequal %}"]|Acc]);
dc377d7 {% blocktrans %} no longer uses identifiers
Evan Miller authored
81 unparse([{'ifnotequal', [Arg1, Arg2], Contents}|Rest], Acc) ->
82 unparse(Rest, [["{% ifnotequal ", unparse_value(Arg1), " ", unparse_value(Arg2), " %}",
c484084 @kaos Fix indentation according to erlang-mode.
kaos authored
83 unparse(Contents),
84 "{% endifnotequal %}"]|Acc]);
dc377d7 {% blocktrans %} no longer uses identifiers
Evan Miller authored
85 unparse([{'ifnotequalelse', [Arg1, Arg2], IfContents, ElseContents}|Rest], Acc) ->
86 unparse(Rest, [["{% ifnotequal ", unparse_value(Arg1), " ", unparse_value(Arg2), " %}",
c484084 @kaos Fix indentation according to erlang-mode.
kaos authored
87 unparse(IfContents),
88 "{% else %}",
89 unparse(ElseContents),
90 "{% endifnotequal %}"]|Acc]);
dc377d7 {% blocktrans %} no longer uses identifiers
Evan Miller authored
91 unparse([{'include', Value, []}|Rest], Acc) ->
92 unparse(Rest, [["{% include ", unparse_value(Value), " %}"]|Acc]);
93 unparse([{'include', Value, Args}|Rest], Acc) ->
94 unparse(Rest, [["{% include ", unparse_value(Value), " with ", unparse_args(Args)]|Acc]);
95 unparse([{'include_only', Value, []}|Rest], Acc) ->
96 unparse(Rest, [["{% include ", unparse_value(Value), " only %}"]|Acc]);
97 unparse([{'include_only', Value, Args}|Rest], Acc) ->
98 unparse(Rest, [["{% include ", unparse_value(Value), " with ", unparse_args(Args), " only %}"]|Acc]);
c624a50 Support for {% regroup %} tag.
Evan Miller authored
99 unparse([{'regroup', {Variable, Identifier1, Identifier2}, Contents}|Rest], Acc) ->
100 unparse(Rest, [["{% regroup ", unparse_value(Variable), " by ", unparse_identifier(Identifier1), " as ", unparse_identifier(Identifier2), " %}",
c484084 @kaos Fix indentation according to erlang-mode.
kaos authored
101 unparse(Contents),
102 "{% endregroup %}"]|Acc]);
dc377d7 {% blocktrans %} no longer uses identifiers
Evan Miller authored
103 unparse([{'spaceless', Contents}|Rest], Acc) ->
104 unparse(Rest, [["{% spaceless %}", unparse(Contents), "{% endspaceless %}"]|Acc]);
105 unparse([{'ssi', Arg}|Rest], Acc) ->
106 unparse(Rest, [["{% ssi ", unparse_value(Arg), " %}"]|Acc]);
107 unparse([{'ssi_parsed', Arg}|Rest], Acc) ->
108 unparse(Rest, [["{% ssi ", unparse_value(Arg), " parsed %}"]|Acc]);
109 unparse([{'string', _, String}|Rest], Acc) ->
110 unparse(Rest, [[String]|Acc]);
111 unparse([{'tag', Identifier, []}|Rest], Acc) ->
112 unparse(Rest, [["{% ", unparse_identifier(Identifier), " %}"]|Acc]);
113 unparse([{'tag', Identifier, Args}|Rest], Acc) ->
114 unparse(Rest, [["{% ", unparse_identifier(Identifier), " ", unparse_args(Args), " %}"]|Acc]);
115 unparse([{'templatetag', Identifier}|Rest], Acc) ->
116 unparse(Rest, [["{% templatetag ", unparse_identifier(Identifier), " %}"]|Acc]);
117 unparse([{'trans', Value}|Rest], Acc) ->
118 unparse(Rest, [["{% trans ", unparse_value(Value), " %}"]|Acc]);
119 unparse([{'widthratio', Numerator, Denominator, Scale}|Rest], Acc) ->
120 unparse(Rest, [["{% widthratio ", unparse_value(Numerator), " ", unparse_value(Denominator), " ", unparse_value(Scale), " %}"]|Acc]);
121 unparse([{'with', Args, Contents}|Rest], Acc) ->
122 unparse(Rest, [["{% with ", unparse_args(Args), " %}",
c484084 @kaos Fix indentation according to erlang-mode.
kaos authored
123 unparse(Contents),
124 "{% endwidth %}"]|Acc]);
dc377d7 {% blocktrans %} no longer uses identifiers
Evan Miller authored
125 unparse([ValueToken|Rest], Acc) ->
126 unparse(Rest, [["{{ ", unparse_value(ValueToken), " }}"]|Acc]).
127
128
129 unparse_identifier({identifier, _, Name}) ->
130 atom_to_list(Name).
131
132 unparse_filters(FilterList) ->
133 unparse_filters(FilterList, []).
134
135 unparse_filters([], Acc) ->
136 lists:reverse(Acc);
137 unparse_filters([Filter], Acc) ->
138 unparse_filters([], [unparse_filter(Filter)|Acc]);
139 unparse_filters([Filter|Rest], Acc) ->
140 unparse_filters(Rest, lists:reverse([unparse_filter(Filter), "|"], Acc)).
141
142 unparse_filter([Identifier]) ->
143 unparse_identifier(Identifier);
144 unparse_filter([Identifier, Arg]) ->
145 [unparse_identifier(Identifier), ":", unparse_value(Arg)].
146
147 unparse_expression({'expr', "in", Arg1, Arg2}) ->
148 [unparse_value(Arg1), " in ", unparse_value(Arg2)];
149 unparse_expression({'expr', "not", {'expr', "in", Arg1, Arg2}}) ->
150 [unparse_value(Arg1), " not in ", unparse_value(Arg2)];
151 unparse_expression({'expr', "not", Expr}) ->
152 ["not ", unparse_expression(Expr)];
153 unparse_expression({'expr', "eq", Arg1, Arg2}) ->
154 [unparse_value(Arg1), " == ", unparse_value(Arg2)];
155 unparse_expression({'expr', "ne", Arg1, Arg2}) ->
156 [unparse_value(Arg1), " != ", unparse_value(Arg2)];
157 unparse_expression({'expr', "ge", Arg1, Arg2}) ->
158 [unparse_value(Arg1), " >= ", unparse_value(Arg2)];
159 unparse_expression({'expr', "le", Arg1, Arg2}) ->
160 [unparse_value(Arg1), " <= ", unparse_value(Arg2)];
161 unparse_expression({'expr', "gt", Arg1, Arg2}) ->
162 [unparse_value(Arg1), " > ", unparse_value(Arg2)];
163 unparse_expression({'expr', "lt", Arg1, Arg2}) ->
164 [unparse_value(Arg1), " < ", unparse_value(Arg2)];
165 unparse_expression({'expr', "or", Arg1, Arg2}) ->
166 [unparse_expression(Arg1), " or ", unparse_expression(Arg2)];
167 unparse_expression({'expr', "and", Arg1, Arg2}) ->
168 [unparse_expression(Arg1), " and ", unparse_expression(Arg2)];
169 unparse_expression(Other) ->
170 unparse_value(Other).
171
172 unparse_value({'string_literal', _, Value}) ->
173 Value;
174 unparse_value({'number_literal', _, Value}) ->
175 Value;
176 unparse_value({'apply_filter', Variable, Filter}) ->
177 [unparse_value(Variable), "|", unparse_filter(Filter)];
178 unparse_value({'attribute', {Variable, Identifier}}) ->
179 [unparse_value(Variable), ".", unparse_identifier(Identifier)];
180 unparse_value({'variable', Identifier}) ->
181 unparse_identifier(Identifier).
182
183 unparse_args(Args) ->
184 unparse_args(Args, []).
185
186 unparse_args([], Acc) ->
187 lists:reverse(Acc);
188 unparse_args([{{identifier, _, Name}, Value}], Acc) ->
189 unparse_args([], [[atom_to_list(Name), "=", unparse_value(Value)]|Acc]);
190 unparse_args([{{identifier, _, Name}, Value}|Rest], Acc) ->
191 unparse_args(Rest, lists:reverse([[atom_to_list(Name), "=", unparse_value(Value)], " "], Acc)).
192
193 unparse_cycle_compat_names(Names) ->
194 unparse_cycle_compat_names(Names, []).
195
196 unparse_cycle_compat_names([], Acc) ->
197 lists:reverse(Acc);
198 unparse_cycle_compat_names([{identifier, _, Name}], Acc) ->
199 unparse_cycle_compat_names([], [atom_to_list(Name)|Acc]);
200 unparse_cycle_compat_names([{identifier, _, Name}|Rest], Acc) ->
201 unparse_cycle_compat_names(Rest, lists:reverse([atom_to_list(Name), ", "], Acc)).
cc35ef8 @kaos support count and plural forms in blocktrans blocks (#131)
kaos authored
202
203 unparse_blocktrans_args(Args) ->
204 unparse_blocktrans_args(Args, []).
205
206 unparse_blocktrans_args([{args, WithArgs}|Args], Acc) ->
207 unparse_blocktrans_args(
208 Args, [["with ", unparse_args(WithArgs)]|Acc]);
209 unparse_blocktrans_args([{count, Count}|Args], Acc) ->
210 unparse_blocktrans_args(
211 Args, [["count ", unparse_args([Count])]|Acc]);
212 unparse_blocktrans_args([{context, Context}|Args], Acc) ->
213 unparse_blocktrans_args(
214 Args, [["context ", unparse_value(Context)]|Acc]);
215 unparse_blocktrans_args([], Acc) ->
216 lists:reverse(Acc).
Something went wrong with that request. Please try again.