Compare commits
4 Commits
master
...
feature/co
Author | SHA1 | Date |
---|---|---|
|
7c6783a2a3 | 3 years ago |
|
777d69e2a0 | 3 years ago |
|
8f35cc913a | 3 years ago |
|
4e76faa35a | 3 years ago |
1 changed files with 129 additions and 12 deletions
@ -1,31 +1,148 @@ |
|||
exports.printVersion = () => { |
|||
console.log('Staples v0.0.1') |
|||
const pjson = require('./package.json') |
|||
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) |
|||
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 |
|||
const nextClose = template.indexOf('}}', index + 1) |
|||
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 |
|||
} |
|||
|
|||
if(nextClose == index || nextClose == -1) { |
|||
index = template.length |
|||
} else { |
|||
index = nextOpen+2 |
|||
const tagContent = template.substring(nextOpen+2, nextClose) |
|||
const tagContentSplit = tagContent.split('.') |
|||
let varLevel = input |
|||
tagContentSplit.forEach( (level) => { |
|||
const isIndex = Number.isInteger(parseInt(level)) |
|||
varLevel = isIndex ? varLevel[parseInt(level)] : varLevel[level] |
|||
tags.push({ |
|||
tag: template.substring(nextOpen, index), |
|||
type: tagType |
|||
}) |
|||
template = template.replace(tagContent, varLevel) |
|||
} |
|||
} |
|||
} |
|||
console.log(template) |
|||
|
|||
// 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 template |
|||
} |
|||
|
|||
|
|||
/* |
|||
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 |
|||
} |
|||
|
|||
while(index < template.length) { |
|||
const nextOpen = template.indexOf('{{', index+1) |
|||
|
|||
if(nextOpen == index || nextOpen == -1) { |
|||
index = template.length |
|||
} else { |
|||
index = nextOpen |
|||
const nextClose = template.indexOf('}}', index+1) |
|||
if(nextClose == index || nextClose == -1) { |
|||
index = template.length |
|||
} else { |
|||
index = nextOpen+2 |
|||
let tagContent = template.substring(nextOpen+2, nextClose) |
|||
|
|||
if(tagContent.substring(0,1) == '#') { |
|||
const isIf = tagContent.substring(0,3) == '#if' |
|||
const isUnless = tagContent.substring(0,7) == '#unless' |
|||
|
|||
console.log('tagContent.substring(0,3)',tagContent.substring(0,3),index) |
|||
if(isIf) { |
|||
const conditionStart = template.indexOf('}}', index+1) |
|||
const conditionStop = template.indexOf('{{/if}}', conditionStart+1) |
|||
const conditionContent = template.substring(conditionStart+2, conditionStop) |
|||
const conditionFull = template.substring(index-2, conditionStop+7) |
|||
const value = getValue(tagContent.substring(4)) ? conditionContent : '' |
|||
template = template.replace(conditionFull, value) |
|||
console.log('conditionFull',conditionFull, `-${value}-`, `-${tagContent.substring(4)}-`, conditionContent) |
|||
index += value.length |
|||
} else if(isUnless) { |
|||
const conditionStart = template.indexOf('}}', index+1) |
|||
const conditionStop = template.indexOf('{{/unless}}', conditionStart+1) |
|||
const conditionContent = template.substring(conditionStart+2, conditionStop) |
|||
const conditionFull = template.substring(index-2, conditionStop+11) |
|||
const value = !getValue(tagContent.substring(8)) ? conditionContent : '' |
|||
template = template.replace(conditionFull, value) |
|||
index += value.length |
|||
} |
|||
console.log('index', index) |
|||
} else { |
|||
const value = getValue(tagContent) |
|||
template = template.replace(`{{${tagContent}}}`, value) |
|||
//index += value.length+1
|
|||
} |
|||
} |
|||
} |
|||
} |
|||
return template |
|||
}*/ |
|||
|
Loading…
Reference in new issue