diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-02-17 11:25:57 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-02-17 11:25:57 +0100 |
commit | f54685e2168bd54b06664b55ffb14334fef4cf80 (patch) | |
tree | 456c6d694813beee7e6eeee4c6293fff596e0955 /lib/output | |
parent | 8141bcb3b63f16c27f8cd6c5e19aed4b5ef6d019 (diff) | |
download | gitbook-f54685e2168bd54b06664b55ffb14334fef4cf80.zip gitbook-f54685e2168bd54b06664b55ffb14334fef4cf80.tar.gz gitbook-f54685e2168bd54b06664b55ffb14334fef4cf80.tar.bz2 |
Change format of conrefs and folder mixins
Diffstat (limited to 'lib/output')
-rw-r--r-- | lib/output/assets-inliner.js | 181 | ||||
-rw-r--r-- | lib/output/conrefs.js | 98 | ||||
-rw-r--r-- | lib/output/folder.js | 158 | ||||
-rw-r--r-- | lib/output/json.js | 12 | ||||
-rw-r--r-- | lib/output/website/index.js | 11 |
5 files changed, 230 insertions, 230 deletions
diff --git a/lib/output/assets-inliner.js b/lib/output/assets-inliner.js index 9733505..b5d076e 100644 --- a/lib/output/assets-inliner.js +++ b/lib/output/assets-inliner.js @@ -2,7 +2,7 @@ var util = require('util'); var path = require('path'); var crc = require('crc'); -var FolderOutput = require('./folder'); +var FolderOutput = require('./folder')(); var Promise = require('../utils/promise'); var fs = require('../utils/fs'); var imagesUtil = require('../utils/images'); @@ -11,123 +11,126 @@ var location = require('../utils/location'); var DEFAULT_ASSETS_FOLDER = 'assets'; /* -Utility mixin to inline all the assets in a book: +Mixin to inline all the assets in a book: - Outline <svg> tags - Download remote images - Convert .svg images as png */ -function AssetsInliner() { - FolderOutput.apply(this, arguments); +module.exports = function assetsInliner(Base) { + Base = Base || FolderOutput; - // Map of svg already converted - this.svgs = {}; - this.inlineSvgs = {}; + function AssetsInliner() { + Base.apply(this, arguments); - // Map of images already downloaded - this.downloaded = {}; -} -util.inherits(AssetsInliner, FolderOutput); + // Map of svg already converted + this.svgs = {}; + this.inlineSvgs = {}; -// Output a SVG buffer as a file -AssetsInliner.prototype.onOutputSVG = function(page, svg) { - this.log.debug.ln('output svg from', page.path); + // Map of images already downloaded + this.downloaded = {}; + } + util.inherits(AssetsInliner, Base); - // Convert svg buffer to a png file - return this.convertSVGBuffer(svg) + // Output a SVG buffer as a file + AssetsInliner.prototype.onOutputSVG = function(page, svg) { + this.log.debug.ln('output svg from', page.path); - // Return relative path from the page - .then(function(filename) { - return page.relative('/' + filename); - }); -}; + // Convert svg buffer to a png file + return this.convertSVGBuffer(svg) + // Return relative path from the page + .then(function(filename) { + return page.relative('/' + filename); + }); + }; -// Output an image as a file -AssetsInliner.prototype.onOutputImage = function(page, src) { - var that = this; - return Promise() + // Output an image as a file + AssetsInliner.prototype.onOutputImage = function(page, src) { + var that = this; - // Download file if external - .then(function() { - if (!location.isExternal(src)) return; + return Promise() - return that.downloadAsset(src) - .then(function(_asset) { - src = '/' + _asset; - }); + // Download file if external + .then(function() { + if (!location.isExternal(src)) return; - }) - .then(function() { - if (path.extname(src).toLowerCase() != '.svg') { - return src; - } + return that.downloadAsset(src) + .then(function(_asset) { + src = '/' + _asset; + }); - // Convert SVG to PNG - return that.convertSVGFile(that.resolveForPage(page, src)); - }) + }) + .then(function() { + if (path.extname(src).toLowerCase() != '.svg') { + return src; + } - // Return relative path from the page - .then(function(filename) { - return page.relative('/' + filename); - }); -}; + // Convert SVG to PNG + return that.convertSVGFile(that.resolveForPage(page, src)); + }) -// Download an asset if not already download; returns the output file -AssetsInliner.prototype.downloadAsset = function(src) { - if (this.downloaded[src]) return Promise(this.downloaded[src]); + // Return relative path from the page + .then(function(filename) { + return page.relative('/' + filename); + }); + }; - var that = this; - var ext = path.extname(src); - var hash = crc.crc32(src).toString(16); + // Download an asset if not already download; returns the output file + AssetsInliner.prototype.downloadAsset = function(src) { + if (this.downloaded[src]) return Promise(this.downloaded[src]); - // Create new file - return this.createNewFile(DEFAULT_ASSETS_FOLDER, hash + ext) - .then(function(filename) { - that.downloaded[src] = filename; + var that = this; + var ext = path.extname(src); + var hash = crc.crc32(src).toString(16); - that.log.debug.ln('downloading asset', src); - return fs.download(src, that.resolve(filename)) - .thenResolve(filename); - }); -}; + // Create new file + return this.createNewFile(DEFAULT_ASSETS_FOLDER, hash + ext) + .then(function(filename) { + that.downloaded[src] = filename; -// Convert a .svg into an .png -// Return the output filename for the .png -AssetsInliner.prototype.convertSVGFile = function(src) { - if (this.svgs[src]) return Promise(this.svgs[src]); + that.log.debug.ln('downloading asset', src); + return fs.download(src, that.resolve(filename)) + .thenResolve(filename); + }); + }; - var that = this; - var hash = crc.crc32(src).toString(16); + // Convert a .svg into an .png + // Return the output filename for the .png + AssetsInliner.prototype.convertSVGFile = function(src) { + if (this.svgs[src]) return Promise(this.svgs[src]); - // Create new file - return this.createNewFile(DEFAULT_ASSETS_FOLDER, hash + '.png') - .then(function(filename) { - that.svgs[src] = filename; + var that = this; + var hash = crc.crc32(src).toString(16); - return imagesUtil.convertSVGToPNG(src, that.resolve(filename)) - .thenResolve(filename); - }); -}; + // Create new file + return this.createNewFile(DEFAULT_ASSETS_FOLDER, hash + '.png') + .then(function(filename) { + that.svgs[src] = filename; -// Convert an inline svg into an .png -// Return the output filename for the .png -AssetsInliner.prototype.convertSVGBuffer = function(buf) { - var that = this; - var hash = crc.crc32(buf).toString(16); + return imagesUtil.convertSVGToPNG(src, that.resolve(filename)) + .thenResolve(filename); + }); + }; - // Already converted? - if (this.inlineSvgs[hash]) return Promise(this.inlineSvgs[hash]); + // Convert an inline svg into an .png + // Return the output filename for the .png + AssetsInliner.prototype.convertSVGBuffer = function(buf) { + var that = this; + var hash = crc.crc32(buf).toString(16); - return this.createNewFile(DEFAULT_ASSETS_FOLDER, hash + '.png') - .then(function(filename) { - that.inlineSvgs[hash] = filename; + // Already converted? + if (this.inlineSvgs[hash]) return Promise(this.inlineSvgs[hash]); - return imagesUtil.convertSVGBufferToPNG(buf, that.resolve(filename)) - .thenResolve(filename); - }); -}; + return this.createNewFile(DEFAULT_ASSETS_FOLDER, hash + '.png') + .then(function(filename) { + that.inlineSvgs[hash] = filename; + return imagesUtil.convertSVGBufferToPNG(buf, that.resolve(filename)) + .thenResolve(filename); + }); + }; -module.exports = AssetsInliner; + return AssetsInliner; +}; diff --git a/lib/output/conrefs.js b/lib/output/conrefs.js index c91885e..f1cd123 100644 --- a/lib/output/conrefs.js +++ b/lib/output/conrefs.js @@ -1,60 +1,68 @@ var path = require('path'); +var util = require('util'); -var Output = require('./base'); +var FolderOutput = require('./folder')(); var Git = require('../utils/git'); var fs = require('../utils/fs'); var pathUtil = require('../utils/path'); /* -Middleware for output to resolve git conrefs +Mixin for output to resolve git conrefs */ -var ConrefsLoader = Output.createMixin(function() { - this.git = new Git(); -}); - -// Read a template by its source URL -ConrefsLoader.prototype.onGetTemplate = function(sourceURL) { - var that = this; - - return this.git.resolve(sourceURL) - .then(function(filepath) { - // Is local file - if (!filepath) { - filepath = that.book.resolve(sourceURL); - } else { - that.book.log.debug.ln('resolve from git', sourceURL, 'to', filepath); - } +module.exports = function conrefsLoader(Base) { + Base = Base || FolderOutput; - // Read file from absolute path - return fs.readFile(filepath) - .then(function(source) { - return { - src: source.toString('utf8'), - path: filepath - }; - }); - }); -}; + function ConrefsLoader() { + Base.apply(this, arguments); -// Generate a source URL for a template -ConrefsLoader.prototype.onResolveTemplate = function(from, to) { - // If origin is in the book, we enforce result file to be in the book - if (this.book.isInBook(from)) { - return this.book.resolve( - this.book.relative(path.dirname(from)), - to - ); + this.git = new Git(); } + util.inherits(ConrefsLoader, Base); - // If origin is in a git repository, we resolve file in the git repository - var gitRoot = this.git.resolveRoot(from); - if (gitRoot) { - return pathUtil.resolveInRoot(gitRoot, to); - } + // Read a template by its source URL + ConrefsLoader.prototype.onGetTemplate = function(sourceURL) { + var that = this; - // If origin is not in the book (include from a git content ref) - return path.resolve(path.dirname(from), to); -}; + return this.git.resolve(sourceURL) + .then(function(filepath) { + // Is local file + if (!filepath) { + filepath = that.book.resolve(sourceURL); + } else { + that.book.log.debug.ln('resolve from git', sourceURL, 'to', filepath); + } + + // Read file from absolute path + return fs.readFile(filepath) + .then(function(source) { + return { + src: source.toString('utf8'), + path: filepath + }; + }); + }); + }; -module.exports = ConrefsLoader; + // Generate a source URL for a template + ConrefsLoader.prototype.onResolveTemplate = function(from, to) { + // If origin is in the book, we enforce result file to be in the book + if (this.book.isInBook(from)) { + return this.book.resolve( + this.book.relative(path.dirname(from)), + to + ); + } + + // If origin is in a git repository, we resolve file in the git repository + var gitRoot = this.git.resolveRoot(from); + if (gitRoot) { + return pathUtil.resolveInRoot(gitRoot, to); + } + + // If origin is not in the book (include from a git content ref) + return path.resolve(path.dirname(from), to); + }; + + return ConrefsLoader; +}; diff --git a/lib/output/folder.js b/lib/output/folder.js index 90a3351..9964ecd 100644 --- a/lib/output/folder.js +++ b/lib/output/folder.js @@ -12,104 +12,106 @@ This output requires the native fs module to output book as a directory (mapping assets and pages) */ -function FolderOutput() { - Output.apply(this, arguments); -} -util.inherits(FolderOutput, Output); - -// Copy an asset file (non-parsable), ex: images, etc -FolderOutput.prototype.onAsset = function(filename) { - return this.copyFile( - this.book.resolve(filename), - filename - ); -}; +module.exports = function folderOutput(Base) { + Base = Base || Output; -// Prepare the generation by creating the output folder -FolderOutput.prototype.prepare = function() { - return fs.mkdirp(this.root()); -}; + function FolderOutput() { + Base.apply(this, arguments); + } + util.inherits(FolderOutput, Base); + // Copy an asset file (non-parsable), ex: images, etc + FolderOutput.prototype.onAsset = function(filename) { + return this.copyFile( + this.book.resolve(filename), + filename + ); + }; -// ----- Utility methods ----- + // Prepare the generation by creating the output folder + FolderOutput.prototype.prepare = function() { + return fs.mkdirp(this.root()); + }; -// Return path to the root folder -FolderOutput.prototype.root = function() { - return path.resolve(process.cwd(), this.book.config.get('output')); -}; -// Resolve a file in the output directory -FolderOutput.prototype.resolve = function(filename) { - return pathUtil.resolveInRoot.apply(null, [this.root()].concat(_.toArray(arguments))); -}; + // ----- Utility methods ----- -// 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); -}; + // Return path to the root folder + FolderOutput.prototype.root = function() { + return path.resolve(process.cwd(), this.book.config.get('output')); + }; + // Resolve a file in the output directory + FolderOutput.prototype.resolve = function(filename) { + return pathUtil.resolveInRoot.apply(null, [this.root()].concat(_.toArray(arguments))); + }; -// Copy a file to the output -FolderOutput.prototype.copyFile = function(from, to) { - var that = this; + // 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); + }; - return Promise() - .then(function() { - to = that.resolve(to); - return fs.copy(from, to); - }); -}; + // Copy a file to the output + FolderOutput.prototype.copyFile = function(from, to) { + var that = this; -// Write a file/buffer to the output folder -FolderOutput.prototype.writeFile = function(filename, buf) { - var that = this; + return Promise() + .then(function() { + to = that.resolve(to); - return Promise() - .then(function() { - filename = that.resolve(filename); - var folder = path.dirname(filename); + return fs.copy(from, to); + }); + }; - // Ensure fodler exists - return fs.mkdirp(folder); - }) + // Write a file/buffer to the output folder + FolderOutput.prototype.writeFile = function(filename, buf) { + var that = this; - // Write the file - .then(function() { - return fs.writeFile(filename, buf); - }); -}; + return Promise() + .then(function() { + filename = that.resolve(filename); + var folder = path.dirname(filename); -// Return true if a file exists in the output folder -FolderOutput.prototype.hasFile = function(filename) { - var that = this; + // Ensure fodler exists + return fs.mkdirp(folder); + }) - return Promise() - .then(function() { - return fs.exists(that.resolve(filename)); - }); -}; + // Write the file + .then(function() { + return fs.writeFile(filename, buf); + }); + }; + // Return true if a file exists in the output folder + FolderOutput.prototype.hasFile = function(filename) { + var that = this; -// Create a new unique file -// Returns its filename -FolderOutput.prototype.createNewFile = function(base, filename) { - var that = this; + return Promise() + .then(function() { + return fs.exists(that.resolve(filename)); + }); + }; - if (!filename) { - filename = path.basename(filename); - base = path.dirname(base); - } + // Create a new unique file + // Returns its filename + FolderOutput.prototype.createNewFile = function(base, filename) { + var that = this; - return fs.uniqueFilename(this.resolve(base), filename) - .then(function(out) { - out = path.join(base, out); + if (!filename) { + filename = path.basename(filename); + base = path.dirname(base); + } - return fs.ensure(that.resolve(out)) - .thenResolve(out); - }); -}; + return fs.uniqueFilename(this.resolve(base), filename) + .then(function(out) { + out = path.join(base, out); + return fs.ensure(that.resolve(out)) + .thenResolve(out); + }); + }; -module.exports = FolderOutput; + return FolderOutput; +}; diff --git a/lib/output/json.js b/lib/output/json.js index f146023..913bc2b 100644 --- a/lib/output/json.js +++ b/lib/output/json.js @@ -1,14 +1,8 @@ -var util = require('util'); -var FolderOutput = require('./folder'); -var ConrefsLoader = require('./conrefs'); var gitbook = require('../gitbook'); +var conrefsLoader = require('./conrefs'); -function JSONOutput() { - FolderOutput.apply(this, arguments); - ConrefsLoader.apply(this); -} -util.inherits(JSONOutput, FolderOutput); -util.inherits(JSONOutput, ConrefsLoader); + +var JSONOutput = conrefsLoader(); JSONOutput.prototype.name = 'json'; diff --git a/lib/output/website/index.js b/lib/output/website/index.js index fc9bc6b..6166ccc 100644 --- a/lib/output/website/index.js +++ b/lib/output/website/index.js @@ -1,15 +1,8 @@ -var util = require('util'); -var FolderOutput = require('../folder'); -var ConrefsLoader = require('../conrefs'); +var conrefsLoader = require('../conrefs'); var Theme = require('./theme'); -function WebsiteOutput() { - FolderOutput.apply(this, arguments); - ConrefsLoader.apply(this); -} -util.inherits(WebsiteOutput, FolderOutput); -util.inherits(WebsiteOutput, ConrefsLoader); +var WebsiteOutput = conrefsLoader(); WebsiteOutput.prototype.name = 'website'; |