summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-02-18 10:45:50 +0100
committerSamy Pessé <samypesse@gmail.com>2016-02-18 10:45:50 +0100
commit1de9d5683b30b4f24c2aba4f796a6d77f0ce7004 (patch)
treed8d3b8a525949f40c741e0ae4e37839d40fb7466
parentb66242db985fd8d6088b52d4b089f0de2ecb8058 (diff)
downloadgitbook-1de9d5683b30b4f24c2aba4f796a6d77f0ce7004.zip
gitbook-1de9d5683b30b4f24c2aba4f796a6d77f0ce7004.tar.gz
gitbook-1de9d5683b30b4f24c2aba4f796a6d77f0ce7004.tar.bz2
Copy assets to output (website)
-rw-r--r--lib/output/website.js45
-rw-r--r--lib/utils/fs.js13
-rw-r--r--package.json5
3 files changed, 47 insertions, 16 deletions
diff --git a/lib/output/website.js b/lib/output/website.js
index f58c68f..b595897 100644
--- a/lib/output/website.js
+++ b/lib/output/website.js
@@ -4,6 +4,7 @@ var util = require('util');
var nunjucks = require('nunjucks');
var Promise = require('../utils/promise');
+var fs = require('../utils/fs');
var conrefsLoader = require('./conrefs');
var Output = require('./base');
@@ -17,7 +18,7 @@ function templatesPath(dir) {
return path.join(dir, '_layouts');
}
-function WebsiteOutput() {
+function _WebsiteOutput() {
Output.apply(this, arguments);
// Nunjucks environment
@@ -29,7 +30,9 @@ function WebsiteOutput() {
// Plugin instance for the default theme
this.defaultTheme;
}
-util.inherits(WebsiteOutput, Output);
+util.inherits(_WebsiteOutput, Output);
+
+var WebsiteOutput = conrefsLoader(_WebsiteOutput);
// Name of the generator
// It's being used as a prefix for templates
@@ -53,12 +56,13 @@ WebsiteOutput.prototype.prepare = function() {
throw new Error('Theme "' + themeName + '" is not installed, add "' + themeID(themeName) + '" to your "book.json"');
}
+ // This list is ordered to give priority to templates in the book
var searchPaths = _.chain([
// The book itself can contains a "_layouts" folder
- '_layouts',
+ that.book.root,
// Installed plugin (it can be identical to themeDefault.root)
- '_layouts',
+ that.theme.root,
// Is default theme still installed
that.themeDefault? that.themeDefault.root : null
@@ -91,6 +95,37 @@ WebsiteOutput.prototype.prepare = function() {
href = path.join('/gitbook', href);
return that.resolveForPage(this.ctx.file.path, href);
});
+ })
+
+ // Copy assets before copyign files from book
+ .then(function() {
+ return Promise.serie([
+ // Assets from the book are already copied
+ // The order is reversed from the template's one
+
+ // Is default theme still installed
+ that.themeDefault && that.themeDefault.root != that.theme.root?
+ that.themeDefault.root : null,
+
+ // Installed plugin (it can be identical to themeDefault.root)
+ that.theme.root
+ ], function(folder) {
+ if (!folder) return;
+
+ // Copy assets only if exists (don't fail otherwise)
+ var assetFolder = path.join(folder, '_assets', that.name);
+ if (!fs.existsSync(assetFolder)) return;
+
+ return fs.copyDir(
+ assetFolder,
+ that.resolve('gitbook'),
+ {
+ deleteFirst: false, // Delete "to" before
+ overwrite: true,
+ confirm: true
+ }
+ );
+ });
});
};
@@ -128,4 +163,4 @@ WebsiteOutput.prototype.templateName = function(name) {
return path.join(this.name, name+'.html');
};
-module.exports = conrefsLoader(WebsiteOutput);
+module.exports = WebsiteOutput;
diff --git a/lib/utils/fs.js b/lib/utils/fs.js
index c2f2bd3..42fd3c6 100644
--- a/lib/utils/fs.js
+++ b/lib/utils/fs.js
@@ -5,6 +5,8 @@ var rmdir = require('rmdir');
var tmp = require('tmp');
var request = require('request');
var path = require('path');
+var cp = require('cp');
+var cpr = require('cpr');
var Promise = require('./promise');
@@ -37,14 +39,6 @@ function writeStream(filename, st) {
return d.promise;
}
-// Copy a file using stream
-function copyFile(from, to) {
- return Promise()
- .then(function() {
- return writeStream(to, fs.createReadStream(from));
- });
-}
-
// Return a promise resolved with a boolean
function fileExists(filename) {
var d = Promise.defer();
@@ -113,7 +107,8 @@ module.exports = {
statSync: fs.statSync,
readdir: Promise.nfbind(fs.readdir),
writeStream: writeStream,
- copy: copyFile,
+ copy: Promise.nfbind(cp),
+ copyDir: Promise.nfbind(cpr),
tmpFile: genTmpFile,
tmpDir: genTmpDir,
download: download,
diff --git a/package.json b/package.json
index 03bc3d1..2ece788 100644
--- a/package.json
+++ b/package.json
@@ -11,7 +11,6 @@
"resolve": "0.6.3",
"mkdirp": "0.5.1",
"error": "7.0.2",
- "fstream-ignore": "1.0.2",
"gitbook-parsers": "0.8.9",
"gitbook-plugin-highlight": "1.0.3",
"gitbook-plugin-sharing": "1.0.1",
@@ -45,7 +44,9 @@
"destroy": "1.0.4",
"ignore": "2.2.19",
"deprecated": "0.0.1",
- "rmdir": "1.2.0"
+ "rmdir": "1.2.0",
+ "cp": "0.2.0",
+ "cpr": "1.0.0"
},
"devDependencies": {
"eslint": "1.5.0",