refactor: format

This commit is contained in:
harttle
2017-10-05 08:21:32 +08:00
parent 6f69cd774d
commit b71928ffd4
2 changed files with 99 additions and 99 deletions
+36 -38
View File
@@ -1,42 +1,40 @@
const Liquid = require('..'); const Liquid = require('..')
const lexical = Liquid.lexical; const lexical = Liquid.lexical
const withRE = new RegExp(`with\\s+(${lexical.value.source})`); const withRE = new RegExp(`with\\s+(${lexical.value.source})`)
const assert = require('../src/util/assert.js'); const assert = require('../src/util/assert.js')
module.exports = function(liquid) { module.exports = function (liquid) {
liquid.registerTag('include', {
parse: function (token) {
var match = lexical.value.exec(token.args)
assert(match, `illegal token ${token.raw}`)
this.value = match[0]
liquid.registerTag('include', { match = withRE.exec(token.args)
parse: function(token){ if (match) {
var match = lexical.value.exec(token.args); this.with = match[1]
assert(match, `illegal token ${token.raw}`); }
this.value = match[0]; },
render: function (scope, hash) {
var filepath = Liquid.evalValue(this.value, scope)
match = withRE.exec(token.args); var register = scope.get('liquid')
if(match){ var originBlocks = register.blocks
this.with = match[1]; register.blocks = {}
}
},
render: function(scope, hash) {
var filepath = Liquid.evalValue(this.value, scope);
var register = scope.get('liquid'); if (this.with) {
var originBlocks = register.blocks; hash[filepath] = Liquid.evalValue(this.with, scope)
register.blocks = {}; }
return liquid.getTemplate(filepath, register.root)
if(this.with){ .then((templates) => {
hash[filepath] = Liquid.evalValue(this.with, scope); scope.push(hash)
} return liquid.renderer.renderTemplates(templates, scope)
return liquid.getTemplate(filepath, register.root) })
.then((templates) => { .then((html) => {
scope.push(hash); scope.pop()
return liquid.renderer.renderTemplates(templates, scope); register.blocks = originBlocks
}) return html
.then((html) => { })
scope.pop(); }
register.blocks = originBlocks; })
return html; }
});
}
});
};
+63 -61
View File
@@ -1,66 +1,68 @@
const Liquid = require('..'); const Liquid = require('..')
const Promise = require('any-promise'); const Promise = require('any-promise')
const lexical = Liquid.lexical; const lexical = Liquid.lexical
const assert = require('../src/util/assert.js'); const assert = require('../src/util/assert.js')
module.exports = function(liquid) { module.exports = function (liquid) {
liquid.registerTag('layout', {
parse: function (token, remainTokens) {
var match = lexical.value.exec(token.args)
assert(match, `illegal token ${token.raw}`)
liquid.registerTag('layout', { this.layout = match[0]
parse: function(token, remainTokens){ this.tpls = liquid.parser.parse(remainTokens)
var match = lexical.value.exec(token.args); },
assert(match, `illegal token ${token.raw}`); render: function (scope, hash) {
var layout = Liquid.evalValue(this.layout, scope)
var register = scope.get('liquid')
this.layout = match[0]; // render the remaining tokens immediately
this.tpls = liquid.parser.parse(remainTokens); return liquid.renderer.renderTemplates(this.tpls, scope)
}, // now register.blocks contains rendered blocks
render: function(scope, hash) { .then(() => liquid.getTemplate(layout, register.root))
var layout = Liquid.evalValue(this.layout, scope); .then(templates => {
var register = scope.get('liquid'); // push the hash
scope.push(hash)
// render the parent
return liquid.renderer.renderTemplates(templates, scope)
})
// pop the hash
.then(partial => {
scope.pop()
return partial
})
}
})
// render the remaining tokens immediately liquid.registerTag('block', {
return liquid.renderer.renderTemplates(this.tpls, scope) parse: function (token, remainTokens) {
// now register.blocks contains rendered blocks var match = /\w+/.exec(token.args)
.then(() => liquid.getTemplate(layout, register.root)) this.block = match ? match[0] : 'anonymous'
// push the hash
.then(templates => (scope.push(hash), templates))
// render the parent
.then(templates => liquid.renderer.renderTemplates(templates, scope))
// pop the hash
.then(partial => (scope.pop(), partial));
}
});
liquid.registerTag('block', { this.tpls = []
parse: function(token, remainTokens){ var stream = liquid.parser.parseStream(remainTokens)
var match = /\w+/.exec(token.args); .on('tag:endblock', () => stream.stop())
this.block = match ? match[0] : 'anonymous'; .on('template', tpl => this.tpls.push(tpl))
.on('end', () => {
this.tpls = []; throw new Error(`tag ${token.raw} not closed`)
var stream = liquid.parser.parseStream(remainTokens) })
.on('tag:endblock', () => stream.stop()) stream.start()
.on('template', tpl => this.tpls.push(tpl)) },
.on('end', () => { render: function (scope) {
throw new Error(`tag ${token.raw} not closed`); var register = scope.get('liquid')
}); var html = register.blocks[this.block]
stream.start(); // if not defined yet
}, if (html === undefined) {
render: function(scope){ return liquid.renderer.renderTemplates(this.tpls, scope)
var register = scope.get('liquid'); .then((partial) => {
var html = register.blocks[this.block]; register.blocks[this.block] = partial
// if not defined yet return partial
if (html === undefined) { })
return liquid.renderer.renderTemplates(this.tpls, scope) } else {
.then((partial) => { // if already defined by desendents
register.blocks[this.block] = partial; register.blocks[this.block] = html
return partial; return Promise.resolve(html)
}); }
} }
// if already defined by desendents })
else { }
register.blocks[this.block] = html;
return Promise.resolve(html);
}
}
});
};