Skip to content

Instantly share code, notes, and snippets.

View evanxg852000's full-sized avatar
πŸ’­
Awesomeness πŸ‘Œ

Evance Soumaoro evanxg852000

πŸ’­
Awesomeness πŸ‘Œ
View GitHub Profile
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() {
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()}
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
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() !== ''
})
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
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')
}
class Templater {
constructor(dir, cached){}
render(file, context){}
compile(file){}
parse(tokens){}
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>'