Module: Flutterby::Filters

Defined in:
lib/flutterby/filters.rb

Class Method Summary collapse

Class Method Details

.add(fmts, &blk) ⇒ Object



27
28
29
30
31
# File 'lib/flutterby/filters.rb', line 27

def self.add(fmts, &blk)
  Array(fmts).each do |fmt|
    define_singleton_method("process_#{fmt}!", &blk)
  end
end

.apply!(input, view:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/flutterby/filters.rb', line 11

def self.apply!(input, view:)
  # Apply all filters
  view.node.filters.inject(input) do |body, filter|
    meth = "process_#{filter}!"

    if Filters.respond_to?(meth)
      Filters.send(meth, body, view: view)
    elsif template = tilt(filter, body)
      template.render(view).html_safe
    else
      Flutterby.logger.warn "Unsupported filter '#{filter}' for #{view.node.url}"
      body
    end
  end
end

.tilt(format, body, options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/flutterby/filters.rb', line 33

def self.tilt(format, body, options = {})
  default_options = {
    "erb" => { engine_class: Erubis::Auto::EscapedEruby }
  }

  options = default_options.fetch(format, {}).merge(options)

  t = Tilt[format] and t.new(options) { body }
end