diff options
-rw-r--r-- | lib/backbone/page.js | 5 | ||||
-rw-r--r-- | lib/generators/json.js | 14 | ||||
-rw-r--r-- | lib/plugins/manager.js | 51 | ||||
-rw-r--r-- | lib/plugins/plugin.js | 62 | ||||
-rw-r--r-- | lib/templating/index.js | 1 |
5 files changed, 129 insertions, 4 deletions
diff --git a/lib/backbone/page.js b/lib/backbone/page.js index bf3494a..eb3cd61 100644 --- a/lib/backbone/page.js +++ b/lib/backbone/page.js @@ -25,5 +25,10 @@ Page.prototype.read = function() { return this.book.readFile(this.filename); }; +// Parse the page and return its content +Page.prototype.parse = function() { + +}; + module.exports = Page; diff --git a/lib/generators/json.js b/lib/generators/json.js index d2c6954..b76a62e 100644 --- a/lib/generators/json.js +++ b/lib/generators/json.js @@ -8,10 +8,20 @@ util.inherits(JSONGenerator, Generator); // Write a page (parsable file) JSONGenerator.prototype.writePage = function(page) { - var json = {}; + var that = this; + // Parse the page + return page.parse() - return this.output.writeFile(page.withExtension('.json'), JSON.stringify(json, null, 4)); + // Write as json + .then(function() { + var json = {}; + + return this.output.writeFile( + page.withExtension('.json'), + JSON.stringify(json, null, 4) + ); + }); }; diff --git a/lib/plugins/manager.js b/lib/plugins/manager.js index 8617044..b6549d6 100644 --- a/lib/plugins/manager.js +++ b/lib/plugins/manager.js @@ -1,10 +1,61 @@ +var _ = require('lodash'); +var Promise = require('../utils/promise'); var BookPlugin = require('./plugin'); + +/* +PluginsManager is an interface to work with multiple plugins at once: +- Extract assets from plugins +- Call hooks for all plugins, etc +*/ + function PluginsManager(book) { this.book = book; this.plugins = []; } +// Returns a plugin by its name +PluginsManager.prototype.get = function(name) { + return _.find(this.plugins, { + id: name + }); +}; + +// Load a plugin, or a list of plugins +PluginsManager.prototype.load = function(name) { + var that = this; + + if (!_.isArray(name)) { + return Promise.serie(name, function(_name) { + return that.load(_name); + }); + } + + return Promise() + + // Initiate and load the plugin + .then(function() { + var plugin; + + if (!_.isString(name)) plugin = name; + else plugin = new BookPlugin(that.book, name); + + if (that.get(plugin.id)) { + throw new Error('Plugin "'+plugin.id+'" is already loaded'); + } + + + if (plugin.isLoaded()) return plugin; + else return plugin.load() + .thenResolve(plugin); + }) + + .then(function(plugin) { + that.plugins.push(plugin); + }); +}; + + module.exports = PluginsManager; diff --git a/lib/plugins/plugin.js b/lib/plugins/plugin.js index 8397205..e9af9d4 100644 --- a/lib/plugins/plugin.js +++ b/lib/plugins/plugin.js @@ -1,8 +1,68 @@ +var path = require('path'); +var resolve = require('resolve'); -function BookPlugin() { +var Promise = require('../utils/promise'); +var PLUGIN_PREFIX = 'gitbook-plugin-'; +// Return an absolute name for the plugin (the one on NPM) +function npmId(name) { + if (name.indexOf(PLUGIN_PREFIX) === 0) return name; + return [PLUGIN_PREFIX, name].join(''); } +function BookPlugin(book, pluginId) { + this.book = book; + this.id = pluginId; + this.npmId = npmId(pluginId); + + this.packageInfos = undefined; + this.content = undefined; + +} + +// Return true if plugin has been loaded correctly +BookPlugin.prototype.isLoaded = function() { + return Boolean(this.packageInfos && this.content); +}; + +// Load this plugin +BookPlugin.prototype.load = function() { + var that = this; + + if (this.isLoaded()) { + return Promise.reject(new Error('Plugin "' + this.id + '" is already loaded')); + } + + // Try loading plugins from different location + return Promise.some([ + this.book.resolve('node_modules'), + __dirname + ], function(baseDir) { + try { + var res = resolve.sync(name+'/package.json', { basedir: baseDir }); + + that.baseDir = path.dirname(res); + that.packageInfos = require(res); + that.content = require(resolve.sync(name, { basedir: baseDir })); + + return true; + } catch (err) { + if (err.code != 'MODULE_NOT_FOUND') throw(err); + + that.packageInfos = undefined; + that.content = undefined; + + return false; + } + }) + + .then(function() { + if (!that.isLoaded()) { + throw new Error('Couldn\'t locate plugin "' + that.id + '", Run \'gitbook install\' to install plugins from registry.'); + } + }); +}; + module.exports = BookPlugin; diff --git a/lib/templating/index.js b/lib/templating/index.js index 1031bdb..3a0fc16 100644 --- a/lib/templating/index.js +++ b/lib/templating/index.js @@ -4,7 +4,6 @@ function TemplatingEngine(book) { this.book = book; this.log = book.log; - this.nunjucks = new nunjucks.Environment( this.loader, { |