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.
 

113 lines
3.6 KiB

exports.printVersion = () => {
const pjson = require('./package.json')
console.log(`Staples ${pjson.version}`)
}
exports.compile = (template, input) => {
let index = 0
const getValue = (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) {
let nextOpen = template.indexOf('{{', index)
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 { tagType = 'DEFAULT' }
if(nextOpen == -1) {
index = template.length
} else {
index = nextOpen
let nextClose = index
switch(tagType) {
case 'DEFAULT':
nextClose = template.indexOf('}}', index)
index = nextClose+2
break
case 'HTML':
nextClose = template.indexOf('}}}', index)
index = nextClose+3
break
case 'IF':
nextClose = template.indexOf('{{/if}}', index)
index = nextClose+7
break
case 'UNLESS':
nextClose = template.indexOf('{{/unless}}', index)
index = nextClose+11
break
}
if(nextClose == index || nextClose == -1) {
index = template.length
} else {
tags.push({
tag: template.substring(nextOpen, index),
type: tagType
})
}
}
}
// Replace tags ####################################################
let contentStart = 0
let contentEnd = 0
let content = 0
let value = 0
tags.forEach( (tag, i) => {
switch(tag.type) {
case 'DEFAULT':
const split = tag.tag.split(' ')
if(split.length == 1) {
template = template.replace(tag.tag, getValue(tag.tag.substring(2, tag.tag.length-2)))
} else {
const funct = split[0].substring(2)
const val1 = split.length == 2 ? split[1].substring(0,split[1].length-2) : split[1]
const val2 = split.length == 3 ? split[2].substring(0,split[2].length-2) : null
template = val2 ? template.replace(tag.tag, this.helpers.helpers[funct](getValue(val1), val2)) :
template.replace(tag.tag, this.helpers.helpers[funct](getValue(val1)))
}
break
case 'HTML':
template = template.replace(tag.tag, getValue(tag.tag.substring(3, tag.tag.length-3)))
break
case 'IF':
contentStart = tag.tag.indexOf('}}')+2
contentEnd = tag.tag.indexOf('{{/if}}',contentStart)
content = tag.tag.substring(contentStart, contentEnd)
value = tag.tag.substring(6, contentStart-2)
template = template.replace(tag.tag, getValue(value) ? content : '')
break
case 'UNLESS':
contentStart = tag.tag.indexOf('}}')+2
contentEnd = tag.tag.indexOf('{{/unless}}',contentStart)
content = tag.tag.substring(contentStart, contentEnd)
value = tag.tag.substring(10, contentStart-2)
template = template.replace(tag.tag, getValue(value) ? '' : content)
break
}
})
return template
}
exports.helpers = require('./helpers.js')