summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/output/base.js76
-rw-r--r--lib/output/folder.js78
-rw-r--r--lib/output/json.js15
-rw-r--r--lib/output/website/index.js8
-rw-r--r--lib/page/index.js20
5 files changed, 114 insertions, 83 deletions
diff --git a/lib/output/base.js b/lib/output/base.js
index 8bc27d4..39e46c2 100644
--- a/lib/output/base.js
+++ b/lib/output/base.js
@@ -1,13 +1,19 @@
var _ = require('lodash');
var Ignore = require('ignore');
-var path = require('path');
var Promise = require('../utils/promise');
-var pathUtil = require('../utils/path');
-var fs = require('../utils/fs');
var PluginsManager = require('../plugins');
+/*
+Output is like a stream interface for a parsed book
+to output "something".
+
+The process is mostly on the behavior of "onPage" and "onAsset"
+*/
+
function Output(book, type) {
+ _.bindAll(this);
+
this.book = book;
this.log = this.book.log;
@@ -26,47 +32,6 @@ function Output(book, type) {
]));
}
-// Return path to the root folder
-Output.prototype.root = function(filename) {
- return path.resolve(process.cwd(), this.book.config.get('output'));
-};
-
-// Resolve a file in the output directory
-Output.prototype.resolve = function(filename) {
- return pathUtil.resolveInRoot.apply(null, [this.root()].concat(_.toArray(arguments)));
-};
-
-// Write a file/buffer to the output folder
-Output.prototype.writeFile = function(filename, buf) {
- var that = this;
-
- return Promise()
- .then(function() {
- filename = that.resolve(filename);
- var folder = path.dirname(filename);
-
- // Ensure fodler exists
- return fs.mkdirp(folder);
- })
-
- // Write the file
- .then(function() {
- return fs.writeFile(filename, buf);
- });
-};
-
-// Copy a file to the output
-Output.prototype.copyFile = function(from, to) {
- var that = this;
-
- return Promise()
- .then(function() {
- to = that.resolve(to);
-
- return fs.copy(from, to);
- });
-};
-
// Start the generation, for a parsed book
Output.prototype.generate = function() {
var that = this;
@@ -83,11 +48,6 @@ Output.prototype.generate = function() {
return that.plugins.load(plugins);
})
- // Create the output folder
- .then(function() {
- return fs.mkdirp(that.root());
- })
-
// Initialize the generation
.then(function() {
return that.prepare();
@@ -104,9 +64,9 @@ Output.prototype.generate = function() {
// Process file as page or asset
if (that.book.hasPage(filename)) {
- return that.writePage(that.book.getPage(filename));
+ return that.onPage(that.book.getPage(filename));
} else {
- return that.copyAsset(filename);
+ return that.onAsset(filename);
}
});
})
@@ -122,14 +82,22 @@ Output.prototype.prepare = function() {
};
+// Write a page (parsable file), ex: markdown, etc
+Output.prototype.onPage = function(page) {
+
+};
+
// Copy an asset file (non-parsable), ex: images, etc
-Output.prototype.copyAsset = function(filename) {
+Output.prototype.onAsset = function(filename) {
};
-// Write a page (parsable file), ex: markdown, etc
-Output.prototype.writePage = function(page) {
+// Resolve an HTML link
+Output.prototype.onRelativeLink = function(currentPage, href) {
+ var to = this.book.getPage(href);
+ if (to) return to.outputPath();
+ return href;
};
// Finish the generation
diff --git a/lib/output/folder.js b/lib/output/folder.js
new file mode 100644
index 0000000..96ea443
--- /dev/null
+++ b/lib/output/folder.js
@@ -0,0 +1,78 @@
+var _ = require('lodash');
+var util = require('util');
+var path = require('path');
+
+var Output = require('./base');
+var fs = require('../utils/fs');
+var pathUtil = require('../utils/path');
+var Promise = require('../utils/promise');
+
+/*
+This output require the native fs module to output
+book as a directory (mapping assets and pages)
+*/
+
+function FolderOutput() {
+ Output.apply(this, arguments);
+}
+util.inherits(FolderOutput, Output);
+
+// Copy an asset file (non-parsable), ex: images, etc
+FolderOutput.prototype.onAsset = function(filename) {
+ return this.copyFile(
+ this.book.resolve(filename),
+ filename
+ );
+};
+
+// Prepare the generation by creating the output folder
+FolderOutput.prototype.prepare = function() {
+ return fs.mkdirp(this.root());
+};
+
+
+// ----- Utility methods -----
+
+// Return path to the root folder
+FolderOutput.prototype.root = function(filename) {
+ return path.resolve(process.cwd(), this.book.config.get('output'));
+};
+
+// Resolve a file in the output directory
+FolderOutput.prototype.resolve = function(filename) {
+ return pathUtil.resolveInRoot.apply(null, [this.root()].concat(_.toArray(arguments)));
+};
+
+// Copy a file to the output
+FolderOutput.prototype.copyFile = function(from, to) {
+ var that = this;
+
+ return Promise()
+ .then(function() {
+ to = that.resolve(to);
+
+ return fs.copy(from, to);
+ });
+};
+
+// Write a file/buffer to the output folder
+FolderOutput.prototype.writeFile = function(filename, buf) {
+ var that = this;
+
+ return Promise()
+ .then(function() {
+ filename = that.resolve(filename);
+ var folder = path.dirname(filename);
+
+ // Ensure fodler exists
+ return fs.mkdirp(folder);
+ })
+
+ // Write the file
+ .then(function() {
+ return fs.writeFile(filename, buf);
+ });
+};
+
+
+module.exports = FolderOutput;
diff --git a/lib/output/json.js b/lib/output/json.js
index b03df6e..8cf2373 100644
--- a/lib/output/json.js
+++ b/lib/output/json.js
@@ -1,18 +1,23 @@
var util = require('util');
-var Output = require('./base');
+var FolderOutput = require('./folder');
var gitbook = require('../gitbook');
function JSONOutput() {
- Output.apply(this, arguments);
+ FolderOutput.apply(this, arguments);
}
-util.inherits(JSONOutput, Output);
+util.inherits(JSONOutput, FolderOutput);
+
+// Don't copy asset on JSON output
+JSONOutput.prototype.onAsset = function(filename) {
+
+};
// Write a page (parsable file)
-JSONOutput.prototype.writePage = function(page) {
+JSONOutput.prototype.onPage = function(page) {
var that = this;
// Parse the page
- return page.parse()
+ return page.parse(this)
// Write as json
.then(function() {
diff --git a/lib/output/website/index.js b/lib/output/website/index.js
index 2b7db73..35e81c2 100644
--- a/lib/output/website/index.js
+++ b/lib/output/website/index.js
@@ -6,14 +6,6 @@ function WebsiteOutput() {
}
util.inherits(WebsiteOutput, Output);
-// Copy an asset file
-WebsiteOutput.prototype.writeAsset = function(filename) {
- return this.copyFile(
- this.book.resolve(filename),
- filename
- );
-};
-
// Write a page (parsable file)
WebsiteOutput.prototype.writePage = function(page) {
diff --git a/lib/page/index.js b/lib/page/index.js
index 7074780..8d1bfed 100644
--- a/lib/page/index.js
+++ b/lib/page/index.js
@@ -80,14 +80,9 @@ Page.prototype.read = function() {
};
// Parse the page and return its content
-Page.prototype.parse = function(opts) {
+Page.prototype.parse = function(output) {
var that = this;
- opts = _.defaults(opts || {}, {
- linkPages: true
- });
-
-
this.log.debug.ln('start parsing file', this.path);
return this.read()
@@ -122,17 +117,10 @@ Page.prototype.parse = function(opts) {
// Normalize HTML output
.then(function() {
- var pipelineOpts = _.extend({
+ var pipelineOpts = {
// Replace links to page of summary
- onRelativeLink: function(href) {
- if (!opts.linkPages) return href;
-
- var to = that.book.getPage(href);
- if (to) return to.outputPath();
-
- return href;
- }
- }, opts);
+ onRelativeLink: _.partial(output.onRelativeLink, that)
+ };
var pipeline = new HTMLPipeline(that.content, pipelineOpts);
return pipeline.output()