2015-12-06 21:19:55 +00:00
|
|
|
/**
|
2015-12-17 15:15:59 +00:00
|
|
|
Definition of the HandlebarsGenerator class.
|
2016-01-06 15:48:51 +00:00
|
|
|
@license MIT. See LICENSE.md for details.
|
2015-12-17 15:15:59 +00:00
|
|
|
@module handlebars-generator.js
|
2015-12-06 21:19:55 +00:00
|
|
|
*/
|
|
|
|
|
2016-01-06 15:48:51 +00:00
|
|
|
|
|
|
|
|
2015-12-06 21:19:55 +00:00
|
|
|
(function() {
|
|
|
|
|
|
|
|
|
2015-12-17 15:15:59 +00:00
|
|
|
|
|
|
|
var _ = require('underscore')
|
|
|
|
, HANDLEBARS = require('handlebars')
|
|
|
|
, FS = require('fs')
|
2016-01-10 03:13:29 +00:00
|
|
|
, registerHelpers = require('../helpers/handlebars-helpers')
|
2016-01-06 15:48:51 +00:00
|
|
|
, PATH = require('path')
|
|
|
|
, parsePath = require('parse-filepath')
|
|
|
|
, READFILES = require('recursive-readdir-sync')
|
|
|
|
, SLASH = require('slash');
|
2015-12-17 15:15:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Perform template-based resume generation using Handlebars.js.
|
2015-12-18 18:17:07 +00:00
|
|
|
@class HandlebarsGenerator
|
2015-12-17 15:15:59 +00:00
|
|
|
*/
|
2015-12-18 18:17:07 +00:00
|
|
|
var HandlebarsGenerator = module.exports = {
|
2015-12-17 15:15:59 +00:00
|
|
|
|
2016-01-06 15:48:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-18 18:17:07 +00:00
|
|
|
generate: function( json, jst, format, cssInfo, opts, theme ) {
|
|
|
|
|
2016-01-06 16:18:50 +00:00
|
|
|
registerPartials( format, theme );
|
2015-12-20 23:24:28 +00:00
|
|
|
registerHelpers( theme );
|
2015-12-17 15:15:59 +00:00
|
|
|
|
2016-01-06 16:18:50 +00:00
|
|
|
// Preprocess text
|
2015-12-31 11:38:30 +00:00
|
|
|
var encData = json;
|
|
|
|
( format === 'html' || format === 'pdf' ) && (encData = json.markdownify());
|
|
|
|
( format === 'doc' ) && (encData = json.xmlify());
|
|
|
|
|
2016-01-06 16:18:50 +00:00
|
|
|
// Compile and run the Handlebars template.
|
2016-01-08 21:27:19 +00:00
|
|
|
var template = HANDLEBARS.compile(jst, { strict: false, assumeObjects: false });
|
2015-12-18 18:17:07 +00:00
|
|
|
return template({
|
2015-12-31 11:38:30 +00:00
|
|
|
r: encData,
|
2015-12-18 18:17:07 +00:00
|
|
|
RAW: json,
|
|
|
|
filt: opts.filters,
|
|
|
|
cssInfo: cssInfo,
|
2016-01-05 14:28:40 +00:00
|
|
|
format: format,
|
2015-12-31 00:45:50 +00:00
|
|
|
opts: opts,
|
2015-12-18 18:17:07 +00:00
|
|
|
headFragment: opts.headFragment || ''
|
|
|
|
});
|
2015-12-17 15:15:59 +00:00
|
|
|
|
2015-12-18 18:17:07 +00:00
|
|
|
}
|
|
|
|
|
2015-12-17 15:15:59 +00:00
|
|
|
|
2016-01-06 15:48:51 +00:00
|
|
|
|
|
|
|
};
|
2016-01-06 16:18:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function registerPartials(format, theme) {
|
2016-01-07 21:39:46 +00:00
|
|
|
if( format === 'html' || format === 'doc' ) {
|
|
|
|
|
|
|
|
// Locate the global partials folder
|
|
|
|
var partialsFolder = PATH.join(
|
|
|
|
parsePath( require.resolve('fresh-themes') ).dirname,
|
|
|
|
'/partials/',
|
|
|
|
format
|
|
|
|
);
|
|
|
|
|
|
|
|
// Register global partials in the /partials folder
|
|
|
|
// TODO: Only do this once per HMR invocation.
|
|
|
|
_.each( READFILES( partialsFolder, function(error){ }), function( el ) {
|
|
|
|
var pathInfo = parsePath( el );
|
|
|
|
var name = SLASH( PATH.relative( partialsFolder, el )
|
|
|
|
.replace(/\.html$|\.xml$/, '') );
|
|
|
|
if( pathInfo.dirname.endsWith('section') ) {
|
|
|
|
name = SLASH(name.replace(/\.html$|\.xml$/, ''));
|
|
|
|
}
|
|
|
|
var tplData = FS.readFileSync( el, 'utf8' );
|
|
|
|
var compiledTemplate = HANDLEBARS.compile( tplData );
|
|
|
|
HANDLEBARS.registerPartial( name, compiledTemplate );
|
|
|
|
theme.partialsInitialized = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2016-01-06 16:18:50 +00:00
|
|
|
|
|
|
|
// Register theme-specific partials
|
|
|
|
_.each( theme.partials, function( el ) {
|
|
|
|
var tplData = FS.readFileSync( el.path, 'utf8' );
|
|
|
|
var compiledTemplate = HANDLEBARS.compile( tplData );
|
|
|
|
HANDLEBARS.registerPartial( el.name, compiledTemplate );
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-06 21:19:55 +00:00
|
|
|
}());
|