diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-02-14 22:49:55 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-02-14 22:49:55 +0100 |
commit | fef822145d43d80dbbefa8712eab88ad7044d6c1 (patch) | |
tree | 611af2ef4292e9e9a918c2840db2a65b7efdd587 /lib/template | |
parent | cfefa7d57992738373649dab16cbaf4754c3e5c7 (diff) | |
download | gitbook-fef822145d43d80dbbefa8712eab88ad7044d6c1.zip gitbook-fef822145d43d80dbbefa8712eab88ad7044d6c1.tar.gz gitbook-fef822145d43d80dbbefa8712eab88ad7044d6c1.tar.bz2 |
Move conrefs to a separate mixin
Diffstat (limited to 'lib/template')
-rw-r--r-- | lib/template/index.js | 2 | ||||
-rw-r--r-- | lib/template/loader.js | 65 |
2 files changed, 14 insertions, 53 deletions
diff --git a/lib/template/index.js b/lib/template/index.js index 3f74267..6074f51 100644 --- a/lib/template/index.js +++ b/lib/template/index.js @@ -27,7 +27,7 @@ function TemplateEngine(output) { this.log = this.book.log; // Create file loader - this.loader = new Loader(this); + this.loader = new Loader(output); // Create nunjucks instance this.env = new nunjucks.Environment( diff --git a/lib/template/loader.js b/lib/template/loader.js index 5d30fb2..6b54015 100644 --- a/lib/template/loader.js +++ b/lib/template/loader.js @@ -1,74 +1,35 @@ -var path = require('path'); var nunjucks = require('nunjucks'); +var location = require('../utils/location'); -var Git = require('../utils/git'); -var pathUtil = require('../utils/path'); +/* +Simple nunjucks loader which is passing the reponsability to the Output +*/ -// The loader should handle relative and git url var Loader = nunjucks.Loader.extend({ async: true, - init: function(engine, opts) { - this.engine = engine; - this.book = engine.book; - this.fs = engine.book.fs; - - this.git = new Git(this.fs.tmpdir()); + init: function(output, opts) { + this.output = output; }, getSource: function(sourceURL, callback) { - var that = this; - - this.git.resolve(sourceURL) - .then(function(filepath) { - // Is local file - if (!filepath) { - filepath = that.book.resolve(sourceURL); - } else { - that.book.log.debug.ln('resolve from git', sourceURL, 'to', filepath); - } - - // Read file from absolute path - return that.fs.readAsString(filepath) - .then(function(source) { - return that.engine.interpolate(filepath, source); - }) - .then(function(source) { - return { - src: source, - path: filepath, + this.output.onGetTemplate(sourceURL) + .then(function(out) { + // We disable cache since content is modified (shortcuts, ...) + out.noCache = true; - // We disable cache since content is modified (shortcuts, ...) - noCache: true - }; - }); + return out; }) .nodeify(callback); }, resolve: function(from, to) { - // If origin is in the book, we enforce result file to be in the book - if (this.book.isInBook(from)) { - return this.book.resolve( - this.book.relative(path.dirname(from)), - to - ); - } - - // If origin is in a git repository, we resolve file in the git repository - var gitRoot = this.git.resolveRoot(from); - if (gitRoot) { - return pathUtil.resolveInRoot(gitRoot, to); - } - - // If origin is not in the book (include from a git content ref) - return path.resolve(path.dirname(from), to); + return this.output.onResolveTemplate(from, to); }, // Handle all files as relative, so that nunjucks pass responsability to 'resolve' - // Only git urls are considered as absolute isRelative: function(filename) { - return !Git.isUrl(filename); + return location.isRelative(filename); } }); |