2016-01-27 10:29:26 +00:00
|
|
|
(function() {
|
2018-02-12 05:05:29 +00:00
|
|
|
/**
|
|
|
|
Definition of the UnderscoreGenerator class.
|
|
|
|
@license MIT. See LICENSE.md for details.
|
|
|
|
@module underscore-generator.js
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
Perform template-based resume generation using Underscore.js.
|
|
|
|
@class UnderscoreGenerator
|
|
|
|
*/
|
2016-02-14 01:40:17 +00:00
|
|
|
var UnderscoreGenerator, _, escapeLaTeX, registerHelpers;
|
2016-01-27 10:29:26 +00:00
|
|
|
|
|
|
|
_ = require('underscore');
|
|
|
|
|
|
|
|
registerHelpers = require('../helpers/underscore-helpers');
|
|
|
|
|
2016-02-14 01:40:17 +00:00
|
|
|
require('../utils/string');
|
|
|
|
|
|
|
|
escapeLaTeX = require('escape-latex');
|
|
|
|
|
2016-01-27 10:29:26 +00:00
|
|
|
UnderscoreGenerator = module.exports = {
|
|
|
|
generateSimple: function(data, tpl) {
|
2018-02-12 05:05:29 +00:00
|
|
|
var HMS, err, t;
|
2016-01-27 10:29:26 +00:00
|
|
|
try {
|
2018-02-12 05:05:29 +00:00
|
|
|
// Compile and run the Handlebars template.
|
2016-02-09 20:27:34 +00:00
|
|
|
t = _.template(tpl);
|
|
|
|
return t(data);
|
2018-02-12 05:05:29 +00:00
|
|
|
} catch (error) {
|
|
|
|
err = error;
|
|
|
|
//console.dir _error
|
2016-02-09 20:27:34 +00:00
|
|
|
HMS = require('../core/status-codes');
|
2016-01-27 10:29:26 +00:00
|
|
|
throw {
|
2016-02-09 20:27:34 +00:00
|
|
|
fluenterror: HMS[t ? 'invokeTemplate' : 'compileTemplate'],
|
2018-02-12 05:05:29 +00:00
|
|
|
inner: err
|
2016-01-27 10:29:26 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
generate: function(json, jst, format, cssInfo, opts, theme) {
|
2016-02-14 01:40:17 +00:00
|
|
|
var ctx, delims, r, traverse;
|
2018-02-12 05:05:29 +00:00
|
|
|
// Tweak underscore's default template delimeters
|
2016-01-27 10:29:26 +00:00
|
|
|
delims = (opts.themeObj && opts.themeObj.delimeters) || opts.template;
|
|
|
|
if (opts.themeObj && opts.themeObj.delimeters) {
|
|
|
|
delims = _.mapObject(delims, function(val, key) {
|
|
|
|
return new RegExp(val, "ig");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_.templateSettings = delims;
|
2016-02-14 01:40:17 +00:00
|
|
|
r = null;
|
|
|
|
switch (format) {
|
|
|
|
case 'html':
|
|
|
|
r = json.markdownify();
|
|
|
|
break;
|
|
|
|
case 'pdf':
|
|
|
|
r = json.markdownify();
|
|
|
|
break;
|
|
|
|
case 'png':
|
|
|
|
r = json.markdownify();
|
|
|
|
break;
|
|
|
|
case 'latex':
|
|
|
|
traverse = require('traverse');
|
|
|
|
r = traverse(json).map(function(x) {
|
|
|
|
if (this.isLeaf && String.is(this.node)) {
|
|
|
|
return escapeLaTeX(this.node);
|
|
|
|
}
|
|
|
|
return this.node;
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
r = json;
|
|
|
|
}
|
2018-02-12 05:05:29 +00:00
|
|
|
// Set up the context
|
2016-01-27 10:29:26 +00:00
|
|
|
ctx = {
|
2016-02-14 01:40:17 +00:00
|
|
|
r: r,
|
2016-01-27 10:29:26 +00:00
|
|
|
filt: opts.filters,
|
|
|
|
XML: require('xml-escape'),
|
|
|
|
RAW: json,
|
|
|
|
cssInfo: cssInfo,
|
2018-02-12 05:05:29 +00:00
|
|
|
//engine: @
|
2016-01-27 10:29:26 +00:00
|
|
|
headFragment: opts.headFragment || '',
|
|
|
|
opts: opts
|
|
|
|
};
|
2018-02-12 05:05:29 +00:00
|
|
|
// Link to our helpers
|
2016-01-27 10:29:26 +00:00
|
|
|
registerHelpers(theme, opts, cssInfo, ctx, this);
|
2018-02-12 05:05:29 +00:00
|
|
|
// Generate!
|
2016-01-27 10:29:26 +00:00
|
|
|
return this.generateSimple(ctx, jst);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}).call(this);
|
2016-02-02 02:14:36 +00:00
|
|
|
|
|
|
|
//# sourceMappingURL=underscore-generator.js.map
|