class Nokogiri::XML::Builder

def cdata string

def cdata string
  node = Nokogiri::XML::CDATA.new(@doc, string.to_s)
  insert(node)
end

def initialize &block

def initialize &block
  namespace = self.class.name.split('::')
  namespace[-1] = 'Document'
  @doc = eval(namespace.join('::')).new
  @parent = @doc
  @context = nil
  if block_given?
    @context = eval('self', block.binding)
  end
  instance_eval(&block) if block_given?
  @parent = @doc
end

def insert(node, &block)

def insert(node, &block)
  node.parent = @parent
  if block_given?
    @parent = node
    instance_eval(&block)
    @parent = node.parent
  end
  NodeBuilder.new(node, self)
end

def method_missing method, *args, &block

def method_missing method, *args, &block
  if @context && @context.respond_to?(method)
    @context.send(method, *args, &block)
  else
    node = Nokogiri::XML::Node.new(method.to_s, @doc) { |n|
      args.each do |arg|
        case arg
        when Hash
          arg.each { |k,v| n[k.to_s] = v.to_s }
        else
          n.content = arg
        end
      end
    }
    insert(node, &block)
  end
end

def text string

def text string
  node = Nokogiri::XML::Text.new(string.to_s, @doc)
  insert(node)
end

def to_xml

def to_xml
  @doc.to_xml
end