|
|
@ -3,6 +3,88 @@ exports.printVersion = () => { |
|
|
|
console.log(`Staples ${pjson.version}`) |
|
|
|
} |
|
|
|
|
|
|
|
exports.compile = (template, input) => { |
|
|
|
let index = 0 |
|
|
|
|
|
|
|
const getValue = (tagContent) => { |
|
|
|
console.log('input', tagContent) |
|
|
|
let tagContentSplit = tagContent.split('.') |
|
|
|
let varLevel = null |
|
|
|
tagContentSplit.forEach( (level) => { |
|
|
|
const isIndex = Number.isInteger(parseInt(level)) |
|
|
|
if(!varLevel) varLevel = input |
|
|
|
varLevel = isIndex ? varLevel[parseInt(level)] : varLevel[level] |
|
|
|
}) |
|
|
|
return typeof varLevel === 'string' ? varLevel.trim() : varLevel |
|
|
|
} |
|
|
|
|
|
|
|
// Find all tags ####################################################
|
|
|
|
let tags = [] |
|
|
|
while(index < template.length) { |
|
|
|
const nextOpen = template.indexOf('{{', index+1) |
|
|
|
let tagType = null |
|
|
|
if(template.substring(nextOpen,nextOpen+3) == '{{{') { tagType = 'HTML' |
|
|
|
} else if(template.substring(nextOpen,nextOpen+5) == '{{#if') { tagType = 'IF' |
|
|
|
} else if(template.substring(nextOpen,nextOpen+9) == '{{#unless') { tagType = 'UNLESS' |
|
|
|
//} else if(template.substring(nextOpen,nextOpen+5) == '{{/if') { tagType = 'CLOSE-IF'
|
|
|
|
//} else if(template.substring(nextOpen,nextOpen+9) == '{{/unless') { tagType = 'CLOSE-UNLESS'
|
|
|
|
} else { tagType = 'DEFAULT' } |
|
|
|
|
|
|
|
if(nextOpen == index || nextOpen == -1) { |
|
|
|
index = template.length |
|
|
|
} else { |
|
|
|
index = nextOpen |
|
|
|
let nextClose = index |
|
|
|
|
|
|
|
switch(tagType) { |
|
|
|
case 'DEFAULT': |
|
|
|
nextClose = template.indexOf('}}', index+2) |
|
|
|
index = nextClose+2 |
|
|
|
break |
|
|
|
case 'HTML': |
|
|
|
nextClose = template.indexOf('}}}', index+3) |
|
|
|
index = nextClose+3 |
|
|
|
break |
|
|
|
case 'IF': |
|
|
|
nextClose = template.indexOf('{{/if}}', index+7) |
|
|
|
index = nextClose+7 |
|
|
|
break |
|
|
|
case 'UNLESS': |
|
|
|
nextClose = template.indexOf('{{/unless}}', index+11) |
|
|
|
index = nextClose+11 |
|
|
|
break |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
console.log(index, nextClose) |
|
|
|
if(nextClose == index || nextClose == -1) { |
|
|
|
index = template.length |
|
|
|
} else { |
|
|
|
tags.push({ |
|
|
|
tag: template.substring(nextOpen, index), |
|
|
|
type: tagType |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Replace tags ####################################################
|
|
|
|
tags.forEach( (tag, i) => { |
|
|
|
switch(tag.type) { |
|
|
|
case 'DEFAULT': |
|
|
|
template = template.replace(tag.tag, getValue(tag.tag.substring(2, tag.tag.length-2))) |
|
|
|
break |
|
|
|
case 'HTML': |
|
|
|
template = template.replace(tag.tag, getValue(tag.tag.substring(3, tag.tag.length-3))) |
|
|
|
break |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
return tags |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
exports.compile = (template, input) => { |
|
|
|
let index = 0 |
|
|
|
|
|
|
@ -65,4 +147,4 @@ exports.compile = (template, input) => { |
|
|
|
} |
|
|
|
} |
|
|
|
return template |
|
|
|
} |
|
|
|
}*/ |
|
|
|