summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/models/templateEngine.js24
-rw-r--r--lib/output/generatePage.js3
-rw-r--r--lib/templating/index.js4
-rw-r--r--lib/templating/listShortcuts.js31
-rw-r--r--lib/templating/render.js30
-rw-r--r--lib/templating/replaceShortcuts.js37
6 files changed, 104 insertions, 25 deletions
diff --git a/lib/models/templateEngine.js b/lib/models/templateEngine.js
index aaa70fd..9a18bd4 100644
--- a/lib/models/templateEngine.js
+++ b/lib/models/templateEngine.js
@@ -1,8 +1,6 @@
var nunjucks = require('nunjucks');
var Immutable = require('immutable');
-var Promise = require('../utils/promise');
-
var TemplateEngine = Immutable.Record({
// List of {TemplateBlock}
blocks: Immutable.List(),
@@ -105,26 +103,4 @@ TemplateEngine.prototype.bindToContext = function(fn) {
return fn.bind(this.getContext());
};
-/**
- Render a template
-
- @param {String} filePath
- @param {String} content
- @param {Object} ctx
- @return {Promise<String>}
-*/
-TemplateEngine.prototype.render = function renderTemplate(filePath, content, context) {
- context = context || {};
- var env = this.toNunjucks();
-
- return Promise.nfcall(
- env.renderString.bind(env),
- content,
- context,
- {
- path: filePath
- }
- );
-};
-
module.exports = TemplateEngine;
diff --git a/lib/output/generatePage.js b/lib/output/generatePage.js
index 072c327..4507f2e 100644
--- a/lib/output/generatePage.js
+++ b/lib/output/generatePage.js
@@ -2,6 +2,7 @@ var Promise = require('../utils/promise');
var error = require('../utils/error');
var Parse = require('../parse');
+var Templating = require('../templating');
var createTemplateEngine = require('./createTemplateEngine');
/**
@@ -34,7 +35,7 @@ function generatePage(output, page) {
// Render templating syntax
.then(function(content) {
var engine = createTemplateEngine(output);
- return engine.render(filePath, content);
+ return Templating.render(engine, filePath, content);
})
// Render page using parser (markdown -> HTML)
diff --git a/lib/templating/index.js b/lib/templating/index.js
new file mode 100644
index 0000000..312146a
--- /dev/null
+++ b/lib/templating/index.js
@@ -0,0 +1,4 @@
+
+module.exports = {
+ render: require('./render')
+};
diff --git a/lib/templating/listShortcuts.js b/lib/templating/listShortcuts.js
new file mode 100644
index 0000000..8f2388b
--- /dev/null
+++ b/lib/templating/listShortcuts.js
@@ -0,0 +1,31 @@
+var Immutable = require('immutable');
+var parsers = require('../parsers');
+
+/**
+ Return a list of all shortcuts that can apply
+ to a file for a TemplatEngine
+
+ @param {TemplateEngine} engine
+ @param {String} filePath
+ @return {List<Shortcut>}
+*/
+function listShortcuts(engine, filePath) {
+ var blocks = engine.getBlocks();
+ var parser = parsers.getForFile(filePath);
+ if (!parser) {
+ return Immutable.List();
+ }
+
+ return blocks
+ .map(function(block) {
+ var shortcuts = block.getShortcuts();
+
+ return shortcuts.filter(function(shortcut) {
+ var parsers = shortcut.get('parsers');
+ return parsers.includes(parser.name);
+ });
+ })
+ .flatten(1);
+}
+
+module.exports = listShortcuts;
diff --git a/lib/templating/render.js b/lib/templating/render.js
new file mode 100644
index 0000000..87d5096
--- /dev/null
+++ b/lib/templating/render.js
@@ -0,0 +1,30 @@
+var Promise = require('../utils/promise');
+
+var replaceShortcuts = require('./replaceShortcuts');
+
+/**
+ Render a template
+
+ @param {TemplateEngine} engine
+ @param {String} filePath
+ @param {String} content
+ @param {Object} ctx
+ @return {Promise<String>}
+*/
+function renderTemplate(engine, filePath, content, context) {
+ context = context || {};
+ var env = engine.toNunjucks();
+
+ content = replaceShortcuts(engine, filePath, content);
+
+ return Promise.nfcall(
+ env.renderString.bind(env),
+ content,
+ context,
+ {
+ path: filePath
+ }
+ );
+}
+
+module.exports = renderTemplate;
diff --git a/lib/templating/replaceShortcuts.js b/lib/templating/replaceShortcuts.js
new file mode 100644
index 0000000..f6a51cb
--- /dev/null
+++ b/lib/templating/replaceShortcuts.js
@@ -0,0 +1,37 @@
+var escapeStringRegexp = require('escape-string-regexp');
+var listShortcuts = require('./listShortcuts');
+
+/*
+ Apply a shortcut of block to a template
+ @param {String} content
+ @param {Shortcut} shortcut
+ @return {String}
+*/
+function applyShortcut(content, shortcut) {
+ var tags = shortcut.get('tag');
+ var start = shortcut.get('start');
+ var end = shortcut.get('end');
+
+ var regex = new RegExp(
+ escapeStringRegexp(start) + '([\\s\\S]*?[^\\$])' + escapeStringRegexp(end),
+ 'g'
+ );
+ return content.replace(regex, function(all, match) {
+ return '{% ' + tags.start + ' %}' + match + '{% ' + tags.end + ' %}';
+ });
+}
+
+/**
+ Replace shortcuts from blocks in a string
+
+ @param {TemplateEngine} engine
+ @param {String} filePath
+ @param {String} content
+ @return {String}
+*/
+function replaceShortcuts(engine, filePath, content) {
+ var shortcuts = listShortcuts(engine, filePath);
+ return shortcuts.reduce(applyShortcut, content);
+}
+
+module.exports = replaceShortcuts;