salix/services/print/application/template.js

176 lines
5.5 KiB
JavaScript

var fs = require('fs');
var mustache = require('mustache');
var locale = require('./locale.js');
var inlineCss = require('inline-css');
var path = require('path');
module.exports = {
/**
* Get template.
* @param {String} template - Template name
* @param {Object} countryCode - Language code
* @param {Object} params - Params
* @param {Object} cb - Callback
*/
get: function(template, params, cb) {
var templatePath = path.join(__dirname, 'template', `${template}`, `index.html`);
var classPath = path.join(__dirname, 'template', `${template}`, `${template}.js`);
var stylePath = path.join(__dirname, 'template', `${template}`, 'static', 'css', 'style.css');
fs.stat(templatePath, (error, stat) => {
if (error)
return cb(new Error('Template ' + template + ' not found'));
let TemplateClass = require(classPath);
let instance = new TemplateClass();
let getRenderedStyles = (error, body) => {
if (error)
return cb(error);
this.renderStyles(stylePath, body, (error, body) => {
if (error)
return cb(error);
// Check if has a subject param
params.subject = params.subject || instance.subject;
if (params.subject == undefined) {
// Try to find a subject from Html source
let title = body.match(new RegExp('<title>(.*?)</title>', 'i'));
if (title)
params.subject = title[1];
}
this.renderImages(template, body, (error, body) => {
if (error)
return cb(error);
cb(null, {body: body});
});
});
};
let getDataCb = () => {
this.render(templatePath, instance, (error, result) => getRenderedStyles(error, result));
};
instance.getData(params, (error, result) => {
if (error)
return cb(error);
locale.load(template, instance.countryCode, (error, result) => {
if (error)
return cb(error);
instance._ = result.locale;
getDataCb(null, result);
});
});
});
},
/**
* Render template
* @param {String} path - Template path
* @param {Object} data - Params
* @param {Object} cb - Callback
*/
render: function(path, data, cb) {
fs.readFile(path, 'utf8', (error, body) => {
// Find matching sub-templates
let regexp = new RegExp(/\{\{\$\.(.*?)\}\}/, 'ig');
let subTpl = body.match(regexp);
if (!subTpl) {
mustache.parse(body);
return cb(null, mustache.render(body, data));
}
let parentBody = body;
this.renderSub(parentBody, subTpl, data, regexp, (error, body) => {
if (error)
return cb(error);
mustache.parse(body);
cb(null, mustache.render(body, data));
});
});
},
renderSub: function(body, subTpl, data, regexp, cb) {
let index = 1;
subTpl.forEach(keyName => {
subTplName = keyName.replace(regexp, '$1');
this.get(subTplName, data, (error, result) => {
if (error)
return cb(error);
let subTplBody = result.body;
body = body.replace(keyName, subTplBody);
if (index === subTpl.length)
cb(null, body);
index++;
});
});
},
/**
* Render template style.
* @param {String} path - Stylesheet path
* @param {String} body - Rendered html
* @param {Object} cb - Callback
*/
renderStyles: function(stylePath, html, cb) {
// Common components
let comPath = path.join(__dirname, '../', 'static', 'css', 'component.css');
fs.readFile(comPath, 'utf8', (error, comCss) => {
fs.stat(stylePath, error => {
if (error)
return cb(new Error('Template stylesheet not found'));
fs.readFile(stylePath, 'utf8', (error, css) => {
let style = '<style>' + comCss + css + '</style>';
let body = style + html;
let options = {url: ' '};
inlineCss(body, options)
.then(function(body) {
cb(null, body);
});
});
});
});
},
/**
* Render template images
* @param {String} template - Template name
* @param {String} body - template body
* @param {Object} cb - Callback
*/
renderImages: function(template, body, cb) {
let tplImages = body.match(new RegExp('src="cid:(.*?)"', 'ig'));
if (!tplImages)
tplImages = {};
// Template default attachments
for (var i = 0; i < tplImages.length; i++) {
let name = tplImages[i].replace('src="cid:', '').replace('"', '');
let imagePath = path.join(__dirname, 'template', `${template}`, 'static', 'image', name);
body = body.replace(tplImages[i], `src="file:///${imagePath}"`);
}
cb(null, body);
}
};