-
Notifications
You must be signed in to change notification settings - Fork 5
/
mustache.pas
359 lines (325 loc) · 9.5 KB
/
mustache.pas
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
{
mustache.pas
Copyright 2015 Stijn Sanders
Made available under terms described in file "LICENSE"
https://github.com/stijnsanders/jsonDoc
https://mustache.github.io/
}
unit mustache;
{$D-}
interface
uses jsonDoc, SysUtils;
type
TMustacheTranslator=function(const x:UTF8String):UTF8String;
EMustacheError=class(Exception);
threadvar
MustacheDefaultEncode:TMustacheTranslator;
MustachePartials:TMustacheTranslator;
function ApplyMustache(const Template:UTF8String;Data:IJSONDocument):UTF8String;
implementation
uses Variants;
function FindNext(const Delimiter,Data:UTF8String;Start,DataSize:cardinal):cardinal; //inline;
var
i,dl,ds:cardinal;
begin
Result:=Start;
dl:=Length(Delimiter);
ds:=DataSize-dl+1;
i:=0;
while (Result<=ds) and (i<dl) do
begin
i:=0;
while (i<dl) and (Data[Result+i]=Delimiter[i+1]) do inc(i);
if i<dl then inc(Result);
end;
if Result>ds then Result:=DataSize+1;
end;
function ApplyMustache(const Template:UTF8String;Data:IJSONDocument):UTF8String;
type
TDelimiterType=(dtStart,stStop);
var
i,j,k,l,m,n,ri,rl:cardinal;
t:UTF8String;
Source:array of record
Data:UTF8String;
Index:cardinal;
end;
SourceSize,SourceIndex:cardinal;
Context:array of record
TagName:UTF8String;
Data:IJSONDocument;
Display:boolean;
Sequence:OleVariant;
SourceIndex,SeqIndex,SeqMax:cardinal;
end;
ContextSize,ContextIndex:cardinal;
dStart,dStop,v:UTF8String;
vv:OleVariant;
b:boolean;
dd:IJSONDocument;
const
rGrowStep=$10000;
procedure rCopy(const xx;x:cardinal);//inline;
begin
if ri+x>rl then
begin
while rl<x do inc(rl,rGrowStep);
SetLength(Result,rl);
end;
Move(xx,Result[ri+1],x);
inc(ri,x);
end;
procedure cPush(const pTagName:UTF8String;
const pData:IJSONDocument;pDisplay:boolean);
begin
if ContextIndex=ContextSize then
begin
inc(ContextSize,$100);//cGrowStep;
SetLength(Context,ContextSize);
end;
inc(ContextIndex);
Context[ContextIndex].TagName:=pTagName;
Context[ContextIndex].Data:=pData;
Context[ContextIndex].Display:=Context[ContextIndex-1].Display and pDisplay;
VarClear(Context[ContextIndex].Sequence);
Context[ContextIndex].SeqIndex:=1;
Context[ContextIndex].SeqMax:=0;
Context[ContextIndex].SourceIndex:=k;
end;
procedure vLookup;
var
vi,vj,vl:integer;
n:cardinal;
vx:UTF8String;
begin
vl:=j-i;
v:=Copy(t,i,vl);
vi:=1;
while (vi<=vl) and (v[vi]<>'.') do inc(vi);
vx:=Trim(Copy(v,1,vi-1));
n:=ContextIndex;
vv:=Context[n].Data[vx];
while VarIsNull(vv) and (n<>0) do
begin
dec(n);
vv:=Context[n].Data[v];
end;
while not(VarIsNull(vv)) and (vi<=vl) do
begin
inc(vi);
vj:=vi;
while (vi<=vl) and (v[vi]<>'.') do inc(vi);
vx:=Trim(Copy(v,vj,vi-vj));
//TODO: support more?
vv:=(IUnknown(vv) as IJSONDocument)[vx];
end;
end;
begin
//defaults
dStart:='{{';
dStop:='}}';
ri:=0;
rl:=0;
SourceIndex:=0;
SourceSize:=0;
SetLength(Source,SourceSize);
ContextIndex:=0;
ContextSize:=$100;
SetLength(Context,ContextSize);
Context[0].Data:=Data;
Context[0].Display:=true;
//first bit
t:=Template;
l:=Length(Template);
i:=FindNext(dStart,t,1,l);
if i<>1 then rCopy(t[1],i-1);
//TODO: line/pos on exceptions?
while (i<l) or (SourceIndex<>0) do
begin
while i<l do
begin
//find closing delimiter
inc(i,Length(dStart));
j:=FindNext(dStop,t,i,l);
if j>l then raise EMustacheError.Create('Unterminated mustache tag');
k:=j+cardinal(Length(dStart));
//TODO: consume whitespace
//TODO: ignore in-tag whitespace
if i=j then
begin
//empty tag
end
else
begin
//handle tag
case t[i] of
'!':;//comment (ignore)
'='://set delimiters
begin
//TODO: {{=@ @=}} keep surrounding whitespace
if t[j-1]<>'=' then
raise EMustacheError.Create('Closing "=" required when setting delimiters');
v:=Trim(Copy(t,i+1,j-i-2));
m:=Length(v);
n:=1;
while (n<=m) and (v[n]<>' ') do inc(n);
if n>m then n:=m div 2;//split halfway? (raise?)
dStart:=Trim(Copy(v,1,n-1));
dStop:=Trim(Copy(v,n,m-n+1));
if (dStart='') or (dStop='') then
raise EMustacheError.Create('Empty delimiters not allowed');
//TODO: require ContextIndex=0?
end;
'#','^'://section,inverted
begin
inc(i);
vLookup;
dd:=nil;
m:=1;
n:=0;
case VarType(vv) of
varNull,varEmpty:b:=false;
varBoolean:b:=vv;
varSmallint,
varInteger,
varShortInt,
varByte,
varWord,
varLongWord,
varInt64:b:=vv<>0;
varSingle,
varDouble,
varCurrency:b:=vv<>0.0;
//TODO: varArray
//TODO: empty list: b:=false;
varUnknown:
begin
//if QueryInterface?
dd:=IUnknown(vv) as IJSONDocument;
b:=true;
//TODO: more?.
end;
varArray or varUnknown,varArray or varVariant:
begin
m:=VarArrayLowBound(vv,1);
n:=VarArrayHighBound(vv,1);
b:=n-m+1<>0;
end;
else
raise EMustacheError.CreateFmt('Unexpected subject type "%s"',[v]);
end;
cPush(v,dd,b xor (t[i]='^'));
//start iteration?
if b and (m<n) then
begin
Context[ContextIndex].Sequence:=vv;//VarArrayRef?
Context[ContextIndex].Data:=IUnknown(vv[m]) as IJSONDocument;
Context[ContextIndex].SeqIndex:=m+1;
Context[ContextIndex].SeqMax:=n;
end;
end;
'/'://pop
begin
v:=Trim(Copy(t,i+1,j-i-2));
if ContextIndex=0 then
raise EMustacheError.CreateFmt('Unexpected section end "%s"',[v]);
//TODO: if allow empty end section tag?
if v<>Context[ContextIndex].TagName then
raise EMustacheError.CreateFmt('Mismatching section end "%s"',[v]);
if Context[ContextIndex].SeqIndex<=Context[ContextIndex].SeqMax then
begin
k:=Context[ContextIndex].SourceIndex;
//TODO: support more?
Context[ContextIndex].Data:=IUnknown(
Context[ContextIndex].Sequence[
Context[ContextIndex].SeqIndex
]) as IJSONDocument;
inc(Context[ContextIndex].SeqIndex);
end
else
begin
//cPop;
VarClear(Context[ContextIndex].Sequence);//release
Context[ContextIndex].Data:=nil;//release
dec(ContextIndex);
end;
end;
'>'://partials
if Context[ContextIndex].Display then //?
begin
//cPush?
if SourceIndex=SourceSize then
begin
inc(SourceSize,$10);
SetLength(Source,SourceSize);
end;
//sPush:
Source[SourceIndex].Data:=t;
Source[SourceIndex].Index:=k;
//Source[SourceIndex].Size:=l;
inc(SourceIndex);
//
if @MustachePartials=nil then
raise EMustacheError.Create('MustachePartials not defined');
t:=MustachePartials(Trim(Copy(t,i+1,j-i-1)));
l:=Length(t);
k:=1;//first bit: see below
end;
'{','&'://without HTMLEncode
begin
if t[i]='{' then
begin
if (t[j-1]<>'}') and (Copy(t,j+1,Length(dStop))=dStop) then
begin
inc(j);
inc(k);
end;
if t[j-1]<>'}' then
raise EMustacheError.Create('Closing "}" required on unencoded');
end;
if Context[ContextIndex].Display then
begin
inc(i);
vLookup;
v:=VarToStr(vv);
if v<>'' then rCopy(v[1],Length(v));
end;
end;
else //
if Context[ContextIndex].Display then
begin
vLookup;
v:=VarToStr(vv);
if v<>'' then
begin
if @MustacheDefaultEncode<>nil then v:=MustacheDefaultEncode(v);
rCopy(v[1],Length(v));
end;
end;
end;
end;
//trailing bit
i:=FindNext(dStart,t,k,l);
if Context[ContextIndex].Display then
if i>l then rCopy(t[k],l-k+1) else
if i<>k then rCopy(t[k],i-k);
end;
if SourceIndex<>0 then
begin
dec(SourceIndex);
//cPop?
t:=Source[SourceIndex].Data;
k:=Source[SourceIndex].Index;
l:=Length(t);//Source[SourceIndex].Size;
//trailing bit
i:=FindNext(dStart,t,k,l);
if Context[ContextIndex].Display then
if i>l then rCopy(t[k],l-k+1) else
if i<>k then rCopy(t[k],i-k);
end;
end;
if ContextIndex<>0 then
raise EMustacheError.CreateFmt('Unclosed sections detected: %d',[ContextIndex]);
SetLength(Result,ri);
end;
end.