html-table 1.3.1 → 1.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +8 -1
- data/MANIFEST +0 -1
- data/Rakefile +24 -24
- data/doc/table_content.rdoc +15 -15
- data/html-table-1.3.2.gem +0 -0
- data/html-table.gemspec +25 -0
- data/lib/html/attribute_handler.rb +411 -411
- data/lib/html/body.rb +38 -38
- data/lib/html/caption.rb +48 -46
- data/lib/html/col.rb +41 -38
- data/lib/html/colgroup.rb +113 -110
- data/lib/html/content.rb +18 -18
- data/lib/html/data.rb +68 -65
- data/lib/html/foot.rb +49 -49
- data/lib/html/head.rb +49 -49
- data/lib/html/header.rb +65 -65
- data/lib/html/html_handler.rb +120 -120
- data/lib/html/row.rb +188 -185
- data/lib/html/table.rb +311 -255
- data/lib/html/tablesection.rb +48 -45
- data/lib/html/tag_handler.rb +123 -123
- data/test/tc_attribute_handler.rb +263 -273
- data/test/tc_body.rb +87 -97
- data/test/tc_caption.rb +80 -90
- data/test/tc_col.rb +40 -50
- data/test/tc_colgroup.rb +89 -99
- data/test/tc_data.rb +77 -87
- data/test/tc_foot.rb +108 -118
- data/test/tc_head.rb +104 -114
- data/test/tc_header.rb +77 -87
- data/test/tc_html_handler.rb +37 -47
- data/test/tc_row.rb +141 -151
- data/test/tc_table.rb +154 -163
- data/test/tc_tablesection.rb +42 -52
- data/test/tc_tag_handler.rb +82 -92
- metadata +29 -10
- data/test/ts_all.rb +0 -24
data/lib/html/table.rb
CHANGED
@@ -1,255 +1,311 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
76
|
-
|
77
|
-
#
|
78
|
-
#
|
79
|
-
#
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
end
|
87
|
-
|
88
|
-
#
|
89
|
-
#
|
90
|
-
#
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
#
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
#
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
#
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
end
|
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
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
#
|
164
|
-
#
|
165
|
-
#
|
166
|
-
#
|
167
|
-
#
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
#
|
173
|
-
#
|
174
|
-
def
|
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
|
-
)
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
|
3
|
+
require 'attribute_handler'
|
4
|
+
require 'html_handler'
|
5
|
+
require 'strongtyping'
|
6
|
+
include StrongTyping
|
7
|
+
|
8
|
+
module HTML
|
9
|
+
class Table < Array
|
10
|
+
include AttributeHandler
|
11
|
+
include HtmlHandler
|
12
|
+
|
13
|
+
# The version of this library
|
14
|
+
VERSION = '1.3.2'
|
15
|
+
|
16
|
+
# The indentation level for the <table> and </table> tags
|
17
|
+
@indent_level = 0
|
18
|
+
|
19
|
+
# The default character case used for printing output
|
20
|
+
@html_case = 'lower'
|
21
|
+
|
22
|
+
# Determines whether or not end tags will be included in printed output
|
23
|
+
@@global_end_tags = true
|
24
|
+
|
25
|
+
# Returns a new Table object. Optionally takes a block which is
|
26
|
+
# eval'd if provided. If an argument is provided it is interpreted as
|
27
|
+
# content. See the Table#content= method for how that data will be
|
28
|
+
# interpreted.
|
29
|
+
#
|
30
|
+
# Examples:
|
31
|
+
#
|
32
|
+
# # A single data item
|
33
|
+
# HTML::Table.new(1).html
|
34
|
+
#
|
35
|
+
# # Output
|
36
|
+
# <table>
|
37
|
+
# <tr>
|
38
|
+
# <td>1</td>
|
39
|
+
# </tr>
|
40
|
+
# </table>
|
41
|
+
#
|
42
|
+
# # One row per data item
|
43
|
+
# HTML::Table.new(['Matz', 'Larry', 'Guido']).html
|
44
|
+
#
|
45
|
+
# # Output
|
46
|
+
# <table>
|
47
|
+
# <tr>
|
48
|
+
# <td>Matz</td>
|
49
|
+
# </tr>
|
50
|
+
# <tr>
|
51
|
+
# <td>Larry</td>
|
52
|
+
# </tr>
|
53
|
+
# <tr>
|
54
|
+
# <td>Guido</td>
|
55
|
+
# </tr>
|
56
|
+
# </tr>
|
57
|
+
# </table>
|
58
|
+
#
|
59
|
+
# # Multiple data items per row
|
60
|
+
# Table.new{ |t|
|
61
|
+
# t.content = [['a','b'], [1,2], ['x','y']]
|
62
|
+
# }.html
|
63
|
+
#
|
64
|
+
# # Output
|
65
|
+
# <table>
|
66
|
+
# <tr>
|
67
|
+
# <td>a</td>
|
68
|
+
# <td>b</td>
|
69
|
+
# </tr>
|
70
|
+
# <tr>
|
71
|
+
# <td>1</td>
|
72
|
+
# <td>2</td>
|
73
|
+
# </tr>
|
74
|
+
# <tr>
|
75
|
+
# <td>x</td>
|
76
|
+
# <td>y</td>
|
77
|
+
# </tr>
|
78
|
+
# </table>
|
79
|
+
#
|
80
|
+
def initialize(arg = nil, &block)
|
81
|
+
@html_begin = '<table'
|
82
|
+
@html_body = ''
|
83
|
+
@html_end = '</table>'
|
84
|
+
instance_eval(&block) if block
|
85
|
+
self.content = arg if arg
|
86
|
+
end
|
87
|
+
|
88
|
+
# Adds content to the table. How this method behaves depends on the
|
89
|
+
# type of argument being passed.
|
90
|
+
#
|
91
|
+
# The +arg+ may be a Table::Row object, an Array of Table::Row objects,
|
92
|
+
# an Array of Array's, an Array of Strings, or a single String. In the
|
93
|
+
# last two cases, a single Table::Row with a single Table::Row::Data
|
94
|
+
# object is created, with the string as the content.
|
95
|
+
#
|
96
|
+
def content=(arg)
|
97
|
+
if arg.kind_of?(Array)
|
98
|
+
arg.each{ |e| self << Table::Row.new(e) }
|
99
|
+
else
|
100
|
+
self << Table::Row.new(arg)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
alias data= content=
|
105
|
+
|
106
|
+
# A shortcut for creating Table::Row::Header objects in the constructor
|
107
|
+
# using the DSL style syntax.
|
108
|
+
#
|
109
|
+
def header(arg = nil)
|
110
|
+
self.header = arg if arg
|
111
|
+
end
|
112
|
+
|
113
|
+
# Adds a Table::Row::Header object (or an Array of them) to the Table
|
114
|
+
# object.
|
115
|
+
#
|
116
|
+
def header=(arg)
|
117
|
+
if arg.kind_of?(Array)
|
118
|
+
arg.each{ |h| self << Table::Row.new(h, true) }
|
119
|
+
else
|
120
|
+
self << Table::Row::Header.new(arg)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
# Returns true or false, depending on whether or not end tags have been
|
125
|
+
# turned on or off, respectively.
|
126
|
+
#
|
127
|
+
def self.global_end_tags?
|
128
|
+
@@global_end_tags
|
129
|
+
end
|
130
|
+
|
131
|
+
# Sets the end tag class variable. This is used to set whether or not
|
132
|
+
# to include optional end tags in the final HTML output. The argument
|
133
|
+
# sent to this method must be true or false. The default value is true.
|
134
|
+
#
|
135
|
+
# Note that mandatory end tags are unaffected by this setting.
|
136
|
+
#
|
137
|
+
def self.global_end_tags=(bool)
|
138
|
+
expect(bool, [TrueClass, FalseClass])
|
139
|
+
@@global_end_tags = bool
|
140
|
+
end
|
141
|
+
|
142
|
+
# Returns either "lower" or "upper", indicating the case of all HTML
|
143
|
+
# tags in the final output.
|
144
|
+
#
|
145
|
+
def self.html_case
|
146
|
+
@html_case
|
147
|
+
end
|
148
|
+
|
149
|
+
# Sets the case of all HTML tags to either lower or upper. The only
|
150
|
+
# valid arguments to this method are 'upper' or 'lower'.
|
151
|
+
#
|
152
|
+
def self.html_case=(arg)
|
153
|
+
expect(arg,String)
|
154
|
+
arg.downcase!
|
155
|
+
unless arg == "upper" || arg == "lower"
|
156
|
+
msg = "Argument to html_case() must be 'upper' or 'lower'"
|
157
|
+
raise ArgumentError, msg
|
158
|
+
end
|
159
|
+
@html_case = arg
|
160
|
+
end
|
161
|
+
|
162
|
+
# Returns the number of spaces that tags for this class are indented.
|
163
|
+
# For the Table class, the indention level defaults to 0.
|
164
|
+
#
|
165
|
+
# Note that each class has its own default indentation level (a multiple
|
166
|
+
# of 3).
|
167
|
+
#
|
168
|
+
def self.indent_level
|
169
|
+
@indent_level
|
170
|
+
end
|
171
|
+
|
172
|
+
# Sets the number of spaces that tags for this class are indented.
|
173
|
+
#
|
174
|
+
def self.indent_level=(num)
|
175
|
+
expect(num, Integer)
|
176
|
+
raise ArgumentError,"indent level must be >= 0" if num < 0
|
177
|
+
@indent_level = num
|
178
|
+
end
|
179
|
+
|
180
|
+
# This method has been redefined to only allow certain subclasses to
|
181
|
+
# be assigned using a direct index notation. Specifically, only
|
182
|
+
# Caption, ColGroup, Body, Foot, Head and Row objects may be use
|
183
|
+
# assigned using direct index notation.
|
184
|
+
#
|
185
|
+
# In addition, a Caption can only be assigned to index 0. A Head can
|
186
|
+
# only be assigned to index 0, or index 1 if a Caption already exists.
|
187
|
+
# A Foot may only be assigned as the last element.
|
188
|
+
#
|
189
|
+
def []=(index,obj)
|
190
|
+
expect(obj, [Caption, ColGroup, Body, Foot, Head, Row])
|
191
|
+
|
192
|
+
# Only allow Caption objects at index 0
|
193
|
+
if index != 0 && obj.kind_of?(HTML::Table::Caption)
|
194
|
+
msg = "CAPTION can only be added at index 0"
|
195
|
+
raise ArgumentError, msg
|
196
|
+
end
|
197
|
+
|
198
|
+
# Only allow Head objects at index 0 or 1
|
199
|
+
if obj.kind_of?(HTML::Table::Head)
|
200
|
+
if self[0].kind_of?(HTML::Table::Caption) && index != 1
|
201
|
+
msg = "THEAD must be at index 1 when Caption is included"
|
202
|
+
raise ArgumentError, msg
|
203
|
+
end
|
204
|
+
if !self[0].kind_of?(HTML::Table::Caption) && index != 0
|
205
|
+
msg = "THEAD must be at index 0 when no Caption is included"
|
206
|
+
raise ArgumentError, msg
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
if obj.kind_of?(HTML::Table::Foot) && index != -1
|
211
|
+
msg = "FOOT must be last element"
|
212
|
+
raise ArgumentError, msg
|
213
|
+
end
|
214
|
+
super
|
215
|
+
end
|
216
|
+
|
217
|
+
# This method has been redefined to only allow certain subclasses to
|
218
|
+
# be accepted as arguments. Specifically, only Caption, ColGroup,
|
219
|
+
# Body, Foot, Head, Row, Row::Data and Row::Header objects may be
|
220
|
+
# pushed onto a Table.
|
221
|
+
#
|
222
|
+
# Pushing a Data or Header object onto a Table object creates its own
|
223
|
+
# row for each. If a Caption object is pushed onto the Table, it will
|
224
|
+
# automatically be bumped to the first element. If a Head object is
|
225
|
+
# pushed onto the Table, it is automatically bumped to the first
|
226
|
+
# element, or the second element if a Caption already exists.
|
227
|
+
#
|
228
|
+
def push(*args)
|
229
|
+
args.each{ |obj|
|
230
|
+
expect(obj, [Caption, ColGroup, Body, Foot, Head,
|
231
|
+
Row, Row::Data, Row::Header]
|
232
|
+
)
|
233
|
+
|
234
|
+
case obj
|
235
|
+
when Table::Row::Data, Table::Row::Header
|
236
|
+
self.push(Table::Row.new(obj))
|
237
|
+
when Table::Caption
|
238
|
+
if self[0].kind_of?(Table::Caption)
|
239
|
+
self[0] = obj
|
240
|
+
else
|
241
|
+
self.unshift(obj)
|
242
|
+
end
|
243
|
+
when Table::Head
|
244
|
+
if self[0].kind_of?(Table::Caption)
|
245
|
+
self.unshift(obj)
|
246
|
+
self[0],self[1] = self[1],self[0]
|
247
|
+
else
|
248
|
+
self.unshift(obj)
|
249
|
+
end
|
250
|
+
else
|
251
|
+
super(obj)
|
252
|
+
end
|
253
|
+
}
|
254
|
+
end
|
255
|
+
|
256
|
+
# This method has been redefined to only allow certain subclasses to
|
257
|
+
# be accepted as arguments.
|
258
|
+
#
|
259
|
+
# The restrictions and behavior are identical to the push() method.
|
260
|
+
#
|
261
|
+
def <<(obj)
|
262
|
+
expect(obj, [Caption, ColGroup, Body, Foot,
|
263
|
+
Head, Row, Row::Data, Row::Header]
|
264
|
+
)
|
265
|
+
|
266
|
+
case obj
|
267
|
+
when Table::Row::Data, Table::Row::Header # Each get their own row
|
268
|
+
self << Table::Row.new(obj)
|
269
|
+
when Table::Caption # Always the first row
|
270
|
+
if self[0].kind_of?(Table::Caption)
|
271
|
+
self[0] = obj
|
272
|
+
else
|
273
|
+
self.unshift(obj)
|
274
|
+
end
|
275
|
+
when Table::Head # Always at row 0 or 1
|
276
|
+
if self[0].kind_of?(Table::Caption)
|
277
|
+
self.unshift(obj)
|
278
|
+
self[0], self[1] = self[1], self[0]
|
279
|
+
else
|
280
|
+
self.unshift(obj)
|
281
|
+
end
|
282
|
+
else
|
283
|
+
super(obj)
|
284
|
+
end
|
285
|
+
end
|
286
|
+
|
287
|
+
# This method has been redefined to only allow certain subclasses to
|
288
|
+
# be unshifted onto a Table object. Specifically, they are Caption,
|
289
|
+
# ColGroup, Body, Foot, Head and Row.
|
290
|
+
#
|
291
|
+
def unshift(obj)
|
292
|
+
expect(obj, [Caption, ColGroup, Body, Foot, Head, Row])
|
293
|
+
super
|
294
|
+
end
|
295
|
+
|
296
|
+
alias to_s html
|
297
|
+
alias to_str html
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
require "content"
|
302
|
+
require "caption"
|
303
|
+
require "colgroup"
|
304
|
+
require "col"
|
305
|
+
require "row"
|
306
|
+
require "header"
|
307
|
+
require "data"
|
308
|
+
require "tablesection"
|
309
|
+
require "head"
|
310
|
+
require "foot"
|
311
|
+
require "body"
|