class Slim::Sections

Handle logic-less mode This filter can be activated with the option "sections" @api private

Public Class Methods

new(opts = {}) click to toggle source
# File lib/slim/sections.rb, line 10
def initialize(opts = {})
  super
  unless [:string, :symbol, :wrapped].include?(options[:dictionary_access])
    raise "Invalid dictionary access #{options[:dictionary_access].inspect}"
  end
end

Public Instance Methods

call(exp) click to toggle source
# File lib/slim/sections.rb, line 17
def call(exp)
  if options[:sections]
    # Store the dictionary in the _slimdict variable
    dictionary = options[:dictionary]
    dictionary = "Slim::Wrapper.new(#{dictionary})" if options[:dictionary_access] == :wrapped
    [:multi,
     [:code, "_slimdict = #{dictionary}"],
     super]
  else
    exp
  end
end
on_code(code) click to toggle source
# File lib/slim/sections.rb, line 56
def on_code(code)
  raise 'Embedded code is forbidden in sections mode'
end
on_dynamic(code) click to toggle source
# File lib/slim/sections.rb, line 52
def on_dynamic(code)
  raise 'Embedded code is forbidden in sections mode'
end
on_slim_attr(name, escape, value) click to toggle source
# File lib/slim/sections.rb, line 44
def on_slim_attr(name, escape, value)
  [:slim, :attr, name, escape, access(value)]
end
on_slim_control(name, content) click to toggle source

Interpret control blocks as sections or inverted sections

# File lib/slim/sections.rb, line 31
def on_slim_control(name, content)
  if name =~ %r\A!\s*(.*)/
    on_slim_inverted_section($1, content)
  else
    on_slim_section(name, content)
  end
end
on_slim_output(escape, name, content) click to toggle source
# File lib/slim/sections.rb, line 39
def on_slim_output(escape, name, content)
  raise 'Output statements with content are forbidden in sections mode' if !empty_exp?(content)
  [:slim, :output, escape, access(name), content]
end
on_slim_splat(code) click to toggle source
# File lib/slim/sections.rb, line 48
def on_slim_splat(code)
  [:slim, :splat, access(code)]
end

Protected Instance Methods

on_slim_inverted_section(name, content) click to toggle source
# File lib/slim/sections.rb, line 62
def on_slim_inverted_section(name, content)
  tmp = unique_name
  [:multi,
   [:code, "#{tmp} = #{access name}"],
   [:if, "!#{tmp} || #{tmp}.respond_to?(:empty) && #{tmp}.empty?",
    compile(content)]]
end
on_slim_section(name, content) click to toggle source
# File lib/slim/sections.rb, line 70
def on_slim_section(name, content)
  content = compile(content)
  tmp1, tmp2 = unique_name, unique_name

  [:if, "#{tmp1} = #{access name}",
   [:if, "#{tmp1} == true",
    content,
    [:multi,
     # Wrap map in array because maps implement each
     [:code, "#{tmp1} = [#{tmp1}] if #{tmp1}.respond_to?(:has_key?) || !#{tmp1}.respond_to?(:map)"],
     [:code, "#{tmp2} = _slimdict"],
     [:block, "#{tmp1}.each do |_slimdict|", content],
     [:code, "_slimdict = #{tmp2}"]]]]
end