html-table 1.3.6 → 1.4.0

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