Class: Amrita2::Core::DefaultFilter

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

Instance Attribute Summary

Attributes included from Util::OptionSupport

#opt

Attributes inherited from Filters::Base

#next_

Instance Method Summary collapse

Methods included from Util::OptionSupport

#[], #method_missing, #parse_opt

Methods inherited from Filters::Base

filter_method, inherited, #parse_filter_a, #|

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Amrita2::Util::OptionSupport

Instance Method Details

#define_element_method(de, cg, &block) ⇒ Object



1050
1051
1052
# File 'lib/amrita2/template.rb', line 1050

def define_element_method(de, cg, &block)
  cg.def_method("render_with", "value", "__binding__", "__cnt__ = -1 ", &block)
end

#element_render_code(de, cg, element, &block) ⇒ Object



1092
1093
1094
1095
1096
1097
1098
# File 'lib/amrita2/template.rb', line 1092

def element_render_code(de, cg, element, &block)
  if ((element.name == 'span' or element.name == '_' or element.name == 'root') and element.attributes.size == 0)
    yield
  else
    cg.put_static_element(element, &block)
  end
end

#filter_element(de, element) ⇒ Object



998
999
1000
# File 'lib/amrita2/template.rb', line 998

def filter_element(de, element)
  element
end

#generate_dynamic_element(de, e) ⇒ Object



1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
# File 'lib/amrita2/template.rb', line 1025

def generate_dynamic_element(de, e)
  src = e.attributes[de.am_src]
  filters_src = e.attributes[de.am_filter]
  if src
    name, fs = src.split('|', 2)
    name.strip!
    filters = de.root.compile_filters(e, name, fs, filters_src)
    DynamicElement.new(de, name , e, filters)
  elsif filters_src
    filters = de.root.compile_filters(e, nil, filters_src)
    DynamicElement.new(de, nil , e, filters)
  else
    nil
  end
end

#loop_check_code(de, cg) ⇒ Object



1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
# File 'lib/amrita2/template.rb', line 1054

def loop_check_code(de, cg)
  cg.if_ "$_.kind_of?(Amrita2::Enum) and not $_.kind_of?(ScalarData)" do
    cg.code("$_.each_with_index  do |v, i| ")
    cg.level_up do
      cg.put_string_expression("render_with(v, __binding__, i) ")
    end
    cg.code("end")
    cg.code("return __stream__")
  end
end

#method_body_code(de, cg) ⇒ Object



1065
1066
1067
1068
1069
1070
# File 'lib/amrita2/template.rb', line 1065

def method_body_code(de, cg)
  de.value_filter_code(cg, "value")
  #cg.code("p #{de.element.name.inspect}, $_, stream")
  de.renderer_code(cg, de.element)
  #cg.code("p #{de.element.name.inspect}, $_, stream")
end

#parse_element(de, element) ⇒ Object



1002
1003
1004
1005
1006
# File 'lib/amrita2/template.rb', line 1002

def parse_element(de, element)
  element.enum_for(:each_child).collect do |node|
    de.parse_node(node)
  end
end

#parse_node(de, node) ⇒ Object



1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
# File 'lib/amrita2/template.rb', line 1008

def parse_node(de, node)
  case node
  when Hpricot::Elem
    de.generate_dynamic_element(node) || CompoundElement.new(de, node)
  when Hpricot::CData
    ErbNode.new(de, node)
  when Hpricot::Text
    StaticNode.new(de, node)
  when Hpricot::Comment
    CommentNode.new(de, node)
  when Hpricot::BogusETag
    StaticNode.new(de, node)
  else
    raise "not implemented #{node.class}"
  end
end

#renderer_code(de, cg, element) ⇒ Object



1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
# File 'lib/amrita2/template.rb', line 1075

def renderer_code(de, cg, element)
  r = de.renderers
  case r.size
  when 0
  when 1
    r.first.generate_body(cg, de, element)
  else
    cg.case_("$_") do
      r.each do |r1|
        r1.generate_when(cg) do
          r1.generate_body(cg, de, element)
        end
      end
    end
  end
end

#setup_type_renderer(de) ⇒ Object



1041
1042
1043
1044
1045
1046
1047
1048
# File 'lib/amrita2/template.rb', line 1041

def setup_type_renderer(de)
  if de.has_dynamic?
    de.renderers << HashRenderer.new unless de.renderers.find {|r| r.kind_of?(HashRenderer) }
  else
    de.renderers << ScalarRenderer.new
  end
  de.renderers << ElseRenderer.new
end

#value_filter_code(de, cg, value_name) ⇒ Object



1072
1073
# File 'lib/amrita2/template.rb', line 1072

def value_filter_code(de, cg, value_name)
end