waffel
Version:
Static site generation done tasty.
108 lines (98 loc) • 2.58 kB
JavaScript
// Generated by CoffeeScript 1.11.1
var _, cheerio, filters, lodash_filters, marked, moment, util;
_ = require('lodash');
util = require('util');
moment = require('moment');
marked = require('marked');
cheerio = require('cheerio');
lodash_filters = ['toArray', 'pluck', 'flatten', 'uniq', 'where', 'findWhere', 'compact'];
filters = {
limit: function(array, count) {
if (array == null) {
array = [];
}
if (count == null) {
count = 10;
}
return array.slice(0, count);
},
format: function(date, format, locale) {
if (locale == null) {
locale = this.options.defaultLanguage;
}
moment.locale(locale);
return moment(date).format(format || this.options.dateFormat);
},
excerpt: function(text, size) {
var $, words;
if (size == null) {
size = 200;
}
$ = cheerio.load(marked(text));
text = $('p').filter(function(index, element) {
return (element.children[0].type === 'text') || _.contains(['em', 'strong'], element.children[0].name);
}).first().text().trim();
if (text.length > size) {
words = text.substring(0, size).split(' ');
words.pop();
return (words.join(' ')) + "…";
} else {
return text;
}
},
toJSON: function(data) {
return JSON.stringify(data);
},
inspect: function(object) {
console.log(util.inspect(object, false, 2, true));
return object;
},
top: function(data, thresh) {
if (thresh == null) {
thresh = 3;
}
data = _.flatten(data);
data = _.reduce(data, function(memo, x) {
if (memo[x]) {
memo[x] = memo[x] + 1;
} else {
memo[x] = 1;
}
return memo;
}, {});
data = _.reduce(data, function(memo, freq, key) {
memo.push({
key: key,
freq: freq
});
return memo;
}, []);
data = _.sortBy(data, function(bin) {
return -bin.freq;
});
return data.slice(0, thresh).map(function(x) {
return x.key;
});
},
loc: function(data, language) {
if (_.isArray(data)) {
return data.map((function(_this) {
return function(item) {
if (item._localised) {
return item[language] || item[_this.options.fallbackLanguage];
} else {
return item;
}
};
})(this));
} else if (!data._localised) {
return data;
} else {
return data[language] || data[this.options.fallbackLanguage];
}
}
};
lodash_filters.forEach(function(m) {
return filters[m] = _[m];
});
module.exports = filters;