summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/constants/defaultBlocks.js14
-rw-r--r--lib/models/plugin.js6
-rw-r--r--lib/models/templateBlock.js9
-rw-r--r--lib/models/templateEngine.js22
-rw-r--r--lib/output/createTemplateEngine.js16
-rw-r--r--lib/output/getModifiers.js42
-rw-r--r--lib/output/modifiers/highlightCode.js48
-rw-r--r--lib/plugins/index.js4
-rw-r--r--lib/plugins/listBlocks.js17
-rw-r--r--lib/plugins/listFilters.js17
-rw-r--r--lib/plugins/listResources.js2
11 files changed, 134 insertions, 63 deletions
diff --git a/lib/constants/defaultBlocks.js b/lib/constants/defaultBlocks.js
index ed19115..74d1f1f 100644
--- a/lib/constants/defaultBlocks.js
+++ b/lib/constants/defaultBlocks.js
@@ -1,15 +1,15 @@
var Immutable = require('immutable');
var TemplateBlock = require('../models/templateBlock');
-module.exports = Immutable.List([
- TemplateBlock({
+module.exports = Immutable.Map({
+ html: TemplateBlock({
name: 'html',
process: function(blk) {
return blk;
}
}),
- TemplateBlock({
+ code: TemplateBlock({
name: 'code',
process: function(blk) {
return {
@@ -19,7 +19,7 @@ module.exports = Immutable.List([
}
}),
- TemplateBlock({
+ markdown: TemplateBlock({
name: 'markdown',
process: function(blk) {
return this.book.renderInline('markdown', blk.body)
@@ -29,7 +29,7 @@ module.exports = Immutable.List([
}
}),
- TemplateBlock({
+ asciidoc: TemplateBlock({
name: 'asciidoc',
process: function(blk) {
return this.book.renderInline('asciidoc', blk.body)
@@ -39,7 +39,7 @@ module.exports = Immutable.List([
}
}),
- TemplateBlock({
+ markup: TemplateBlock({
name: 'markup',
process: function(blk) {
return this.book.renderInline(this.ctx.file.type, blk.body)
@@ -48,4 +48,4 @@ module.exports = Immutable.List([
});
}
})
-]);
+});
diff --git a/lib/models/plugin.js b/lib/models/plugin.js
index 909ca0b..dd7bc90 100644
--- a/lib/models/plugin.js
+++ b/lib/models/plugin.js
@@ -100,7 +100,7 @@ Plugin.prototype.getFilters = function() {
/**
Return map of blocks
- @return {List<TemplateBlock>}
+ @return {Map<String:TemplateBlock>}
*/
Plugin.prototype.getBlocks = function() {
var blocks = this.getContent().get('blocks');
@@ -109,9 +109,7 @@ Plugin.prototype.getBlocks = function() {
return blocks
.map(function(block, blockName) {
return TemplateBlock.create(blockName, block);
- })
- .valueSeq()
- .toList();
+ });
};
/**
diff --git a/lib/models/templateBlock.js b/lib/models/templateBlock.js
index 4094b89..92a0bf3 100644
--- a/lib/models/templateBlock.js
+++ b/lib/models/templateBlock.js
@@ -1,4 +1,5 @@
var is = require('is');
+var extend = require('extend');
var Immutable = require('immutable');
var Promise = require('../utils/promise');
@@ -72,7 +73,7 @@ TemplateBlock.prototype.getExtensionName = function() {
@return {Nunjucks.Extension}
*/
-TemplateBlock.prototype.toNunjucksExt = function() {
+TemplateBlock.prototype.toNunjucksExt = function(mainContext) {
var that = this;
var name = this.getName();
var endTag = this.getEndTag();
@@ -175,7 +176,11 @@ TemplateBlock.prototype.toNunjucksExt = function() {
Promise()
.then(function() {
- return that.applyBlock(mainBlock, context);
+ var ctx = extend({
+ ctx: context
+ }, mainContext || {});
+
+ return that.applyBlock(mainBlock, ctx);
})
.then(function(result) {
return that.blockResultToHtml(result);
diff --git a/lib/models/templateEngine.js b/lib/models/templateEngine.js
index 114aa73..0d0a015 100644
--- a/lib/models/templateEngine.js
+++ b/lib/models/templateEngine.js
@@ -2,10 +2,10 @@ var nunjucks = require('nunjucks');
var Immutable = require('immutable');
var TemplateEngine = Immutable.Record({
- // List of {TemplateBlock}
- blocks: Immutable.List(),
+ // Map of {TemplateBlock}
+ blocks: Immutable.Map(),
- // List of Extension
+ // Map of Extension
extensions: Immutable.Map(),
// Map of filters: {String} name -> {Function} fn
@@ -68,12 +68,12 @@ TemplateEngine.prototype.getBlock = function(name) {
@return {Nunjucks.Environment}
*/
TemplateEngine.prototype.toNunjucks = function() {
- var that = this;
var loader = this.getLoader();
var blocks = this.getBlocks();
var filters = this.getFilters();
var globals = this.getGlobals();
var extensions = this.getExtensions();
+ var context = this.getContext();
var env = new nunjucks.Environment(
loader,
@@ -95,13 +95,13 @@ TemplateEngine.prototype.toNunjucks = function() {
// Add filters
filters.forEach(function(filterFn, filterName) {
- env.addFilter(filterName, that.bindToContext(filterFn));
+ env.addFilter(filterName, filterFn.bind(context));
});
// Add blocks
blocks.forEach(function(block) {
var extName = block.getExtensionName();
- var Ext = block.toNunjucksExt();
+ var Ext = block.toNunjucksExt(context);
env.addExtension(extName, new Ext());
});
@@ -120,16 +120,6 @@ TemplateEngine.prototype.toNunjucks = function() {
};
/**
- Bind a function to the context
-
- @param {Function} fn
- @return {Function}
-*/
-TemplateEngine.prototype.bindToContext = function(fn) {
- return fn.bind(this.getContext());
-};
-
-/**
Create a template engine
@param {Object} def
diff --git a/lib/output/createTemplateEngine.js b/lib/output/createTemplateEngine.js
index 9b29b2a..523c850 100644
--- a/lib/output/createTemplateEngine.js
+++ b/lib/output/createTemplateEngine.js
@@ -3,6 +3,8 @@ var Immutable = require('immutable');
var Templating = require('../templating');
var TemplateEngine = require('../models/templateEngine');
+var Plugins = require('../plugins');
+
var defaultBlocks = require('../constants/defaultBlocks');
var defaultFilters = require('../constants/defaultFilters');
@@ -19,19 +21,11 @@ function createTemplateEngine(output) {
var rootFolder = book.getContentRoot();
var logger = book.getLogger();
- var filters = plugins
- .reduce(function(result, plugin) {
- return result.merge(plugin.getFilters());
- }, Immutable.Map());
-
- var blocks = plugins
- .map(function(plugin) {
- return plugin.getBlocks();
- })
- .flatten(1);
+ var filters = Plugins.listFilters(plugins);
+ var blocks = Plugins.listBlocks(plugins);
// Extend with default
- blocks = defaultBlocks.concat(blocks);
+ blocks = defaultBlocks.merge(blocks);
filters = defaultFilters.merge(filters);
// Create loader
diff --git a/lib/output/getModifiers.js b/lib/output/getModifiers.js
index 34a0b9e..1dd62ba 100644
--- a/lib/output/getModifiers.js
+++ b/lib/output/getModifiers.js
@@ -1,25 +1,61 @@
var Modifiers = require('./modifiers');
var resolveFileToURL = require('./helper/resolveFileToURL');
+var Api = require('../api');
+var Plugins = require('../plugins');
+var Promise = require('../utils/promise');
+var defaultBlocks = require('../constants/defaultBlocks');
+
+var CODEBLOCK = 'code';
/**
- Return default modifier to prepare a page
+ Return default modifier to prepare a page for
+ rendering.
@return <Array>
*/
function getModifiers(output, page) {
var book = output.getBook();
+ var plugins = output.getPlugins();
var glossary = book.getGlossary();
var entries = glossary.getEntries();
-
var file = page.getFile();
+ // Get TemplateBlock for highlighting
+ var blocks = Plugins.listBlocks(plugins);
+ var code = blocks.get(CODEBLOCK) || defaultBlocks.get(CODEBLOCK);
+
+ // Current context
+ var context = Api.encodeGlobal(output);
+
return [
+ // Normalize IDs on headings
Modifiers.addHeadingId,
+
+ // Resolve links (.md -> .html)
Modifiers.resolveLinks.bind(null,
file.getPath(),
resolveFileToURL.bind(null, output)
),
- Modifiers.annotateText.bind(null, entries)
+
+ // Annotate text with glossary entries
+ Modifiers.annotateText.bind(null, entries),
+
+ // Highlight code blocks using "code" block
+ Modifiers.highlightCode.bind(null, function(lang, source) {
+ return Promise(code.applyBlock({
+ body: source,
+ kwargs: {
+ language: lang
+ }
+ }, context))
+ .then(function(result) {
+ if (result.html === false) {
+ return { text: result.body };
+ } else {
+ return { html: result.body };
+ }
+ });
+ })
];
}
diff --git a/lib/output/modifiers/highlightCode.js b/lib/output/modifiers/highlightCode.js
index deabd68..dcd9d24 100644
--- a/lib/output/modifiers/highlightCode.js
+++ b/lib/output/modifiers/highlightCode.js
@@ -1,7 +1,35 @@
+var is = require('is');
var Promise = require('../../utils/promise');
var editHTMLElement = require('./editHTMLElement');
/**
+ Return language for a code blocks from a list of class names
+
+ @param {Array<String>}
+ @return {String}
+*/
+function getLanguageForClass(classNames) {
+ return classNames
+ .map(function(cl) {
+ // Markdown
+ if (cl.search('lang-') === 0) {
+ return cl.slice('lang-'.length);
+ }
+
+ // Asciidoc
+ if (cl.search('language-') === 0) {
+ return cl.slice('language-'.length);
+ }
+
+ return null;
+ })
+ .find(function(cl) {
+ return Boolean(cl);
+ });
+}
+
+
+/**
Highlight all code elements
@param {Function(lang, body) -> String} highlight
@@ -11,28 +39,12 @@ var editHTMLElement = require('./editHTMLElement');
function highlightCode(highlight, $) {
return editHTMLElement($, 'code', function($code) {
var classNames = ($code.attr('class') || '').split(' ');
- var lang = classNames
- .map(function(cl) {
- // Markdown
- if (cl.search('lang-') === 0) {
- return cl.slice('lang-'.length);
- }
-
- // Asciidoc
- if (cl.search('language-') === 0) {
- return cl.slice('language-'.length);
- }
-
- return null;
- })
- .find(function(cl) {
- return Boolean(cl);
- });
+ var lang = getLanguageForClass(classNames);
var source = $code.text();
return Promise(highlight(lang, source))
.then(function(r) {
- if (r.html) {
+ if (is.string(r.html)) {
$code.html(r.html);
} else {
$code.text(r.text);
diff --git a/lib/plugins/index.js b/lib/plugins/index.js
index 8842342..607a7f1 100644
--- a/lib/plugins/index.js
+++ b/lib/plugins/index.js
@@ -3,6 +3,8 @@ module.exports = {
loadForBook: require('./loadForBook'),
validateConfig: require('./validateConfig'),
installPlugins: require('./installPlugins'),
- listResources: require('./listResources')
+ listResources: require('./listResources'),
+ listBlocks: require('./listBlocks'),
+ listFilters: require('./listFilters')
};
diff --git a/lib/plugins/listBlocks.js b/lib/plugins/listBlocks.js
new file mode 100644
index 0000000..f738937
--- /dev/null
+++ b/lib/plugins/listBlocks.js
@@ -0,0 +1,17 @@
+var Immutable = require('immutable');
+
+/**
+ List blocks from a list of plugins
+
+ @param {OrderedMap<String:Plugin>}
+ @return {Map<String:TemplateBlock>}
+*/
+function listBlocks(plugins) {
+ return plugins
+ .reverse()
+ .reduce(function(result, plugin) {
+ return result.merge(plugin.getBlocks());
+ }, Immutable.Map());
+}
+
+module.exports = listBlocks;
diff --git a/lib/plugins/listFilters.js b/lib/plugins/listFilters.js
new file mode 100644
index 0000000..4d8a471
--- /dev/null
+++ b/lib/plugins/listFilters.js
@@ -0,0 +1,17 @@
+var Immutable = require('immutable');
+
+/**
+ List filters from a list of plugins
+
+ @param {OrderedMap<String:Plugin>}
+ @return {Map<String:Function>}
+*/
+function listFilters(plugins) {
+ return plugins
+ .reverse()
+ .reduce(function(result, plugin) {
+ return result.merge(plugin.getFilters());
+ }, Immutable.Map());
+}
+
+module.exports = listFilters;
diff --git a/lib/plugins/listResources.js b/lib/plugins/listResources.js
index b4a16cb..4a73a2c 100644
--- a/lib/plugins/listResources.js
+++ b/lib/plugins/listResources.js
@@ -5,7 +5,7 @@ var LocationUtils = require('../utils/location');
var PLUGIN_RESOURCES = require('../constants/pluginResources');
/**
- Encode plugins as JSON
+ List all resources from a list of plugins
@param {OrderedMap<String:Plugin>}
@param {String} type