Skip to content

Instantly share code, notes, and snippets.

@shadowmint
Created June 19, 2015 06:24
Show Gist options
  • Save shadowmint/d9831b55c68174342bde to your computer and use it in GitHub Desktop.
Save shadowmint/d9831b55c68174342bde to your computer and use it in GitHub Desktop.
Wintersmith gen sample
module.exports = function (env, callback) {
function Page(id, count, articles, total_pages) {
var key = id == 0 ? '' : id;
var rtn = new env.plugins.Page();
var targets = articles.length <= count ? articles : articles.splice(0, count);
rtn.getFilename = function() {
return 'index' + key + '.html';
};
rtn.getView = function() {
return function(env, locals, contents, templates, callback) {
var error = null;
var context = {
targets: targets,
pagination: {
current: id,
total: total_pages
}
};
env.utils.extend(context, locals);
var buffer = new Buffer(templates['index.jade'].fn(context));
callback(error, buffer);
};
};
return rtn;
};
/** Generates a custom index page */
function gen(contents, callback) {
// Find all articles
var articles = env.helpers.tagged(contents);
var per_page = env.helpers.config('pagination');
var pages = Math.ceil(articles.length / per_page);
// Generate an index per page
var output = {};
for (var i = 0; i < pages; ++i) {
var count = per_page;
if (per_page > articles.length) {
count = articles.length;
}
var p = Page(i, count, articles, pages);
output[p.getFilename()] = p;
}
// Return
var error = null;
callback(error, output);
};
env.registerGenerator('magic', gen);
callback();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment