diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-02-13 15:28:07 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-02-13 15:28:07 +0100 |
commit | ee3faaad1106bb773078e580c9c1611c8f31607c (patch) | |
tree | 5a6298dd565290e1776caec97f5ee9f42c65dd51 /lib | |
parent | d2aad34935cb243dcdaeabfc28dce150c68f6340 (diff) | |
download | gitbook-ee3faaad1106bb773078e580c9c1611c8f31607c.zip gitbook-ee3faaad1106bb773078e580c9c1611c8f31607c.tar.gz gitbook-ee3faaad1106bb773078e580c9c1611c8f31607c.tar.bz2 |
Complete assets inliner
Diffstat (limited to 'lib')
-rw-r--r-- | lib/output/assets-inliner.js | 4 | ||||
-rw-r--r-- | lib/output/base.js | 26 | ||||
-rw-r--r-- | lib/output/folder.js | 17 | ||||
-rw-r--r-- | lib/utils/fs.js | 9 |
4 files changed, 44 insertions, 12 deletions
diff --git a/lib/output/assets-inliner.js b/lib/output/assets-inliner.js index fb348b4..9733505 100644 --- a/lib/output/assets-inliner.js +++ b/lib/output/assets-inliner.js @@ -65,7 +65,7 @@ AssetsInliner.prototype.onOutputImage = function(page, src) { } // Convert SVG to PNG - return that.convertSVGFile(page.resolve(src)); + return that.convertSVGFile(that.resolveForPage(page, src)); }) // Return relative path from the page @@ -88,7 +88,7 @@ AssetsInliner.prototype.downloadAsset = function(src) { that.downloaded[src] = filename; that.log.debug.ln('downloading asset', src); - return fs.download(src, filename) + return fs.download(src, that.resolve(filename)) .thenResolve(filename); }); }; diff --git a/lib/output/base.js b/lib/output/base.js index 688ecb7..9acfb87 100644 --- a/lib/output/base.js +++ b/lib/output/base.js @@ -57,17 +57,27 @@ Output.prototype.generate = function() { .then(function() { return that.book.fs.listAllFiles(that.book.root); }) + + // We want to process assets first, then pages + // Since pages can have logic based on existance of assets .then(function(files) { - return Promise.serie(files, function(filename) { - // Ignore file present in a language book - if (isMultilingual && that.book.isInLanguageBook(filename)) return; + // Ignore file present in a language book + files = _.filter(files, function(filename) { + return !(isMultilingual && that.book.isInLanguageBook(filename)); + }); + + // List assets + var byTypes = _.groupBy(files, function(filename) { + return (that.book.hasPage(filename)? 'page' : 'asset'); + }); - // Process file as page or asset - if (that.book.hasPage(filename)) { + return Promise.serie(byTypes.asset, function(filename) { + return that.onAsset(filename); + }) + .then(function() { + return Promise.serie(byTypes.page, function(filename) { return that.onPage(that.book.getPage(filename)); - } else { - return that.onAsset(filename); - } + }); }); }) diff --git a/lib/output/folder.js b/lib/output/folder.js index 8b899f6..90a3351 100644 --- a/lib/output/folder.js +++ b/lib/output/folder.js @@ -43,6 +43,13 @@ FolderOutput.prototype.resolve = function(filename) { return pathUtil.resolveInRoot.apply(null, [this.root()].concat(_.toArray(arguments))); }; +// Resolve a file path from a page (in the output folder) +FolderOutput.prototype.resolveForPage = function(page, filename) { + var abs = page.resolveLocal(filename); + return this.resolve(abs); +}; + + // Copy a file to the output FolderOutput.prototype.copyFile = function(from, to) { var that = this; @@ -88,12 +95,20 @@ FolderOutput.prototype.hasFile = function(filename) { // Create a new unique file // Returns its filename FolderOutput.prototype.createNewFile = function(base, filename) { + var that = this; + if (!filename) { filename = path.basename(filename); base = path.dirname(base); } - return fs.uniqueFilename(this.resolve(base), filename); + return fs.uniqueFilename(this.resolve(base), filename) + .then(function(out) { + out = path.join(base, out); + + return fs.ensure(that.resolve(out)) + .thenResolve(out); + }); }; diff --git a/lib/utils/fs.js b/lib/utils/fs.js index 3f1c11f..c0ff0c9 100644 --- a/lib/utils/fs.js +++ b/lib/utils/fs.js @@ -83,6 +83,12 @@ function uniqueFilename(base, filename) { return Promise(path.relative(base, _filename)); } +// Create all required folder to create a file +function ensureFile(filename) { + var base = path.dirname(filename); + return Promise.nfcall(mkdirp, base); +} + module.exports = { exists: fileExists, existsSync: fs.existsSync, @@ -96,5 +102,6 @@ module.exports = { copy: copyFile, tmpFile: genTmpFile, download: download, - uniqueFilename: uniqueFilename + uniqueFilename: uniqueFilename, + ensure: ensureFile }; |