X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fmkws-handlebars.js;h=537779e5b21860a9dbb29c7535780746dd153fdb;hb=94a3083ce79f3cdf185add39bb933d695815b623;hp=5fae9053f2e757629fbc04216002f7cb4220b9e2;hpb=63a242875ed57a63be26b2295ed1693836553ac6;p=mkws-moved-to-github.git diff --git a/src/mkws-handlebars.js b/src/mkws-handlebars.js index 5fae905..537779e 100644 --- a/src/mkws-handlebars.js +++ b/src/mkws-handlebars.js @@ -1,71 +1,81 @@ // Handlebars helpers -Handlebars.registerHelper('json', function(obj) { - return $.toJSON(obj); + +Handlebars.registerHelper('mkws-json', function(obj) { + return $.toJSON(obj); }); -Handlebars.registerHelper('paragraphs', function(obj) { - var acc = []; - for (var i = 0; i < obj.length; i++) { - acc.push('

', obj[i], '

'); - } - return acc.join(''); +// This is intended to handle paragraphs from Wikipedia, hence the +// rather hacky code to remove numbered references. +// +Handlebars.registerHelper('mkws-paragraphs', function(obj, count) { + var acc = []; + + // For some reason, Handlebars provides the value + // {"hash":{},"data":{}} for undefined parameters + if (count.hasOwnProperty('hash')) count = undefined; + if (!count || count > obj.length) count = obj.length; + + for (var i = 0; i < count; i++) { + acc.push('

', obj[i].replace(/\[[0-9,]+\]/g, ''), '

'); + } + return acc.join(''); }); -Handlebars.registerHelper('translate', function(s) { - return mkws.M(s); +Handlebars.registerHelper('mkws-translate', function(s) { + return mkws.M(s); }); -// We need {{attr '@name'}} because Handlebars can't parse {{@name}} -Handlebars.registerHelper('attr', function(attrName) { - return this[attrName]; +// We need {{mkws-attr '@name'}} because Handlebars can't parse {{@name}} +Handlebars.registerHelper('mkws-attr', function(attrName) { + return this[attrName]; }); /* - * Use as follows: {{#if-any NAME1 having="NAME2"}} + * Use as follows: {{#mkws-if-any NAME1 having="NAME2"}} * Applicable when NAME1 is the name of an array * The guarded code runs only if at least one element of the NAME1 * array has a subelement called NAME2. */ -Handlebars.registerHelper('if-any', function(items, options) { - var having = options.hash.having; - for (var i in items) { - var item = items[i] - if (!having || item[having]) { - return options.fn(this); - } +Handlebars.registerHelper('mkws-if-any', function(items, options) { + var having = options.hash.having; + for (var i in items) { + var item = items[i] + if (!having || item[having]) { + return options.fn(this); } - return ""; + } + return ""; }); -Handlebars.registerHelper('first', function(items, options) { - var having = options.hash.having; - for (var i in items) { - var item = items[i] - if (!having || item[having]) { - return options.fn(item); - } +Handlebars.registerHelper('mkws-first', function(items, options) { + var having = options.hash.having; + for (var i in items) { + var item = items[i] + if (!having || item[having]) { + return options.fn(item); } - return ""; + } + return ""; }); -Handlebars.registerHelper('commaList', function(items, options) { - var out = ""; +Handlebars.registerHelper('mkws-commaList', function(items, options) { + var out = ""; - for (var i in items) { - if (i > 0) out += ", "; - out += options.fn(items[i]) - } + for (var i in items) { + if (i > 0) out += ", "; + out += options.fn(items[i]) + } - return out; + return out; }); -Handlebars.registerHelper('index1', function(obj) { - return obj.data.index + 1; +Handlebars.registerHelper('mkws-index1', function(obj) { + return obj.data.index + 1; });