You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.7 KiB

3 years ago
exports.printVersion = () => {
3 years ago
const pjson = require('./package.json')
console.log(`Staples ${pjson.version}`)
3 years ago
}
exports.compile = (template, input) => {
let index = 0
3 years ago
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
}
3 years ago
while(index < template.length) {
3 years ago
const nextOpen = template.indexOf('{{', index+1)
3 years ago
if(nextOpen == index || nextOpen == -1) {
index = template.length
} else {
index = nextOpen
3 years ago
const nextClose = template.indexOf('}}', index+1)
3 years ago
if(nextClose == index || nextClose == -1) {
index = template.length
} else {
index = nextOpen+2
3 years ago
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
}
3 years ago
}
}
}
3 years ago
return template
3 years ago
}