Spreadsheet-HTML 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Changes +4 -0
- data/README +17 -2
- data/lib/Spreadsheet/HTML.rb +56 -19
- data/lib/Spreadsheet/HTML/version.rb +1 -1
- data/t/04-headless.rb +6 -0
- data/t/07-attrs.rb +113 -0
- data/t/09-padding.rb +30 -0
- data/t/11-empty.rb +7 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 74459c74abf314da4530023e49d8fe8215d4749b
|
4
|
+
data.tar.gz: 831d56fd9fa50caa9fde4549e299b0f12a8f0fe7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1ea3934540e350a44509d2f3cc7e8dc2c302b8d1c578464467a5cc781a502f750678d0a7d2f543b2c46ba936e8b8b8c97c8d66dc420cdbe77c976b5fae2455b8
|
7
|
+
data.tar.gz: 4b71690518be878ec820a4cc0349a29d2c5601d4fbd2ad9b814a3c518eb7ce80d3d737a9f954a1f8a896686e140cbfe4d230ae077c97129655224638bf5b967b
|
data/Changes
CHANGED
data/README
CHANGED
@@ -109,8 +109,23 @@ fill
|
|
109
109
|
fill => '8x12' # 8 rows, 12 columns
|
110
110
|
|
111
111
|
== Dynamic Parameters
|
112
|
-
|
113
|
-
|
112
|
+
Dynamic parameters provide a means to control the micro elements of the table,
|
113
|
+
such as modifying headings by their name and rows and columns by their indices.
|
114
|
+
They contain leading underscores to seperate them from literal and tag parameters.
|
115
|
+
|
116
|
+
_rX
|
117
|
+
Hash. Apply these attributes to row X (zero index based).
|
118
|
+
|
119
|
+
_cX
|
120
|
+
Hash. Apply these attributes to colum X (zero index based). You can also alias any
|
121
|
+
column by the value of the heading name in that column prepended with underscore (_)
|
122
|
+
|
123
|
+
_occupation => { 'class' => 'foo' }
|
124
|
+
|
125
|
+
_salary => { 'class' => 'foo' }
|
126
|
+
|
127
|
+
_rXcY
|
128
|
+
Hash. Apply these attributes to colum Y in row X (zero index based).
|
114
129
|
|
115
130
|
== Tag Parameters
|
116
131
|
Tag Parameters provide a means to control the attributes of the table's tags, and in
|
data/lib/Spreadsheet/HTML.rb
CHANGED
@@ -127,39 +127,56 @@ module Spreadsheet
|
|
127
127
|
def _process( args )
|
128
128
|
params = _args( args )
|
129
129
|
|
130
|
-
# headings is an alias for
|
131
|
-
params['
|
130
|
+
# headings is an alias for _r0
|
131
|
+
params['_r0'] = params['headings']
|
132
132
|
|
133
133
|
index = {}
|
134
134
|
if params['data'][0].size()
|
135
|
-
|
135
|
+
for i in 0 .. params['data'][0].size() - 1
|
136
|
+
key = params['data'][0][i] || ''
|
137
|
+
index["_#{key}"] = i
|
138
|
+
end
|
139
|
+
|
140
|
+
params.keys.grep( /^_/ ) do |key|
|
141
|
+
k = index[key]
|
142
|
+
params["_c#{k}"] = params[key] if index.has_key?( key )
|
143
|
+
end
|
136
144
|
end
|
137
145
|
|
138
|
-
|
139
|
-
empty = params.has_key?('empty') ? params['empty'] : ' '
|
146
|
+
empty = params['empty'] || ' '
|
140
147
|
tag = ( params['matrix'] or params['headless'] ) ? 'td' : 'th'
|
141
148
|
|
142
149
|
encoder = Enco::Der.new
|
143
|
-
for
|
150
|
+
for r in 0 .. params['_max_rows'] - 1
|
144
151
|
|
145
|
-
data[i] ||= []
|
146
152
|
unless params['_layout']
|
147
|
-
|
148
|
-
(
|
153
|
+
params['data'][r] ||= []
|
154
|
+
(params['_max_cols'] - params['data'][r].size).times { params['data'][r].push( nil ) } # pad
|
155
|
+
(params['data'][r].size - params['_max_cols']).times { params['data'][r].pop } # truncate
|
149
156
|
end
|
150
157
|
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
158
|
+
row = []
|
159
|
+
for c in 0 .. params['_max_cols'] - 1
|
160
|
+
|
161
|
+
attr = params[tag] || {}
|
162
|
+
cdata = params['data'][r][c].to_s
|
163
|
+
|
164
|
+
[ tag, "_c#{c}", "_r#{r}", "_r#{r}c#{c}" ].each do |dyna_param|
|
165
|
+
if params.has_key?( dyna_param )
|
166
|
+
( cdata, attr ) = _extrapolate( cdata, attr, params[dyna_param] );
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
cdata = encoder.encode( cdata, params['encodes'] ) if params['encode'] or !params['encodes'].to_s.empty?
|
171
|
+
cdata = cdata.gsub( /^\s*$/, empty.to_s )
|
172
|
+
row.push( { 'tag' => tag, 'attr' => attr, 'cdata' => cdata } )
|
173
|
+
|
157
174
|
end
|
158
|
-
|
175
|
+
|
176
|
+
params['data'][r] = row
|
159
177
|
tag = 'td'
|
160
178
|
end
|
161
179
|
|
162
|
-
params['data'] = data
|
163
180
|
params['data'].shift if params['headless']
|
164
181
|
|
165
182
|
return params
|
@@ -216,13 +233,33 @@ module Spreadsheet
|
|
216
233
|
|
217
234
|
if params['fill']
|
218
235
|
(row,col) = params['fill'].split(/\D/)
|
219
|
-
|
220
|
-
|
236
|
+
if row.to_i > 0 && col.to_i > 0
|
237
|
+
params['_max_rows'] = row.to_i if row.to_i > params['_max_rows']
|
238
|
+
params['_max_cols'] = col.to_i if col.to_i > params['_max_cols']
|
239
|
+
end
|
221
240
|
end
|
222
241
|
|
223
242
|
return params
|
224
243
|
end
|
225
244
|
|
245
|
+
def _extrapolate( cdata, orig_attr, thingy )
|
246
|
+
new_attr = {}
|
247
|
+
thingy = [ thingy ] unless thingy.kind_of?(Array)
|
248
|
+
thingy.each do |t|
|
249
|
+
if t.kind_of?(Hash)
|
250
|
+
new_attr = t
|
251
|
+
#elsif t.kind_of?(Code)
|
252
|
+
# puts "What now?"
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
attr = {}
|
257
|
+
orig_attr.each { |key,val| attr[key] = val }
|
258
|
+
new_attr.each { |key,val| attr[key] = val }
|
259
|
+
|
260
|
+
return [ cdata, attr ]
|
261
|
+
end
|
262
|
+
|
226
263
|
end
|
227
264
|
|
228
265
|
end
|
data/t/04-headless.rb
CHANGED
@@ -40,6 +40,12 @@ class Test_Headless < Test::Unit::TestCase
|
|
40
40
|
"one named arg"
|
41
41
|
)
|
42
42
|
|
43
|
+
assert_equal(
|
44
|
+
'<table><tr><th>1</th><th>a</th></tr><tr><td>2</td><td>b</td></tr><tr><td>3</td><td>c</td></tr></table>',
|
45
|
+
gen.generate( 'data' => data ),
|
46
|
+
"headless does not change orig data"
|
47
|
+
)
|
48
|
+
|
43
49
|
end
|
44
50
|
|
45
51
|
def test_class
|
data/t/07-attrs.rb
CHANGED
@@ -181,4 +181,117 @@ class Test_Attributes < Test::Unit::TestCase
|
|
181
181
|
)
|
182
182
|
|
183
183
|
end
|
184
|
+
|
185
|
+
def test_headings
|
186
|
+
|
187
|
+
data = Array[
|
188
|
+
%w(header1 header2 header3 header4),
|
189
|
+
%w(foo1 bar1 baz1 qux1),
|
190
|
+
%w(foo2 bar2 baz2 qux2),
|
191
|
+
%w(foo3 bar3 baz3 qux3),
|
192
|
+
%w(foo4 bar4 baz4 qux4)
|
193
|
+
]
|
194
|
+
|
195
|
+
html = '<table><tr><th style="color: red;">header1</th><th style="color: green;">header2</th><th style="color: blue;">header3</th><th style="color: red;">header4</th></tr><tr><td>foo1</td><td>bar1</td><td>baz1</td><td>qux1</td></tr><tr><td>foo2</td><td>bar2</td><td>baz2</td><td>qux2</td></tr><tr><td>foo3</td><td>bar3</td><td>baz3</td><td>qux3</td></tr><tr><td>foo4</td><td>bar4</td><td>baz4</td><td>qux4</td></tr></table>'
|
196
|
+
|
197
|
+
assert_equal(
|
198
|
+
html,
|
199
|
+
Spreadsheet::HTML.new( 'data' => data, 'headings' => { 'style' => { 'color' => %w{ red green blue } } } ).generate(),
|
200
|
+
"via constructor only"
|
201
|
+
)
|
202
|
+
assert_equal(
|
203
|
+
html,
|
204
|
+
Spreadsheet::HTML.new().generate( 'data' => data, 'headings' => { 'style' => { 'color' => %w{ red green blue } } } ),
|
205
|
+
"via method only"
|
206
|
+
)
|
207
|
+
assert_equal(
|
208
|
+
html,
|
209
|
+
Spreadsheet::HTML.new( 'data' => data ).generate( 'headings' => { 'style' => { 'color' => %w{ red green blue } } } ),
|
210
|
+
"via constructor and method"
|
211
|
+
)
|
212
|
+
|
213
|
+
html = '<table><tr><th>header1</th><th style="color: red;">header2</th><th>header3</th><th>header4</th></tr><tr><td>foo1</td><td style="color: green;">bar1</td><td>baz1</td><td>qux1</td></tr><tr><td>foo2</td><td style="color: blue;">bar2</td><td>baz2</td><td>qux2</td></tr><tr><td>foo3</td><td style="color: red;">bar3</td><td>baz3</td><td>qux3</td></tr><tr><td>foo4</td><td style="color: green;">bar4</td><td>baz4</td><td>qux4</td></tr></table>'
|
214
|
+
|
215
|
+
assert_equal(
|
216
|
+
html,
|
217
|
+
Spreadsheet::HTML.new( 'data' => data, '_header2' => { 'style' => { 'color' => %w{ red green blue } } } ).generate(),
|
218
|
+
"via constructor only"
|
219
|
+
)
|
220
|
+
assert_equal(
|
221
|
+
html,
|
222
|
+
Spreadsheet::HTML.new().generate( 'data' => data, '_header2' => { 'style' => { 'color' => %w{ red green blue } } } ),
|
223
|
+
"via method only"
|
224
|
+
)
|
225
|
+
assert_equal(
|
226
|
+
html,
|
227
|
+
Spreadsheet::HTML.new( 'data' => data ).generate( '_header2' => { 'style' => { 'color' => %w{ red green blue } } } ),
|
228
|
+
"via constructor and method"
|
229
|
+
)
|
230
|
+
|
231
|
+
end
|
232
|
+
|
233
|
+
def test_dynamic_params
|
234
|
+
|
235
|
+
data = Array[
|
236
|
+
%w(header1 header2 header3),
|
237
|
+
%w(foo1 bar1 baz1),
|
238
|
+
%w(foo2 bar2 baz2),
|
239
|
+
]
|
240
|
+
|
241
|
+
html = '<table><tr><th>header1</th><th style="color: red;">header2</th><th>header3</th></tr><tr><td>foo1</td><td style="color: green;">bar1</td><td>baz1</td></tr><tr><td>foo2</td><td style="color: blue;">bar2</td><td>baz2</td></tr></table>';
|
242
|
+
|
243
|
+
assert_equal(
|
244
|
+
html,
|
245
|
+
Spreadsheet::HTML.new( 'data' => data, '_c1' => { 'style' => { 'color' => %w{ red green blue } } } ).generate(),
|
246
|
+
"_c1 via constructor only"
|
247
|
+
)
|
248
|
+
assert_equal(
|
249
|
+
html,
|
250
|
+
Spreadsheet::HTML.new().generate( 'data' => data, '_c1' => { 'style' => { 'color' => %w{ red green blue } } } ),
|
251
|
+
"_c1 via method only"
|
252
|
+
)
|
253
|
+
assert_equal(
|
254
|
+
html,
|
255
|
+
Spreadsheet::HTML.new( 'data' => data ).generate( '_c1' => { 'style' => { 'color' => %w{ red green blue } } } ),
|
256
|
+
"_c1 via constructor and method"
|
257
|
+
)
|
258
|
+
|
259
|
+
html = '<table><tr><th>header1</th><th>header2</th><th>header3</th></tr><tr><td style="color: red;">foo1</td><td style="color: green;">bar1</td><td style="color: blue;">baz1</td></tr><tr><td>foo2</td><td>bar2</td><td>baz2</td></tr></table>';
|
260
|
+
|
261
|
+
assert_equal(
|
262
|
+
html,
|
263
|
+
Spreadsheet::HTML.new( 'data' => data, '_r1' => { 'style' => { 'color' => %w{ red green blue } } } ).generate(),
|
264
|
+
"_r1 via constructor only"
|
265
|
+
)
|
266
|
+
assert_equal(
|
267
|
+
html,
|
268
|
+
Spreadsheet::HTML.new().generate( 'data' => data, '_r1' => { 'style' => { 'color' => %w{ red green blue } } } ),
|
269
|
+
"_r1 via method only"
|
270
|
+
)
|
271
|
+
assert_equal(
|
272
|
+
html,
|
273
|
+
Spreadsheet::HTML.new( 'data' => data ).generate( '_r1' => { 'style' => { 'color' => %w{ red green blue } } } ),
|
274
|
+
"_r1 via constructor and method"
|
275
|
+
)
|
276
|
+
|
277
|
+
html = '<table><tr><th>header1</th><th>header2</th><th>header3</th></tr><tr><td>foo1</td><td>bar1</td><td style="color: red;">baz1</td></tr><tr><td>foo2</td><td>bar2</td><td>baz2</td></tr></table>';
|
278
|
+
|
279
|
+
assert_equal(
|
280
|
+
html,
|
281
|
+
Spreadsheet::HTML.new( 'data' => data, '_r1c2' => { 'style' => { 'color' => %w{ red green blue } } } ).generate(),
|
282
|
+
"_r1c2 via constructor only"
|
283
|
+
)
|
284
|
+
assert_equal(
|
285
|
+
html,
|
286
|
+
Spreadsheet::HTML.new().generate( 'data' => data, '_r1c2' => { 'style' => { 'color' => %w{ red green blue } } } ),
|
287
|
+
"_r1c2 via method only"
|
288
|
+
)
|
289
|
+
assert_equal(
|
290
|
+
html,
|
291
|
+
Spreadsheet::HTML.new( 'data' => data ).generate( '_r1c2' => { 'style' => { 'color' => %w{ red green blue } } } ),
|
292
|
+
"_r1c2 via constructor and method"
|
293
|
+
)
|
294
|
+
|
295
|
+
end
|
296
|
+
|
184
297
|
end
|
data/t/09-padding.rb
CHANGED
@@ -49,12 +49,42 @@ class Test_Padding < Test::Unit::TestCase
|
|
49
49
|
"fill works with no data"
|
50
50
|
)
|
51
51
|
|
52
|
+
assert_equal(
|
53
|
+
'<table><tr><th>header1</th><th>header2</th></tr><tr><td>foo1</td><td>bar1</td></tr><tr><td>foo2</td><td>bar2</td></tr></table>',
|
54
|
+
gen.generate( 'fill' => '1x2', 'data' => data ),
|
55
|
+
"fill does not truncate larger data"
|
56
|
+
)
|
57
|
+
|
58
|
+
assert_equal(
|
59
|
+
'<table><tr><th> </th></tr></table>',
|
60
|
+
gen.generate( 'fill' => nil ),
|
61
|
+
"fill defaults to 1x1 with with invalid data (nil)"
|
62
|
+
)
|
63
|
+
|
64
|
+
assert_equal(
|
65
|
+
'<table><tr><th> </th></tr></table>',
|
66
|
+
gen.generate( 'fill' => '' ),
|
67
|
+
"fill defaults to 1x1 with with invalid data (empty)"
|
68
|
+
)
|
69
|
+
|
52
70
|
assert_equal(
|
53
71
|
'<table><tr><th> </th></tr></table>',
|
54
72
|
gen.generate( 'fill' => '0x0' ),
|
55
73
|
"fill defaults to 1x1 with with invalid data (zero)"
|
56
74
|
)
|
57
75
|
|
76
|
+
assert_equal(
|
77
|
+
'<table><tr><th> </th></tr></table>',
|
78
|
+
gen.generate( 'fill' => '-2x4' ),
|
79
|
+
"fill defaults to 1x1 with with invalid data (first negative)"
|
80
|
+
)
|
81
|
+
|
82
|
+
assert_equal(
|
83
|
+
'<table><tr><th> </th></tr></table>',
|
84
|
+
gen.generate( 'fill' => '1x-3' ),
|
85
|
+
"fill defaults to 1x1 with with invalid data (second negative)"
|
86
|
+
)
|
87
|
+
|
58
88
|
assert_equal(
|
59
89
|
'<table><tr><th> </th></tr></table>',
|
60
90
|
gen.generate( 'fill' => 'axb' ),
|
data/t/11-empty.rb
CHANGED
@@ -21,7 +21,13 @@ class Test_Empty < Test::Unit::TestCase
|
|
21
21
|
assert_equal(
|
22
22
|
'<table><tr><th>header1</th><th>header2</th><th>header3</th></tr><tr><td>foo1</td><td> </td><td>baz1</td></tr><tr><td> </td><td>bar2</td><td> </td></tr></table>',
|
23
23
|
gen.generate( 'empty' => ' ' ),
|
24
|
-
"empty values
|
24
|
+
"empty values can be overriden (space)"
|
25
|
+
)
|
26
|
+
|
27
|
+
assert_equal(
|
28
|
+
'<table><tr><th>header1</th><th>header2</th><th>header3</th></tr><tr><td>foo1</td><td>0</td><td>baz1</td></tr><tr><td>0</td><td>bar2</td><td>0</td></tr></table>',
|
29
|
+
gen.generate( 'empty' => 0 ),
|
30
|
+
"empty values can be overriden (zero)"
|
25
31
|
)
|
26
32
|
|
27
33
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: Spreadsheet-HTML
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jeffa
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|