require 'rubygems'
require 'builder'

x = Builder::XmlMarkup.new

test = 
  x.html {
    x.body {
     begin
       x.p {
         raise Exception.new('boom')
       }
     rescue Exception => e
       x.pre e
     end
    }
  }

if test.index('<p>') and !test.index('</p>')
  module Builder
    class XmlMarkup
      def method_missing(sym, *args, &block)
          text = nil
        attrs = nil
        sym = "#{sym}:#{args.shift}" if args.first.kind_of?(Symbol)
        args.each do |arg|
          case arg
          when Hash
            attrs ||= {}
            attrs.merge!(arg)
          else
            text ||= ''
            text << arg.to_s
          end
        end
        if block
          unless text.nil?
            raise ArgumentError, "XmlMarkup cannot mix a text argument with a block"
          end
          _indent
          _start_tag(sym, attrs)
          _newline
          begin ### Added
            _nested_structures(block)
          ensure ### Added
            _indent
            _end_tag(sym)
            _newline
          end ### Added
        elsif text.nil?
          _indent
          _start_tag(sym, attrs, true)
          _newline
        else
          _indent
          _start_tag(sym, attrs)
          text! text
          _end_tag(sym)
          _newline
        end
        @target
      end
    end
  end
end
