aboutsummaryrefslogtreecommitdiff
path: root/js/luigi-template.js
blob: 8fcf6462f00d84f81feaba0976f9eb2a900a3862 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/**
 * Luigi Template: Simple JavaScript string templating library inspired by
 * Unix pipes.
 *
 * @author Paul Duncan <pabs@pablotron.org>
 * @license MIT
 * @copyright 2010-2018 Paul Duncan (pabs@pablotron.org)
 * @version 0.4.2
 *
 */

/**
 * Luigi Template namespace.
 */
var Luigi = (function() {
  "use strict";

  // Array.each polyfill
  var each = (function() {
    if (Array.prototype.forEach) {
      return function(a, fn) {
        a.forEach(fn);
      };
    } else {
      return function(a, fn) {
        var i, l;

        for (i = 0, l = a.length; i < l; i++)
          fn(a[i], i, a);
      };
    }
  })();

  // Array.map polyfill
  var map = (function() {
    if (Array.prototype.map) {
      return function(a, fn) {
        return a.map(fn);
      };
    } else {
      return function(a, fn) {
        var r = new Array(a.length);

        each(a, function(v, i) {
          r[i] = v;
        });

        return r;
      };
    }
  })();

  // Array.reduce polyfill
  var reduce = (function() {
    if (Array.prototype.reduce) {
      return function(a, fn, iv) {
        return a.reduce(fn, iv);
      };
    } else {
      return function(a, fn, r) {
        each(a, function(v, i) {
          r = fn(r, v, i, a);
        });

        return r;
      };
    }
  })();

  // String.trim polyfill
  var trim = (function() {
    if (String.prototype.trim) {
      return function(s) {
        return (s || '').trim();
      };
    } else {
      var re = /^\s+|\s+$/g;

      return function(s) {
        (s || '').replace(re, '');
      };
    }
  })();

  // String.scan polyfill
  function scan(s, re, fn) {
    var m;

    if (!re.global)
      throw 'non-global regex';

    while ((m = re.exec(s)) !== null)
      fn(m);
  }

  var RES = {
    actions: /%\{\s*([^\s\|\}]+)\s*((\s*\|(\s*[^\s\|\}]+)+)*)\s*\}|([^%]+|%)/g,
    filter:   /(\S+)((\s*\S+)*)\s*/,
    delim_filters: /\s*\|\s*/,
    delim_args:    /\s+/
  };

  function parse_template(s) {
    var r = [];

    scan(s, RES.actions, function(m) {
      if (m[1]) {
        // action
        r.push({
          is_text: false,
          key: m[1],
          filters: parse_filters(m[2])
        });
      } else {
        // text
        r.push({
          is_text: true,
          text: m[5]
        });
      }
    });

    return r;
  }

  function parse_filters(filters) {
    var r = [];

    each(filters.split(RES.delim_filters), function(f) {
      f = trim(f);
      if (!f)
        return;

      var m = f.match(RES.filter);
      if (!m)
        throw new Error('invalid filter: ' + f);

      var as = trim(m[2]);

      r.push({
        name: m[1],
        args: as.length ? as.split(RES.delim_args) : []
      });
    });

    return r;
  }

  /**
   * Luigi Template namespace.
   */
  var Luigi = {
    /**
     * Version of Luigi Template.
     */
    VERSION: '0.4.2',

    /**
     * Default filter set.
     *
     * The default filters are:
     * * `uc`: Upper-case string value.
     * * `lc`: Lower-case string value.
     * * `s`: Pluralize a value by returning `""` if the value is 1, and `"s"` otherwise.
     * * `length`: Get the length of an array.
     * * `trim`: Trim leading and trailing whitespace from a string.
     * * `h`: HTML-escape a string value.
     * * `u`: URL-escape a string value.
     * * `json`: JSON-encode a value.
     */
    FILTERS: {
      uc: function(v) {
        return (v || '').toUpperCase();
      },

      lc: function(v) {
        return (v || '').toLowerCase();
      },

      s: function(v) {
        return (v == 1) ? '' : 's';
      },

      length: function(v) {
        return (v || '').length;
      },

      trim: function(v) {
        return trim(v);
      },

      h: (function() {
        var LUT = {
          '"': '&quot;',
          "'": '&apos;',
          '>': '&gt;',
          '<': '&lt;',
          '&': '&amp;'
        };

        return function(v) {
          if (v === undefined || v === null)
            return '';

          return v.toString().replace(/(['"<>&])/g, function(s) {
            return LUT[s];
          });
        };
      })(),

      u: function(s) {
        return encodeURIComponent(s || '').replace('%20', '+').replace(/[!'()*]/g, function(c) {
          return '%' + c.charCodeAt(0).toString(16);
        });
      },

      json: function(v) {
        return JSON.stringify(v);
      },
    },
  };

  /**
   * Create a new Template instance.
   *
   * @param s {string} Template string (required).
   * @param filters {hash} Filters (optional).
   *
   * @constructor
   */
  Luigi.Template = function(template, filters) {
    this.s = template;
    this.filters = filters || Luigi.FILTERS;
    this.actions = parse_template(template);
  };

  /**
   * Run template with given parameters.
   *
   * @param args {hash} Template parameters (required).
   *
   * @returns {string} Result of applying arguments to template.
   */
  Luigi.Template.prototype.run = function(args) {
    var i, l, f, fs, me = this;

    // debug
    // print(JSON.stringify(this.actions));

    return map(this.actions, function(row) {
      if (row.is_text === true) {
        return row.text;
      } else {
        if (!(row.key in args)) {
          throw new Error('unknown key: ' + row.key);
        }

        return reduce(row.filters, function(r, f) {
          if (!(f.name in me.filters)) {
            throw new Error('unknown filter: ' + f.name);
          }

          return me.filters[f.name](r, f.args, args, me);
        }, args[row.key]);
      }
    }).join('');
  };

  function get_inline_template(key) {
    // get script element
    var e = document.getElementById(key);
    if (!e)
      throw new Error('unknown inline template key: ' + key);

    // return result
    return e.innerText || '';
  }

  /**
   * Create a new template cache.
   *
   * @param templates {hash} name to template map (required).
   * @param filters {hash} custom filter map (optional).
   *
   * @constructor
   */
  Luigi.Cache = function(templates, filters, try_dom) {
    this.templates = templates;
    this.filters = filters || Luigi.FILTERS;
    this.try_dom = !!try_dom;
    this.cache = {};
  };

  /**
   * Find named template in cache and run it with the given arguments.
   *
   * @param key {hash} Template key (required).
   * @param args {hash} Template run arguments (required).
   *
   * @returns {string} Result of applying arguments to template.
   */
  Luigi.Cache.prototype.run = function(key, args) {
    if (!(key in this.cache)) {
      var s = null;

      if (key in this.templates) {
        s = this.templates[key];
        s = (s.constructor === Array) ? s.join('') : s;
      } else if (this.try_dom) {
        // get source from inline script tag
        s = get_inline_template(key);
      } else {
        throw new Error('unknown template: ' + key);
      }

      // cache template
      this.cache[key] = new Luigi.Template(s, this.filters);
    }

    // run template
    return this.cache[key].run(args);
  };

  /**
   * Create a new template cache with the given templates and
   * (optionally) filters.
   *
   * @param templates {hash} name to template map (required).
   * @param filters {hash} custom filter map (optional).
   *
   * @returns {Cache} Template cache.
   */
  Luigi.cache = function(templates, filters) {
    return new Luigi.Cache(templates, filters || Luigi.FILTERS);
  }

  // declare domcache constructor
  Luigi.DOMCache = function() {
    this.cache = {};
  };

  // domcache run method
  Luigi.DOMCache.prototype.run = function(key, args) {
    if (!(key in this.cache))
      this.cache[key] = new Luigi.Template(get_inline_template(key));

    // run template
    return this.cache[key].run(args);
  };

  // create DOMCache singleton
  Luigi.dom = new Luigi.DOMCache();

  /**
   * Create and run template with given template string, parameters, and
   * (optionally) filters.
   *
   * @param template {string} Template parameters (required).
   * @param args {hash} Template parameters (required).
   * @param filters {hash} Custom filters (optional).
   *
   * @returns {string} Result of applying arguments to template.
   */
  Luigi.run = function(template, args, filters) {
    return new Luigi.Template(template, filters).run(args);
  };

  // expose interface
  return Luigi;
}());