Files
pdfer/templates.js
Thomas Ruoff 1be838f589 initial commit
2017-02-14 22:26:13 +01:00

31 lines
840 B
JavaScript

const fs = require('fs');
const path = require('path');
function replaceOption(string, key, value) {
const re = new RegExp(`\\$\{${key}\}`, 'g');
return string.replace(re, value);
}
function cleanUnsetVars(string) {
const re = new RegExp('\\$\{[a-zA-Z-_]+\}', 'g');
return string.replace(re, '');
}
module.exports.get = (templateName, options, callback) => {
const filename = path.join(__dirname, 'templates', templateName + '.template');
fs.readFile(filename, 'utf-8', (err, template) => {
if (err) {
callback(err);
return;
}
const replaced = Object.keys(options).reduce((memo, key) => {
return replaceOption(memo, key, options[key]);
}, template);
const result = cleanUnsetVars(replaced);
callback(null, result);
});
};