Class: Amrita2::Filters::Attr::Renderer

Inherits:
Renderers::Base show all
Includes:
Util, OptionSupport
Defined in:
lib/amrita2/template.rb

Constant Summary

Constants included from Util

Util::AMP_WITHOUT_REFRENCE, Util::DefaultAllowedScheme, Util::NAME, Util::NAMECHAR, Util::NOT_REFERENCE, Util::UrlInvalidChar

Instance Method Summary collapse

Methods included from Util

sanitize_attribute_value, sanitize_text, sanitize_url

Methods inherited from Renderers::Base

#generate_when

Constructor Details

#initialize(*args) ⇒ Renderer

Returns a new instance of Renderer.



2034
2035
2036
# File 'lib/amrita2/template.rb', line 2034

def initialize(*args)
  parse_opt(*args)
end

Instance Method Details

#body_for_dynamic(cg, de, element) ⇒ Object



2095
2096
2097
2098
2099
2100
2101
# File 'lib/amrita2/template.rb', line 2095

def body_for_dynamic(cg, de, element)
  cg.put_string_expression("start_tag(e)")
  de.children.each do |c|
    c.render_me(cg)
  end
  cg.put_string_expression("end_tag(e)")
end

#body_for_static(cg) ⇒ Object



2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
# File 'lib/amrita2/template.rb', line 2084

def body_for_static(cg)
  cg.if_("body") do
    cg.put_string_expression("start_tag(e)")
    cg.put_string_expression("body.amrita_value")
    cg.put_string_expression("end_tag(e)")
    cg.else_ do
      cg.put_string_expression("e.to_s")
    end
  end
end

#generate_body(cg, de, element) ⇒ Object



2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
# File 'lib/amrita2/template.rb', line 2038

def generate_body(cg, de, element)
  use_body = (not de.has_dynamic?) && @opt.has_key?(:body)
  body_key = nil
  if use_body
    body_key = @opt[:body]
    body_key = :body unless body_key.kind_of?(Symbol)
    cg.code("body = $_.amrita_value(#{body_key.inspect})")
  end
  if @opt.size > 0
    cg.code("a = {}")
    @opt.each do |key, v|
      next if key == :body
      if element.get_attribute(key)
        cg.case_("v = $_.amrita_value(#{v.inspect})") do
          cg.when_("true") do
            cg.code("a[#{key.inspect}] = #{element.get_attribute(key).inspect}")
          end
          cg.when_("false, nil") do
            cg.code("a.delete(#{key.inspect})")
          end
          cg.else_ do
            cg.code("a[#{key.inspect}] = $_.amrita_value(#{v.inspect})")
          end
        end
      else
        cg.code("v = $_.amrita_value(#{v.inspect})")
        cg.code("a[#{key.inspect}] = v if v")
      end
    end
  else
    cg.code("a = $_ ")
  end

  a = { }
  element.attributes.each do |k, v|
    next if @opt[k.intern]
    a[k] = v
  end
  cg.code("e = new_element(#{element.name.inspect}, #{a.inspect}.merge(a))")
  if use_body
    body_for_static(cg)
  else
    body_for_dynamic(cg, de, element)
  end
end