summaryrefslogtreecommitdiff
path: root/ruby/lib/luigi-template.rb
blob: 8b4d476927d0f4da18bb3ef27dd3ca4ead69d1c9 (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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
require 'uri'
require 'json'
require 'openssl'
# require 'pp'

#
# String templating library.  See Luigi::Template for details.
#
module Luigi
  #
  # Version of Luigi Template.
  #
  VERSION = '0.4.2'

  #
  # Base class for all errors raised by Luigi Template.
  #
  class LuigiError < Exception
  end

  #
  # Base class for unknown entry errors raised by Luigi Template.
  #
  class BaseUnknownError < LuigiError
    #
    # Type of unknown entry (Symbol).
    #
    attr_reader :type

    #
    # Name of unknown entry (String).
    #
    attr_reader :name

    #
    # Create a new BaseUnknownError instance.
    #
    # Parameters:
    #
    # * +type+: Type name (ex: "template", "filter", or "key").
    # * +name+: Item name.
    #
    def initialize(type, name)
      @type, @name = type, name
      super("unknown #{type}: #{name}")
    end
  end

  #
  # Thrown by Luigi::Template#run when an unknown key is encountered.
  #
  # The key is available in the +name+ attribute.
  #
  class UnknownKeyError < BaseUnknownError
    #
    # Create a new UnknownFilterError instance.
    #
    # Parameters:
    #
    # * +name+: Unknown key.
    #
    def initialize(name)
      super(:key, name)
    end
  end

  #
  # Thrown by Luigi::Template#run when an unknown filter is encountered.
  #
  # The unknown filter name is available in the +name+ attribute.
  #
  class UnknownFilterError < BaseUnknownError
    #
    # Create a new UnknownFilterError instance.
    #
    # Parameters:
    #
    # * +name+: Name of the unknown filter.
    #
    def initialize(name)
      super(:filter, name)
    end
  end


  #
  # Thrown by Luigi::Cache#run when an unknown template is encountered.
  #
  # The unknown template name is available in the +name+ attribute.
  #
  class UnknownTemplateError < BaseUnknownError
    #
    # Create a new UnknownTemplateError instance.
    #
    # Parameters:
    #
    # * +name+: Unknown template name.
    #
    def initialize(name)
      super(:template, name);
    end
  end

  #
  # HTML entity map.
  #
  # Used by built-in +h+ filter.
  #
  HTML_ENTITIES = {
    38 => '&amp;',
    60 => '&lt;',
    62 => '&gt;',
    34 => '&quot;',
    39 => '&apos;',
  }

  #
  # Map of built-in global filters.
  #
  # Default Filters:
  #
  # * +uc+: Convert string to upper-case.
  # * +lc+: Convert string to lower-case.
  # * +h+: HTML-escape string.
  # * +u+: URL-escape string.
  # * +json+: JSON-encode value.
  # * +trim+: Strip leading and trailing whitespace from string.
  # * +base64+: Base64-encode value.
  # * +hash+: Hash value.  Requires hash algorithm parameter (ex:
  #   "sha1", "md5", etc).
  #
  # You can add your own global filters, like so:
  #
  #     # create custom global filter named 'foobarify'
  #     Luigi::FILTERS[:foobarify] = proc { |s| "foo-#{s}-bar" }
  #
  #     # create template which uses custom "foobarify" filter
  #     tmpl = Luigi::Template.new('hello %{name | foobarify}')
  #
  #     # run template and print result
  #     puts tmpl.run({
  #       name: 'Paul'
  #     })
  #
  #     # prints "hello foo-Paul-bar"
  #
  FILTERS = {
    # upper-case string
    uc: proc { |v|
      (v || '').to_s.upcase
    },

    # lower-case string
    lc: proc { |v|
      (v || '').to_s.downcase
    },

    # html-escape string
    h: proc { |v|
      (v || '').to_s.bytes.map { |b|
        if b < 32 || b > 126
          "&##{b};"
        elsif HTML_ENTITIES.key?(b)
          HTML_ENTITIES[b]
        else
          b.chr
        end
      }.join
    },

    # uri-escape string
    u: proc { |v|
      URI.encode_www_form_component((v || '').to_s)
    },

    # json-encode value
    json: proc { |v|
      JSON.unparse(v)
    },

    # trim leading and trailing whitespace from string
    trim: proc { |v, args, row, t|
      (v || '').to_s.strip
    },

    # base64-encode string
    base64: proc { |v, args, row, t|
      [(v || '').to_s].pack('m').strip
    },

    # hash string
    hash: proc { |v, args, row, t|
      OpenSSL::Digest.new(args[0]).hexdigest((v || '').to_s)
    },
  }

  #
  # Template parser.
  #
  module Parser # :nodoc: all
    RES = {
      action: %r{
        # match opening brace
        %\{

        # match optional whitespace
        \s*

        # match key
        (?<key>[^\s\|\}]+)

        # match filter(s)
        (?<filters>(\s*\|(\s*[^\s\|\}]+)+)*)

        # match optional whitespace
        \s*

        # match closing brace
        \}

        # or match up all non-% chars or a single % char
        | (?<text>[^%]* | %)
      }mx,

      filter: %r{
        # match filter name
        (?<name>\S+)

        # match filter arguments (optional)
        (?<args>(\s*\S+)*)

        # optional trailing whitespace
        \s*
      }mx,

      delim_filters: %r{
        \s*\|\s*
      }mx,

      delim_args: %r{
        \s+
      },
    }.reduce({}) do |r, row|
      r[row[0]] = row[1].freeze
      r
    end.freeze

    #
    # Parse a (possibly empty) string into an array of actions.
    #
    def self.parse_template(str)
      str.scan(RES[:action]).map { |m|
        if m[0] && m[0].length > 0
          fs = parse_filters(m[1]).freeze
          { type: :action, key: m[0].intern, filters: fs }
        else
          # literal text
          { type: :text, text: m[2].freeze }
        end.freeze
      }.freeze
    end

    #
    # Parse a (possibly empty) string into an array of filters.
    #
    def self.parse_filters(str)
      # strip leading and trailing whitespace
      str = (str || '').strip

      if str.length > 0
        str.strip.split(RES[:delim_filters]).inject([]) do |r, f|
          # strip whitespace
          f = f.strip

          if f.length > 0
            md = f.match(RES[:filter])
            raise "invalid filter: #{f}" unless md
            # pp md

            # get args
            args = md[:args].strip

            # add to result
            r << {
              name: md[:name].intern,
              args: args.length > 0 ? args.split(RES[:delim_args]) : [],
            }
          end

          # return result
          r
        end
      else
        # return empty filter set
        []
      end
    end
  end

  #
  # Template class.
  #
  # Parse a template string into a Luigi::Template instance, and then
  # apply the Luigi::Template via the Luigi::Template#run() method.
  #
  # Example:
  #
  #     # load luigi template
  #     require 'luigi-template'
  #
  #     # create template
  #     tmpl = Luigi::Template.new('hello %{name}')
  #
  #     # run template and print result
  #     puts tmpl.run({
  #       name: 'Paul'
  #     })
  #
  #     # prints "hello Paul"
  #
  # You can also filter values in templates, using the pipe symbol:
  #
  #     # create template that converts name to upper-case
  #     tmpl = Luigi::Template.new('hello %{name | uc}')
  #
  #     # run template and print result
  #     puts tmpl.run({
  #       name: 'Paul'
  #     })
  #
  #     # prints "hello PAUL"
  #
  # Filters can be chained:
  #
  #     # create template that converts name to upper-case and then
  #     # strips leading and trailing whitespace
  #     tmpl = Luigi::Template.new('hello %{name | uc | trim}')
  #
  #     # run template and print result
  #     puts tmpl.run({
  #       name: '   Paul    '
  #     })
  #
  #     # prints "hello PAUL"
  #
  # Filters can take arguments:
  #
  #     # create template that converts name to lowercase and then
  #     # calculates the SHA-1 digest of the result
  #     tmpl = Luigi::Template.new('hello %{name | lc | hash sha1}')
  #
  #     # run template and print result
  #     puts tmpl.run({
  #       name: 'Paul',
  #     })
  #
  #     # prints "hello a027184a55211cd23e3f3094f1fdc728df5e0500"
  #
  # You can define custom global filters:
  #
  #     # create custom global filter named 'foobarify'
  #     Luigi::FILTERS[:foobarify] = proc { |s| "foo-#{s}-bar" }
  #
  #     # create template which uses custom "foobarify" filter
  #     tmpl = Luigi::Template.new('hello %{name | foobarify}')
  #
  #     # run template and print result
  #     puts tmpl.run({
  #       name: 'Paul'
  #     })
  #
  #     # prints "hello foo-Paul-bar"
  #
  # Or define custom filters for a template:
  #
  #     # create template with custom filters rather than global filters
  #     tmpl = Luigi::Template.new('hello %{name | reverse}', {
  #       reverse: proc { |s| s.reverse }
  #     })
  #
  #     # run template and print result
  #     puts tmpl.run({
  #       name: 'Paul',
  #     })
  #
  #     # prints "hello luaP"
  #
  # Your custom filters can accept arguments, too:
  #
  #     # create custom global filter named 'foobarify'
  #     Luigi::FILTERS[:wrap] = proc { |s, args|
  #       case args.length
  #       when 2
  #         '(%s, %s, %s)' % [args[0], s, args[1]]
  #       when 1
  #         '(%s in %s)' % [s, args[0]]
  #       when 0
  #         s
  #       else
  #         raise 'invalid argument count'
  #       end
  #     }
  #
  #     # create template that uses custom "wrap" filter
  #     tmpl = Luigi::Template.new('sandwich: %{meat | wrap slice heel}, taco: %{meat | wrap shell}')
  #
  #     # run template and print result
  #     puts tmpl.run({
  #       meat: 'chicken'
  #     })
  #
  #     # prints "sandwich: (slice, chicken, heel), taco: (chicken in shell)"
  #
  class Template
    #
    # Original template string.
    #
    attr_reader :str

    #
    # Create a new template, expand it with the given arguments and
    # filters, and print the result.
    #
    # Parameters:
    #
    # * +str+: Template string.
    # * +args+: Argument key/value map.
    # * +filters+: Hash of filters.  Defaults to Luigi::FILTERS if
    #   unspecified.
    #
    # Example:
    #
    #     # create a template object, expand it, and print the result
    #     puts Luigi::Template.run('hello %{name}', {
    #       name: 'Paul'
    #     })
    #
    #     # prints "hello Paul"
    #
    def self.run(str, args = {}, filters = FILTERS)
      Template.new(str, filters).run(args)
    end

    #
    # Create a new Template from the given string.
    #
    def initialize(str, filters = FILTERS)
      @str, @filters = str.freeze, filters
      @actions = Parser.parse_template(str).freeze
    end

    #
    # Expand template with the given arguments and return the result.
    #
    # Parameters:
    #
    # * +args+: Argument key/value map.
    #
    # Example:
    #
    #     # create a template object
    #     tmpl = Luigi::Template.new('hello %{name}')
    #
    #     # apply template, print result
    #     puts tmpl.run({ name: 'Paul'})
    #
    #     # prints "hello Paul"
    #
    # This method is aliased as "%", so you can do this:
    #
    #     # create template
    #     tmpl = Luigi::Template.new('hello %{name | uc}')
    #
    #     # run template and print result
    #     puts tmpl % { name: 'Paul' }
    #
    #     # prints "hello PAUL"
    #
    def run(args)
      @actions.map { |a|
        # pp a

        case a[:type]
        when :action
          # check key and get value
          val = if args.key?(a[:key])
            args[a[:key]]
          elsif args.key?(a[:key].to_s)
            args[a[:key].to_s]
          else
            # invalid key
            raise UnknownKeyError.new(a[:key])
          end

          # filter value
          a[:filters].inject(val) do |r, f|
            # check filter name
            unless @filters.key?(f[:name])
              raise UnknownFilterError.new(f[:name])
            end

            # call filter, return result
            @filters[f[:name]].call(r, f[:args], args, self)
          end
        when :text
          # literal text
          a[:text]
        else
          # never reached
          raise "unknown action type: #{a[:type]}"
        end
      }.join
    end

    alias :'%' :run

    #
    # Return the input template string.
    #
    # Example:
    #
    #     # create a template object
    #     tmpl = Luigi::Template.new('hello %{name}')
    #
    #     # create a template object
    #     puts tmpl.to_s
    #
    #     # prints "hello %{name}"
    #
    def to_s
      @str
    end
  end

  #
  # Minimal lazy-loading template cache.
  #
  # Group a set of templates together and only parse them on an
  # as-needed basis.
  #
  class Cache
    #
    # Create a new template cache with the given templates.
    #
    # Parameters:
    #
    # * +strings+: Map of template names to template strings.
    # * +filters+: Hash of filters.  Defaults to Luigi::FILTERS if
    #   unspecified.
    #
    # Example:
    #
    #     # create template cache
    #     cache = Luigi::Cache.new({
    #       hi: 'hi %{name}!'
    #     })
    #
    #     # run template from cache
    #     puts cache.run(:hi, {
    #       name: 'Paul'
    #     })
    #
    #     # prints "hi paul!"
    #
    def initialize(strings, filters = FILTERS)
      # work with frozen copy of strings hash
      strings = strings.freeze

      @templates = Hash.new do |h, k|
        # always deal with symbols
        k = k.intern

        # make sure template exists
        raise UnknownTemplateError.new(k) unless strings.key?(k)

        # create template
        h[k] = Template.new(strings[k], filters)
      end
    end

    #
    # Get given template, or raise an UnknownTemplateError if the given
    # template does not exist.
    #
    # Example:
    #
    #     # create template cache
    #     cache = Luigi::Cache.new({
    #       hi: 'hi %{name}!'
    #     })
    #
    #     # get template from cache
    #     tmpl = cache[:hi]
    #
    #     # run template, print result
    #     puts tmpl.run(:hi, {
    #       name: 'Paul'
    #     })
    #
    #     # prints "hi Paul"
    #
    def [](key)
      @templates[key]
    end

    #
    # Run specified template from cache with the given templates.
    #
    # Raises an UnknownTemplateError if the given template key does not
    # exist.
    #
    # Parameters:
    #
    # * +key+: Template key.
    # * +args+: Argument key/value map.
    #
    # Example:
    #
    #     # create template cache
    #     cache = Luigi::Cache.new({
    #       hi: 'hi %{name}!'
    #     })
    #
    #     # run template from cache
    #     puts cache.run(:hi, {
    #       name: 'Paul'
    #     })
    #
    #     # prints "hi paul!"
    #
    def run(key, args)
      # run template with args and return result
      @templates[key].run(args)
    end
  end
end