diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-05-05 11:00:05 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-05-05 11:00:05 +0200 |
commit | 34947b5e207e84ef43a8194c0ec7bac19dccf709 (patch) | |
tree | 06e687700d95590cae678f8bef381e8a62dc0129 /lib/templating | |
parent | c621380b664bcbef087df571b662e7a34e098168 (diff) | |
download | gitbook-34947b5e207e84ef43a8194c0ec7bac19dccf709.zip gitbook-34947b5e207e84ef43a8194c0ec7bac19dccf709.tar.gz gitbook-34947b5e207e84ef43a8194c0ec7bac19dccf709.tar.bz2 |
Add mode TemplateOutput to represent {content + blocks} from template
Diffstat (limited to 'lib/templating')
-rw-r--r-- | lib/templating/postRender.js | 39 | ||||
-rw-r--r-- | lib/templating/render.js | 16 | ||||
-rw-r--r-- | lib/templating/renderFile.js | 2 | ||||
-rw-r--r-- | lib/templating/replaceBlocks.js | 34 |
4 files changed, 46 insertions, 45 deletions
diff --git a/lib/templating/postRender.js b/lib/templating/postRender.js index c4e82a5..2662814 100644 --- a/lib/templating/postRender.js +++ b/lib/templating/postRender.js @@ -1,5 +1,27 @@ var Promise = require('../utils/promise'); -var replaceBlocks = require('./replaceBlocks'); + + +/** + Replace position markers of blocks by body after processing + This is done to avoid that markdown/asciidoc processer parse the block content + + @param {String} content + @return {Object} {blocks: Set, content: String} +*/ +function replaceBlocks(content, blocks) { + var newContent = content.replace(/\{\{\-\%([\s\S]+?)\%\-\}\}/g, function(match, key) { + var replacedWith = match; + + var block = blocks.get(key); + if (block) { + replacedWith = replaceBlocks(block.get('body'), blocks); + } + + return replacedWith; + }); + + return newContent; +} /** Post render a template: @@ -7,22 +29,25 @@ var replaceBlocks = require('./replaceBlocks'); - Replace block content @param {TemplateEngine} engine - @param {String} content + @param {TemplateOutput} content @return {Promise<String>} */ -function postRender(engine, content) { +function postRender(engine, output) { + var content = output.getContent(); + var blocks = output.getBlocks(); + var result = replaceBlocks(content); - return Promise.forEach(result.blocks, function(blockType) { - var block = engine.getBlock(blockType); - var post = block.getPost(); + return Promise.forEach(blocks, function(block) { + var post = block.get('post'); + if (!post) { return; } return post(); }) - .thenResolve(result.content); + .thenResolve(result); } module.exports = postRender; diff --git a/lib/templating/render.js b/lib/templating/render.js index 22c0dc4..584890a 100644 --- a/lib/templating/render.js +++ b/lib/templating/render.js @@ -1,6 +1,6 @@ var Promise = require('../utils/promise'); var timing = require('../utils/timing'); - +var TemplateOutput = require('../models/templateOutput'); var replaceShortcuts = require('./replaceShortcuts'); /** @@ -10,16 +10,23 @@ var replaceShortcuts = require('./replaceShortcuts'); @param {String} filePath @param {String} content @param {Object} context - @return {Promise<String>} + @return {Promise<TemplateOutput>} */ function renderTemplate(engine, filePath, content, context) { context = context || {}; - var env = engine.toNunjucks(); + // Mutable objects to contains all blocks requiring post-processing + var blocks = {}; + + // Create nunjucks environment + var env = engine.toNunjucks(blocks); + + // Replace shortcuts from plugin's blocks content = replaceShortcuts(engine, filePath, content); return timing.measure( 'template.render', + Promise.nfcall( env.renderString.bind(env), content, @@ -28,6 +35,9 @@ function renderTemplate(engine, filePath, content, context) { path: filePath } ) + .then(function(content) { + return TemplateOutput.create(content, blocks); + }) ); } diff --git a/lib/templating/renderFile.js b/lib/templating/renderFile.js index 9b74e5b..185bec1 100644 --- a/lib/templating/renderFile.js +++ b/lib/templating/renderFile.js @@ -8,7 +8,7 @@ var render = require('./render'); @param {TemplateEngine} engine @param {String} filePath @param {Object} context - @return {Promise<String>} + @return {Promise<TemplateOutput>} */ function renderTemplateFile(engine, filePath, context) { var loader = engine.getLoader(); diff --git a/lib/templating/replaceBlocks.js b/lib/templating/replaceBlocks.js deleted file mode 100644 index 4b1c37f..0000000 --- a/lib/templating/replaceBlocks.js +++ /dev/null @@ -1,34 +0,0 @@ -var Immutable = require('immutable'); -var TemplateBlock = require('../models/templateBlock'); - -/** - Replace position markers of blocks by body after processing - This is done to avoid that markdown/asciidoc processer parse the block content - - @param {String} content - @return {Object} {blocks: Set, content: String} -*/ -function replaceBlocks(content) { - var blockTypes = new Immutable.Set(); - var newContent = content.replace(/\{\{\-\%([\s\S]+?)\%\-\}\}/g, function(match, key) { - var replacedWith = match; - - var block = TemplateBlock.getBlockResultByKey(key); - if (block) { - var result = replaceBlocks(block.body); - - blockTypes = blockTypes.add(block.name); - blockTypes = blockTypes.concat(result.blocks); - replacedWith = result.content; - } - - return replacedWith; - }); - - return { - content: newContent, - blocks: blockTypes - }; -} - -module.exports = replaceBlocks; |