Browse Source

add custom helpers

feature/custom
dslak 3 years ago
parent
commit
6885c4ab3b
  1. 10
      helpers.js
  2. 107
      index.js

10
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} ...`
})

107
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':
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')

Loading…
Cancel
Save