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