mirror of
https://github.com/harttle/liquidjs.git
synced 2026-10-03 09:05:14 -07:00
fix #61: static filename not parsed for subdirectories
This commit is contained in:
+13
-6
@@ -1,14 +1,21 @@
|
|||||||
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 staticFileRE = /\S+/
|
||||||
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 = staticFileRE.exec(token.args)
|
||||||
assert(match, `illegal token ${token.raw}`)
|
if (match) {
|
||||||
|
this.staticValue = match[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
match = lexical.value.exec(token.args)
|
||||||
|
if (match) {
|
||||||
this.value = match[0]
|
this.value = match[0]
|
||||||
|
}
|
||||||
|
|
||||||
match = withRE.exec(token.args)
|
match = withRE.exec(token.args)
|
||||||
if (match) {
|
if (match) {
|
||||||
@@ -16,10 +23,10 @@ module.exports = function (liquid) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
render: function (scope, hash) {
|
render: function (scope, hash) {
|
||||||
var filepath = this.value
|
var filepath = scope.opts.dynamicPartials
|
||||||
if (scope.opts.dynamicPartials) {
|
? Liquid.evalValue(this.value, scope)
|
||||||
filepath = Liquid.evalValue(this.value, scope)
|
: this.staticValue
|
||||||
}
|
assert(filepath, `cannot include with empty filename`)
|
||||||
|
|
||||||
var originBlocks = scope.opts.blocks
|
var originBlocks = scope.opts.blocks
|
||||||
var originBlockMode = scope.opts.blockMode
|
var originBlockMode = scope.opts.blockMode
|
||||||
|
|||||||
+11
-3
@@ -2,6 +2,7 @@ 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')
|
||||||
|
const staticFileRE = /\S+/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* blockMode:
|
* blockMode:
|
||||||
@@ -12,14 +13,21 @@ 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 = staticFileRE.exec(token.args)
|
||||||
assert(match, `illegal token ${token.raw}`)
|
if (match) {
|
||||||
|
this.staticLayout = match[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
match = lexical.value.exec(token.args)
|
||||||
|
if (match) {
|
||||||
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 = scope.opts.dynamicPartials ? Liquid.evalValue(this.layout, scope) : this.layout
|
var layout = scope.opts.dynamicPartials ? Liquid.evalValue(this.layout, scope) : this.staticLayout
|
||||||
|
assert(layout, `cannot apply layout with empty filename`)
|
||||||
|
|
||||||
// render the remaining tokens immediately
|
// render the remaining tokens immediately
|
||||||
scope.opts.blockMode = 'store'
|
scope.opts.blockMode = 'store'
|
||||||
|
|||||||
+33
-11
@@ -24,13 +24,13 @@ describe('tags/include', function () {
|
|||||||
.eventually.equal('barfoobar')
|
.eventually.equal('barfoobar')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should throw when illegal', function () {
|
it('should throw when not exist', function () {
|
||||||
mock({
|
mock({
|
||||||
'/illegal.html': '{%include%}'
|
'/parent.html': '{%include not-exist%}'
|
||||||
})
|
})
|
||||||
return liquid.renderFile('/illegal.html').catch(function (e) {
|
return liquid.renderFile('/parent.html').catch(function (e) {
|
||||||
expect(e.name).to.equal('ParseError')
|
expect(e.name).to.equal('RenderError')
|
||||||
expect(e.message).to.match(/illegal token {%include%}/)
|
expect(e.message).to.match(/cannot include with empty filename/)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -89,13 +89,35 @@ describe('tags/include', function () {
|
|||||||
.eventually.equal('This is a person <p>Joe Shmoe<br/>City: Dallas</p>')
|
.eventually.equal('This is a person <p>Joe Shmoe<br/>City: Dallas</p>')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should support static filename', function () {
|
describe('static partial', function () {
|
||||||
var staticLiquid = new Liquid({dynamicPartials: false, root: '/'})
|
it('should support filename with extention', function () {
|
||||||
mock({
|
mock({
|
||||||
'/with.html': 'X{% include color.html shape: "rect" %}Y',
|
'/parent.html': 'X{% include child.html color:"red" %}Y',
|
||||||
'/color.html': 'shape:{{shape}}'
|
'/child.html': 'child with {{color}}'
|
||||||
|
})
|
||||||
|
var staticLiquid = new Liquid({dynamicPartials: false, root: '/'})
|
||||||
|
return expect(staticLiquid.renderFile('parent.html')).to
|
||||||
|
.eventually.equal('Xchild with redY')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should support parent paths', function () {
|
||||||
|
mock({
|
||||||
|
'/parent.html': 'X{% include bar/./../foo/child.html %}Y',
|
||||||
|
'/foo/child.html': 'child'
|
||||||
|
})
|
||||||
|
var staticLiquid = new Liquid({dynamicPartials: false, root: '/'})
|
||||||
|
return expect(staticLiquid.renderFile('parent.html')).to
|
||||||
|
.eventually.equal('XchildY')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should support subpaths', function () {
|
||||||
|
mock({
|
||||||
|
'/parent.html': 'X{% include foo/child.html %}Y',
|
||||||
|
'/foo/child.html': 'child'
|
||||||
|
})
|
||||||
|
var staticLiquid = new Liquid({dynamicPartials: false, root: '/'})
|
||||||
|
return expect(staticLiquid.renderFile('parent.html')).to
|
||||||
|
.eventually.equal('XchildY')
|
||||||
})
|
})
|
||||||
return expect(staticLiquid.renderFile('with.html')).to
|
|
||||||
.eventually.equal('Xshape:rectY')
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
+24
-1
@@ -96,7 +96,9 @@ describe('tags/layout', function () {
|
|||||||
return expect(liquid.renderFile('/main.html')).to
|
return expect(liquid.renderFile('/main.html')).to
|
||||||
.eventually.equal('blackredA')
|
.eventually.equal('blackredA')
|
||||||
})
|
})
|
||||||
it('should support static filename', function () {
|
|
||||||
|
describe('static partial', function () {
|
||||||
|
it('should support filename with extention', function () {
|
||||||
mock({
|
mock({
|
||||||
'/parent.html': '{{color}}{%block%}{%endblock%}',
|
'/parent.html': '{{color}}{%block%}{%endblock%}',
|
||||||
'/main.html': '{% layout parent.html color:"black"%}{%block%}A{%endblock%}'
|
'/main.html': '{% layout parent.html color:"black"%}{%block%}A{%endblock%}'
|
||||||
@@ -105,4 +107,25 @@ describe('tags/layout', function () {
|
|||||||
return expect(staticLiquid.renderFile('/main.html')).to
|
return expect(staticLiquid.renderFile('/main.html')).to
|
||||||
.eventually.equal('blackA')
|
.eventually.equal('blackA')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should support parent paths', function () {
|
||||||
|
mock({
|
||||||
|
'/foo/parent.html': '{{color}}{%block%}{%endblock%}',
|
||||||
|
'/main.html': '{% layout bar/../foo/parent.html color:"black"%}{%block%}A{%endblock%}'
|
||||||
|
})
|
||||||
|
var staticLiquid = Liquid({ root: '/', dynamicPartials: false })
|
||||||
|
return expect(staticLiquid.renderFile('/main.html')).to
|
||||||
|
.eventually.equal('blackA')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should support subpaths', function () {
|
||||||
|
mock({
|
||||||
|
'/foo/parent.html': '{{color}}{%block%}{%endblock%}',
|
||||||
|
'/main.html': '{% layout foo/parent.html color:"black"%}{%block%}A{%endblock%}'
|
||||||
|
})
|
||||||
|
var staticLiquid = Liquid({ root: '/', dynamicPartials: false })
|
||||||
|
return expect(staticLiquid.renderFile('/main.html')).to
|
||||||
|
.eventually.equal('blackA')
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user