-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMustache.pck.st
1541 lines (1262 loc) · 47.9 KB
/
Mustache.pck.st
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'From Cuis 6.0 [latest update: #6068] on 14 November 2023 at 7:15:36 am'!
'Description '!
!provides: 'Mustache' 1 62!
!requires: 'Cuis-Base' 60 6068 nil!
SystemOrganization addCategory: #'Mustache-Core'!
SystemOrganization addCategory: #'Mustache-Tests'!
SystemOrganization addCategory: #'Mustache-Cli'!
SystemOrganization addCategory: #'Mustache-PharoCompatibility'!
!classDefinition: #ValueLink category: #'Mustache-PharoCompatibility'!
Link subclass: #ValueLink
instanceVariableNames: 'value'
classVariableNames: ''
poolDictionaries: ''
category: 'Mustache-PharoCompatibility'!
!classDefinition: 'ValueLink class' category: #'Mustache-PharoCompatibility'!
ValueLink class
instanceVariableNames: ''!
!classDefinition: #MustacheTests category: #'Mustache-Tests'!
TestCase subclass: #MustacheTests
instanceVariableNames: 'testFile'
classVariableNames: ''
poolDictionaries: ''
category: 'Mustache-Tests'!
!classDefinition: 'MustacheTests class' category: #'Mustache-Tests'!
MustacheTests class
instanceVariableNames: ''!
!classDefinition: #MustacheParser category: #'Mustache-Core'!
Object subclass: #MustacheParser
instanceVariableNames: 'stream startDelimiter endDelimiter stack delimiterExtensions'
classVariableNames: ''
poolDictionaries: ''
category: 'Mustache-Core'!
!classDefinition: 'MustacheParser class' category: #'Mustache-Core'!
MustacheParser class
instanceVariableNames: ''!
!classDefinition: #MustachePart category: #'Mustache-Core'!
Object subclass: #MustachePart
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Mustache-Core'!
!classDefinition: 'MustachePart class' category: #'Mustache-Core'!
MustachePart class
instanceVariableNames: ''!
!classDefinition: #MustacheCompositePart category: #'Mustache-Core'!
MustachePart subclass: #MustacheCompositePart
instanceVariableNames: 'parts'
classVariableNames: ''
poolDictionaries: ''
category: 'Mustache-Core'!
!classDefinition: 'MustacheCompositePart class' category: #'Mustache-Core'!
MustacheCompositePart class
instanceVariableNames: ''!
!classDefinition: #MustacheSection category: #'Mustache-Core'!
MustacheCompositePart subclass: #MustacheSection
instanceVariableNames: 'selector'
classVariableNames: ''
poolDictionaries: ''
category: 'Mustache-Core'!
!classDefinition: 'MustacheSection class' category: #'Mustache-Core'!
MustacheSection class
instanceVariableNames: ''!
!classDefinition: #MustacheInvertedSection category: #'Mustache-Core'!
MustacheSection subclass: #MustacheInvertedSection
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Mustache-Core'!
!classDefinition: 'MustacheInvertedSection class' category: #'Mustache-Core'!
MustacheInvertedSection class
instanceVariableNames: ''!
!classDefinition: #MustacheTemplate category: #'Mustache-Core'!
MustacheCompositePart subclass: #MustacheTemplate
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Mustache-Core'!
!classDefinition: 'MustacheTemplate class' category: #'Mustache-Core'!
MustacheTemplate class
instanceVariableNames: ''!
!classDefinition: #MustachePartial category: #'Mustache-Core'!
MustachePart subclass: #MustachePartial
instanceVariableNames: 'name'
classVariableNames: ''
poolDictionaries: ''
category: 'Mustache-Core'!
!classDefinition: 'MustachePartial class' category: #'Mustache-Core'!
MustachePartial class
instanceVariableNames: ''!
!classDefinition: #MustacheStringChunk category: #'Mustache-Core'!
MustachePart subclass: #MustacheStringChunk
instanceVariableNames: 'string'
classVariableNames: ''
poolDictionaries: ''
category: 'Mustache-Core'!
!classDefinition: 'MustacheStringChunk class' category: #'Mustache-Core'!
MustacheStringChunk class
instanceVariableNames: ''!
!classDefinition: #MustacheToken category: #'Mustache-Core'!
MustachePart subclass: #MustacheToken
instanceVariableNames: 'token'
classVariableNames: ''
poolDictionaries: ''
category: 'Mustache-Core'!
!classDefinition: 'MustacheToken class' category: #'Mustache-Core'!
MustacheToken class
instanceVariableNames: ''!
!classDefinition: #MustacheHtmlEscapedToken category: #'Mustache-Core'!
MustacheToken subclass: #MustacheHtmlEscapedToken
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Mustache-Core'!
!classDefinition: 'MustacheHtmlEscapedToken class' category: #'Mustache-Core'!
MustacheHtmlEscapedToken class
instanceVariableNames: ''!
!classDefinition: #MustacheVisitor category: #'Mustache-Core'!
Object subclass: #MustacheVisitor
instanceVariableNames: 'partials'
classVariableNames: ''
poolDictionaries: ''
category: 'Mustache-Core'!
!classDefinition: 'MustacheVisitor class' category: #'Mustache-Core'!
MustacheVisitor class
instanceVariableNames: ''!
!classDefinition: #MustacheWriteVisitor category: #'Mustache-Core'!
MustacheVisitor subclass: #MustacheWriteVisitor
instanceVariableNames: 'strings context'
classVariableNames: ''
poolDictionaries: ''
category: 'Mustache-Core'!
!classDefinition: 'MustacheWriteVisitor class' category: #'Mustache-Core'!
MustacheWriteVisitor class
instanceVariableNames: ''!
!classDefinition: #MustacheContextDelegator category: #'Mustache-Tests'!
Object subclass: #MustacheContextDelegator
instanceVariableNames: 'delegate'
classVariableNames: ''
poolDictionaries: ''
category: 'Mustache-Tests'!
!classDefinition: 'MustacheContextDelegator class' category: #'Mustache-Tests'!
MustacheContextDelegator class
instanceVariableNames: ''!
!classDefinition: #MustacheCommandLineHandler category: #'Mustache-Cli'!
ProtoObject subclass: #MustacheCommandLineHandler
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Mustache-Cli'!
!classDefinition: 'MustacheCommandLineHandler class' category: #'Mustache-Cli'!
MustacheCommandLineHandler class
instanceVariableNames: ''!
!ValueLink commentStamp: '' prior: 0!
A ValueLink is a Link containing a Value.
Adding an object to a LinkedList which is not a Link will create a ValueLink containing that object.
value - The object this link points to.!
!MustacheParser commentStamp: '' prior: 0!
A MustacheParser is the internally used parser that is used my MustacheTemplate to compile a template string into a template structure!
!MustachePart commentStamp: '' prior: 0!
A MustachePart is the base class for all entities that are parsed from a template string. The structure is held internally by MustacheTemplate to render a string from a template.
For public access have a look at MustacheTemplate!
!MustacheTemplate commentStamp: '' prior: 0!
A MustacheTemplate is the main class to interact when dealing with mustache templates.
A MustacheTemplate can be created from a string containing mustache template markup either with
MustacheTemplate on: aString
or
aString asMustacheTemplate
To fill out a template just provide a context object and do
aMustacheTemplate value: aContextObject
Nested templates can be used be providing a dictionary with keys as the names used in the master template and values being individual mustache templates!
!MustacheWriteVisitor commentStamp: '' prior: 0!
A MustacheWriteVisitor is the main visitor produces a final string from a template and a context object.
It is used by MustacheTemplate!
!MustacheCommandLineHandler commentStamp: '<historical>' prior: 0!
I am the top level command line handler for Mustache.
My #compile class method is used to compile a template.
My parameters:
-template file name
-JSON file name
-partials (optional) list of filenames inside quotationmarks separated by a space. If you don't have partials but want to use the directory parameter use empty quotationmarks.
-directory where the previous files are stored, if missing I use the current working directory
Eg. 1: squeak -headless "Cuis6.0-XXXX.image" -d "MustacheCommandLineHandler compile" template.txt data.json "partial1.txt partial2.txt" "/home/MyUser/MustacheTemplates"
If you use partials the filename's #baseName will be used as a key in the dictionary containing the partial templates. So use yor filename as a reference in the template.
Eg. 2:
{{#buttonOne}}{{> partial1}}{{/buttonOne}}
{{#buttonTwo}}{{> partial2}}{{/buttonTwo}}
Eg. 3: squeak -headless "Cuis6.0-XXXX.image" -d "MustacheCommandLineHandler compile" template.txt data.json "" "/home/MyUser/MustacheTemplates"!
!ValueLink methodsFor: 'evaluating'!
value
^value! !
!ValueLink methodsFor: 'dispatched'!
asLinkPrepend: anObject
^ (super asLinkPrepend: anObject)
value: anObject;
yourself! !
!ValueLink methodsFor: 'printing'!
printOn: aStream
aStream nextPutAll: 'ValueLink('.
value printOn: aStream.
aStream nextPut: $)! !
!ValueLink methodsFor: 'comparing'!
= anotherObject
^self species == anotherObject species
and: [self value = anotherObject value
and: [self nextLink == anotherObject nextLink]]! !
!ValueLink methodsFor: 'comparing'!
hash
^self value hash bitXor: nextLink identityHash! !
!ValueLink methodsFor: 'accessing'!
value: aValue
value := aValue! !
!ValueLink class methodsFor: 'instance creation'!
value: aValue
^self new value: aValue! !
!MustacheTests methodsFor: 'tests'!
testBlockSection
| template result |
template := MustacheTemplate on: '{{#wrapped}} {{name}} is awesome {{/wrapped}}'.
result := template value: {
'name' -> 'Willy'.
'wrapped' -> [ :render | '<b>', render value, '</b>' ] } asDictionary .
self assert: result = '<b> Willy is awesome </b>'.
! !
!MustacheTests methodsFor: 'tests'!
testBlockValue
| template result |
template := MustacheTemplate on: 'This is a test for {{ name }}.'.
result := template value: { 'name' -> [ 'blockcontent' ] } asDictionary .
self assert: result = 'This is a test for blockcontent.'.
! !
!MustacheTests methodsFor: 'tests'!
testChainingOfContext
| template delegatingObject result |
template := 'Text {{ capitalized }} text' asMustacheTemplate.
delegatingObject := MustacheContextDelegator new
delegate: 'some string'.
result := template value: delegatingObject.
self assert: result equals: 'Text Some string text'
! !
!MustacheTests methodsFor: 'tests'!
testChangeDelimiter
| template result |
template := MustacheTemplate on: 'This is a test for {{ name }} {{=<% %>=}} and something that uses <% delimiter %> delimiter. And change it back to <%={{ }}=%>{{back}}.'.
result := template value: {
'name' -> 'simpletoken' .
'delimiter' -> 'another' .
'back' -> 'normal' } asDictionary .
self assert: result = 'This is a test for simpletoken and something that uses another delimiter. And change it back to normal.'. ! !
!MustacheTests methodsFor: 'tests'!
testComment
| template result |
template := MustacheTemplate on: 'This is a test for {{!! ignore me }}.'.
result := template value: { 'name' -> 'simpletoken' } asDictionary .
self assert: result = 'This is a test for .'
! !
!MustacheTests methodsFor: 'tests'!
testContextDo
| called block |
called := 0.
block := [ :el |called := called + 1 ].
false mustacheDo: block inverted: false.
self assert: called = 0.
called := 0.
true mustacheDo: block inverted: false.
self assert: called = 1.
called := 0.
nil mustacheDo: block inverted: false.
self assert: called = 0.
called := 0.
Object new mustacheDo: block inverted: false.
self assert: called = 1.
called := 0.
#() mustacheDo: block inverted: false.
self assert: called = 0.
called := 0.
#(1 2 3) mustacheDo: block inverted: false.
self assert: called = 3.
! !
!MustacheTests methodsFor: 'tests'!
testContextDoInverted
| called block |
called := 0.
block := [ :el |called := called + 1 ].
false mustacheDo: block inverted: true.
self assert: called = 1.
called := 0.
true mustacheDo: block inverted: true.
self assert: called = 0.
called := 0.
nil mustacheDo: block inverted: true.
self assert: called = 1.
called := 0.
Object new mustacheDo: block inverted: true.
self assert: called = 0.
called := 0.
#() mustacheDo: block inverted: true.
self assert: called = 1.
called := 0.
#(1 2 3) mustacheDo: block inverted: true.
self assert: called = 0.
! !
!MustacheTests methodsFor: 'tests'!
testDefaultUnescapedToken
| template result |
template := MustacheTemplate on: 'This is a test for {{{ name }}}.'.
result := template value: { 'name' -> '&' } asDictionary .
self assert: result = 'This is a test for &.'.
! !
!MustacheTests methodsFor: 'tests' stamp: 'mnqpr 11/6/2023 20:50:51'!
testDictionaryInDictionary
| template result |
template := MustacheTemplate on: '{{#person?}} Hi {{name}}!! {{/person?}}' .
result := template
value: { 'person?' ->
{ 'name' -> 'Jon' } asDictionary } asDictionary.
self assert: result = ' Hi Jon!! ' ! !
!MustacheTests methodsFor: 'tests'!
testDotToken
| template result |
template := MustacheTemplate on: '{{ . }}'.
result := template value: #a.
self assert: result = 'a' ! !
!MustacheTests methodsFor: 'tests'!
testDotTokenWithDictionary
| template result |
template := MustacheTemplate on: '{{ . }}'.
result := template value: {
'foo' -> 'bar' } asDictionary.
self assert: result = 'a Dictionary(''foo''->''bar'' )' ! !
!MustacheTests methodsFor: 'tests'!
testHTMLEscapedToken
| template result |
template := MustacheTemplate on: 'This is a test for {{ name }}.'.
result := template value: { 'name' -> '&' } asDictionary .
self assert: result = 'This is a test for &.'.
! !
!MustacheTests methodsFor: 'tests'!
testHTMLMenuExample
| template result |
template := MustacheTemplate on: '<ul>
{{#entries}}<li class="menuEntry{{#active}} active{{/active}}">{{label}}</li>
{{/entries}}
</ul>' .
result := template
value: { 'entries' -> {
{ 'label' -> 'first' } asDictionary.
{ 'label' -> 'second' . 'active' -> true } asDictionary.
{ 'label' -> 'third' } asDictionary } } asDictionary.
self assert: result = '<ul>
<li class="menuEntry">first</li>
<li class="menuEntry active">second</li>
<li class="menuEntry">third</li>
</ul>' ! !
!MustacheTests methodsFor: 'tests'!
testInvertedSectionWithEmptyListContext
| template result |
template := MustacheTemplate on: 'list{{^ list }} is {{/ list}}displayed'.
result := template value: {
'list' -> { }
} asDictionary.
self assert: result = 'list is displayed' ! !
!MustacheTests methodsFor: 'tests'!
testInvertedSectionWithFalseContext
| template result |
template := MustacheTemplate on: 'list{{^ list }} is {{/ list}}displayed'.
result := template value: {
'list' -> false
} asDictionary.
self assert: result = 'list is displayed' ! !
!MustacheTests methodsFor: 'tests'!
testInvertedSectionWithNonEmptyListContext
| template result |
template := MustacheTemplate on: 'list{{^ list }} is {{/ list}}displayed'.
result := template value: {
'list' -> { 1 }
} asDictionary.
self assert: result = 'listdisplayed' ! !
!MustacheTests methodsFor: 'tests'!
testInvertedSectionWithTrueContext
| template result |
template := MustacheTemplate on: 'list{{^ list }} is {{/ list}}displayed'.
result := template value: {
'list' -> false
} asDictionary.
self assert: result = 'list is displayed' ! !
!MustacheTests methodsFor: 'tests'!
testMultipleTokens
| template result |
template := MustacheTemplate on: '1 = {{ one }}, 2 = {{ two }}, and so on'.
result := template value: { 'one' -> 1 . 'two' -> 2 } asDictionary .
self assert: result = '1 = 1, 2 = 2, and so on'
! !
!MustacheTests methodsFor: 'tests'!
testObject
| template result |
template := MustacheTemplate on: '{{#stream}}Class: {{class}}, Position: {{position}}, Next: {{next}}, Position: {{position}}{{/stream}}' .
result := template
value: { 'stream' -> 'Hello' readStream } asDictionary.
self assert: result = 'Class: ReadStream, Position: 0, Next: H, Position: 1' ! !
!MustacheTests methodsFor: 'tests'!
testObjectNoSection
| template result |
template := MustacheTemplate on: 'Class: {{stream.class}}, Position: {{stream.position}}, Next: {{stream.next}}, Position: {{stream.position}}' .
result := template
value: { 'stream' -> 'Hello' readStream } asDictionary.
self assert: result = 'Class: ReadStream, Position: 0, Next: H, Position: 1' ! !
!MustacheTests methodsFor: 'tests' stamp: 'mnqpr 11/6/2023 21:10:50'!
testObjectNoSectionBreakingDemeterLaw
| template result |
template := MustacheTemplate on: '{{stream.class.name.first.asLowercase}}' .
result := template
value: { 'stream' -> 'Hello' readStream } asDictionary.
self assert: result = 'r' ! !
!MustacheTests methodsFor: 'tests'!
testPartialAsString
| template result |
template := 'This is a test for {{> partial }} .' asMustacheTemplate.
result := template
value: { 'name' -> 'partial template' } asDictionary
partials: { 'partial' -> '{{name}} rendering' } asDictionary.
self assert: result = 'This is a test for partial template rendering .'! !
!MustacheTests methodsFor: 'tests'!
testPartialListContext
| template result |
template := MustacheTemplate on: 'We can have a lists ({{# list}} [ {{> partial }} ] {{/ list}}) .'.
result := template
value: { 'list' -> {
{ 'name' -> 'first list' } asDictionary.
{ 'name' -> 'last list' } asDictionary } } asDictionary
partials: (Dictionary new
at: 'partial' put: (MustacheTemplate on: 'including {{name}} item');
yourself) .
self assert: result = 'We can have a lists ( [ including first list item ] [ including last list item ] ) .' ! !
!MustacheTests methodsFor: 'tests'!
testPartialObjectContext
| template result |
template := MustacheTemplate on: 'This is a test for {{> partial }} .'.
result := template
value: { 'name' -> 'partial template' } asDictionary
partials: (Dictionary new
at: 'partial' put: (MustacheTemplate on: '{{name}} rendering');
yourself) .
self assert: result = 'This is a test for partial template rendering .'! !
!MustacheTests methodsFor: 'tests'!
testSectionWithDotAsTokenListValue
| template result |
template := MustacheTemplate on: '{{# list }}<{{ . }}>{{/ list}}'.
result := template value: {
'list' -> #( a b c )
} asDictionary.
self assert: result = '<a><b><c>' ! !
!MustacheTests methodsFor: 'tests'!
testSectionWithDotAsTokenStringValue
| template result |
template := MustacheTemplate on: '{{# list }}<{{ . }}>{{/ list}}'.
result := template value: {
'list' -> 'abc'
} asDictionary.
self assert: result = '<abc>' ! !
!MustacheTests methodsFor: 'tests'!
testSectionWithEmptyListContext
| template result |
template := MustacheTemplate on: 'list {{# list }}{{ label }} {{/ list}}trailer'.
result := template value: {
'list' -> {
}} asDictionary.
self assert: result = 'list trailer' ! !
!MustacheTests methodsFor: 'tests'!
testSectionWithFalseContext
| template result |
template := MustacheTemplate on: 'list{{# list }} is {{/ list}}displayed'.
result := template value: {
'list' -> false
} asDictionary.
self assert: result = 'listdisplayed' ! !
!MustacheTests methodsFor: 'tests'!
testSectionWithNonEmptyListContext
| template result |
template := MustacheTemplate on: 'list {{# list }}{{ label }} {{/ list}}trailer'.
result := template value: {
'list' -> {
{ 'label' -> 'has 2' } asDictionary.
{ 'label' -> 'elements'} asDictionary
}} asDictionary.
self assert: result = 'list has 2 elements trailer' ! !
!MustacheTests methodsFor: 'tests'!
testSectionWithStringContextUsingDot
| template result |
template := MustacheTemplate on: '{{#list}}before {{.}} after{{/list}}'.
result := template value: {
'list' -> 'list'
} asDictionary.
self assert: result = 'before list after' ! !
!MustacheTests methodsFor: 'tests'!
testSectionWithTrueContext
| template result |
template := MustacheTemplate on: 'list{{# list }} is {{/ list}}displayed'.
result := template value: {
'list' -> true
} asDictionary.
self assert: result = 'list is displayed' ! !
!MustacheTests methodsFor: 'tests'!
testSetDelimiter
| template result |
template := MustacheTemplate on: '* {{default_tags}} {{=<% %>=}} * <%erb_style_tags%> <%={{ }}=%> * {{default_tags_again}} *'.
result := template value: (Array with: 'default_tags' -> '1' with: 'erb_style_tags' -> '2' with: 'default_tags_again' -> '3') asDictionary .
self assert: result = '* 1 * 2 * 3 *'.! !
!MustacheTests methodsFor: 'tests'!
testSingleToken
| template result |
template := MustacheTemplate on: 'This is a test for {{ name }}.'.
result := template value: { 'name' -> 'simpletoken' } asDictionary .
self assert: result = 'This is a test for simpletoken.'
! !
!MustacheTests methodsFor: 'tests'!
testSingleTokenBegin
| template result |
template := MustacheTemplate on: '{{ name }} test it is.'.
result := template value: { 'name' -> 'simpletoken' } asDictionary .
self assert: result = 'simpletoken test it is.'
! !
!MustacheTests methodsFor: 'tests'!
testSingleTokenEnd
| template result |
template := MustacheTemplate on: 'test for {{ name }}'.
result := template value: { 'name' -> 'simpletoken' } asDictionary .
self assert: result = 'test for simpletoken'
! !
!MustacheTests methodsFor: 'tests'!
testSingleTokenNoneExisting
| template result |
template := MustacheTemplate on: 'This is a test for {{ name }}.'.
result := template value: { 'noname' -> 'simpletoken' } asDictionary .
self assert: result = 'This is a test for .'
! !
!MustacheTests methodsFor: 'tests'!
testSingleTokenUndefinedValue
| template result |
template := MustacheTemplate on: 'This is a test for {{ name }}.'.
result := template value: { 'name' -> nil } asDictionary .
self assert: result = 'This is a test for .'
! !
!MustacheTests methodsFor: 'tests' stamp: 'mnqpr 11/14/2023 07:13:54'!
testTemplateFromFile
| template result |
testFile writeStreamDo: [ :stream |
stream nextPutAll: '12{{i}}4' ].
template := testFile asMustacheTemplate.
result := template value:
{'i' -> '3'} asDictionary.
self
assert: result
equals: '1234'.! !
!MustacheTests methodsFor: 'tests' stamp: 'mnqpr 11/14/2023 07:14:45'!
testTemplateFromFileWithBlock
| result |
testFile writeStreamDo: [ :stream |
stream nextPutAll: '12{{i}}4' ].
testFile mustacheTemplateDuring: [ :template |
result := template value:
{'i' -> '3'} asDictionary ].
self
assert: result
equals: '1234'.! !
!MustacheTests methodsFor: 'tests'!
testUnescapedToken
| template result |
template := MustacheTemplate on: 'This is a test for {{& name }}.'.
result := template value: { 'name' -> '&' } asDictionary .
self assert: result = 'This is a test for &.'.
! !
!MustacheTests methodsFor: 'tests'!
testWriteComment
| template |
"right now we just ignore comments and therefor they do not appear when
the template is serialized"
template := MustacheTemplate on: 'before {{!!name}} after'.
self assert: template storeString equals: 'before after'.! !
!MustacheTests methodsFor: 'tests'!
testWriteInvertedSection
| template |
template := MustacheTemplate on: '{{^wrapped}} {{name}} is awesome {{/wrapped}}'.
self assert: template storeString equals: '{{^wrapped}} {{name}} is awesome {{/wrapped}}'.! !
!MustacheTests methodsFor: 'tests'!
testWriteMultipleTokens
| template |
template := MustacheTemplate on: '1 = {{ one }}, 2 = {{ two }}, and so on'.
self assert: template storeString equals: '1 = {{one}}, 2 = {{two}}, and so on' ! !
!MustacheTests methodsFor: 'tests'!
testWritePartial
| template |
"right now we just ignore comments and therefor they do not appear when
the template is serialized"
template := MustacheTemplate on: 'before {{>name}} after'.
self assert: template storeString equals: 'before {{>name}} after'.! !
!MustacheTests methodsFor: 'tests'!
testWriteSection
| template |
template := MustacheTemplate on: '{{#wrapped}} {{name}} is awesome {{/wrapped}}'.
self assert: template storeString equals: '{{#wrapped}} {{name}} is awesome {{/wrapped}}'.! !
!MustacheTests methodsFor: 'tests'!
testWriteUnescapedToken
| template |
template := MustacheTemplate on: 'before {{{name}}} after'.
self assert: template storeString equals: 'before {{{name}}} after'.! !
!MustacheTests methodsFor: 'tests'!
testWriteUnescapedTokenAmpersand
| template |
template := MustacheTemplate on: 'before {{&name}} after'.
"at the moment there is no special handling to remember which unescape
option it was. So the result will be the canonical one"
self assert: template storeString equals: 'before {{{name}}} after'.! !
!MustacheTests methodsFor: 'setUp/tearDown' stamp: 'mnqpr 11/6/2023 17:13:13'!
setUp
testFile := DirectoryEntry smalltalkImageDirectory // 'file'.! !
!MustacheTests methodsFor: 'setUp/tearDown' stamp: 'mnqpr 11/6/2023 17:13:41'!
tearDown
testFile ifExists: [ :aFile | aFile delete].! !
!MustacheParser methodsFor: 'as yet unclassified'!
compileTemplate: aTemplate
self push: aTemplate.
self compile! !
!MustacheParser methodsFor: 'initialize-release' stamp: 'mnqpr 11/6/2023 17:19:23'!
initialize
super initialize.
stack := LinkedList new! !
!MustacheParser methodsFor: 'reading'!
consumeWhiteSpace
[ stream atEnd not and: [ stream peek isSeparator ] ] whileTrue: [
stream next ]! !
!MustacheParser methodsFor: 'reading' stamp: 'mnqpr 11/6/2023 20:05:58'!
endSection
| token |
token := self readToken.
(self stack first value selector = token) ifFalse: [
Error signal: 'end section does not match ', self stack first value selector asString ].
self stack removeFirst! !
!MustacheParser methodsFor: 'reading'!
readChangeDelimiter
self startDelimiter: (String streamContents: [ :s|
[ stream peek isSeparator ] whileFalse: [
s nextPut: stream next ]]).
self consumeWhiteSpace.
self endDelimiter: (stream upToAll: self endDelimiter) allButLast! !
!MustacheParser methodsFor: 'reading'!
readComment
"just read and forget"
self readToken! !
!MustacheParser methodsFor: 'reading'!
readDefaultTag
self readHtmlEscapedToken! !
!MustacheParser methodsFor: 'reading'!
readDefaultUnescapedToken
self readUnescapedToken.
(stream next = $}) ifFalse: [
Error signal: $} asString, ' character expected' ].
! !
!MustacheParser methodsFor: 'reading'!
readExtendedTag
^ (self delimiterExtensions at: stream next) value! !
!MustacheParser methodsFor: 'reading'!
readHtmlEscapedToken
self addPart: (MustacheHtmlEscapedToken token: self readToken)! !
!MustacheParser methodsFor: 'reading'!
readPartial
self addPart: (MustachePartial name: self readToken)! !
!MustacheParser methodsFor: 'reading'!
readStringChunk
(stream upToAll: self startDelimiter) ifNotNil: [:chunk|
self addPart: (MustacheStringChunk string: chunk) ].! !
!MustacheParser methodsFor: 'reading'!
readTag
(self isDelimiterExtension: stream peek)
ifTrue: [ self readExtendedTag ]
ifFalse: [ self readDefaultTag ]! !
!MustacheParser methodsFor: 'reading'!
readToken
^ (stream upToAll: self endDelimiter) trimBoth
! !
!MustacheParser methodsFor: 'reading'!
readUnescapedToken
self addPart: (MustacheToken token: self readToken)! !
!MustacheParser methodsFor: 'reading'!
readUpToEndUsing: aBlock
| nextRead |
nextRead := aBlock.
[ stream atEnd ] whileFalse: [
nextRead := nextRead value ]! !
!MustacheParser methodsFor: 'reading' stamp: 'mnqpr 11/6/2023 20:06:14'!
startSection: aSectionClass
| section |
section := aSectionClass selector: self readToken.
self addPart: section.
self stack addFirst: section asLink! !
!MustacheParser methodsFor: 'accessing' stamp: 'mnqpr 11/6/2023 20:05:15'!
addPart: aMustachePart
self stack first value addPart: aMustachePart.! !
!MustacheParser methodsFor: 'accessing'!
buildDelimiterExtensions
^ Dictionary new
at: $# put: [ self startSection: MustacheSection ];
at: $/ put: [ self endSection ];
at: ${ put: [ self readDefaultUnescapedToken ];
at: $& put: [ self readUnescapedToken ];
at: $!! put: [ self readComment ];
at: $^ put: [ self startSection: MustacheInvertedSection ];
at: $= put: [ self readChangeDelimiter ];
at: $> put: [ self readPartial ];
yourself! !
!MustacheParser methodsFor: 'accessing'!
defaultEndDelimiter
^ '}}'! !
!MustacheParser methodsFor: 'accessing'!
defaultStartDelimiter
^ '{{'! !
!MustacheParser methodsFor: 'accessing'!
delimiterExtensions
^ delimiterExtensions ifNil: [
delimiterExtensions := self buildDelimiterExtensions ]! !
!MustacheParser methodsFor: 'accessing'!
endDelimiter
^ endDelimiter ifNil: [
endDelimiter := self defaultEndDelimiter ]! !
!MustacheParser methodsFor: 'accessing'!
endDelimiter: aString
endDelimiter := aString! !
!MustacheParser methodsFor: 'accessing' stamp: 'mnqpr 11/6/2023 20:06:07'!
push: aTemplate
self stack addFirst: aTemplate asLink ! !
!MustacheParser methodsFor: 'accessing' stamp: 'mnqpr 11/6/2023 20:05:01'!
stack
^ stack. ! !
!MustacheParser methodsFor: 'accessing'!
startDelimiter
^ startDelimiter ifNil: [
startDelimiter := self defaultStartDelimiter ]! !
!MustacheParser methodsFor: 'accessing'!
startDelimiter: aString
startDelimiter := aString! !
!MustacheParser methodsFor: 'accessing'!
stream: aStream
stream := aStream! !
!MustacheParser methodsFor: 'generating'!
compile
| sequenceStart |
sequenceStart := [
self readStringChunk.
[ self readTag. sequenceStart ] ].
self readUpToEndUsing: sequenceStart ! !
!MustacheParser methodsFor: 'testing'!
isDelimiterExtension: aCharacter
^ self delimiterExtensions keys includes: aCharacter! !
!MustacheParser class methodsFor: 'instance creation'!
on: aStringOrStream
^ self new
stream: aStringOrStream readStream ! !
!MustachePart methodsFor: 'writing'!
printString
^ self storeString! !
!MustachePart methodsFor: 'writing'!
storeSize
self shouldBeImplemented ! !
!MustachePart methodsFor: 'writing'!
storeString
^ String
new: self storeSize
streamContents: [ :stream |
self storeOn: stream ]! !
!MustachePart methodsFor: 'resolving'!
lookup: aToken inContext: anObject
^ anObject mustacheLookup: aToken! !
!MustacheCompositePart methodsFor: 'initialize-release'!
initialize
super initialize.
parts := OrderedCollection new.! !
!MustacheCompositePart methodsFor: 'accessing'!
parts
^ parts! !
!MustacheCompositePart methodsFor: 'accessing'!
storeSize
^ parts sum: #storeSize! !
!MustacheCompositePart methodsFor: 'writing'!
storeOn: aWriteStream
parts do: [ :part |
part storeOn: aWriteStream ]! !
!MustacheCompositePart methodsFor: 'adding'!
addPart: aMustachePart
parts add: aMustachePart ! !
!MustacheSection methodsFor: 'writing' stamp: 'mnqpr 11/5/2023 20:14:26'!
storeOn: aWriteStream
aWriteStream nextPutAll: '{{#'; nextPutAll: selector; nextPutAll: '}}'.
super storeOn: aWriteStream.
aWriteStream nextPutAll: '{{/'; nextPutAll: selector; nextPutAll: '}}'! !
!MustacheSection methodsFor: 'visiting'!
accept: aVisitor
aVisitor visitSection: self! !
!MustacheSection methodsFor: 'resolving'!
mustacheDo: aBlock inContext: aContext
aContext mustacheDo: aBlock inverted: self isInverted! !
!MustacheSection methodsFor: 'accessing'!
lookupInContext: anObject
^ self lookup: selector inContext: anObject! !
!MustacheSection methodsFor: 'accessing'!
selector
^ selector! !
!MustacheSection methodsFor: 'accessing'!
selector: aString
selector := aString! !
!MustacheSection methodsFor: 'accessing'!
storeSize
"the size of a section is the size of its content plus two times
the selector size plus 5 extra markup characters {{#}} and {{/}}"
^ super storeSize + (2 * (selector size + 5))! !
!MustacheSection methodsFor: 'accessing'!
valueInContext: anObject
^ self lookupInContext: anObject! !
!MustacheSection methodsFor: 'testing'!
isInverted
^ false! !
!MustacheSection class methodsFor: 'instance creation'!
selector: aString
^ self new
selector: aString! !
!MustacheInvertedSection methodsFor: 'writing' stamp: 'mnqpr 11/5/2023 20:16:25'!
storeOn: aWriteStream
aWriteStream nextPutAll: '{{^'; nextPutAll: selector; nextPutAll: '}}'.
parts do: [ :part |
part storeOn: aWriteStream ].
aWriteStream nextPutAll: '{{/'; nextPutAll: selector; nextPutAll: '}}'! !
!MustacheInvertedSection methodsFor: 'testing'!