Class: Spreadsheet::HTML

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

Constant Summary collapse

VERSION =
"0.0.6"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ HTML

Returns a new instance of HTML.



92
93
94
95
96
97
# File 'lib/Spreadsheet/HTML.rb', line 92

def initialize( *args )
    (args[0] || []).each do |key,val|
        self.instance_eval { class << self; self end }.send(:attr_accessor, key)
        self.send( "#{key}=", val )
    end
end

Class Method Details

.gen(*args) ⇒ Object



13
14
15
# File 'lib/Spreadsheet/HTML.rb', line 13

def self.gen( *args )
    self.new.generate( *args )
end

Instance Method Details

#_args(things) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/Spreadsheet/HTML.rb', line 161

def _args( things )

    data   = []
    args   = []
    while !things.empty?

        if things[0].kind_of?(Array)
            data.push( things.shift() )
            if things[0].kind_of?(Array)
                data.push( things.shift() )
            else
                args.push( things.shift() ) if !things.empty?
                args.push( things.shift() ) if !things.empty?
            end
        else
            args.push( things.shift() ) if !things.empty?
            args.push( things.shift() ) if !things.empty?
        end

    end

    params = {}
    self.instance_variables.each do |attr|
        params[attr[1..-1]] = self.instance_variable_get attr
    end

    (args || []).each do |hash|
        hash.each do |key,val|
            params[key] = val
        end
    end

    params['auto'] = Auto::Tag.new(
        'encodes'   => params.has_key?('encodes') ? params['encodes'] : '',
        'indent'    => params['indent'],
        'level'     => params['level'],
        'sorted'    => params['attr_sort'],
    )

    if !params['data'] and data[0].kind_of?(Array)
        data = [ data ] if !data[0][0].kind_of?(Array)
        params['data'] = data[0]
    end

    params['_max_rows'] = params['data'].size
    params['_max_cols'] = params['data'][0].size
    params['data']      = params['data'].clone

    return params
end

#_make_table(params) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/Spreadsheet/HTML.rb', line 99

def _make_table( params )
    cdata = [] # TODO: insert caption and colgroup

    if params['tgroups'] && params['tgroups'] > 0

        body = params['data']
        head = body.shift() unless params['matrix'] && data.size() > 2
        foot = body.pop() if !params['matrix'] && params['tgroups'] > 1 and data.size() > 2

        head_row  = { 'tag' => 'tr', 'attr' => params['thead.tr'] || {}, 'cdata' => head }
        foot_row  = { 'tag' => 'tr', 'attr' => params['tfoot.tr'] || {}, 'cdata' => foot }
        body_rows = body.map{ |r| { 'tag' => 'tr', 'attr' => params['tr'] || {}, 'cdata' => r } }

        cdata.push({ 'tag' => 'thead', 'attr' => params['thead'] || {}, 'cdata' => head_row }) if head
        cdata.push({ 'tag' => 'tfoot', 'attr' => params['tfoot'] || {}, 'cdata' => foot_row }) if foot
        cdata.push({ 'tag' => 'tbody', 'attr' => params['tbody'] || {}, 'cdata' => body_rows })

    else
        cdata.push( params['data'].map { |c| { 'tag' => 'tr', 'attr' => params['tr'] || {}, 'cdata' => c } } )
    end

    return params['auto'].tag( 'tag' => 'table', 'attr' => params['table'] || {}, 'cdata' => cdata )
end

#_process(args) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/Spreadsheet/HTML.rb', line 123

def _process( args )
    params = _args( args )

    # headings is an alias for -r0
    params['-r0'] = params['headings']

    index = {}
    if params['data'][0].size()
        # implement index mapping
    end

    data  = []
    empty = params.has_key?('empty') ? params['empty'] : '&nbsp;'
    tag   = ( params['matrix'] or params['headless'] ) ? 'td' : 'th'

    params['data'].each do |row|

        unless params['_layout']
            (params['_max_cols'] - row.size).times { row.push( nil ) }  # pad
            (row.size - params['_max_cols']).times { row.pop }          # truncate
        end

        r = []
        row.each do |col|
            col = col.to_s
            col = col.gsub( /^\s*$/, empty )
            r.push( { 'tag' => tag, 'attr' => params[tag] || {}, 'cdata' => col } )
        end
        data.push( r )
        tag = 'td'
    end

    params['data'] = data
    params['data'].shift if params['headless']

    return params
end

#east(*args) ⇒ Object



33
34
35
# File 'lib/Spreadsheet/HTML.rb', line 33

def east( *args )
    generate( *args, 'theta' => 90, 'tgroups' => 0, 'pinhead' => 1 )
end

#generate(*args) ⇒ Object



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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/Spreadsheet/HTML.rb', line 41

def generate( *args )
    params = _process( args )

    if params['theta'] and params['flip'] 
        params['theta'] *= -1 
    end

    if !params['theta']           # north
        if params['flip']
            params['data'] = params['data'].map {|a| a.reverse }
        end

    elsif params['theta'] == 90   # east
        if params['pinhead'] and !params['headless']
            params['data'] = params['data'].transpose.map{|a| a.push( a.shift ) }
        else
            params['data'] = params['data'].transpose.map{|a| a.reverse }
        end

    elsif params['theta'] == -90
        if params['pinhead'] and !params['headless']
            params['data'] = params['data'].transpose.reverse.map {|a| a.push( a.shift ) } 
        else
            params['data'] = params['data'].transpose.reverse.map {|a| a.reverse }
        end

    elsif params['theta'] == 180
        if params['pinhead'] and !params['headless']
            params['data'] = params['data'].push( params['data'].shift ).map {|a| a.reverse }
        else
            params['data'] = params['data'].reverse.map {|a| a.reverse }
        end

    elsif params['theta'] == -180 # south
        if params['pinhead'] and !params['headless']
            params['data'] = params['data'].push( params['data'].shift )
        else
            params['data'] = params['data'].reverse
        end

    elsif params['theta'] == 270
        params['data'] = params['data'].transpose.reverse

    elsif params['theta'] == -270 # west
        params['data'] = params['data'].transpose

    end

    return _make_table( params )
end

#landscape(*args) ⇒ Object



29
30
31
# File 'lib/Spreadsheet/HTML.rb', line 29

def landscape( *args )
    generate( *args, 'theta' => -270, 'tgroups' => 0 )
end

#north(*args) ⇒ Object



17
18
19
# File 'lib/Spreadsheet/HTML.rb', line 17

def north( *args )
    generate( *args, 'theta' => 0 )
end

#portrait(*args) ⇒ Object



21
22
23
# File 'lib/Spreadsheet/HTML.rb', line 21

def portrait( *args )
    generate( *args, 'theta' => 0 )
end

#south(*args) ⇒ Object



37
38
39
# File 'lib/Spreadsheet/HTML.rb', line 37

def south( *args )
    generate( *args, 'theta' => -180, 'tgroups' => 0, 'pinhead' => 1 )
end

#west(*args) ⇒ Object



25
26
27
# File 'lib/Spreadsheet/HTML.rb', line 25

def west( *args )
    generate( *args, 'theta' => -270, 'tgroups' => 0 )
end