2017-02-20 18:32:27 +00:00
|
|
|
import nunjucks from 'nunjucks';
|
|
|
|
import * as extensions from './extensions';
|
2017-06-01 02:04:27 +00:00
|
|
|
import BaseExtension from './base-extension';
|
2017-02-20 18:32:27 +00:00
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
// Some constants
|
|
|
|
export const RENDER_ALL = 'all';
|
|
|
|
export const RENDER_VARS = 'variables';
|
|
|
|
export const RENDER_TAGS = 'tags';
|
|
|
|
|
2017-05-15 20:55:05 +00:00
|
|
|
// Cached globals
|
|
|
|
let nunjucksVariablesOnly = null;
|
2017-07-11 01:05:54 +00:00
|
|
|
let nunjucksTagsOnly = null;
|
|
|
|
let nunjucksAll = null;
|
2017-05-15 20:55:05 +00:00
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
/**
|
|
|
|
* Render text based on stuff
|
|
|
|
* @param {String} text - Nunjucks template in text form
|
|
|
|
* @param {Object} [config] - Config options for rendering
|
|
|
|
* @param {Object} [config.context] - Context to render with
|
2017-03-28 22:45:23 +00:00
|
|
|
* @param {Object} [config.path] - Path to include in the error message
|
2017-07-11 01:05:54 +00:00
|
|
|
* @param {Object} [config.renderMode] - Only render variables (not tags)
|
2017-02-20 18:32:27 +00:00
|
|
|
*/
|
2017-03-09 06:23:23 +00:00
|
|
|
export function render (text, config = {}) {
|
2017-02-20 18:32:27 +00:00
|
|
|
const context = config.context || {};
|
2017-03-28 22:45:23 +00:00
|
|
|
const path = config.path || null;
|
2017-07-11 01:05:54 +00:00
|
|
|
const renderMode = config.renderMode || RENDER_ALL;
|
2017-02-20 18:32:27 +00:00
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-07-11 01:05:54 +00:00
|
|
|
const nj = getNunjucks(renderMode);
|
2017-05-15 20:55:05 +00:00
|
|
|
|
|
|
|
nj.renderString(text, context, (err, result) => {
|
2017-02-20 18:32:27 +00:00
|
|
|
if (err) {
|
2017-02-27 21:00:13 +00:00
|
|
|
const sanitizedMsg = err.message
|
2017-03-31 21:59:12 +00:00
|
|
|
.replace(/\(unknown path\)\s/, '')
|
2017-03-28 22:45:23 +00:00
|
|
|
.replace(/\[Line \d+, Column \d*]/, '')
|
|
|
|
.replace(/^\s*Error:\s*/, '')
|
|
|
|
.trim();
|
2017-02-27 21:00:13 +00:00
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
const location = err.message.match(/\[Line (\d)+, Column (\d)*]/);
|
|
|
|
const line = location ? parseInt(location[1]) : 1;
|
|
|
|
const column = location ? parseInt(location[2]) : 1;
|
|
|
|
const reason = err.message.includes('attempted to output null or undefined value')
|
|
|
|
? 'undefined'
|
|
|
|
: 'error';
|
|
|
|
|
|
|
|
const newError = new Error(sanitizedMsg);
|
|
|
|
newError.path = path || null;
|
|
|
|
newError.message = sanitizedMsg;
|
|
|
|
newError.location = {line, column};
|
|
|
|
newError.type = 'render';
|
|
|
|
newError.reason = reason;
|
|
|
|
reject(newError);
|
2017-02-20 18:32:27 +00:00
|
|
|
} else {
|
|
|
|
resolve(result);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-01 02:04:27 +00:00
|
|
|
/**
|
|
|
|
* Reload Nunjucks environments. Useful for if plugins change.
|
|
|
|
*/
|
2017-07-11 01:05:54 +00:00
|
|
|
export function reload () {
|
|
|
|
nunjucksAll = null;
|
2017-06-01 02:04:27 +00:00
|
|
|
nunjucksVariablesOnly = null;
|
2017-07-11 01:05:54 +00:00
|
|
|
nunjucksTagsOnly = null;
|
2017-06-01 02:04:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get definitions of template tags
|
|
|
|
*/
|
2017-05-23 22:05:31 +00:00
|
|
|
export function getTagDefinitions () {
|
|
|
|
const env = getNunjucks();
|
|
|
|
|
|
|
|
return Object.keys(env.extensions)
|
|
|
|
.map(k => env.extensions[k])
|
2017-06-01 02:04:27 +00:00
|
|
|
.filter(ext => !ext.isDeprecated())
|
|
|
|
.sort((a, b) => a.getPriority() > b.getPriority() ? 1 : -1)
|
2017-05-23 22:05:31 +00:00
|
|
|
.map(ext => ({
|
|
|
|
name: ext.getTag(),
|
|
|
|
displayName: ext.getName(),
|
|
|
|
description: ext.getDescription(),
|
2017-06-01 02:04:27 +00:00
|
|
|
args: ext.getArgs()
|
|
|
|
}));
|
2017-05-23 22:05:31 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
function getNunjucks (renderMode) {
|
|
|
|
if (renderMode === RENDER_VARS && nunjucksVariablesOnly) {
|
2017-05-15 20:55:05 +00:00
|
|
|
return nunjucksVariablesOnly;
|
|
|
|
}
|
2017-02-27 21:00:13 +00:00
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
if (renderMode === RENDER_TAGS && nunjucksTagsOnly) {
|
|
|
|
return nunjucksTagsOnly;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (renderMode === RENDER_ALL && nunjucksAll) {
|
|
|
|
return nunjucksAll;
|
2017-05-15 20:55:05 +00:00
|
|
|
}
|
2017-02-20 18:32:27 +00:00
|
|
|
|
2017-05-15 20:55:05 +00:00
|
|
|
// ~~~~~~~~~~~~ //
|
|
|
|
// Setup Config //
|
|
|
|
// ~~~~~~~~~~~~ //
|
2017-02-20 18:32:27 +00:00
|
|
|
|
2017-05-15 20:55:05 +00:00
|
|
|
const config = {
|
|
|
|
autoescape: false, // Don't escape HTML
|
|
|
|
throwOnUndefined: true, // Strict mode
|
|
|
|
tags: {
|
|
|
|
blockStart: '{%',
|
|
|
|
blockEnd: '%}',
|
|
|
|
variableStart: '{{',
|
|
|
|
variableEnd: '}}',
|
|
|
|
commentStart: '{#',
|
|
|
|
commentEnd: '#}'
|
2017-02-20 18:32:27 +00:00
|
|
|
}
|
2017-05-15 20:55:05 +00:00
|
|
|
};
|
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
if (renderMode === RENDER_VARS) {
|
2017-05-15 20:55:05 +00:00
|
|
|
// Set tag syntax to something that will never happen naturally
|
|
|
|
config.tags.blockStart = '<[{[{[{[{[$%';
|
|
|
|
config.tags.blockEnd = '%$]}]}]}]}]>';
|
2017-02-20 18:32:27 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
if (renderMode === RENDER_TAGS) {
|
|
|
|
// Set tag syntax to something that will never happen naturally
|
|
|
|
config.tags.variableStart = '<[{[{[{[{[$%';
|
|
|
|
config.tags.variableEnd = '%$]}]}]}]}]>';
|
|
|
|
}
|
|
|
|
|
2017-05-15 20:55:05 +00:00
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
// Create Env with Extensions //
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
|
2017-02-27 21:00:13 +00:00
|
|
|
|
2017-05-15 20:55:05 +00:00
|
|
|
const nj = nunjucks.configure(config);
|
2017-02-27 21:00:13 +00:00
|
|
|
|
2017-06-01 02:04:27 +00:00
|
|
|
const allExtensions = extensions.all();
|
|
|
|
for (let i = 0; i < allExtensions.length; i++) {
|
|
|
|
const ext = allExtensions[i];
|
|
|
|
ext.priority = ext.priority || i * 100;
|
|
|
|
const instance = new BaseExtension(ext);
|
2017-06-09 01:10:12 +00:00
|
|
|
nj.addExtension(instance.getTag(), instance);
|
2017-07-11 01:05:54 +00:00
|
|
|
|
|
|
|
// Hidden helper filter to debug complicated things
|
|
|
|
// eg. `{{ foo | urlencode | debug | upper }}`
|
|
|
|
nj.addFilter('debug', o => {
|
|
|
|
console.log('DEBUG', {o}, JSON.stringify(o));
|
|
|
|
return o;
|
|
|
|
});
|
2017-05-15 20:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
// Cache Env and Return //
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
if (renderMode === RENDER_VARS) {
|
2017-05-15 20:55:05 +00:00
|
|
|
nunjucksVariablesOnly = nj;
|
2017-07-11 01:05:54 +00:00
|
|
|
} else if (renderMode === RENDER_TAGS) {
|
|
|
|
nunjucksTagsOnly = nj;
|
2017-05-15 20:55:05 +00:00
|
|
|
} else {
|
2017-07-11 01:05:54 +00:00
|
|
|
nunjucksAll = nj;
|
2017-02-27 21:00:13 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 20:55:05 +00:00
|
|
|
return nj;
|
2017-02-27 21:00:13 +00:00
|
|
|
}
|