diff --git a/lib/constants/templatesFolder.js b/lib/constants/templatesFolder.js new file mode 100644 index 000000000..aad6a72a9 --- /dev/null +++ b/lib/constants/templatesFolder.js @@ -0,0 +1,2 @@ + +module.exports = '_layouts'; diff --git a/lib/output/index.js b/lib/output/index.js index d3534908d..9112666c7 100644 --- a/lib/output/index.js +++ b/lib/output/index.js @@ -1,5 +1,6 @@ module.exports = { generate: require('./generateBook'), JSONGenerator: require('./json'), + WebsiteGenerator: require('./website'), EbookGenerator: require('./ebook') }; diff --git a/lib/output/website/createTemplateEngine.js b/lib/output/website/createTemplateEngine.js index 1a706679e..b90c3ad25 100644 --- a/lib/output/website/createTemplateEngine.js +++ b/lib/output/website/createTemplateEngine.js @@ -1,7 +1,18 @@ +var path = require('path'); + +var TEMPLATES_FOLDER = require('../../constants/templatesFolder'); + var Templating = require('../../templating'); var TemplateEngine = require('../../models/templateEngine'); var listSearchPaths = require('./listSearchPaths'); +/** + Directory for a theme with the templates +*/ +function templateFolder(dir) { + return path.join(dir, TEMPLATES_FOLDER); +} + /** Create templating engine to render themes @@ -10,7 +21,11 @@ var listSearchPaths = require('./listSearchPaths'); */ function createTemplateEngine(output) { var searchPaths = listSearchPaths(output); - var loader = new Templating.ThemesLoader(searchPaths); + + // Search paths for templates + var tplSearchPaths = searchPaths.map(templateFolder); + + var loader = new Templating.ThemesLoader(tplSearchPaths); return new TemplateEngine({ loader: loader diff --git a/lib/output/website/index.js b/lib/output/website/index.js index e24c127b8..bb01ed604 100644 --- a/lib/output/website/index.js +++ b/lib/output/website/index.js @@ -1,6 +1,6 @@ module.exports = { - name: 'json', + name: 'website', Options: require('./options'), onPage: require('./onPage') }; diff --git a/lib/output/website/onPage.js b/lib/output/website/onPage.js index 47274b3a4..b6856b3ad 100644 --- a/lib/output/website/onPage.js +++ b/lib/output/website/onPage.js @@ -12,6 +12,8 @@ var createTemplateEngine = require('./createTemplateEngine'); @param {Page} page */ function onPage(output, page) { + var options = output.getOptions(); + var prefix = options.get('prefix'); var engine = createTemplateEngine(output); return Modifiers.modifyHTML(page, getModifiers(output, page)) @@ -20,7 +22,7 @@ function onPage(output, page) { var context = JSONUtils.encodeBookWithPage(output.getBook(), resultPage); // Render the theme - return Templating.renderFile(engine, 'website/page.html', context) + return Templating.renderFile(engine, prefix + '/page.html', context) // Write it to the disk .then(function(html) { diff --git a/lib/output/website/options.js b/lib/output/website/options.js index 79167b180..eca8a8dc1 100644 --- a/lib/output/website/options.js +++ b/lib/output/website/options.js @@ -2,7 +2,10 @@ var Immutable = require('immutable'); var Options = Immutable.Record({ // Root folder for the output - root: String() + root: String(), + + // Prefix for generation + prefix: String('website') }); module.exports = Options; diff --git a/lib/templating/render.js b/lib/templating/render.js index 87d5096ed..bf21cfe05 100644 --- a/lib/templating/render.js +++ b/lib/templating/render.js @@ -8,7 +8,7 @@ var replaceShortcuts = require('./replaceShortcuts'); @param {TemplateEngine} engine @param {String} filePath @param {String} content - @param {Object} ctx + @param {Object} context @return {Promise} */ function renderTemplate(engine, filePath, content, context) { diff --git a/lib/templating/renderFile.js b/lib/templating/renderFile.js index 13a9c5b30..9b74e5bcd 100644 --- a/lib/templating/renderFile.js +++ b/lib/templating/renderFile.js @@ -1,27 +1,38 @@ var Promise = require('../utils/promise'); - -var replaceShortcuts = require('./replaceShortcuts'); +var error = require('../utils/error'); +var render = require('./render'); /** Render a template @param {TemplateEngine} engine @param {String} filePath - @param {String} content - @param {Object} ctx + @param {Object} context @return {Promise} */ -function renderTemplateFile(engine, filePath, content, context) { - context = context || {}; - var env = engine.toNunjucks(); +function renderTemplateFile(engine, filePath, context) { + var loader = engine.getLoader(); - content = replaceShortcuts(engine, filePath, content); + return Promise() + .then(function() { + if (!loader.async) { + return loader.getSource(filePath); + } + + var deferred = Promise.defer(); + loader.getSource(filePath, deferred.makeNodeResolver()); + return deferred.promise; + }) + .then(function(result) { + if (!result) { + throw error.TemplateError(new Error('Not found'), { + filename: filePath + }); + } + + return render(engine, result.path, result.src, context); + }); - return Promise.nfcall( - env.render.bind(env), - content, - context - ); } module.exports = renderTemplateFile; diff --git a/test.js b/test.js index 0de26b788..e1f3ac96d 100644 --- a/test.js +++ b/test.js @@ -16,7 +16,7 @@ var book = gitbook.Book.createForFS(fs); // Parse the book gitbook.Parse.parseBook(book) .then(function(_book) { - return Output.generate(Output.JSONGenerator, _book, { + return Output.generate(Output.WebsiteGenerator, _book, { root: path.join(__dirname, '_book') }); })