Class: HTML::AutoTag

Inherits:
Object
  • Object
show all
Defined in:
lib/HTML/AutoTag.rb,
lib/HTML/AutoTag/version.rb

Constant Summary collapse

VERSION =
"1.0.8"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ AutoTag

Defaults to empty string which produces no encoding.



13
14
15
16
17
18
19
20
21
# File 'lib/HTML/AutoTag.rb', line 13

def initialize( params = {} )
    @encodes    = params['encodes']
    @encode     = params['encode']  || !@encodes.nil?
    @indent     = params['indent']  || ''
    @level      = params['level']   || 0
    @sorted     = params['sorted']  ? 1 : 0
    @newline    = params['indent']  ? "\n" : ''
    @encoder    = HTML::Encoder.new
end

Instance Attribute Details

#encodeObject

Returns the value of attribute encode.



9
10
11
# File 'lib/HTML/AutoTag.rb', line 9

def encode
  @encode
end

#encodesObject

Returns the value of attribute encodes.



9
10
11
# File 'lib/HTML/AutoTag.rb', line 9

def encodes
  @encodes
end

#indentObject

Returns the value of attribute indent.



9
10
11
# File 'lib/HTML/AutoTag.rb', line 9

def indent
  @indent
end

#levelObject

Returns the value of attribute level.



9
10
11
# File 'lib/HTML/AutoTag.rb', line 9

def level
  @level
end

#newlineObject

Returns the value of attribute newline.



9
10
11
# File 'lib/HTML/AutoTag.rb', line 9

def newline
  @newline
end

#sortedObject

Returns the value of attribute sorted.



9
10
11
# File 'lib/HTML/AutoTag.rb', line 9

def sorted
  @sorted
end

Instance Method Details

#tag(params) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/HTML/AutoTag.rb', line 23

def tag( params )

    # TODO: make these method args if possible
    tag   = params['tag']
    attr  = params['attr']
    cdata = params['cdata']

    unless attr.kind_of?( HTML::AutoAttr )
        attr = HTML::AutoAttr.new( attr, @sorted )
    end

    # emtpy tag
    unless cdata and cdata.to_s.length
        return ( @indent * @level ) + '<' + tag + attr.to_s + ' />' + @newline
    end

    rendered       = ''
    no_post_indent = 0

    if cdata.kind_of?( Array )

        if cdata[0].kind_of?( Hash )
            @level += 1
            rendered = @newline

            cdata.each do |hash|
                rendered += tag( 'tag' => hash['tag'], 'attr' => hash['attr'], 'cdata' => hash['cdata'] )
            end
            @level -= 1
        else
            str = ''
            cdata.each do |scalar|
                str += tag( 'tag' => tag, 'attr' => attr, 'cdata' => scalar )
            end
            return str
        end

    elsif cdata.kind_of?( Hash )
        @level += 1
        rendered = @newline + tag( 'tag' => cdata['tag'], 'attr' => cdata['attr'], 'cdata' => cdata['cdata'] )
        @level -= 1

    else
        rendered = @encode ? @encoder.encode( cdata, @encodes ) : cdata
        no_post_indent = 1
    end

    return (@indent * @level)  \
        + '<' + tag + attr.to_s + '>'  \
        + rendered.to_s + ( no_post_indent == 1 ? '' : ( @indent * @level ) )  \
        + '</' + tag + '>' + @newline

end