Class: Amrita2::Filters::MacroFilter

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

Constant Summary

Constants included from Util

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

Constants included from Amrita2

Amrita2::FilterMethods, Hooki, SanitizedString

Instance Attribute Summary collapse

Attributes inherited from Base

#next_

Instance Method Summary collapse

Methods included from Util

sanitize_attribute_value, sanitize_text, sanitize_url

Methods inherited from Base

filter_method, inherited, #parse_filter_a, #|

Constructor Details

#initialize(*macros) ⇒ MacroFilter

Returns a new instance of MacroFilter.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/amrita2/macro.rb', line 83

def initialize(*macros)
  @macros = macros.collect do |m|
    case m
    when Class
      m.new
    else
      m
    end
  end
  @element_names = {}
  @macros.each do |m|
    @element_names[m.get_element_name.to_s] = true
  end
end

Instance Attribute Details

#macrosObject (readonly)

Returns the value of attribute macros.



81
82
83
# File 'lib/amrita2/macro.rb', line 81

def macros
  @macros
end

Instance Method Details

#check_element(element) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/amrita2/macro.rb', line 98

def check_element(element)
  return true if @element_names[element.name]
  element.each_child do |c|
    next unless c.kind_of?(Hpricot::Elem)
    return true if @element_names[c.name] or check_element(c)
  end
  false
end

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



144
145
146
147
148
149
150
# File 'lib/amrita2/macro.rb', line 144

def element_render_code(de, cg, element, &block)
  if (element.name == 'macroroot')
    yield
  else
    super
  end
end

#filter_element(de, element) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/amrita2/macro.rb', line 107

def filter_element(de, element)
  return element unless check_element(element)
  
  @macros.each do |m|
    if m.match_element(element)
      ret = m.process(de, element)
      ret = Core::PreProcessor.new.process(ret.dup)
      ret.gsub!('<%%', '<%')
      ret.gsub!('%%>', '%>')
      root = Hpricot.make("<_ />").first
      Hpricot.make(ret, :xml=>true).each do |e|
        root.insert_after(e, nil)
      end
      element = replace_target_src(root)
    else
      element.each_child do |c|
        next unless c.kind_of?(Hpricot::Elem)
        cc = filter_element(de, c)
        element.replace_child(c, cc) if c != cc
      end
    end
  end
  element
end

#replace_target_src(e) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/amrita2/macro.rb', line 132

def replace_target_src(e)
  %w(src filter v skipif for).each do |k|
    e.set_attribute("am:#{k}", e.attributes["target_#{k}"]) if e.attributes["target_#{k}"]
    e.delete_attribute("target_#{k}")
  end
  e.each_child do |c|
    next unless c.kind_of?(Hpricot::Elem)
    replace_target_src(c)
  end
  e
end