require 'rexml/document' require 'rexml/streamlistener' require 'nitro/element' require "glue/html" module Nitro # A compiler that handles the processing of Elements class Elements # :nodoc: all class Listener # :nodoc: all include REXML::StreamListener attr_accessor :buffer attr_accessor :stack def initialize super @buffer = '' @stack = [] end PREFIX_RE = /^#{Element.prefix}:/ CAPITALIZED_RE = /^[A-Z]/ def tag_start(name, attributes) # check if the name starts with the element prefix, or # is capitalized. if name =~ PREFIX_RE or name =~ CAPITALIZED_RE name = name.split(':')[1].camelize if name =~ PREFIX_RE obj = Object.const_get(name).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 def tag_end(name) # check if the name starts with the element prefix, or # is capitalized. if name =~ PREFIX_RE or name =~ CAPITALIZED_RE name = name.split(':')[1].camelize if name =~ PREFIX_RE obj, @buffer, @parent = @stack.pop @buffer << obj.render else @buffer << "" end end def text(str) @buffer << str end def instruction(name, attributes) @buffer << "" end def comment(c) unless Template.strip_xml_comments @buffer << "" end end end class << self def parse(source) self.new.parse(source) end def transform(source) self.new.transform(source) end end # Expand the elemens found in source. def transform(source) listener = Listener.new REXML::Document.parse_stream(source, listener) # gmosx, FIXME: optimize this, how? # gmosx, FIXME: this is a hack fix, improve. # TODO:farms why is cleanup called this many times?!?!? ... waste of gsubs return listener.buffer end end # An (old) alias. unless const_defined? :ElementProcessor ElementProcessor = Elements end end # * George Moschovitis # * Chris Farmiloe