diff --git a/helpers.js b/helpers.js new file mode 100644 index 0000000..ecb3127 --- /dev/null +++ b/helpers.js @@ -0,0 +1,9 @@ +exports.helpers = [] + +exports.addHelper = (name, fun) => { + this.helpers[name] = fun +} + +this.addHelper('ellipsis', (string, words = 10) => { + return `${string.split(' ').slice(0, words).join(' ')}...` +}) diff --git a/index.js b/index.js index 259c15f..7b73db8 100644 --- a/index.js +++ b/index.js @@ -1,31 +1,113 @@ 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) => { + 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) - if(nextOpen == index || nextOpen == -1) { + 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 - const nextClose = template.indexOf('}}', index + 1) + 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 { - 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 #################################################### + 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')