Skip to content

Instantly share code, notes, and snippets.

@alanstevens
Created January 27, 2016 11:37
Show Gist options
  • Save alanstevens/2e027f3ef0969e8f7d75 to your computer and use it in GitHub Desktop.
Save alanstevens/2e027f3ef0969e8f7d75 to your computer and use it in GitHub Desktop.
var path = require("path");
var fs = require("fs");
var rimraf = require("rimraf");
var Handlebars = require("handlebars");
var Templates = require("./templates");
module.exports.generate = function(data) {
Templates.init();
var templatePath = path.resolve(__dirname, "../templates/resume-template.html");
var fileName = path.resolve(__dirname, "../../index.html");
var templateSource = fs.readFileSync(templatePath, "utf-8");
var template = Handlebars.compile(templateSource);
var resumeContent = template(data);
rimraf(fileName,
function() {
fs.writeFile(fileName, resumeContent, function(err) {
if (err) {
console.log("ERROR generating resume:\n", err);
}
console.log("SUCCESS! Resume generated to: ", fileName);
}); // fs.writeFile
} // anonymous function
); //rimraf
}; // generate
var path = require("path");
var fs = require("fs");
var _ = require("lodash");
var Handlebars = require("handlebars");
function registerPartials() {
var templatePath = path.resolve(__dirname, "../templates"),
templates = fs.readdirSync(templatePath);
templates.forEach(function(template) {
var partialName = template.substr(2, (template.lastIndexOf(".") - 2));
var partialFilepath = path.resolve(__dirname, "../templates", template);
var partialContent = fs.readFileSync(partialFilepath, "utf-8", function(err) {
if (err) {
console.log("ERROR registering partial:", partialFilepath, err);
}
});
Handlebars.registerPartial(partialName, partialContent);
});
}
function registerHelpers() {
// #compare - adapted from http://doginthehat.com.au/2012/02/comparison-block-helper-for-handlebars-templates/#comment-44
Handlebars.registerHelper("compare", function(lval, operator, rval, options) {
var operators, result;
if (arguments.length < 3) {
throw new Error("Handlebars helper 'compare' needs 3 parameters");
}
if (options === undefined) {
options = rval;
rval = operator;
operator = "===";
}
operators = {
"==": function(l, r) {
return l == r;
},
"===": function(l, r) {
return l === r;
},
"!=": function(l, r) {
return l != r;
},
"!==": function(l, r) {
return l !== r;
},
"<": function(l, r) {
return l < r;
},
">": function(l, r) {
return l > r;
},
"<=": function(l, r) {
return l <= r;
},
">=": function(l, r) {
return l >= r;
},
"typeof": function(l, r) {
return typeof l == r;
}
};
if (!operators[operator]) {
throw new Error("Handlebars helper 'compare' doesn't know the operator: " + operator);
}
result = operators[operator](lval, rval);
if (result) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
// #stringify
Handlebars.registerHelper("stringify", function(target) {
return JSON.stringify(target, null, 2);
});
// #stripChars
Handlebars.registerHelper("stripChars", function(target) {
return target.replace(".", "");
});
}
module.exports.init = function() {
registerHelpers();
registerPartials();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment