1
|
|
|
import Handlebars from 'handlebars' |
2
|
|
|
|
3
|
|
|
import { |
4
|
|
|
cmsData |
5
|
|
|
,config |
6
|
|
|
,cmsTemplates |
7
|
|
|
} from '../../' |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* THIS: |
11
|
|
|
<span>{{abe type='text' key='text_visible'}}</span> |
12
|
|
|
|
13
|
|
|
* BECOME: |
14
|
|
|
<span data-abe-text_visible="text_visible" >{{abe type='text' key='text_visible'}}</span> |
15
|
|
|
|
16
|
|
|
* @param {[type]} template [description] |
17
|
|
|
*/ |
18
|
|
|
export function addAbeDataAttrForHtmlTag(template) { |
19
|
|
|
var match |
20
|
|
|
while (match = cmsData.regex.abePattern.exec(template)) { |
21
|
|
|
var getattr = cmsData.regex.getAttr(match, 'key').replace(/\./g, '-') |
22
|
|
|
template = template.replace( |
23
|
|
|
cmsData.regex.escapeTextToRegex(match[0], 'g'), |
24
|
|
|
' data-abe-' + cmsData.regex.validDataAbe(getattr) + '="' + getattr + '" ' + match[0] |
25
|
|
|
) |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return template |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* |
33
|
|
|
* THIS: |
34
|
|
|
<img src="{{abe type='image' key='image_key' tab='default'}}" alt=""> |
35
|
|
|
|
36
|
|
|
* BECOME: |
37
|
|
|
<img data-abe-attr-image_key="src" data-abe-image_key="image_key" data-abe-attr-image_key="src" |
38
|
|
|
data-abe-image_key="image_key" src="{{abe type='image' key='image_key' tab='default' has-abe=1 has-abe=1}}" alt=""> |
39
|
|
|
|
40
|
|
|
* @param {[type]} template [description] |
41
|
|
|
*/ |
42
|
|
|
export function addAbeDataAttrForHtmlAttributes(template) { |
43
|
|
|
var match |
44
|
|
|
while (match = cmsData.regex.abeAsAttributePattern.exec(template)) { // While regexp match {{attribut}}, ex: link, image ... |
45
|
|
|
if(cmsData.regex.isSingleAbe(match[0], template)){ |
46
|
|
|
var more_attr = '' |
47
|
|
|
var getattr = cmsData.regex.getAttr(match, 'key').replace(/\./g, '-') |
48
|
|
|
template = template.replace( |
49
|
|
|
new RegExp(match[0]), |
50
|
|
|
' data-abe-attr-' + cmsData.regex.validDataAbe(getattr) + '="' + (match[0].split('=')[0]).trim() + '"' + |
51
|
|
|
' data-abe-' + cmsData.regex.validDataAbe(getattr) + '="' + getattr + '"' + |
52
|
|
|
more_attr + match[0].replace('}}', ' has-abe=1}}') |
53
|
|
|
) |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return template |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Example: |
62
|
|
|
* |
63
|
|
|
* |
64
|
|
|
* THIS: |
65
|
|
|
{{abe type='data' key='data_key' source='select title from article' display='title' editable='true' tab='default'}} |
66
|
|
|
|
67
|
|
|
{{#each data_key}} |
68
|
|
|
{{title}} |
69
|
|
|
{{/each}} |
70
|
|
|
|
71
|
|
|
* |
72
|
|
|
* BECOME THIS |
73
|
|
|
|
74
|
|
|
{{abe type='data' key='data_key' source='select title from article' display='title' editable='true' tab='default'}} |
75
|
|
|
|
76
|
|
|
{{#each data_key}} |
77
|
|
|
{{title}} |
78
|
|
|
{{/each}}<!-- [[data_key]] %7B%7B%23each%20data_key%7D%7D%0A%09%7B%7Btitle%7D%7D%0A%7B%7B/each%7D%7D --> |
79
|
|
|
|
80
|
|
|
* @param {[type]} template [description] |
81
|
|
|
* @param {[type]} json [description] |
82
|
|
|
*/ |
83
|
|
|
export function addAbeSourceComment(template, json) { |
84
|
|
|
|
85
|
|
|
// Don't know what it does... |
86
|
|
|
if(typeof json.abe_source !== 'undefined' && json.abe_source !== null) { |
87
|
|
|
var keys = Object.keys(json.abe_source) |
88
|
|
|
|
89
|
|
|
for(var i in keys) { |
|
|
|
|
90
|
|
|
var replaceEach = new RegExp(`<!-- \\[\\[${keys[i]}\\]\\][\\s\\S]*?-->`, 'g') |
91
|
|
|
template = template.replace(replaceEach, '') |
92
|
|
|
|
93
|
|
|
var patAttrSource = new RegExp(' ([A-Za-z0-9\-\_]+)=["|\'].*?({{' + keys[i] + '}}).*?["|\']', 'g') |
94
|
|
|
var patAttrSourceMatch = template.match(patAttrSource) |
95
|
|
|
|
96
|
|
|
if(patAttrSourceMatch != null) { |
97
|
|
|
let checkEscapedRegex = /["|'](.*?)["|']/ |
98
|
|
|
let patAttrSourceInside = new RegExp('(\\S+)=["\']?((?:.(?!["\']?\\s+(?:\\S+)=|[>"\']))+.)["\']?({{' + keys[i] + '}}).*?["|\']', 'g') |
99
|
|
|
Array.prototype.forEach.call(patAttrSourceMatch, (pat) => { |
100
|
|
|
let patAttrSourceCheck = patAttrSourceInside.exec(pat) |
101
|
|
|
if(patAttrSourceCheck != null) { |
102
|
|
|
|
103
|
|
|
let checkEscaped = checkEscapedRegex.exec(patAttrSourceCheck[0]) |
104
|
|
|
if(checkEscaped != null && checkEscaped.length > 0) { |
105
|
|
|
checkEscaped = escape(checkEscaped[1]) |
106
|
|
|
template = template.replace( |
|
|
|
|
107
|
|
|
patAttrSourceCheck[0], |
108
|
|
|
` data-abe-attr="${patAttrSourceCheck[1]}" data-abe-attr-escaped="${checkEscaped}" data-abe="${keys[i]}" ${patAttrSourceCheck[0]}` |
|
|
|
|
109
|
|
|
) |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
}) |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
var eachSource = new RegExp(`({{#each ${keys[i]}}[\\s\\S a-z]*?{{\/each}})`, 'g') |
116
|
|
|
var matches = template.match(eachSource) |
117
|
|
|
if(typeof matches !== 'undefined' && matches !== null) { |
118
|
|
|
Array.prototype.forEach.call(matches, (match) => { |
119
|
|
|
template = template.replace(match, `${match}<!-- [[${keys[i]}]] ${cmsTemplates.encodeAbeTagAsComment(match)} -->`) |
|
|
|
|
120
|
|
|
}) |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return template |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* THIS: |
130
|
|
|
<span>{{abe type='text' key='text_visible'}}</span> |
131
|
|
|
|
132
|
|
|
* BECOME: |
133
|
|
|
<span><abe>{{abe type='text' key='text_visible'}}</abe></span> |
134
|
|
|
|
135
|
|
|
* @param {[type]} template [description] |
136
|
|
|
*/ |
137
|
|
|
export function addAbeHtmlTagBetweenAbeTags(template) { |
138
|
|
|
var match |
139
|
|
|
while (match = cmsData.regex.abePattern.exec(template)) { |
140
|
|
|
template = template.replace(cmsData.regex.escapeTextToRegex(match[1], 'g'), '<abe>' + match[1].trim() + '</abe>') |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return template |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* THIS: |
148
|
|
|
[index]. |
149
|
|
|
|
150
|
|
|
* BECOME: |
151
|
|
|
{{@index}}- |
152
|
|
|
|
153
|
|
|
* @param {[type]} template [description] |
154
|
|
|
* @return {[type]} [description] |
155
|
|
|
*/ |
156
|
|
|
export function replaceAbeEachIndex(template) { |
157
|
|
|
return template.replace(/\[index\]\./g, '{{@index}}-') |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
export function removeHiddenAbeTag(template) { |
161
|
|
|
return template.replace(/(\{\{abe.*visible=[\'|\"]false.*\}\})/g, '') |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Remove {{abe type=*}} from html if attribute visible="false" |
166
|
|
|
* @param {[type]} template [description] |
167
|
|
|
* @return {[type]} [description] |
168
|
|
|
*/ |
169
|
|
|
export function removeHandlebarsRawFromHtml(template) { |
170
|
|
|
return template.replace(/\{\{\{\{\/?raw\}\}\}\}/g, '') |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* split {{#each}}...{{/each}} into an array |
175
|
|
|
* |
176
|
|
|
* @param {[type]} template [description] |
177
|
|
|
* @return {[type]} [description] |
178
|
|
|
*/ |
179
|
|
|
export function splitEachBlocks(template) { |
180
|
|
|
var block |
181
|
|
|
var blocks = [] |
182
|
|
|
|
183
|
|
|
while (block = cmsData.regex.blockPattern.exec(template)) { |
184
|
|
|
blocks.push(block[1]) |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return blocks |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
export function indexEachBlocks(template, onlyHtml) { |
191
|
|
|
// create an array of {{each}} blocks |
192
|
|
|
var blocks = cmsTemplates.prepare.splitEachBlocks(template) |
193
|
|
|
|
194
|
|
|
Array.prototype.forEach.call(blocks, (block) => { |
195
|
|
|
var key = block.match(/#each (.*)\}\}/) |
196
|
|
|
key = key[1] |
197
|
|
|
var match |
198
|
|
|
|
199
|
|
|
if(!onlyHtml) { |
200
|
|
|
|
201
|
|
|
var voidData = {} |
202
|
|
|
voidData[key] = [{}] |
203
|
|
|
var blockCompiled = Handlebars.compile(block.replace(/{{abe (.*?)}}/g, '[[abe $1]]').replace(new RegExp(`\\.\\.\/${config.meta.name}`, 'g'), config.meta.name)) |
204
|
|
|
var blockHtml = blockCompiled(voidData, {data: {intl: config.intlData}}).replace(/\[\[abe (.*?)\]\]/g, '{{abe $1}}') |
205
|
|
|
|
206
|
|
|
// je rajoute un data-abe-block avec index sur tous les tags html du bloc each |
207
|
|
|
var textEachWithIndex = block.replace(/(<(?![\/])[A-Za-z0-9!-]*)/g, '$1 data-abe-block="' + key + '{{@index}}"') |
208
|
|
|
|
209
|
|
|
// je remplace le block dans le texte par ça |
210
|
|
|
template = template.replace(block, textEachWithIndex + `<!-- [[${key}]] ${cmsTemplates.encodeAbeTagAsComment(blockHtml)} -->`) |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
// Pour chaque tag Abe, je mets en forme ce tag avec des data- supplémentaires |
214
|
|
|
while (match = cmsData.regex.abePattern.exec(block)) { |
215
|
|
|
template = cmsTemplates.prepare.insertAbeEach(template, match, key, cmsData.regex.eachBlockPattern.lastIndex - block.length, onlyHtml) |
|
|
|
|
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
// Pour chaque tag Abe attribut de HTML, je mets en forme ce tag avec des data- supplémentaires sur le tag html parent |
219
|
|
|
while (match = cmsData.regex.abeAsAttributePattern.exec(block)) { |
220
|
|
|
template = cmsTemplates.prepare.insertAbeEach(template, match, key, cmsData.regex.eachBlockPattern.lastIndex - block.length, onlyHtml) |
221
|
|
|
} |
222
|
|
|
}) |
223
|
|
|
|
224
|
|
|
return template |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
export function insertAbeEach(template, theMatch, key, lastIndex, onlyHtml) { |
228
|
|
|
var matchBlock = theMatch[0] |
229
|
|
|
if(cmsData.regex.isEachStatement(matchBlock)) return |
|
|
|
|
230
|
|
|
if(cmsData.regex.isBlockAbe(matchBlock)){ |
231
|
|
|
var matchblockattr = (matchBlock.split('=')[0]).trim() |
232
|
|
|
var getattr = cmsData.regex.getAttr(matchBlock, 'key').replace('.', '[index].') |
233
|
|
|
var newMatchBlock = ((!onlyHtml) ? |
234
|
|
|
(/=[\"\']\{\{(.*?)\}\}/g.test(matchBlock) ? |
235
|
|
|
' data-abe-attr-' + cmsData.regex.validDataAbe(getattr) + '="' + matchblockattr + '"' : |
236
|
|
|
'') + |
237
|
|
|
' data-abe-' + cmsData.regex.validDataAbe(getattr) + '="' + getattr + '" ' + matchBlock : |
238
|
|
|
matchBlock) |
239
|
|
|
.replace(new RegExp('(key=[\'|"])' + key + '.', 'g'), '$1' + key + '[index].') |
240
|
|
|
.replace(/\{\{abe/, '{{abe dictionnary=\'' + key + '\'') |
241
|
|
|
|
242
|
|
|
template = template.replace(matchBlock, newMatchBlock) |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return template |
246
|
|
|
} |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: