3 Handlebars.registerHelper('mkws-json', function(obj) {
4 return mkws.$.toJSON(obj);
8 // This is intended to handle paragraphs from Wikipedia, hence the
9 // rather hacky code to remove numbered references.
11 Handlebars.registerHelper('mkws-paragraphs', function(obj, nPara, nSent) {
14 // For some reason, Handlebars provides the value
15 // {"hash":{},"data":{}} for parameters that are not provided. So we
16 // have to be prepared for actual numbers, explicitly undefined
17 // values and this dumb magic value.
18 if (obj && (nPara === undefined || nPara.hasOwnProperty('hash') || nPara == 0 || nPara > obj.length)) {
21 if (nSent === undefined || nSent.hasOwnProperty('hash') || nSent == 0) {
25 for (var i = 0; i < nPara; i++) {
26 // Remove numbered references such as "[1,3,4]" from text
27 var text = obj[i].replace(/\[[0-9,]+\]/g, '');
28 // Next line from http://stackoverflow.com/questions/18914629/split-string-into-sentences-in-javascript
29 var sentences = text.replace(/([.?!])\s*(?=[A-Z])/g, "$1|").split("|");
30 if (sentences.length > nSent)
31 sentences.length = nSent;
33 acc.push('<p>', sentences.join(' '), '</p>');
34 nSent -= sentences.length;
43 Handlebars.registerHelper('mkws-translate', function(s) {
48 // We need {{mkws-attr '@name'}} because Handlebars can't parse {{@name}}
49 Handlebars.registerHelper('mkws-attr', function(attrName) {
50 return this[attrName];
55 * Use as follows: {{#mkws-if-any NAME1 having="NAME2"}}
56 * Applicable when NAME1 is the name of an array
57 * The guarded code runs only if at least one element of the NAME1
58 * array has a subelement called NAME2.
60 Handlebars.registerHelper('mkws-if-any', function(items, options) {
61 var having = options.hash.having;
62 for (var i in items) {
64 if (!having || item[having]) {
65 return options.fn(this);
72 Handlebars.registerHelper('mkws-first', function(items, options) {
73 var having = options.hash.having;
74 for (var i in items) {
76 if (!having || item[having]) {
77 return options.fn(item);
84 Handlebars.registerHelper('mkws-commaList', function(items, options) {
87 for (var i in items) {
88 if (i > 0) out += ", ";
89 out += options.fn(items[i])
96 Handlebars.registerHelper('mkws-index1', function(obj) {
97 return obj.data.index + 1;
100 Handlebars.registerHelper('mkws-indexplus', function(delta, obj) {
101 return obj.data.index + delta;
104 Handlebars.registerHelper('mkws-repeat', function(count, options) {
106 for (var i = 0; i < count; i++) {
107 out += options.fn(this);
112 // Ridiculous that Handlebars has no way to do "or"
113 Handlebars.registerHelper('mkws-if-either', function(cond1, cond2, options) {
114 if (typeof cond1 === 'function') { cond1 = cond1.call(this); }
115 if (typeof cond2 === 'function') { cond2 = cond2.call(this); }
117 if (cond1 || cond2) {
118 return options.fn(this);
120 return options.inverse(this);
124 // Ridiculous that this, too, is not part of regular Handlebars
125 // This code is by Mike Griffin, taken from this blog comment:
126 // http://doginthehat.com.au/2012/02/comparison-block-helper-for-handlebars-templates/#comment-44
128 Handlebars.registerHelper('compare', function (lvalue, operator, rvalue, options) {
129 var operators, result;
131 if (arguments.length < 3) {
132 throw new Error("Handlerbars Helper 'compare' needs 2 parameters");
135 if (options === undefined) {
142 '==': function (l, r) { return l == r; },
143 '===': function (l, r) { return l === r; },
144 '!=': function (l, r) { return l != r; },
145 '!==': function (l, r) { return l !== r; },
146 '<': function (l, r) { return l < r; },
147 '>': function (l, r) { return l > r; },
148 '<=': function (l, r) { return l <= r; },
149 '>=': function (l, r) { return l >= r; },
150 'typeof': function (l, r) { return typeof l == r; },
151 'matches': function (l, r) { return l.match(r); }
154 if (!operators[operator]) {
155 throw new Error("Handlerbars Helper 'compare' doesn't know the operator " + operator);
158 result = operators[operator](lvalue, rvalue);
161 return options.fn(this);
163 return options.inverse(this);