diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/generate/generator.js | 4 | ||||
-rw-r--r-- | lib/generate/index.js | 32 | ||||
-rw-r--r-- | lib/generate/plugin.js | 16 | ||||
-rw-r--r-- | lib/generate/site/index.js | 33 | ||||
-rw-r--r-- | lib/parse/git.js | 56 | ||||
-rw-r--r-- | lib/parse/index.js | 3 |
6 files changed, 129 insertions, 15 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") + }); }); }; diff --git a/lib/parse/git.js b/lib/parse/git.js new file mode 100644 index 0000000..4478f20 --- /dev/null +++ b/lib/parse/git.js @@ -0,0 +1,56 @@ +var Q = require('q'); +var _ = require('lodash'); +var cp = require('child_process'); +var url = require('url'); + +// Get the remote of a given repo +function gitURL(path) { + var d = Q.defer(); + + cp.exec("git config --get remote.origin.url", { + cwd: path, + env: process.env, + }, function(err, stdout, stderr) { + if(err) { + return d.reject(err); + } + + return d.resolve(stdout); + }); + + return d.promise + .then(function(output) { + return output.replace(/(\r\n|\n|\r)/gm, ""); + }); +} + +// Poorman's parsing +// Parse a git URL to a github ID : username/reponame +function githubID(_url) { + // Remove .git if it's in _url + var sliceEnd = _url.slice(-4) === '.git' ? -4 : _url.length; + + // Detect HTTPS repos + var parsed = url.parse(_url); + if(parsed.protocol === 'https:' && parsed.host === 'github.com') { + return parsed.path.slice(1, sliceEnd); + } + + // Detect SSH repos + if(_url.indexOf('git@') === 0) { + return _url.split(':', 2)[1].slice(0, sliceEnd); + } + + // None found + return null; +} + +function titleCase(str) { + return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); +} + +module.exports = { + url: gitURL, + githubID: githubID, + titleCase: titleCase +}; diff --git a/lib/parse/index.js b/lib/parse/index.js index 0ebb03a..bb7779f 100644 --- a/lib/parse/index.js +++ b/lib/parse/index.js @@ -5,5 +5,6 @@ module.exports = { lex: require('./lex'), progress: require('./progress'), navigation: require('./navigation'), - readme: require('./readme') + readme: require('./readme'), + git: require('./git') }; |