     return tag
 
 
-sync_tag = (name, attrs, children, opts) ->
-    unless typeof attrs is 'object'
-        [opts, children, attrs] = [children, attrs, {}]
-    else
-        attrs ?= {}
-    opts ?= {}
-    opts.direct ?= yes
-    self_ending_children_scope = ->
-        @children children, direct:yes
-        @end()
-    @tag.call this, name, attrs, self_ending_children_scope, opts
-
 
 class Tag extends EventEmitter
     constructor: (@name, @attrs, children, @opts) ->

        attrs ?= {}
    opts ?= {}
    pipe = {}
    opts.level = @level+1
    opts = deep_merge @opts, opts # possibility to overwrite existing opts, like pretty


    @pending.push tag = new @Tag name, attrs, _children, opts

    tag.up = (opts = {}) => # set parent
        opts.end ?= true

        this



class Builder extends EventEmitter
    constructor: (@opts = {}) ->
        @builder = this
        @buffer = [] # for child output
        @closed = no
        @pending = [] # no open child tag
        @opts.builder ?= this
        @opts.pretty ?= off
        @level = @opts.level ? 0
        @Tag = @opts.Tag or Tag

    _new_tag: (parent, args...) =>
        if parent.headers
            parent.emit 'data', "#{indent parent}#{parent.headers}>"
            delete parent.headers
        new_tag.apply parent, [this].concat args

    _new_sync_tag: (parent, name, attrs, children, opts) =>
        # sync tag, - same as normal tag, but closes it automaticly
        unless typeof attrs is 'object'
            [opts, children, attrs] = [children, attrs, {}]
        else
            attrs ?= {}
        opts ?= {}
        self_ending_children_scope = ->
            @children children
            @end()
        @_new_tag parent, name, attrs, self_ending_children_scope, opts

    tag: (args...) =>
        @level--
        tag = @_new_tag.apply this, [this].concat args
        @level++
        tag

    $tag: (args...) =>
        @level--
        tag = @_new_sync_tag.apply this, [this].concat args
        @level++
        tag

    end: (data) =>
        if @pending.length
