From 4e76faa35a51d738ed33bbe816c380610b45dbf5 Mon Sep 17 00:00:00 2001 From: dslak Date: Mon, 26 Sep 2022 18:53:52 +0200 Subject: [PATCH 1/6] add if condition --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 259c15f..66b473a 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ exports.printVersion = () => { - console.log('Staples v0.0.1') + const pjson = require('./package.json') + console.log(`Staples ${pjson.version}`) } exports.compile = (template, input) => { From 8f35cc913ae004b8d92df58d2ce65f74ebd4215e Mon Sep 17 00:00:00 2001 From: dslak Date: Mon, 26 Sep 2022 19:31:29 +0200 Subject: [PATCH 2/6] WIP conditions --- index.js | 58 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 66b473a..de94abb 100644 --- a/index.js +++ b/index.js @@ -6,27 +6,63 @@ exports.printVersion = () => { 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) + const nextOpen = template.indexOf('{{', index+1) + if(nextOpen == index || nextOpen == -1) { index = template.length } else { index = nextOpen - const nextClose = template.indexOf('}}', index + 1) + const nextClose = template.indexOf('}}', index+1) 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] - }) - template = template.replace(tagContent, varLevel) + 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 + } } } } - console.log(template) + return template } From 777d69e2a0c752f01b5abf1b1bf57a70932e7c4a Mon Sep 17 00:00:00 2001 From: dslak Date: Wed, 28 Sep 2022 16:06:54 +0200 Subject: [PATCH 3/6] WIP new logic --- index.js | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index de94abb..426abfa 100644 --- a/index.js +++ b/index.js @@ -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 -} +}*/ From 7c6783a2a3ce3885918ac45fa18b3e6cf3bbbf5f Mon Sep 17 00:00:00 2001 From: dslak Date: Wed, 28 Sep 2022 16:19:10 +0200 Subject: [PATCH 4/6] done new logic --- index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/index.js b/index.js index 426abfa..c464c7a 100644 --- a/index.js +++ b/index.js @@ -55,8 +55,6 @@ exports.compile = (template, input) => { break } - - console.log(index, nextClose) if(nextClose == index || nextClose == -1) { index = template.length } else { @@ -80,7 +78,7 @@ exports.compile = (template, input) => { } }) - return tags + return template } From 6885c4ab3b0f44e00d5587677b87a1d3f2263992 Mon Sep 17 00:00:00 2001 From: dslak Date: Thu, 29 Sep 2022 12:29:18 +0200 Subject: [PATCH 5/6] add custom helpers --- helpers.js | 10 +++++ index.js | 109 ++++++++++++++++++----------------------------------- 2 files changed, 46 insertions(+), 73 deletions(-) create mode 100644 helpers.js diff --git a/helpers.js b/helpers.js new file mode 100644 index 0000000..ef81893 --- /dev/null +++ b/helpers.js @@ -0,0 +1,10 @@ +exports.helpers = [] + +exports.addHelper = (name, fun) => { + this.helpers[name] = fun +} + +this.addHelper('ellipsis', (e) => { + let words = arg1.split(' ').slice(0, 10).join(' ') + return `${words} ...` +}) diff --git a/index.js b/index.js index c464c7a..f8ecd76 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,6 @@ exports.compile = (template, input) => { let index = 0 const getValue = (tagContent) => { - console.log('input', tagContent) let tagContentSplit = tagContent.split('.') let varLevel = null tagContentSplit.forEach( (level) => { @@ -21,16 +20,14 @@ exports.compile = (template, input) => { // Find all tags #################################################### let tags = [] while(index < template.length) { - const nextOpen = template.indexOf('{{', index+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 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) { + if(nextOpen == -1) { index = template.length } else { index = nextOpen @@ -38,19 +35,19 @@ exports.compile = (template, input) => { switch(tagType) { case 'DEFAULT': - nextClose = template.indexOf('}}', index+2) + nextClose = template.indexOf('}}', index) index = nextClose+2 break case 'HTML': - nextClose = template.indexOf('}}}', index+3) + nextClose = template.indexOf('}}}', index) index = nextClose+3 break case 'IF': - nextClose = template.indexOf('{{/if}}', index+7) + nextClose = template.indexOf('{{/if}}', index) index = nextClose+7 break case 'UNLESS': - nextClose = template.indexOf('{{/unless}}', index+11) + nextClose = template.indexOf('{{/unless}}', index) index = nextClose+11 break } @@ -63,86 +60,52 @@ exports.compile = (template, input) => { 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': - template = template.replace(tag.tag, getValue(tag.tag.substring(2, tag.tag.length-2))) + 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 val = split[1].substring(0,split[1].length-2) + template = template.replace(tag.tag, this.helpers.helpers[funct](getValue(val))) + } 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.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 -}*/ +exports.helpers = require('./helpers.js') From 2549f0d0e970136e25981135fe1137476fa6e8f7 Mon Sep 17 00:00:00 2001 From: dslak Date: Thu, 29 Sep 2022 13:08:01 +0200 Subject: [PATCH 6/6] multiple helper params --- helpers.js | 5 ++--- index.js | 6 ++++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/helpers.js b/helpers.js index ef81893..ecb3127 100644 --- a/helpers.js +++ b/helpers.js @@ -4,7 +4,6 @@ exports.addHelper = (name, fun) => { this.helpers[name] = fun } -this.addHelper('ellipsis', (e) => { - let words = arg1.split(' ').slice(0, 10).join(' ') - return `${words} ...` +this.addHelper('ellipsis', (string, words = 10) => { + return `${string.split(' ').slice(0, words).join(' ')}...` }) diff --git a/index.js b/index.js index f8ecd76..7b73db8 100644 --- a/index.js +++ b/index.js @@ -78,8 +78,10 @@ exports.compile = (template, input) => { template = template.replace(tag.tag, getValue(tag.tag.substring(2, tag.tag.length-2))) } else { const funct = split[0].substring(2) - const val = split[1].substring(0,split[1].length-2) - template = template.replace(tag.tag, this.helpers.helpers[funct](getValue(val))) + 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':