1 module handlebars.components.scope_;
2
3 import handlebars.components.base;
4
5 import std.string;
6
7 /// Component that will handle the if blocks
8 class ScopeComponentCt(Token[] tokens, Properties properties) : HbsComponent!"" {
9
10 ///
11 this(string propertyName, string localName, string index, string indexName) {
12 }
13
14 string render(T, Components...)(T controller) {
15 return "";
16 }
17 }
18
19 /// Component that will handle the if blocks
20 class ScopeComponent : HbsComponent!"" {
21
22 ///
23 enum ComponentName = "scope";
24
25 private {
26 string propertyName;
27 string localName;
28 string index;
29 string indexName;
30 }
31
32 ///
33 this(string propertyName, string localName, string index, string indexName) {
34 this.propertyName = propertyName;
35 this.localName = localName;
36 this.index = index;
37 this.indexName = indexName;
38 }
39
40 ///
41 string render(Component, Components...)() {
42 Token[] localContent;
43
44 foreach(item; this.content) {
45 Token token = copy(item);
46
47 if(token.value == localName) {
48 token.value = propertyName ~ "[" ~ index ~ "]";
49 }
50
51 if(token.value.indexOf(localName ~ ".") == 0) {
52 auto pieces = token.value.split(".");
53 token.value = propertyName ~ "[" ~ index ~ "]." ~ pieces[1..$].join(".");
54 }
55
56 if(token.value == indexName) {
57 token.value = index;
58 token.type = Token.Type.plain;
59 }
60
61 foreach(ref property; token.properties.list) {
62 if(property.isEvaluated) {
63 continue;
64 }
65
66 if(property.value == localName) {
67 property.value = propertyName ~ "[" ~ index ~ "]";
68 }
69
70 if(property.value == indexName) {
71 property.value = index;
72 property.isEvaluated = true;
73 }
74 }
75
76 foreach(ref property; token.properties.hash) {
77 if(property.isEvaluated) {
78 continue;
79 }
80
81 if(property.value == localName) {
82 property.value = propertyName ~ "[" ~ index ~ "]";
83 }
84
85 if(property.value == indexName) {
86 property.value = index;
87 property.isEvaluated = true;
88 }
89 }
90
91 localContent ~= token;
92 }
93
94 return lifecycle.yield(localContent);
95 }
96 }
97
98 ///
99 Token copy(Token item) {
100 Token token;
101
102 token.value = item.value;
103 token.type = item.type;
104
105 token.properties.localName = item.properties.localName;
106 token.properties.indexName = item.properties.indexName;
107 token.properties.name = item.properties.name;
108
109 foreach(property; item.properties.list) {
110 token.properties.list ~= Properties.Property(property.value, property.isEvaluated);
111 }
112
113 foreach(key, property; item.properties.hash) {
114 token.properties.hash[key] = Properties.Property(property.value, property.isEvaluated);
115 }
116
117 return token;
118 }