diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-04-20 00:10:46 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-04-20 00:10:46 +0200 |
commit | 74d628676b3d5a49b0f461261ba39c6839557410 (patch) | |
tree | e22d2b7a6fc53235d4218e8ad3d3fb36f20ac09f /lib | |
parent | 6fdd98ec3aa9158b74a5f80e1b35dec1604531d2 (diff) | |
download | gitbook-74d628676b3d5a49b0f461261ba39c6839557410.zip gitbook-74d628676b3d5a49b0f461261ba39c6839557410.tar.gz gitbook-74d628676b3d5a49b0f461261ba39c6839557410.tar.bz2 |
Add option to change templates from plugin
Diffstat (limited to 'lib')
-rw-r--r-- | lib/generate/plugin.js | 70 | ||||
-rw-r--r-- | lib/generate/site/index.js | 23 |
2 files changed, 58 insertions, 35 deletions
diff --git a/lib/generate/plugin.js b/lib/generate/plugin.js index b51d1ff..77bbee5 100644 --- a/lib/generate/plugin.js +++ b/lib/generate/plugin.js @@ -80,6 +80,35 @@ Plugin.prototype.callHook = function(name, args) { }); }; +// Normalize a list of plugin name to use +Plugin.normalizeNames = function(names) { + // Normalize list to an array + names = _.isString(names) ? names.split(":") : (names || []); + + // List plugins to remove + var toremove = _.chain(names) + .filter(function(name) { + return name.length > 0 && name[0] == "-"; + }) + .map(function(name) { + return name.slice(1) + }) + .value(); + + // Merge with defaults + names = _.chain(names) + .concat(Plugin.defaults) + .uniq() + .value(); + + // Remove plugins starting with + names = _.filter(names, function(name) { + return !_.contains(toremove, name) && !(name.length > 0 && name[0] == "-"); + }); + + return names; +}; + // Extract data from a list of plugin Plugin.fromList = function(names) { var failed = []; @@ -118,40 +147,19 @@ Plugin.fromList = function(names) { return plugin.callHook(name, args); }) }, Q()); - } + }, + 'template': function(name) { + var withTpl = _.find(plugins, function(plugin) { + return (plugin.infos.templates + && plugin.infos.templates[name]); + }); + + if (!withTpl) return null; + return withTpl.resolveFile(withTpl.infos.templates[name]); + } }); }; - -// Normalize a list of plugin name to use -Plugin.normalizeNames = function(names) { - // Normalize list to an array - names = _.isString(names) ? names.split(":") : (names || []); - - // List plugins to remove - var toremove = _.chain(names) - .filter(function(name) { - return name.length > 0 && name[0] == "-"; - }) - .map(function(name) { - return name.slice(1) - }) - .value(); - - // Merge with defaults - names = _.chain(names) - .concat(Plugin.defaults) - .uniq() - .value(); - - // Remove plugins starting with - names = _.filter(names, function(name) { - return !_.contains(toremove, name) && !(name.length > 0 && name[0] == "-"); - }); - - return names; -}; - // Default plugins Plugin.defaults = [ "mixpanel" diff --git a/lib/generate/site/index.js b/lib/generate/site/index.js index 9d738be..eaba6ee 100644 --- a/lib/generate/site/index.js +++ b/lib/generate/site/index.js @@ -30,13 +30,28 @@ var Generator = function() { this.revision = Date.now(); this.indexer = indexer(); - - // Load base template - this.template = swig.compileFile(path.resolve(this.options.theme, 'templates/site.html')); - this.langsTemplate = swig.compileFile(path.resolve(this.options.theme, 'templates/langs.html')); }; util.inherits(Generator, BaseGenerator); +// Load all templates +Generator.prototype.loadTemplates = function() { + this.template = swig.compileFile( + this.plugins.template("site") || path.resolve(this.options.theme, 'templates/site.html') + ); + this.langsTemplate = swig.compileFile( + this.plugins.template("langs") || path.resolve(this.options.theme, 'templates/langs.html') + ); +}; + +// Load plugins +Generator.prototype.loadPlugins = function() { + var that = this; + + return BaseGenerator.prototype.loadPlugins.apply(this) + .then(function() { + return that.loadTemplates(); + }); +}; // Generate a template Generator.prototype._writeTemplate = function(tpl, options, output) { |