summaryrefslogtreecommitdiffstats
path: root/packages
diff options
context:
space:
mode:
authorSamy Pesse <samypesse@gmail.com>2016-10-08 18:19:49 +0200
committerSamy Pesse <samypesse@gmail.com>2016-10-08 18:19:49 +0200
commit03b89194d785807e12f3a7b40d5558d300e3cc49 (patch)
treebd63626c3186ce8699a473457127dd536bc0064a /packages
parent68ec88c62096faf5c8538bec4431868919ae9652 (diff)
downloadgitbook-03b89194d785807e12f3a7b40d5558d300e3cc49.zip
gitbook-03b89194d785807e12f3a7b40d5558d300e3cc49.tar.gz
gitbook-03b89194d785807e12f3a7b40d5558d300e3cc49.tar.bz2
Prepare for multiple role rendering for ebooks
Diffstat (limited to 'packages')
-rw-r--r--packages/gitbook-core/src/lib/bootstrap.js7
-rw-r--r--packages/gitbook-core/src/lib/renderWithContext.js12
-rw-r--r--packages/gitbook-plugin-theme-default/src/index.js2
-rw-r--r--packages/gitbook/src/browser/loadPlugins.js10
-rw-r--r--packages/gitbook/src/browser/render.js30
-rw-r--r--packages/gitbook/src/json/encodeState.js7
-rw-r--r--packages/gitbook/src/models/ignore.js65
-rw-r--r--packages/gitbook/src/output/__tests__/generateMock.js2
-rw-r--r--packages/gitbook/src/output/ebook/getPDFTemplate.js39
-rw-r--r--packages/gitbook/src/output/ebook/onFinish.js50
-rw-r--r--packages/gitbook/src/output/ebook/onPage.js11
-rw-r--r--packages/gitbook/src/output/ebook/options.js11
-rw-r--r--packages/gitbook/src/output/generateBook.js6
-rw-r--r--packages/gitbook/src/output/generatePage.js10
-rw-r--r--packages/gitbook/src/output/website/onPage.js2
15 files changed, 132 insertions, 132 deletions
diff --git a/packages/gitbook-core/src/lib/bootstrap.js b/packages/gitbook-core/src/lib/bootstrap.js
index f5183d1..f3c99b7 100644
--- a/packages/gitbook-core/src/lib/bootstrap.js
+++ b/packages/gitbook-core/src/lib/bootstrap.js
@@ -5,9 +5,10 @@ const createContext = require('./createContext');
const renderWithContext = require('./renderWithContext');
/**
- * Bootstrap GitBook on the browser (this function should not be called on the server side)
+ * Bootstrap GitBook on the browser (this function should not be called on the server side).
+ * @param {Object} matching
*/
-function bootstrap() {
+function bootstrap(matching) {
const initialState = getPayload(window.document);
const plugins = window.gitbookPlugins;
@@ -19,7 +20,7 @@ function bootstrap() {
window.gitbookContext = context;
// Render with the store
- const el = renderWithContext(context);
+ const el = renderWithContext(context, matching);
ReactDOM.render(el, mountNode);
}
diff --git a/packages/gitbook-core/src/lib/renderWithContext.js b/packages/gitbook-core/src/lib/renderWithContext.js
index 44f8ba4..f9a093c 100644
--- a/packages/gitbook-core/src/lib/renderWithContext.js
+++ b/packages/gitbook-core/src/lib/renderWithContext.js
@@ -9,7 +9,8 @@ const contextShape = require('../shapes/context');
const GitBookApplication = React.createClass({
propTypes: {
- context: contextShape
+ context: contextShape,
+ matching: React.PropTypes.object
},
componentDidMount() {
@@ -23,13 +24,13 @@ const GitBookApplication = React.createClass({
},
render() {
- const { context } = this.props;
+ const { context, matching } = this.props;
return (
<ContextProvider context={context}>
<PJAXWrapper>
<I18nProvider>
- <InjectedComponent matching={{ role: 'Body' }} />
+ <InjectedComponent matching={matching} />
</I18nProvider>
</PJAXWrapper>
</ContextProvider>
@@ -42,11 +43,12 @@ const GitBookApplication = React.createClass({
* Render the application for a GitBook context.
*
* @param {GitBookContext} context
+ * @param {Object} matching
* @return {React.Element} element
*/
-function renderWithContext(context) {
+function renderWithContext(context, matching) {
return (
- <GitBookApplication context={context} />
+ <GitBookApplication context={context} matching={matching} />
);
}
diff --git a/packages/gitbook-plugin-theme-default/src/index.js b/packages/gitbook-plugin-theme-default/src/index.js
index c391908..ad96175 100644
--- a/packages/gitbook-plugin-theme-default/src/index.js
+++ b/packages/gitbook-plugin-theme-default/src/index.js
@@ -7,7 +7,7 @@ const locales = require('./i18n');
module.exports = GitBook.createPlugin({
activate: (dispatch, state, { Components, I18n }) => {
- dispatch(Components.registerComponent(Theme, { role: 'Body' }));
+ dispatch(Components.registerComponent(Theme, { role: 'website:body' }));
dispatch(I18n.registerLocales(locales));
},
reduce: reduceState
diff --git a/packages/gitbook/src/browser/loadPlugins.js b/packages/gitbook/src/browser/loadPlugins.js
index 0f26aae..bd8a195 100644
--- a/packages/gitbook/src/browser/loadPlugins.js
+++ b/packages/gitbook/src/browser/loadPlugins.js
@@ -1,18 +1,20 @@
const path = require('path');
/**
- * Load all browser plugins
+ * Load all browser plugins.
+ *
* @param {OrderedMap<Plugin>} plugins
+ * @param {String} type ('browser', 'ebook')
* @return {Array}
*/
-function loadPlugins(plugins) {
+function loadPlugins(plugins, type) {
return plugins
.valueSeq()
- .filter(plugin => plugin.getPackage().has('browser'))
+ .filter(plugin => plugin.getPackage().has(type))
.map(plugin => {
const browserFile = path.resolve(
plugin.getPath(),
- plugin.getPackage().get('browser')
+ plugin.getPackage().get(type)
);
return require(browserFile);
diff --git a/packages/gitbook/src/browser/render.js b/packages/gitbook/src/browser/render.js
index 616b157..d5062c6 100644
--- a/packages/gitbook/src/browser/render.js
+++ b/packages/gitbook/src/browser/render.js
@@ -4,9 +4,7 @@ const GitBook = require('gitbook-core');
const loadPlugins = require('./loadPlugins');
-const BOOTSTRAP_CODE = '(function() { require("gitbook-core").bootstrap() })()';
-
-function HTML({head, innerHTML, payload, scripts}) {
+function HTML({head, innerHTML, payload, scripts, bootstrap}) {
const attrs = head.htmlAttributes.toComponent();
return (
@@ -23,7 +21,7 @@ function HTML({head, innerHTML, payload, scripts}) {
return <script key={script} src={script} />;
})}
<script type="application/payload+json" dangerouslySetInnerHTML={{__html: payload}} />
- <script type="application/javascript" dangerouslySetInnerHTML={{__html: BOOTSTRAP_CODE}} />
+ <script type="application/javascript" dangerouslySetInnerHTML={{__html: bootstrap}} />
{head.script.toComponent()}
</body>
</html>
@@ -33,27 +31,40 @@ HTML.propTypes = {
head: React.PropTypes.object,
innerHTML: React.PropTypes.string,
payload: React.PropTypes.string,
+ bootstrap: React.PropTypes.string,
scripts: React.PropTypes.arrayOf(React.PropTypes.string)
};
/**
- * Render a page
+ * Get bootstrap code for a role
+ * @param {String} role
+ * @return {String}
+ */
+function getBootstrapCode(role) {
+ return `(function() { require("gitbook-core").bootstrap({ role: "${role}" }) })()`;
+}
+
+/**
+ * Render a view using plugins.
+ *
* @param {OrderedMap<String:Plugin>} plugin
* @param {Object} initialState
+ * @param {String} type ("ebook" or "browser")
+ * @param {String} role
* @return {String} html
*/
-function render(plugins, initialState) {
+function render(plugins, initialState, type, role) {
// Load the plugins
- const browserPlugins = loadPlugins(plugins);
+ const browserPlugins = loadPlugins(plugins, type);
const payload = JSON.stringify(initialState);
const context = GitBook.createContext(browserPlugins, initialState);
const scripts = plugins.toList()
- .filter(plugin => plugin.getPackage().has('browser'))
+ .filter(plugin => plugin.getPackage().has(type))
.map(plugin => 'gitbook/plugins/' + plugin.getName() + '.js')
.toArray();
- const el = GitBook.renderWithContext(context);
+ const el = GitBook.renderWithContext(context, { role });
// We're done with the context
context.deactivate();
@@ -69,6 +80,7 @@ function render(plugins, initialState) {
head={head}
innerHTML={innerHTML}
payload={payload}
+ bootstrap={getBootstrapCode(role)}
scripts={['gitbook/core.js'].concat(scripts)}
/>;
diff --git a/packages/gitbook/src/json/encodeState.js b/packages/gitbook/src/json/encodeState.js
index d264835..50e7880 100644
--- a/packages/gitbook/src/json/encodeState.js
+++ b/packages/gitbook/src/json/encodeState.js
@@ -11,13 +11,12 @@ const encodeFile = require('./encodeFile');
* This JSON representation is used as initial state for the redux store.
*
* @param {Output} output
- * @param {Page} page
+ * @param {Page} page?
* @return {JSON}
*/
function encodeStateToJSON(output, page) {
const book = output.getBook();
const urls = output.getURLIndex();
- const file = page.getFile();
return {
output: {
@@ -34,8 +33,8 @@ function encodeStateToJSON(output, page) {
config: book.getConfig().getValues().toJS(),
languages: book.isMultilingual() ? encodeLanguages(book.getLanguages()) : undefined,
- page: encodePage(page, book.getSummary(), urls),
- file: encodeFile(file, urls)
+ page: page ? encodePage(page, book.getSummary(), urls) : undefined,
+ file: page ? encodeFile(page.getFile(), urls) : undefined
};
}
diff --git a/packages/gitbook/src/models/ignore.js b/packages/gitbook/src/models/ignore.js
index 99ade9a..547f6b4 100644
--- a/packages/gitbook/src/models/ignore.js
+++ b/packages/gitbook/src/models/ignore.js
@@ -1,42 +1,43 @@
-const Immutable = require('immutable');
+const { Record } = require('immutable');
const IgnoreMutable = require('ignore');
/*
Immutable version of node-ignore
*/
-const Ignore = Immutable.Record({
- ignore: new IgnoreMutable()
-}, 'Ignore');
-
-Ignore.prototype.getIgnore = function() {
- return this.get('ignore');
-};
-/**
- Test if a file is ignored by these rules
-
- @param {String} filePath
- @return {Boolean}
-*/
-Ignore.prototype.isFileIgnored = function(filename) {
- const ignore = this.getIgnore();
- return ignore.filter([filename]).length == 0;
+const DEFAULTS = {
+ ignore: new IgnoreMutable()
};
-/**
- Add rules
-
- @param {String}
- @return {Ignore}
-*/
-Ignore.prototype.add = function(rule) {
- const ignore = this.getIgnore();
- const newIgnore = new IgnoreMutable();
-
- newIgnore.add(ignore);
- newIgnore.add(rule);
-
- return this.set('ignore', newIgnore);
-};
+class Ignore extends Record(DEFAULTS) {
+ getIgnore() {
+ return this.get('ignore');
+ }
+
+ /**
+ * Test if a file is ignored by these rules.
+ * @param {String} filePath
+ * @return {Boolean} isIgnored
+ */
+ isFileIgnored(filename) {
+ const ignore = this.getIgnore();
+ return ignore.filter([filename]).length == 0;
+ }
+
+ /**
+ * Add rules.
+ * @param {String}
+ * @return {Ignore}
+ */
+ add(rule) {
+ const ignore = this.getIgnore();
+ const newIgnore = new IgnoreMutable();
+
+ newIgnore.add(ignore);
+ newIgnore.add(rule);
+
+ return this.set('ignore', newIgnore);
+ }
+}
module.exports = Ignore;
diff --git a/packages/gitbook/src/output/__tests__/generateMock.js b/packages/gitbook/src/output/__tests__/generateMock.js
index a0be244..6ae1de2 100644
--- a/packages/gitbook/src/output/__tests__/generateMock.js
+++ b/packages/gitbook/src/output/__tests__/generateMock.js
@@ -29,7 +29,7 @@ function generateMock(Generator, files) {
book = book.setLogLevel('disabled');
return parseBook(book)
- .then(function(resultBook) {
+ .then((resultBook) => {
return generateBook(Generator, resultBook, {
root: dir.name
});
diff --git a/packages/gitbook/src/output/ebook/getPDFTemplate.js b/packages/gitbook/src/output/ebook/getPDFTemplate.js
index c0faed3..53c7a82 100644
--- a/packages/gitbook/src/output/ebook/getPDFTemplate.js
+++ b/packages/gitbook/src/output/ebook/getPDFTemplate.js
@@ -1,40 +1,35 @@
const juice = require('juice');
-const WebsiteGenerator = require('../website');
const JSONUtils = require('../../json');
-const Templating = require('../../templating');
+const render = require('../../browser/render');
const Promise = require('../../utils/promise');
-
/**
- Generate PDF header/footer templates
-
- @param {Output} output
- @param {String} type
- @return {String}
-*/
+ * Generate PDF header/footer templates
+ *
+ * @param {Output} output
+ * @param {String} type ("footer" or "header")
+ * @return {String} html
+ */
function getPDFTemplate(output, type) {
- const filePath = 'pdf_' + type + '.html';
const outputRoot = output.getRoot();
- const engine = WebsiteGenerator.createTemplateEngine(output, filePath);
+ const plugins = output.getPlugins();
- // Generate context
- const context = JSONUtils.encodeOutput(output);
- context.page = {
+ // Generate initial state
+ const initialState = JSONUtils.encodeState(output);
+ initialState.page = {
num: '_PAGENUM_',
title: '_SECTION_'
};
// Render the theme
- return Templating.renderFile(engine, 'ebook/' + filePath, context)
+ const html = render(plugins, initialState, 'ebook', `pdf:${type}`);
- // Inline css and assets
- .then(function(tplOut) {
- return Promise.nfcall(juice.juiceResources, tplOut.getContent(), {
- webResources: {
- relativeTo: outputRoot
- }
- });
+ // Inline CSS
+ return Promise.nfcall(juice.juiceResources, html, {
+ webResources: {
+ relativeTo: outputRoot
+ }
});
}
diff --git a/packages/gitbook/src/output/ebook/onFinish.js b/packages/gitbook/src/output/ebook/onFinish.js
index adff798..7db757f 100644
--- a/packages/gitbook/src/output/ebook/onFinish.js
+++ b/packages/gitbook/src/output/ebook/onFinish.js
@@ -1,45 +1,39 @@
const path = require('path');
-const WebsiteGenerator = require('../website');
const JSONUtils = require('../../json');
-const Templating = require('../../templating');
const Promise = require('../../utils/promise');
const error = require('../../utils/error');
const command = require('../../utils/command');
const writeFile = require('../helper/writeFile');
+const render = require('../../browser/render');
const getConvertOptions = require('./getConvertOptions');
const SUMMARY_FILE = 'SUMMARY.html';
/**
- Write the SUMMARY.html
-
- @param {Output}
- @return {Output}
-*/
+ * Write the SUMMARY.html
+ *
+ * @param {Output} output
+ * @return {Output} output
+ */
function writeSummary(output) {
- const options = output.getOptions();
- const prefix = options.get('prefix');
+ const plugins = output.getPlugins();
- const filePath = SUMMARY_FILE;
- const engine = WebsiteGenerator.createTemplateEngine(output, filePath);
- const context = JSONUtils.encodeOutput(output);
+ // Generate initial state
+ const initialState = JSONUtils.encodeState(output);
- // Render the theme
- return Templating.renderFile(engine, prefix + '/summary.html', context)
+ // Render using React
+ const html = render(plugins, initialState, 'ebook', 'ebook:summary');
- // Write it to the disk
- .then(function(tplOut) {
- return writeFile(output, filePath, tplOut.getContent());
- });
+ return writeFile(output, SUMMARY_FILE, html);
}
/**
- Generate the ebook file as "index.pdf"
-
- @param {Output}
- @return {Output}
-*/
+ * Generate the ebook file as "index.pdf"
+ *
+ * @param {Output} output
+ * @return {Output} output
+ */
function runEbookConvert(output) {
const logger = output.getLogger();
const options = output.getOptions();
@@ -78,11 +72,11 @@ function runEbookConvert(output) {
}
/**
- Finish the generation, generates the SUMMARY.html
-
- @param {Output}
- @return {Output}
-*/
+ * Finish the generation, generates the SUMMARY.html
+ *
+ * @param {Output} output
+ * @return {Output} output
+ */
function onFinish(output) {
return writeSummary(output)
.then(runEbookConvert);
diff --git a/packages/gitbook/src/output/ebook/onPage.js b/packages/gitbook/src/output/ebook/onPage.js
index 520d296..a7c2137 100644
--- a/packages/gitbook/src/output/ebook/onPage.js
+++ b/packages/gitbook/src/output/ebook/onPage.js
@@ -2,11 +2,12 @@ const WebsiteGenerator = require('../website');
const Modifiers = require('../modifiers');
/**
- Write a page for ebook output
-
- @param {Output} output
- @param {Output}
-*/
+ * Write a page for ebook output. It renders it just as the website generator
+ * except that it inline assets.
+ *
+ * @param {Output} output
+ * @param {Output} output
+ */
function onPage(output, page) {
const options = output.getOptions();
diff --git a/packages/gitbook/src/output/ebook/options.js b/packages/gitbook/src/output/ebook/options.js
index 4156fac..d192fd2 100644
--- a/packages/gitbook/src/output/ebook/options.js
+++ b/packages/gitbook/src/output/ebook/options.js
@@ -2,16 +2,13 @@ const Immutable = require('immutable');
const Options = Immutable.Record({
// Root folder for the output
- root: String(),
-
+ root: String(),
// Prefix for generation
- prefix: String('ebook'),
-
+ prefix: String('ebook'),
// Format to generate using ebook-convert
- format: String(),
-
+ format: String(),
// Force use of absolute urls ("index.html" instead of "/")
- directoryIndex: Boolean(false)
+ directoryIndex: Boolean(false)
});
module.exports = Options;
diff --git a/packages/gitbook/src/output/generateBook.js b/packages/gitbook/src/output/generateBook.js
index e27d3ce..0e2c230 100644
--- a/packages/gitbook/src/output/generateBook.js
+++ b/packages/gitbook/src/output/generateBook.js
@@ -58,7 +58,7 @@ function processOutput(generator, startOutput) {
)
)
- .then(function(output) {
+ .then((output) => {
if (!generator.onInit) {
return output;
}
@@ -69,7 +69,7 @@ function processOutput(generator, startOutput) {
.then(generateAssets.bind(null, generator))
.then(generatePages.bind(null, generator))
- .tap(function(output) {
+ .tap((output) => {
const book = output.getBook();
if (!book.isMultilingual()) {
@@ -111,7 +111,7 @@ function processOutput(generator, startOutput) {
)
)
- .then(function(output) {
+ .then((output) => {
if (!generator.onFinish) {
return output;
}
diff --git a/packages/gitbook/src/output/generatePage.js b/packages/gitbook/src/output/generatePage.js
index 36a5780..7375f1d 100644
--- a/packages/gitbook/src/output/generatePage.js
+++ b/packages/gitbook/src/output/generatePage.js
@@ -49,15 +49,11 @@ function generatePage(output, page) {
return Templating.render(engine, absoluteFilePath, content, context);
})
- .then((content) => {
- return parser.parsePage(content)
- .then(function(result) {
- return output.setContent(result.content);
- });
- })
+ // Parse with markdown/asciidoc parser
+ .then((content) => parser.parsePage(content))
// Return new page
- .then((content) => {
+ .then(({content}) => {
return resultPage.set('content', content);
})
diff --git a/packages/gitbook/src/output/website/onPage.js b/packages/gitbook/src/output/website/onPage.js
index 7dd35f3..90eec63 100644
--- a/packages/gitbook/src/output/website/onPage.js
+++ b/packages/gitbook/src/output/website/onPage.js
@@ -24,7 +24,7 @@ function onPage(output, page) {
const initialState = JSONUtils.encodeState(output, resultPage);
// Render the theme
- const html = render(plugins, initialState);
+ const html = render(plugins, initialState, 'browser', 'website:body');
// Write it to the disk
return writeFile(output, filePath, html);