Class: Raw::ElementsFilter::Listener

Inherits:
Object
  • Object
show all
Includes:
REXML::StreamListener
Defined in:
lib/raw/compiler/filter/elements.rb

Overview

:nodoc: all

Constant Summary collapse

PREFIX_RE =
/^#{Element.prefix}:/
CAPITALIZED_RE =
/^[A-Z]/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeListener

Returns a new instance of Listener.



30
31
32
33
34
# File 'lib/raw/compiler/filter/elements.rb', line 30

def initialize
  super()
  @buffer = ''
  @stack = []
end

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer.



28
29
30
# File 'lib/raw/compiler/filter/elements.rb', line 28

def buffer
  @buffer
end

Instance Method Details

#cdata(content) ⇒ Object



141
142
143
# File 'lib/raw/compiler/filter/elements.rb', line 141

def cdata(content)
  @buffer << "<![CDATA[#{content}]]>"
end

#comment(c) ⇒ Object



145
146
147
148
149
# File 'lib/raw/compiler/filter/elements.rb', line 145

def comment(c)
  unless Template.strip_xml_comments
    @buffer << "<!--#{c}-->"
  end
end

#doctype(name, pub_sys, long_name, uri) ⇒ Object



151
152
153
# File 'lib/raw/compiler/filter/elements.rb', line 151

def doctype(name, pub_sys, long_name, uri)   
  @buffer << "<!DOCTYPE #{name} #{pub_sys} #{long_name} #{uri}>\n"
end

#instruction(name, attributes) ⇒ Object



137
138
139
# File 'lib/raw/compiler/filter/elements.rb', line 137

def instruction(name, attributes)
  @buffer << "<?#{name}#{attributes}?>"
end

#is_element?(name) ⇒ Boolean

Check if a tag is an Element. If found, it also tries to auto-extend the klass. Returns the Element class if found.

Tries many classes in the following order:

  • Controller::XXX

  • :element_namespace)::XXX

  • Nitro::Element::XXX

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/raw/compiler/filter/elements.rb', line 81

def is_element?(name)
  controller = Controller.current
  
  return false unless name =~ PREFIX_RE or name =~ CAPITALIZED_RE

  name = name.gsub(PREFIX_RE,'').camelize if name =~ PREFIX_RE

  # Try to use Controller::xxx
  # gmosx, THINK: this looks a bit dangerous to me!
  
  begin
    # gmosx, FIXME: Class.by_name also returns top level
    # classes, how can we fix this?

    klass = constant("#{controller}::#{name}")
  rescue
    # drink it!
  end

  # Try to use the Controller's :element_namespace annotation

  if namespace = controller.ann(:self, :element_namespace)
    begin
      klass = constant("#{namespace}::#{name}")
    rescue
      # drink it!
    end
  end unless klass

  # Try to use Nitro::Element::xxx then ::xxx
  
  begin
    klass = constant("Nitro::Element::#{name}")
  rescue
    # drink it!
  end unless klass

  return false unless klass.kind_of? Class

  # Try to auto-extend.

  unless klass.ancestor? Element
    if Element.auto_extend
      klass.send(:include, ElementMixin)
    else
      return false
    end
  end

  return klass
end

#tag_end(name) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/raw/compiler/filter/elements.rb', line 63

def tag_end(name)    
  if is_element? name
    obj, @buffer, @parent = @stack.pop
    @buffer << obj.render
  else
    @buffer << "</#{name}>"
  end
end

#tag_start(name, attributes) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/raw/compiler/filter/elements.rb', line 36

def tag_start(name, attributes)    
  if klass = is_element?(name)
    obj = klass.new
     
    attributes.each do | k, v | 
      obj.instance_variable_set("@#{k}", v)
    end        
      
    @stack.push [obj, @buffer, @parent]
      
    @buffer = obj._text
    @parent.add_child(obj) if @parent
      
    @parent = obj
  else # This is a static element.
    attrs = []
    
    attributes.each do | k, v | 
      attrs << %|#{k}="#{v}"|
    end
    
    attrs = attrs.empty? ? nil : " #{attrs.join(' ')}"
    
    @buffer << "<#{name}#{attrs}>"
  end
end

#text(str) ⇒ Object



133
134
135
# File 'lib/raw/compiler/filter/elements.rb', line 133

def text(str)
  @buffer << str
end