diff options
-rw-r--r-- | docs/README.md | 2 | ||||
-rw-r--r-- | jest/customMatchers.js | 24 | ||||
-rw-r--r-- | lib/models/output.js | 27 | ||||
-rw-r--r-- | lib/output/ebook/index.js | 12 | ||||
-rw-r--r-- | lib/output/ebook/onFinish.js | 19 | ||||
-rw-r--r-- | lib/output/ebook/onInit.js | 21 | ||||
-rw-r--r-- | lib/output/ebook/onPage.js | 8 | ||||
-rw-r--r-- | lib/output/modifiers/__tests__/fetchRemoteImages.js | 25 | ||||
-rw-r--r-- | lib/output/modifiers/__tests__/svgToImg.js | 25 | ||||
-rw-r--r-- | lib/output/website/createTemplateEngine.js | 1 | ||||
-rw-r--r-- | lib/output/website/onPage.js | 2 | ||||
-rw-r--r-- | package.json | 10 |
12 files changed, 153 insertions, 23 deletions
diff --git a/docs/README.md b/docs/README.md index d626231..16917dd 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,5 +1,7 @@ # GitBook Toolchain Documentation + + This document aims to be a comprehensive guide to GitBook. It contains the full documentation for version **{{ book.version }}**. Help for GitBook.com specific questions can be found at [help.gitbook.com](https://help.gitbook.com). ### What is GitBook? diff --git a/jest/customMatchers.js b/jest/customMatchers.js new file mode 100644 index 0000000..a0dd7f9 --- /dev/null +++ b/jest/customMatchers.js @@ -0,0 +1,24 @@ +var path = require('path'); +var fs = require('fs'); + +var matchers = { + /** + Verify that a file exists in a directory + */ + toHaveFile: function () { + return { + compare: function (actual, expected) { + var filePath = path.join(actual, expected); + var exists = fs.existsSync(filePath); + + return { + pass: exists + }; + } + }; + } +}; + +jasmine.getEnv().beforeEach(function () { + jasmine.addMatchers(matchers); +});
\ No newline at end of file diff --git a/lib/models/output.js b/lib/models/output.js index c11017e..43e36f8 100644 --- a/lib/models/output.js +++ b/lib/models/output.js @@ -62,23 +62,32 @@ Output.prototype.getRoot = function() { }; /** - Return logegr for this output (same as book) + Update state of output - @return {Logger} + @param {Map} newState + @return {Output} */ -Output.prototype.getLogger = function() { - return this.getBook().getLogger(); +Output.prototype.setState = function(newState) { + return this.set('state', newState); }; /** - Update state of output + Update options - @param {Output} output - @param {Map} newState + @param {Map} newOptions @return {Output} */ -Output.updateState = function(output, newState) { - return output.set('state', newState); +Output.prototype.setOptions = function(newOptions) { + return this.set('options', newOptions); +}; + +/** + Return logegr for this output (same as book) + + @return {Logger} +*/ +Output.prototype.getLogger = function() { + return this.getBook().getLogger(); }; module.exports = Output; diff --git a/lib/output/ebook/index.js b/lib/output/ebook/index.js index 46a94e3..3aaa7e7 100644 --- a/lib/output/ebook/index.js +++ b/lib/output/ebook/index.js @@ -1,5 +1,9 @@ +var extend = require('extend'); +var websiteGenerator = require('../website'); -module.exports = { - - -}; +module.exports = extend({}, websiteGenerator, { + name: 'ebook', + onInit: require('./onInit'), + onPage: require('./onPage'), + onFinish: require('./onFinish') +}); diff --git a/lib/output/ebook/onFinish.js b/lib/output/ebook/onFinish.js new file mode 100644 index 0000000..5ea5148 --- /dev/null +++ b/lib/output/ebook/onFinish.js @@ -0,0 +1,19 @@ +var websiteGenerator = require('../website'); + +/** + Finish the generation, generate the ebook file using ebook-convert + + @param {Output} + @return {Output} +*/ +function onFinish(output) { + return websiteGenerator.onFinish(output) + .then(function(resultOutput) { + + // todo + + return resultOutput; + }); +} + +module.exports = onFinish; diff --git a/lib/output/ebook/onInit.js b/lib/output/ebook/onInit.js new file mode 100644 index 0000000..9cee141 --- /dev/null +++ b/lib/output/ebook/onInit.js @@ -0,0 +1,21 @@ +var websiteGenerator = require('../website'); + +/** + Initialize the generator + + @param {Output} + @return {Output} +*/ +function onInit(output) { + return websiteGenerator.onInit(output) + .then(function(resultOutput) { + var options = resultOutput.getOptions(); + + options = options.set('directoryIndex', false); + options = options.set('prefix', 'ebook'); + + return resultOutput.setOptions(options); + }); +} + +module.exports = onInit; diff --git a/lib/output/ebook/onPage.js b/lib/output/ebook/onPage.js index ab15133..13edf17 100644 --- a/lib/output/ebook/onPage.js +++ b/lib/output/ebook/onPage.js @@ -1,18 +1,18 @@ var website = require('../website'); -var Modifier = require('../modifier'); +var Modifiers = require('../modifiers'); /** Write a page for ebook output @param {Output} output - @param {Page} page + @param {Output} */ function onPage(output, page) { var options = output.getOptions(); // Inline assets - return Modifier.modifyHTML(page, [ - Modifier.inlineAssets(options.get('root')) + return Modifiers.modifyHTML(page, [ + Modifiers.inlineAssets(options.get('root')) ]) // Write page using website generator diff --git a/lib/output/modifiers/__tests__/fetchRemoteImages.js b/lib/output/modifiers/__tests__/fetchRemoteImages.js new file mode 100644 index 0000000..543aca0 --- /dev/null +++ b/lib/output/modifiers/__tests__/fetchRemoteImages.js @@ -0,0 +1,25 @@ +var cheerio = require('cheerio'); +var tmp = require('tmp'); + +describe('fetchRemoteImages', function() { + var dir; + var fetchRemoteImages = require('../fetchRemoteImages'); + + beforeEach(function() { + dir = tmp.dirSync(); + }); + + pit('should download image file', function() { + var $ = cheerio.load('<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png" />'); + + return fetchRemoteImages(dir.name, $) + .then(function() { + var $img = $('img'); + var src = '.' + $img.attr('src'); + + expect(dir.name).toHaveFile(src); + }); + }); +}); + + diff --git a/lib/output/modifiers/__tests__/svgToImg.js b/lib/output/modifiers/__tests__/svgToImg.js new file mode 100644 index 0000000..762a02e --- /dev/null +++ b/lib/output/modifiers/__tests__/svgToImg.js @@ -0,0 +1,25 @@ +var cheerio = require('cheerio'); +var tmp = require('tmp'); + +describe('svgToImg', function() { + var dir; + var svgToImg = require('../svgToImg'); + + beforeEach(function() { + dir = tmp.dirSync(); + }); + + pit('should write svg as a file', function() { + var $ = cheerio.load('<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100" version="1.1"><rect width="200" height="100" stroke="black" stroke-width="6" fill="green"/></svg>'); + + return svgToImg(dir.name, $) + .then(function() { + var $img = $('img'); + var src = '.' + $img.attr('src'); + + expect(dir.name).toHaveFile(src); + }); + }); +}); + + diff --git a/lib/output/website/createTemplateEngine.js b/lib/output/website/createTemplateEngine.js index 7d87248..24990b6 100644 --- a/lib/output/website/createTemplateEngine.js +++ b/lib/output/website/createTemplateEngine.js @@ -35,7 +35,6 @@ function createTemplateEngine(output, currentFile) { var i18n = state.getI18n(); var config = book.getConfig(); var summary = book.getSummary(); - var pages = output.getPages(); var outputFolder = output.getRoot(); // Search paths for templates diff --git a/lib/output/website/onPage.js b/lib/output/website/onPage.js index ddae986..751a430 100644 --- a/lib/output/website/onPage.js +++ b/lib/output/website/onPage.js @@ -53,7 +53,7 @@ function onPage(output, page) { gitbook: context.gitbook, basePath: basePath, book: { - language: null // context.book.language + language: book.getLanguage() } }; } diff --git a/package.json b/package.json index c35c7f9..7840e81 100644 --- a/package.json +++ b/package.json @@ -63,9 +63,7 @@ }, "devDependencies": { "eslint": "2.7.0", - "jest-cli": "^11.0.2", - "mocha": "2.4.5", - "should": "8.3.0" + "jest-cli": "^11.0.2" }, "scripts": { "test": "node_modules/.bin/jest --bail", @@ -97,5 +95,9 @@ "name": "Samy Pessé", "email": "samy@gitbook.com" } - ] + ], + "jest": { + "automock": false, + "setupTestFrameworkScriptFile": "<rootDir>/jest/customMatchers.js" + } } |