diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-02-17 11:25:57 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-02-17 11:25:57 +0100 |
commit | f54685e2168bd54b06664b55ffb14334fef4cf80 (patch) | |
tree | 456c6d694813beee7e6eeee4c6293fff596e0955 /lib/output/conrefs.js | |
parent | 8141bcb3b63f16c27f8cd6c5e19aed4b5ef6d019 (diff) | |
download | gitbook-f54685e2168bd54b06664b55ffb14334fef4cf80.zip gitbook-f54685e2168bd54b06664b55ffb14334fef4cf80.tar.gz gitbook-f54685e2168bd54b06664b55ffb14334fef4cf80.tar.bz2 |
Change format of conrefs and folder mixins
Diffstat (limited to 'lib/output/conrefs.js')
-rw-r--r-- | lib/output/conrefs.js | 98 |
1 files changed, 53 insertions, 45 deletions
diff --git a/lib/output/conrefs.js b/lib/output/conrefs.js index c91885e..f1cd123 100644 --- a/lib/output/conrefs.js +++ b/lib/output/conrefs.js @@ -1,60 +1,68 @@ var path = require('path'); +var util = require('util'); -var Output = require('./base'); +var FolderOutput = require('./folder')(); var Git = require('../utils/git'); var fs = require('../utils/fs'); var pathUtil = require('../utils/path'); /* -Middleware for output to resolve git conrefs +Mixin for output to resolve git conrefs */ -var ConrefsLoader = Output.createMixin(function() { - this.git = new Git(); -}); - -// Read a template by its source URL -ConrefsLoader.prototype.onGetTemplate = function(sourceURL) { - var that = this; - - return 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); - } +module.exports = function conrefsLoader(Base) { + Base = Base || FolderOutput; - // Read file from absolute path - return fs.readFile(filepath) - .then(function(source) { - return { - src: source.toString('utf8'), - path: filepath - }; - }); - }); -}; + function ConrefsLoader() { + Base.apply(this, arguments); -// Generate a source URL for a template -ConrefsLoader.prototype.onResolveTemplate = 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 - ); + this.git = new Git(); } + util.inherits(ConrefsLoader, Base); - // 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); - } + // Read a template by its source URL + ConrefsLoader.prototype.onGetTemplate = function(sourceURL) { + var that = this; - // If origin is not in the book (include from a git content ref) - return path.resolve(path.dirname(from), to); -}; + return 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 fs.readFile(filepath) + .then(function(source) { + return { + src: source.toString('utf8'), + path: filepath + }; + }); + }); + }; -module.exports = ConrefsLoader; + // Generate a source URL for a template + ConrefsLoader.prototype.onResolveTemplate = 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 ConrefsLoader; +}; |