|
|
@ -1,77 +1,53 @@
|
|
|
|
'use strict';
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
|
|
var async = require('async');
|
|
|
|
const util = require('util');
|
|
|
|
var nconf = require('nconf');
|
|
|
|
const nconf = require('nconf');
|
|
|
|
var validator = require('validator');
|
|
|
|
const validator = require('validator');
|
|
|
|
var winston = require('winston');
|
|
|
|
const winston = require('winston');
|
|
|
|
|
|
|
|
|
|
|
|
var plugins = require('../plugins');
|
|
|
|
const plugins = require('../plugins');
|
|
|
|
var meta = require('../meta');
|
|
|
|
const meta = require('../meta');
|
|
|
|
var translator = require('../translator');
|
|
|
|
const translator = require('../translator');
|
|
|
|
var widgets = require('../widgets');
|
|
|
|
const widgets = require('../widgets');
|
|
|
|
var utils = require('../utils');
|
|
|
|
const utils = require('../utils');
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = function (middleware) {
|
|
|
|
module.exports = function (middleware) {
|
|
|
|
|
|
|
|
const renderHeaderFooterAsync = util.promisify(renderHeaderFooter);
|
|
|
|
|
|
|
|
|
|
|
|
middleware.processRender = function processRender(req, res, next) {
|
|
|
|
middleware.processRender = function processRender(req, res, next) {
|
|
|
|
// res.render post-processing, modified from here: https://gist.github.com/mrlannigan/5051687
|
|
|
|
// res.render post-processing, modified from here: https://gist.github.com/mrlannigan/5051687
|
|
|
|
var render = res.render;
|
|
|
|
const render = res.render;
|
|
|
|
res.render = function renderOverride(template, options, fn) {
|
|
|
|
res.render = async function renderOverride(template, options, fn) {
|
|
|
|
var self = this;
|
|
|
|
const self = this;
|
|
|
|
var req = this.req;
|
|
|
|
const req = this.req;
|
|
|
|
var defaultFn = function (err, str) {
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
|
|
return next(err);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
self.send(str);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
options = options || {};
|
|
|
|
options = options || {};
|
|
|
|
if (typeof options === 'function') {
|
|
|
|
if (typeof options === 'function') {
|
|
|
|
fn = options;
|
|
|
|
fn = options;
|
|
|
|
options = {};
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (typeof fn !== 'function') {
|
|
|
|
|
|
|
|
fn = defaultFn;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var ajaxifyData;
|
|
|
|
|
|
|
|
var templateToRender;
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
|
|
|
function (next) {
|
|
|
|
|
|
|
|
options.loggedIn = req.uid > 0;
|
|
|
|
options.loggedIn = req.uid > 0;
|
|
|
|
options.relative_path = nconf.get('relative_path');
|
|
|
|
options.relative_path = nconf.get('relative_path');
|
|
|
|
options.template = { name: template };
|
|
|
|
options.template = { name: template, [template]: true };
|
|
|
|
options.template[template] = true;
|
|
|
|
|
|
|
|
options.url = (req.baseUrl + req.path.replace(/^\/api/, ''));
|
|
|
|
options.url = (req.baseUrl + req.path.replace(/^\/api/, ''));
|
|
|
|
options.bodyClass = buildBodyClass(req, res, options);
|
|
|
|
options.bodyClass = buildBodyClass(req, res, options);
|
|
|
|
plugins.fireHook('filter:' + template + '.build', { req: req, res: res, templateData: options }, next);
|
|
|
|
|
|
|
|
},
|
|
|
|
const buildResult = await plugins.fireHook('filter:' + template + '.build', { req: req, res: res, templateData: options });
|
|
|
|
function (data, next) {
|
|
|
|
const templateToRender = buildResult.templateData.templateToRender || template;
|
|
|
|
templateToRender = data.templateData.templateToRender || template;
|
|
|
|
|
|
|
|
plugins.fireHook('filter:middleware.render', { req: req, res: res, templateData: data.templateData }, next);
|
|
|
|
const renderResult = await plugins.fireHook('filter:middleware.render', { req: req, res: res, templateData: buildResult.templateData });
|
|
|
|
},
|
|
|
|
options = renderResult.templateData;
|
|
|
|
function parseTags(data, next) {
|
|
|
|
|
|
|
|
meta.tags.parse(req, data, res.locals.metaTags, res.locals.linkTags, function (err, tags) {
|
|
|
|
|
|
|
|
options._header = {
|
|
|
|
options._header = {
|
|
|
|
tags: tags,
|
|
|
|
tags: await meta.tags.parse(req, renderResult, res.locals.metaTags, res.locals.linkTags),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
next(err, data);
|
|
|
|
options.widgets = await widgets.render(req.uid, {
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
function (data, next) {
|
|
|
|
|
|
|
|
options = data.templateData;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
widgets.render(req.uid, {
|
|
|
|
|
|
|
|
template: template + '.tpl',
|
|
|
|
template: template + '.tpl',
|
|
|
|
url: options.url,
|
|
|
|
url: options.url,
|
|
|
|
templateData: options,
|
|
|
|
templateData: options,
|
|
|
|
req: req,
|
|
|
|
req: req,
|
|
|
|
res: res,
|
|
|
|
res: res,
|
|
|
|
}, next);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
function (data, next) {
|
|
|
|
|
|
|
|
options.widgets = data;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res.locals.template = template;
|
|
|
|
res.locals.template = template;
|
|
|
|
options._locals = undefined;
|
|
|
|
options._locals = undefined;
|
|
|
|
|
|
|
|
|
|
|
@ -82,37 +58,32 @@ module.exports = function (middleware) {
|
|
|
|
req.app.set('json spaces', global.env === 'development' || req.query.pretty ? 4 : 0);
|
|
|
|
req.app.set('json spaces', global.env === 'development' || req.query.pretty ? 4 : 0);
|
|
|
|
return res.json(options);
|
|
|
|
return res.json(options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ajaxifyData = JSON.stringify(options).replace(/<\//g, '<\\/');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const renderAsync = util.promisify((templateToRender, options, next) => render.call(self, templateToRender, options, next));
|
|
|
|
|
|
|
|
|
|
|
|
ajaxifyData = JSON.stringify(options).replace(/<\//g, '<\\/');
|
|
|
|
const results = await utils.promiseParallel({
|
|
|
|
|
|
|
|
header: renderHeaderFooterAsync('renderHeader', req, res, options),
|
|
|
|
async.parallel({
|
|
|
|
content: renderAsync(templateToRender, options),
|
|
|
|
header: function (next) {
|
|
|
|
footer: renderHeaderFooterAsync('renderFooter', req, res, options),
|
|
|
|
renderHeaderFooter('renderHeader', req, res, options, next);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
content: function (next) {
|
|
|
|
const str = results.header +
|
|
|
|
render.call(self, templateToRender, options, next);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
footer: function (next) {
|
|
|
|
|
|
|
|
renderHeaderFooter('renderFooter', req, res, options, next);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
}, next);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
function (results, next) {
|
|
|
|
|
|
|
|
var str = results.header +
|
|
|
|
|
|
|
|
(res.locals.postHeader || '') +
|
|
|
|
(res.locals.postHeader || '') +
|
|
|
|
results.content + '<script id="ajaxify-data"></script>' +
|
|
|
|
results.content + '<script id="ajaxify-data"></script>' +
|
|
|
|
(res.locals.preFooter || '') +
|
|
|
|
(res.locals.preFooter || '') +
|
|
|
|
results.footer;
|
|
|
|
results.footer;
|
|
|
|
|
|
|
|
|
|
|
|
translate(str, req, res, next);
|
|
|
|
let translated = await translate(str, req, res);
|
|
|
|
},
|
|
|
|
|
|
|
|
function (translated, next) {
|
|
|
|
|
|
|
|
translated = translated.replace('<script id="ajaxify-data"></script>', function () {
|
|
|
|
translated = translated.replace('<script id="ajaxify-data"></script>', function () {
|
|
|
|
return '<script id="ajaxify-data" type="application/json">' + ajaxifyData + '</script>';
|
|
|
|
return '<script id="ajaxify-data" type="application/json">' + ajaxifyData + '</script>';
|
|
|
|
});
|
|
|
|
});
|
|
|
|
next(null, translated);
|
|
|
|
|
|
|
|
},
|
|
|
|
if (typeof fn !== 'function') {
|
|
|
|
], fn);
|
|
|
|
self.send(translated);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
fn(null, translated);
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
next();
|
|
|
@ -128,20 +99,19 @@ module.exports = function (middleware) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function translate(str, req, res, next) {
|
|
|
|
async function translate(str, req, res) {
|
|
|
|
var language = (res.locals.config && res.locals.config.userLang) || 'en-GB';
|
|
|
|
let language = (res.locals.config && res.locals.config.userLang) || 'en-GB';
|
|
|
|
if (res.locals.renderAdminHeader) {
|
|
|
|
if (res.locals.renderAdminHeader) {
|
|
|
|
language = (res.locals.config && res.locals.config.acpLang) || 'en-GB';
|
|
|
|
language = (res.locals.config && res.locals.config.acpLang) || 'en-GB';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
language = req.query.lang ? validator.escape(String(req.query.lang)) : language;
|
|
|
|
language = req.query.lang ? validator.escape(String(req.query.lang)) : language;
|
|
|
|
translator.translate(str, language, function (translated) {
|
|
|
|
const translated = await translator.translate(str, language);
|
|
|
|
next(null, translator.unescape(translated));
|
|
|
|
return translator.unescape(translated);
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function buildBodyClass(req, res, templateData) {
|
|
|
|
function buildBodyClass(req, res, templateData) {
|
|
|
|
var clean = req.path.replace(/^\/api/, '').replace(/^\/|\/$/g, '');
|
|
|
|
const clean = req.path.replace(/^\/api/, '').replace(/^\/|\/$/g, '');
|
|
|
|
var parts = clean.split('/').slice(0, 3);
|
|
|
|
const parts = clean.split('/').slice(0, 3);
|
|
|
|
parts.forEach(function (p, index) {
|
|
|
|
parts.forEach(function (p, index) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
p = utils.slugify(decodeURIComponent(p));
|
|
|
|
p = utils.slugify(decodeURIComponent(p));
|
|
|
|