summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2014-04-19 19:31:02 +0200
committerSamy Pessé <samypesse@gmail.com>2014-04-19 19:31:02 +0200
commitfb14caf5b3cfc321910ccb3ff57082dd7a43e51a (patch)
treed4f4a81043721709b35dabee32be78f4a43a23a4 /lib
parentcf51772f541ae09265f37ff0662da529ca857d4f (diff)
downloadgitbook-fb14caf5b3cfc321910ccb3ff57082dd7a43e51a.zip
gitbook-fb14caf5b3cfc321910ccb3ff57082dd7a43e51a.tar.gz
gitbook-fb14caf5b3cfc321910ccb3ff57082dd7a43e51a.tar.bz2
Add resources to site build
Diffstat (limited to 'lib')
-rw-r--r--lib/generate/plugin.js24
-rw-r--r--lib/generate/site/index.js20
2 files changed, 37 insertions, 7 deletions
diff --git a/lib/generate/plugin.js b/lib/generate/plugin.js
index 1271eb3..f753834 100644
--- a/lib/generate/plugin.js
+++ b/lib/generate/plugin.js
@@ -1,10 +1,14 @@
var _ = require("lodash");
var Q = require("q");
var semver = require("semver");
+var path = require("path");
+var crc = require('crc');
var fs = require("./fs");
var pkg = require("../../package.json");
+var RESOURCES = ["js", "css"];
+
var Plugin = function(name) {
this.name = name;
this.packageInfos = {};
@@ -12,6 +16,7 @@ var Plugin = function(name) {
_.each([
name,
+ "gitbook-"+name,
"gitbook-plugin-"+name,
"gitbook-theme-"+name
], function(_name) {
@@ -25,7 +30,6 @@ Plugin.prototype.load = function(name) {
this.packageInfos = require(name+"/package.json");
this.infos = require(name);
this.name = name;
-
return true;
} catch (e) {
return false;
@@ -37,7 +41,14 @@ Plugin.prototype.getResources = function(resource) {
if (!this.infos.book || !this.infos.book[resource]) {
return [];
}
- return this.infos.book[resource];
+ return _.chain(this.infos.book[resource])
+ .map(function(resource) {
+ return {
+ "path": this.resolveFile(resource),
+ "id": crc.hex32(crc.crc32(resource))
+ }
+ }.bind(this))
+ .value();
};
// Test if it's a valid plugin
@@ -51,6 +62,11 @@ Plugin.prototype.isValid = function() {
);
};
+// Resolve file path
+Plugin.prototype.resolveFile = function(filename) {
+ return path.resolve(path.dirname(require.resolve(this.name)), filename);
+};
+
// Extract data from a list of plugin
Plugin.fromList = function(names) {
var failed = [];
@@ -65,9 +81,7 @@ Plugin.fromList = function(names) {
if (_.size(failed) > 0) return Q.reject(new Error("Error loading plugins: "+failed.join(":")));
// Get all resources
- var resources = _.chain([
- "js", "css"
- ])
+ var resources = _.chain(RESOURCES)
.map(function(resource) {
return [
resource,
diff --git a/lib/generate/site/index.js b/lib/generate/site/index.js
index 4bea547..9d738be 100644
--- a/lib/generate/site/index.js
+++ b/lib/generate/site/index.js
@@ -54,7 +54,9 @@ Generator.prototype._writeTemplate = function(tpl, options, output) {
githubHost: that.options.githubHost,
summary: that.options.summary,
- allNavigation: that.options.navigation
+ allNavigation: that.options.navigation,
+
+ plugins: that.plugins
}, options));
})
.then(function(html) {
@@ -134,10 +136,24 @@ Generator.prototype.langsIndex = function(langs) {
Generator.prototype.copyAssets = function() {
var that = this;
+ // Copy gitbook assets
return fs.copy(
path.join(that.options.theme, "assets"),
path.join(that.options.output, "gitbook")
- );
+ )
+ // Copy plugins assets
+ .then(function() {
+ return Q.all(
+ _.map(that.plugins.resources, function(resources, resourceType) {
+ return Q.all(_.map(resources, function(resource) {
+ return fs.copy(
+ resource.path,
+ path.join(that.options.output, "gitbook/plugins/", resource.id+"."+resourceType)
+ );
+ }));
+ })
+ );
+ })
};
// Dump search index to disk