This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn balance<T: Into<String>>(input: T) -> bool { | |
let mut stk = Vec::new(); | |
for c in input.into().chars(){ | |
match c { | |
a @ '(' | a @ '{' |a @ '[' => { | |
stk.push(a); | |
}, | |
a @ ')' |a @ '}' |a @ ']' => { | |
match stk.pop() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Templater { | |
... | |
generate(ast){ | |
return `module.exports = function (context) { | |
context = context || {} | |
for(let varname of Object.keys(context)){ | |
this[varname]= context[varname] | |
} | |
let __njsOutput = '' | |
${ast.generate()} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Templater { | |
... | |
_parse(ast, stops){ | |
while(this._parser.pos < this._parser.tokens.length){ | |
let token = this._parser.tokens[this._parser.pos] | |
let expr = token.replace(/[%{}]/g, '').trim().split(/\s(.+)/) | |
if(stops && Array.isArray(expr) && stops.includes(expr[0])) { | |
this._parser.pos += 1 | |
return expr[0] //stop parsing nested block |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
compile(file){ | |
let ast = this.parse(this._tokenize(this._readTmplSource(file))) | |
return ast | |
} | |
_tokenize(templateTxt){ | |
const token_regex = /({{.*?}}|{%.*?%})/ | |
return templateTxt.split(token_regex).filter( match => { | |
return match.trim() !== '' | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Templater { | |
... | |
render(file, context){ | |
let sourceFile = path.join(this._dir, file) | |
let compiledFile = this._compiledFile(file) | |
if(!fs.existsSync(sourceFile)){ | |
throw Error('Template not found!') | |
} | |
let render = null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Templater { | |
... | |
constructor(dir, cached){ | |
this._dir = path.resolve(dir) | |
this._cached = cached || false | |
this._compiledDir = path.join(this._dir, '.tmp') | |
// create compiled directory if not exist | |
if(!fs.existsSync(this._dir)){ | |
throw Error('Template directory does not exist') | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Templater { | |
constructor(dir, cached){} | |
render(file, context){} | |
compile(file){} | |
parse(tokens){} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function (context) { | |
//extract context as local variables | |
for(let varname of Object.keys(context)){ | |
this[varname]= context[varname] | |
} | |
let __njsOutput = '' | |
__njsOutput += '<p>The page content ... </p>' | |