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
+22 -24
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', { liquid.registerTag('include', {
parse: function (token) { parse: function (token) {
var match = lexical.value.exec(token.args); var match = lexical.value.exec(token.args)
assert(match, `illegal token ${token.raw}`); assert(match, `illegal token ${token.raw}`)
this.value = match[0]; this.value = match[0]
match = withRE.exec(token.args); match = withRE.exec(token.args)
if (match) { if (match) {
this.with = match[1]; this.with = match[1]
} }
}, },
render: function (scope, hash) { render: function (scope, hash) {
var filepath = Liquid.evalValue(this.value, scope); var filepath = Liquid.evalValue(this.value, scope)
var register = scope.get('liquid'); var register = scope.get('liquid')
var originBlocks = register.blocks; var originBlocks = register.blocks
register.blocks = {}; register.blocks = {}
if (this.with) { if (this.with) {
hash[filepath] = Liquid.evalValue(this.with, scope); hash[filepath] = Liquid.evalValue(this.with, scope)
} }
return liquid.getTemplate(filepath, register.root) return liquid.getTemplate(filepath, register.root)
.then((templates) => { .then((templates) => {
scope.push(hash); scope.push(hash)
return liquid.renderer.renderTemplates(templates, scope); return liquid.renderer.renderTemplates(templates, scope)
}) })
.then((html) => { .then((html) => {
scope.pop(); scope.pop()
register.blocks = originBlocks; register.blocks = originBlocks
return html; return html
}); })
}
})
} }
});
};
+35 -33
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', { liquid.registerTag('layout', {
parse: function (token, remainTokens) { parse: function (token, remainTokens) {
var match = lexical.value.exec(token.args); var match = lexical.value.exec(token.args)
assert(match, `illegal token ${token.raw}`); assert(match, `illegal token ${token.raw}`)
this.layout = match[0]; this.layout = match[0]
this.tpls = liquid.parser.parse(remainTokens); this.tpls = liquid.parser.parse(remainTokens)
}, },
render: function (scope, hash) { render: function (scope, hash) {
var layout = Liquid.evalValue(this.layout, scope); var layout = Liquid.evalValue(this.layout, scope)
var register = scope.get('liquid'); var register = scope.get('liquid')
// render the remaining tokens immediately // render the remaining tokens immediately
return liquid.renderer.renderTemplates(this.tpls, scope) return liquid.renderer.renderTemplates(this.tpls, scope)
// now register.blocks contains rendered blocks // now register.blocks contains rendered blocks
.then(() => liquid.getTemplate(layout, register.root)) .then(() => liquid.getTemplate(layout, register.root))
.then(templates => {
// push the hash // push the hash
.then(templates => (scope.push(hash), templates)) scope.push(hash)
// render the parent // render the parent
.then(templates => liquid.renderer.renderTemplates(templates, scope)) return liquid.renderer.renderTemplates(templates, scope)
})
// pop the hash // pop the hash
.then(partial => (scope.pop(), partial)); .then(partial => {
scope.pop()
return partial
})
} }
}); })
liquid.registerTag('block', { liquid.registerTag('block', {
parse: function (token, remainTokens) { parse: function (token, remainTokens) {
var match = /\w+/.exec(token.args); var match = /\w+/.exec(token.args)
this.block = match ? match[0] : 'anonymous'; this.block = match ? match[0] : 'anonymous'
this.tpls = []; this.tpls = []
var stream = liquid.parser.parseStream(remainTokens) var stream = liquid.parser.parseStream(remainTokens)
.on('tag:endblock', () => stream.stop()) .on('tag:endblock', () => stream.stop())
.on('template', tpl => this.tpls.push(tpl)) .on('template', tpl => this.tpls.push(tpl))
.on('end', () => { .on('end', () => {
throw new Error(`tag ${token.raw} not closed`); throw new Error(`tag ${token.raw} not closed`)
}); })
stream.start(); stream.start()
}, },
render: function (scope) { render: function (scope) {
var register = scope.get('liquid'); var register = scope.get('liquid')
var html = register.blocks[this.block]; var html = register.blocks[this.block]
// if not defined yet // if not defined yet
if (html === undefined) { if (html === undefined) {
return liquid.renderer.renderTemplates(this.tpls, scope) return liquid.renderer.renderTemplates(this.tpls, scope)
.then((partial) => { .then((partial) => {
register.blocks[this.block] = partial; register.blocks[this.block] = partial
return partial; return partial
}); })
} } else {
// if already defined by desendents // if already defined by desendents
else { register.blocks[this.block] = html
register.blocks[this.block] = html; return Promise.resolve(html)
return Promise.resolve(html);
} }
} }
}); })
}
};