1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-11-05 09:56:22 +00:00
HackMyResume/dist/renderers/handlebars-generator.js

110 lines
3.3 KiB
JavaScript
Raw Normal View History

2016-01-27 10:29:26 +00:00
(function() {
2018-02-12 05:05:29 +00:00
/**
Definition of the HandlebarsGenerator class.
@license MIT. See LICENSE.md for details.
@module renderers/handlebars-generator
*/
/**
Perform template-based resume generation using Handlebars.js.
@class HandlebarsGenerator
*/
2016-01-27 10:29:26 +00:00
var FS, HANDLEBARS, HMSTATUS, HandlebarsGenerator, PATH, READFILES, SLASH, _, parsePath, registerHelpers, registerPartials;
_ = require('underscore');
HANDLEBARS = require('handlebars');
FS = require('fs');
registerHelpers = require('../helpers/handlebars-helpers');
PATH = require('path');
parsePath = require('parse-filepath');
READFILES = require('recursive-readdir-sync');
HMSTATUS = require('../core/status-codes');
SLASH = require('slash');
HandlebarsGenerator = module.exports = {
generateSimple: function(data, tpl) {
2018-02-12 05:05:29 +00:00
var err, template;
2016-01-27 10:29:26 +00:00
try {
2018-02-12 05:05:29 +00:00
// Compile and run the Handlebars template.
2016-01-27 10:29:26 +00:00
template = HANDLEBARS.compile(tpl, {
strict: false,
2016-12-15 02:14:45 +00:00
assumeObjects: false,
2018-02-04 06:43:51 +00:00
noEscape: data.opts.noescape
2016-01-27 10:29:26 +00:00
});
return template(data);
2018-02-12 05:05:29 +00:00
} catch (error1) {
err = error1;
2016-01-27 10:29:26 +00:00
throw {
fluenterror: HMSTATUS[template ? '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, curFmt, opts, theme) {
var ctx, encData;
2018-02-12 05:05:29 +00:00
// Preprocess text
2016-01-27 10:29:26 +00:00
encData = json;
if (format === 'html' || format === 'pdf') {
encData = json.markdownify();
}
if (format === 'doc') {
encData = json.xmlify();
}
2018-02-12 05:05:29 +00:00
// Set up partials and helpers
2018-02-07 10:49:02 +00:00
registerPartials(format, theme);
registerHelpers(theme, encData, opts);
2018-02-12 05:05:29 +00:00
// Set up the context
2016-01-27 10:29:26 +00:00
ctx = {
r: encData,
RAW: json,
filt: opts.filters,
format: format,
opts: opts,
engine: this,
results: curFmt.files,
headFragment: opts.headFragment || ''
};
2018-02-12 05:05:29 +00:00
// Render the template
2016-01-27 10:29:26 +00:00
return this.generateSimple(ctx, jst);
}
};
registerPartials = function(format, theme) {
var partialsFolder;
if (_.contains(['html', 'doc', 'md', 'txt', 'pdf'], format)) {
2018-02-12 05:05:29 +00:00
// Locate the global partials folder
2016-01-27 10:29:26 +00:00
partialsFolder = PATH.join(parsePath(require.resolve('fresh-themes')).dirname, '/partials/', format === 'pdf' ? 'html' : format);
2018-02-12 05:05:29 +00:00
// Register global partials in the /partials/[format] folder
// TODO: Only do this once per HMR invocation.
2016-01-27 10:29:26 +00:00
_.each(READFILES(partialsFolder, function(error) {
return {};
}), function(el) {
var compiledTemplate, name, pathInfo, tplData;
pathInfo = parsePath(el);
name = SLASH(PATH.relative(partialsFolder, el).replace(/\.(?:html|xml|hbs|md|txt)$/i, ''));
tplData = FS.readFileSync(el, 'utf8');
compiledTemplate = HANDLEBARS.compile(tplData);
HANDLEBARS.registerPartial(name, compiledTemplate);
return theme.partialsInitialized = true;
});
}
2018-02-12 05:05:29 +00:00
// Register theme-specific partials
2016-01-27 10:29:26 +00:00
return _.each(theme.partials, function(el) {
var compiledTemplate, tplData;
tplData = FS.readFileSync(el.path, 'utf8');
compiledTemplate = HANDLEBARS.compile(tplData);
return HANDLEBARS.registerPartial(el.name, compiledTemplate);
});
};
}).call(this);
2016-02-02 02:14:36 +00:00
//# sourceMappingURL=handlebars-generator.js.map