mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 01:53:26 +00:00
Add test for assets inliner
This commit is contained in:
@@ -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?
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
+20
-11
@@ -61,6 +61,26 @@ Output.prototype.getRoot = function() {
|
||||
return this.getOptions().get('root');
|
||||
};
|
||||
|
||||
/**
|
||||
Update state of output
|
||||
|
||||
@param {Map} newState
|
||||
@return {Output}
|
||||
*/
|
||||
Output.prototype.setState = function(newState) {
|
||||
return this.set('state', newState);
|
||||
};
|
||||
|
||||
/**
|
||||
Update options
|
||||
|
||||
@param {Map} newOptions
|
||||
@return {Output}
|
||||
*/
|
||||
Output.prototype.setOptions = function(newOptions) {
|
||||
return this.set('options', newOptions);
|
||||
};
|
||||
|
||||
/**
|
||||
Return logegr for this output (same as book)
|
||||
|
||||
@@ -70,15 +90,4 @@ Output.prototype.getLogger = function() {
|
||||
return this.getBook().getLogger();
|
||||
};
|
||||
|
||||
/**
|
||||
Update state of output
|
||||
|
||||
@param {Output} output
|
||||
@param {Map} newState
|
||||
@return {Output}
|
||||
*/
|
||||
Output.updateState = function(output, newState) {
|
||||
return output.set('state', newState);
|
||||
};
|
||||
|
||||
module.exports = Output;
|
||||
|
||||
@@ -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')
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -53,7 +53,7 @@ function onPage(output, page) {
|
||||
gitbook: context.gitbook,
|
||||
basePath: basePath,
|
||||
book: {
|
||||
language: null // context.book.language
|
||||
language: book.getLanguage()
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
+6
-4
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user