summaryrefslogtreecommitdiffstats
path: root/lib/generate
diff options
context:
space:
mode:
Diffstat (limited to 'lib/generate')
-rw-r--r--lib/generate/generator.js4
-rw-r--r--lib/generate/index.js32
-rw-r--r--lib/generate/plugin.js16
-rw-r--r--lib/generate/site/index.js33
4 files changed, 71 insertions, 14 deletions
diff --git a/lib/generate/generator.js b/lib/generate/generator.js
index 144af23..ecd646c 100644
--- a/lib/generate/generator.js
+++ b/lib/generate/generator.js
@@ -12,8 +12,8 @@ var BaseGenerator = function(options) {
this.plugins = [];
};
-BaseGenerator.prototype.callHook = function(name) {
- return this.plugins.hook(name, this);
+BaseGenerator.prototype.callHook = function(name, data) {
+ return this.plugins.hook(name, this, data);
};
BaseGenerator.prototype.loadPlugins = function() {
diff --git a/lib/generate/index.js b/lib/generate/index.js
index 0c3e693..898e71f 100644
--- a/lib/generate/index.js
+++ b/lib/generate/index.js
@@ -31,6 +31,9 @@ var generate = function(options) {
input: null,
output: null,
+ // Config file (relative to input)
+ configFile: "book.json",
+
// Output generator
generator: "site",
@@ -63,6 +66,35 @@ var generate = function(options) {
// Clean output folder
return fs.remove(options.output)
+ // Read config file
+ .then(function() {
+ return fs.readFile(path.resolve(options.input, options.configFile))
+ .then(function(_config) {
+ // Extend current config
+ _config = JSON.parse(_config);
+ _config = _.omit(_config, 'input', 'configFile');
+
+ _.extend(options, _config);
+ }, function() {
+ // No config file: not a big deal
+ return Q();
+ })
+ })
+
+ // Get repo's URL
+ .then(function() {
+ return parse.git.url(options.input)
+ .then(function(_url) {
+ // Get ID of repo
+ return parse.git.githubID(_url);
+ }, function(err) {
+ return null;
+ })
+ .then(function(repoId) {
+ options.github = options.github || repoId;
+ });
+ })
+
.then(function() {
return fs.mkdirp(options.output);
})
diff --git a/lib/generate/plugin.js b/lib/generate/plugin.js
index d92b918..6b30073 100644
--- a/lib/generate/plugin.js
+++ b/lib/generate/plugin.js
@@ -74,15 +74,15 @@ Plugin.prototype.resolveFile = function(filename) {
};
// Resolve file path
-Plugin.prototype.callHook = function(name, args) {
+Plugin.prototype.callHook = function(name, context, data) {
var hookFunc = this.infos.hooks? this.infos.hooks[name] : null;
- args = _.isArray(args) ? args : [args];
+ data = data || {};
- if (!hookFunc) return Q();
+ if (!hookFunc) return Q(data);
return Q()
.then(function() {
- return hookFunc.apply(null, args);
+ return hookFunc.apply(context, [data]);
});
};
@@ -159,12 +159,12 @@ Plugin.fromList = function(names, root) {
return Q({
'list': plugins,
'resources': resources,
- 'hook': function(name, args) {
+ 'hook': function(name, context, data) {
return _.reduce(plugins, function(prev, plugin) {
- return prev.then(function() {
- return plugin.callHook(name, args);
+ return prev.then(function(ret) {
+ return plugin.callHook(name, context, ret);
})
- }, Q());
+ }, Q(data));
},
'template': function(name) {
var withTpl = _.find(plugins, function(plugin) {
diff --git a/lib/generate/site/index.js b/lib/generate/site/index.js
index f8986df..87be7b4 100644
--- a/lib/generate/site/index.js
+++ b/lib/generate/site/index.js
@@ -59,8 +59,11 @@ Generator.prototype.loadPlugins = function() {
};
// Generate a template
-Generator.prototype._writeTemplate = function(tpl, options, output) {
+Generator.prototype._writeTemplate = function(tpl, options, output, interpolate) {
var that = this;
+
+ interpolate = interpolate || _.identity;
+
return Q()
.then(function(sections) {
return tpl(_.extend({
@@ -80,6 +83,7 @@ Generator.prototype._writeTemplate = function(tpl, options, output) {
pluginsConfig: JSON.stringify(that.options.pluginsConfig)
}, options));
})
+ .then(interpolate)
.then(function(html) {
return fs.writeFile(
output,
@@ -107,6 +111,15 @@ Generator.prototype.convertFile = function(content, _input) {
return Q()
.then(function() {
+ // Send content to plugins
+ return that.callHook("page:before", {
+ path: _input,
+ content: content
+ });
+ })
+ .then(function(_content) {
+ content = _content.content;
+
// Lex page
return parse.lex(content);
})
@@ -124,17 +137,29 @@ Generator.prototype.convertFile = function(content, _input) {
});
})
.then(function(sections) {
+ // Use plugin hook
+ return that.callHook("page", {
+ path: _input,
+ sections: sections
+ })
+ })
+ .then(function(_page) {
that.manifest.add("CACHE", _output);
return that._writeTemplate(that.template, {
- progress: progress,
+ progress: _page.progress,
_input: _input,
- content: sections,
+ content: _page.sections,
basePath: basePath,
staticBase: path.join(basePath, "gitbook"),
- }, output);
+ }, output, function(html) {
+ return that.callHook("page:after", {
+ path: _input,
+ content: html
+ }).get("content")
+ });
});
};