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/output | |
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/output')
-rw-r--r-- | lib/output/base.js | 12 | ||||
-rw-r--r-- | lib/output/conrefs.js | 47 | ||||
-rw-r--r-- | lib/output/json.js | 2 |
3 files changed, 56 insertions, 5 deletions
diff --git a/lib/output/base.js b/lib/output/base.js index 8142761..fede3da 100644 --- a/lib/output/base.js +++ b/lib/output/base.js @@ -1,5 +1,6 @@ var _ = require('lodash'); var Ignore = require('ignore'); +var path = require('path'); var Promise = require('../utils/promise'); var PluginsManager = require('../plugins'); @@ -107,7 +108,7 @@ Output.prototype.prepare = function() { // Write a page (parsable file), ex: markdown, etc Output.prototype.onPage = function(page) { - return page.parse(this); + return page.toHTML(this); }; // Copy an asset file (non-parsable), ex: images, etc @@ -134,9 +135,14 @@ Output.prototype.onOutputImage = function(page, imgFile) { return page.relative(imgFile); }; -// By default don;t resolve conrefs -Output.prototype.onResolveTemplate = function(from, to) { +// Read a template by its source URL +Output.prototype.onGetTemplate = function(sourceUrl) { + throw new Error('template not found '+sourceUrl); +}; +// Generate a source URL for a template +Output.prototype.onResolveTemplate = function(from, to) { + return path.resolve(path.dirname(from), to); }; // Finish the generation diff --git a/lib/output/conrefs.js b/lib/output/conrefs.js index 2883c7b..fb2d5bb 100644 --- a/lib/output/conrefs.js +++ b/lib/output/conrefs.js @@ -1,7 +1,9 @@ var util = require('util'); +var path = require('path'); var Output = require('./base'); var Git = require('../utils/git'); +var fs = require('../utils/fs'); var pathUtil = require('../utils/path'); /* @@ -14,9 +16,52 @@ function ConrefsLoader() { } util.inherits(ConrefsLoader, Output); -// Resolve an include in the template engine +// 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); + } + + // Read file from absolute path + return fs.readFile(filepath) + .then(function(source) { + return source.toString('utf8'); + //return that.engine.interpolate(filepath, source); + }) + .then(function(source) { + return { + src: source, + path: filepath + }; + }); + }); +}; + +// 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); }; module.exports = ConrefsLoader; diff --git a/lib/output/json.js b/lib/output/json.js index 8cf2373..4ad5231 100644 --- a/lib/output/json.js +++ b/lib/output/json.js @@ -17,7 +17,7 @@ JSONOutput.prototype.onPage = function(page) { var that = this; // Parse the page - return page.parse(this) + return page.toHTML(this) // Write as json .then(function() { |