use native path

This commit is contained in:
harttle
2016-11-06 17:20:03 +08:00
parent a82e50e1ab
commit fd2df81f9c
6 changed files with 16 additions and 40 deletions
+1 -1
View File
@@ -63,7 +63,7 @@ engine.renderFile("hello", {name: 'alice'})
* `root` is a directory or an array of directories to resolve layouts and includes, as well as the filename passed in when calling `.renderFile()`. * `root` is a directory or an array of directories to resolve layouts and includes, as well as the filename passed in when calling `.renderFile()`.
If an array, the files are looked up in the order they occur in the array. If an array, the files are looked up in the order they occur in the array.
Defaults to `["."]` Defaults to `process.cwd()`
* `extname` is used to lookup the template file when filepath doesn't include an extension name. Defaults to `.liquid` * `extname` is used to lookup the template file when filepath doesn't include an extension name. Defaults to `.liquid`
+3 -3
View File
@@ -4,7 +4,7 @@ const assert = require('./src/util/assert.js');
const tokenizer = require('./src/tokenizer.js'); const tokenizer = require('./src/tokenizer.js');
const statFileAsync = require('./src/util/fs.js').statFileAsync; const statFileAsync = require('./src/util/fs.js').statFileAsync;
const readFileAsync = require('./src/util/fs.js').readFileAsync; const readFileAsync = require('./src/util/fs.js').readFileAsync;
const pathResolve = require('./src/util/fs.js').pathResolve; const path = require('path');
const Render = require('./src/render.js'); const Render = require('./src/render.js');
const lexical = require('./src/lexical.js'); const lexical = require('./src/lexical.js');
const Tag = require('./src/tag.js'); const Tag = require('./src/tag.js');
@@ -73,7 +73,7 @@ var _engine = {
}, },
lookup: function(filepath, root) { lookup: function(filepath, root) {
root = this.options.root.concat(root || []); root = this.options.root.concat(root || []);
var paths = root.map(root => pathResolve(root, filepath)); var paths = root.map(root => path.resolve(root, filepath));
return anySeries(paths, path => statFileAsync(path).then(() => path)) return anySeries(paths, path => statFileAsync(path).then(() => path))
.catch((e) => { .catch((e) => {
if (e.code === 'ENOENT') { if (e.code === 'ENOENT') {
@@ -83,7 +83,7 @@ var _engine = {
}); });
}, },
getTemplate: function(filepath, root) { getTemplate: function(filepath, root) {
if (!filepath.match(/\.\w+$/)) { if(!path.extname(filepath)){
filepath += this.options.extname; filepath += this.options.extname;
} }
return this return this
+1
View File
@@ -8,6 +8,7 @@ default:
dist: dist:
[ -d dist/ ] || mkdir dist/ [ -d dist/ ] || mkdir dist/
$(BROWSERIFY) index.js -s Liquid \ $(BROWSERIFY) index.js -s Liquid \
--ignore path \
-t [ babelify --global true --presets [ es2015 ] ] \ -t [ babelify --global true --presets [ es2015 ] ] \
> dist/liquid.js > dist/liquid.js
$(MINIFY) dist/liquid.js --compress warnings=false --mangle --output dist/liquid.min.js $(MINIFY) dist/liquid.js --compress warnings=false --mangle --output dist/liquid.min.js
-14
View File
@@ -14,21 +14,7 @@ function statFileAsync(path) {
}); });
}; };
function pathResolve(root, path) {
if (path[0] == '/') return path;
var arr = root.split('/').concat(path.split('/'));
var result = [];
arr.forEach(function(slug) {
if (slug == '..') result.pop();
else if (!slug || slug == '.');
else result.push(slug);
});
return '/' + result.join('/');
}
module.exports = { module.exports = {
readFileAsync, readFileAsync,
pathResolve,
statFileAsync statFileAsync
}; };
+11
View File
@@ -96,6 +96,17 @@ describe('liquid', function() {
return expect(engine.renderFile('files/foo.html')) return expect(engine.renderFile('files/foo.html'))
.to.eventually.equal('foo'); .to.eventually.equal('foo');
}); });
it('should default root to cwd', function(){
var files = {};
files[process.cwd() + '/foo.html'] = 'FOO';
mock(files);
engine = Liquid({
extname: '.html'
});
return expect(engine.renderFile('foo.html'))
.to.eventually.equal('FOO');
});
it('should render file with context', function() { it('should render file with context', function() {
return engine.renderFile('/root/files/name.html', ctx).should.eventually.equal('My name is harttle.'); return engine.renderFile('/root/files/name.html', ctx).should.eventually.equal('My name is harttle.');
}); });
-22
View File
@@ -1,22 +0,0 @@
const chai = require("chai");
const expect = chai.expect;
const fs = require('../../src/util/fs.js');
const pathResolve = fs.pathResolve;
describe('fs', function() {
describe('.pathResolve(root, path)', function() {
it('should accept root with no trailing slash', function() {
expect(pathResolve('/root', 'files/foo.html')).to.equal('/root/files/foo.html');
});
it('should accept root with trailing slash', function() {
expect(pathResolve('/root/', 'files/foo.html')).to.equal('/root/files/foo.html');
});
it('should accept dot path', function() {
expect(pathResolve('/root', './foo.html')).to.equal('/root/foo.html');
});
it('should accept double-dot path', function() {
expect(pathResolve('/root', 'files/../foo.html')).to.equal('/root/foo.html');
});
});
});