 #########################################################################
 
 
-xml = new Builder pretty:'·'
+xml = new Builder pretty:'.   '
 
-xml.on 'data', console.log
+xml.on 'data',   console.log
+xml.on 'end', -> console.log "done."
 
-a = xml.tag('test') version:3, alt:"info", border:0
-a.tag('middle')(center:yes).end()
-b = a.tag('foo')(bar:'moo', border:0)
+global = xml.tag('global')()
+test = global.tag('test') version:3, alt:"info", border:0
+test.tag('top').end center:yes
+foo = test.tag('foo')(bar:'moo', border:0)
+
+global.end()
+xml.end()
+
+foo.tag('first').end()
+bar = foo.tag('bar') x:2
+foo.tag('center').end args:true
+foo.tag('last').end()
+xxx = foo.tag('xxx') ccc:yes
+
+bar.end()
+xxx.tag('pok').end()
+foo.end()
+
+xxx.tag('asd').end()
+
+xxx.tag('happy').end dodo:null
+test.end()
+
+xxx.end()
 
 
-b.tag('lol').end()
-c = b.tag('bar') x:2
-
-c.end()
-b.end()
-a.end()
-xml.end()
 
 # html template engine
 


    end: () =>
        if @headers
            @emit 'data', "#{indent this}#{@headers}/>"
        else
            @emit 'data', "#{indent this}</#{@name}>"
        @emit 'end'


class Builder extends EventEmitter
    constructor: (@opts = {}) ->
        @opts.pretty ?= off
        @level = @opts.level ? 0


        new_tag.call this, name, opts:@opts, level:@level

    end: () =>
        @emit 'end' unless @pending.length

    # classes
    Tag:Tag
