html-table 1.6.0 → 1.6.1

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.
@@ -0,0 +1,124 @@
1
+ module HTML
2
+ module Mixin
3
+ module HtmlHandler
4
+
5
+ $upper = false
6
+
7
+ # Used on HTML attributes. It creates proper HTML text based on the argument
8
+ # type. A string looks like "attr='text'", a number looks like "attr=1",
9
+ # while a true value simply looks like "attr" (no equal sign).
10
+ #--
11
+ # This is private method.
12
+ #
13
+ def modify_html(attribute,arg=nil)
14
+ if @html_begin.scan(/\b#{attribute}\b/).empty?
15
+ if arg.kind_of?(Integer)
16
+ @html_begin << " #{attribute}=#{arg}"
17
+ elsif arg.kind_of?(TrueClass)
18
+ @html_begin << " #{attribute}"
19
+ else
20
+ @html_begin << " #{attribute}='#{arg}'"
21
+ end
22
+ else
23
+ if arg.kind_of?(Integer)
24
+ @html_begin.gsub!(/#{attribute}=\d+/,"#{attribute}=#{arg}")
25
+ elsif arg.kind_of?(FalseClass)
26
+ @html_begin.gsub!(/#{attribute}/,'')
27
+ else
28
+ @html_begin.gsub!(/#{attribute}=['\w\.]+/,"#{attribute}='#{arg}'")
29
+ end
30
+ end
31
+ end
32
+
33
+ # Returns the HTML text for the current object. Indentation and end tag
34
+ # options are optional, based on the settings of the classes themselves.
35
+ #
36
+ # If +formatting+ is false, then formatting and whitespace is not applied
37
+ # and you will get a single, very long string. Note that case is still
38
+ # honored.
39
+ #
40
+ def html(formatting = true)
41
+ if self.class.respond_to?(:html_case)
42
+ $upper = true if self.class.html_case == "upper"
43
+ end
44
+
45
+ if $upper
46
+ @html_begin.upcase!
47
+ @html_end.upcase!
48
+ end
49
+
50
+ ilevel = 0
51
+
52
+ if formatting && self.class.respond_to?(:indent_level)
53
+ ilevel = self.class.indent_level
54
+ end
55
+
56
+ html = ' ' * ilevel + @html_begin[0..-1]
57
+ len = html.length
58
+ html[len,len] = '>'
59
+
60
+ if self.kind_of?(Array)
61
+ if formatting
62
+ html << self.map{ |e| "\n" + e.html(formatting).to_s }.join
63
+ else
64
+ html << self.map{ |e| e.html(formatting).to_s }.join
65
+ end
66
+ else
67
+ html << @html_body
68
+ end
69
+
70
+ #####################################################################
71
+ # Add end tags, or not, depending on whether the class supports the
72
+ # end_tags class method. Those that don't have an end_tags class
73
+ # method necessarily means that the end tag must be included.
74
+ #
75
+ # The Table.global_end_tags method overrides the individual class
76
+ # preferences with regards to end tags.
77
+ #####################################################################
78
+ if self.kind_of?(Array)
79
+ if HTML::Table.global_end_tags?
80
+ if self.class.respond_to?(:end_tags?)
81
+ if formatting
82
+ if self.class.end_tags?
83
+ html << "\n" + (' ' * ilevel) + @html_end
84
+ end
85
+ else
86
+ html << (' ' * ilevel) + @html_end if self.class.end_tags?
87
+ end
88
+ else
89
+ if formatting
90
+ html << "\n" + (' ' * ilevel) + @html_end
91
+ else
92
+ html << (' ' * ilevel) + @html_end
93
+ end
94
+ end
95
+ else
96
+ unless self.class.respond_to?(:end_tags?)
97
+ if formatting
98
+ html << "\n" + (' ' * ilevel) + @html_end
99
+ else
100
+ html << (' ' * ilevel) + @html_end
101
+ end
102
+ end
103
+ end
104
+ else
105
+ if HTML::Table.global_end_tags?
106
+ if self.class.respond_to?(:end_tags?)
107
+ html << @html_end if self.class.end_tags?
108
+ else
109
+ html << @html_end
110
+ end
111
+ else
112
+ unless self.class.respond_to?(:end_tags?)
113
+ html << @html_end
114
+ end
115
+ end
116
+ end
117
+
118
+ return html
119
+ end
120
+
121
+ private :modify_html
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,17 @@
1
+ # A pure-ruby replacement for strongtyping gem
2
+
3
+ module HTML
4
+ module Mixin
5
+ module StrongTyping
6
+ class ArgumentTypeError < ArgumentError; end
7
+
8
+ def expect(arg, allowed_types)
9
+ return true if Array(allowed_types).any? do |klass|
10
+ arg.kind_of?(klass)
11
+ end
12
+
13
+ raise ArgumentTypeError.new("#{arg} must be of type #{allowed_types}")
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,125 @@
1
+ ###################################################################
2
+ # tag_handler.rb
3
+ #
4
+ # Module for handling standard html physical tags (<b>, <i>, etc).
5
+ # Only used for Table::Content objects, which are in turn used by
6
+ # Table::Row::Data, Table::Row::Header and Table::Caption.
7
+ ###################################################################
8
+ module HTML
9
+ module Mixin
10
+ module TagHandler
11
+ def bold(bool = nil)
12
+ @bold ||= nil
13
+ self.bold = bool if bool
14
+ @bold
15
+ end
16
+
17
+ def bold=(bool)
18
+ handle_physical_tag('b', bool)
19
+ @bold = bool
20
+ end
21
+
22
+ def big(bool = nil)
23
+ @big ||= nil
24
+ self.big = bool if bool
25
+ @big
26
+ end
27
+
28
+ def big=(bool)
29
+ handle_physical_tag('big', bool)
30
+ @big = bool
31
+ end
32
+
33
+ def blink(bool = nil)
34
+ @blink ||= nil
35
+ self.blink = bool if bool
36
+ @blink
37
+ end
38
+
39
+ def blink=(bool)
40
+ warn BlinkWarning, "The 'blink' tag is very annoying. Please reconsider."
41
+ handle_physical_tag('blink', bool)
42
+ @blink = bool
43
+ end
44
+
45
+ def italic(bool = nil)
46
+ @italic ||= nil
47
+ self.italic = bool if bool
48
+ @italic
49
+ end
50
+
51
+ def italic=(bool)
52
+ handle_physical_tag('i', bool)
53
+ @italic = bool
54
+ end
55
+
56
+ def strike(bool = nil)
57
+ @strike ||= nil
58
+ self.strike = bool if bool
59
+ @strike
60
+ end
61
+
62
+ def strike=(bool)
63
+ handle_physical_tag('strike', bool)
64
+ @strike = bool
65
+ end
66
+
67
+ def sub(bool = nil)
68
+ @sub ||= nil
69
+ self.sub = bool if bool
70
+ @sub
71
+ end
72
+
73
+ def sub=(bool)
74
+ handle_physical_tag('sub', bool)
75
+ @sub = bool
76
+ end
77
+
78
+ def sup(bool = nil)
79
+ @sup ||= nil
80
+ self.sup = bool if bool
81
+ @sup
82
+ end
83
+
84
+ def sup=(bool)
85
+ handle_physical_tag('sup', bool)
86
+ @sup = bool
87
+ end
88
+
89
+ def tt(bool = nil)
90
+ @tt ||= nil
91
+ self.tt = bool if bool
92
+ @tt
93
+ end
94
+
95
+ def tt=(bool)
96
+ handle_physical_tag('tt', bool)
97
+ @tt = bool
98
+ end
99
+
100
+ def underline(bool = nil)
101
+ @underline ||= nil
102
+ self.underline = bool if bool
103
+ @underline
104
+ end
105
+
106
+ def underline=(bool)
107
+ handle_physical_tag('u', bool)
108
+ @bool = bool
109
+ end
110
+
111
+ private
112
+
113
+ def handle_physical_tag(tag, bool)
114
+ begin_tag = "<#{tag}>"
115
+ end_tag = "</#{tag}>"
116
+
117
+ if bool
118
+ self.replace(begin_tag << self << end_tag)
119
+ else
120
+ self.replace(self.gsub(/#{begin_tag}|#{end_tag}/,''))
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
data/lib/html/row.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module HTML
2
2
  class Table::Row < Array
3
- include AttributeHandler
4
- include HtmlHandler
3
+ include HTML::Mixin::AttributeHandler
4
+ include HTML::Mixin::HtmlHandler
5
5
 
6
6
  @indent_level = 3
7
7
  @end_tags = true
data/lib/html/table.rb CHANGED
@@ -1,8 +1,8 @@
1
- require_relative 'attribute_handler'
2
- require_relative 'html_handler'
3
- require 'strongtyping'
1
+ require_relative 'mixin/attribute_handler'
2
+ require_relative 'mixin/html_handler'
3
+ require_relative 'mixin/strongtyping'
4
4
  require 'structured_warnings'
5
- include StrongTyping
5
+ include HTML::Mixin::StrongTyping
6
6
 
7
7
  # Warning raised if a non-standard extension is used.
8
8
  class NonStandardExtensionWarning < StructuredWarnings::StandardWarning; end
@@ -16,11 +16,11 @@ module HTML
16
16
  # The Table class encapsulates methods associated with an html table
17
17
  # element. It is the "outermost" class of the html-table classes.
18
18
  class Table < Array
19
- include AttributeHandler
20
- include HtmlHandler
19
+ include HTML::Mixin::AttributeHandler
20
+ include HTML::Mixin::HtmlHandler
21
21
 
22
22
  # The version of the html-table library
23
- VERSION = '1.6.0'.freeze
23
+ VERSION = '1.6.1'.freeze
24
24
 
25
25
  # The indentation level for the <table> and </table> tags
26
26
  @indent_level = 0
@@ -2,8 +2,8 @@ module HTML
2
2
  # Superclass for THEAD, TBODY, TFOOT
3
3
  #
4
4
  class Table::TableSection < Array
5
- include AttributeHandler
6
- include HtmlHandler
5
+ include HTML::Mixin::AttributeHandler
6
+ include HTML::Mixin::HtmlHandler
7
7
 
8
8
  def initialize(&block)
9
9
  instance_eval(&block) if block_given?
data/test/test_table.rb CHANGED
@@ -16,7 +16,7 @@ class TC_HTML_Table < Test::Unit::TestCase
16
16
  end
17
17
 
18
18
  def test_version
19
- assert_equal('1.6.0', Table::VERSION)
19
+ assert_equal('1.6.1', Table::VERSION)
20
20
  assert_true(Table::VERSION.frozen?)
21
21
  end
22
22
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html-table
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.0
4
+ version: 1.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel J. Berger
@@ -146,11 +146,14 @@ files:
146
146
  - lib
147
147
  - lib/html
148
148
  - lib/html/row.rb
149
- - lib/html/attribute_handler.rb
150
149
  - lib/html/head.rb
151
150
  - lib/html/table.rb
152
- - lib/html/html_handler.rb
153
151
  - lib/html/content.rb
152
+ - lib/html/mixin
153
+ - lib/html/mixin/attribute_handler.rb
154
+ - lib/html/mixin/html_handler.rb
155
+ - lib/html/mixin/strongtyping.rb
156
+ - lib/html/mixin/tag_handler.rb
154
157
  - lib/html/data.rb
155
158
  - lib/html/tablesection.rb
156
159
  - lib/html/foot.rb
@@ -158,10 +161,8 @@ files:
158
161
  - lib/html/col.rb
159
162
  - lib/html/caption.rb
160
163
  - lib/html/colgroup.rb
161
- - lib/html/tag_handler.rb
162
164
  - lib/html/header.rb
163
165
  - lib/html-table.rb
164
- - lib/strongtyping.rb
165
166
  - certs
166
167
  - certs/djberg96_pub.pem
167
168
  homepage: http://github.com/djberg96/html-table
metadata.gz.sig CHANGED
Binary file
@@ -1,403 +0,0 @@
1
- # This module creates methods for each of the various attributes associated
2
- # with HTML tables. In some cases validation is done on the setters.
3
- #--
4
- # The seemingly redundant writer methods were left here for backwards
5
- # compatibility and for those who may not prefer the DSI.
6
- #
7
- module AttributeHandler
8
- def abbr(string = nil)
9
- @abbr ||= nil
10
- self.abbr = string if string
11
- @abbr
12
- end
13
-
14
- def abbr=(string)
15
- @abbr = string
16
- modify_html("abbr", string)
17
- end
18
-
19
- def align(position = nil)
20
- @align ||= nil
21
- self.align = position if position
22
- @align
23
- end
24
-
25
- def align=(position)
26
- valid = %w/top bottom left center right/
27
- raise ArgumentError unless valid.include?(position.downcase)
28
- @align = position
29
- modify_html("align", position)
30
- end
31
-
32
- def axis(string = nil)
33
- @axis ||= nil
34
- self.axis = string if string
35
- @axis
36
- end
37
-
38
- def axis=(string)
39
- @axis = string
40
- modify_html("axis", string)
41
- end
42
-
43
- def background(url = nil)
44
- @background ||= nil
45
- self.background = url if url
46
- @background
47
- end
48
-
49
- def background=(url)
50
- raise TypeError unless url.kind_of?(String)
51
- msg = "'background' is a non-standard extension"
52
- warn NonStandardExtensionWarning, msg
53
- @background = url
54
- modify_html("background", url)
55
- end
56
-
57
- def bgcolor(color = nil)
58
- @bgcolor ||= nil
59
- self.bgcolor = color if color
60
- @bgcolor
61
- end
62
-
63
- def bgcolor=(color)
64
- @bgcolor = color
65
- modify_html("bgcolor", color)
66
- end
67
-
68
- def border(num = nil)
69
- @border ||= nil
70
- self.border = num if num
71
- @border
72
- end
73
-
74
- # Allow either true/false or an integer
75
- def border=(num)
76
- if num.kind_of?(TrueClass)
77
- modify_html("border", true)
78
- elsif num.kind_of?(FalseClass)
79
- # Do nothing
80
- else
81
- @border = num.to_i
82
- modify_html("border", num.to_i)
83
- end
84
- end
85
-
86
- def bordercolor(color = nil)
87
- @bordercolor ||= nil
88
- self.bordercolor = color if color
89
- @bordercolor
90
- end
91
-
92
- def bordercolor=(color)
93
- @bordercolor = color
94
- msg = "'bordercolor' is a non-standard extension"
95
- warn NonStandardExtensionWarning, msg
96
- modify_html("bordercolor", color)
97
- end
98
-
99
- def bordercolordark(color = nil)
100
- @bordercolordark ||= nil
101
- self.bordercolordark = color if color
102
- @bordercolordark
103
- end
104
-
105
- def bordercolordark=(color)
106
- @bordercolordark = color
107
- msg = "'bordercolordark' is a non-standard extension"
108
- warn NonStandardExtensionWarning, msg
109
- modify_html("bordercolordark", color)
110
- end
111
-
112
- def bordercolorlight(color = nil)
113
- @bordercolorlight ||= nil
114
- self.bordercolorlight = color if color
115
- @bordercolorlight
116
- end
117
-
118
- def bordercolorlight=(color)
119
- @bordercolorlight = color
120
- msg = "'bordercolorlight' is a non-standard extension"
121
- warn NonStandardExtensionWarning, msg
122
- modify_html("bordercolorlight", @bordercolorlight)
123
- end
124
-
125
- def cellpadding(num = nil)
126
- @cellpadding ||= nil
127
- self.cellpadding = num if num
128
- @cellpadding
129
- end
130
-
131
- def cellpadding=(num)
132
- raise ArgumentError if num.to_i < 0
133
- @cellpadding = num.to_i
134
- modify_html("cellpadding", @cellpadding)
135
- end
136
-
137
- def cellspacing(num = nil)
138
- @cellspacing ||= nil
139
- self.cellspacing = num if num
140
- @cellspacing
141
- end
142
-
143
- def cellspacing=(num)
144
- raise ArgumentError if num.to_i < 0
145
- @cellspacing = num.to_i
146
- modify_html("cellspacing", @cellspacing)
147
- end
148
-
149
- def char(character = nil)
150
- @char ||= nil
151
- self.char = character if character
152
- @char
153
- end
154
-
155
- def char=(character)
156
- raise ArgumentError if character.to_s.length > 1
157
- @char = character.to_s
158
- modify_html("char", character.to_s)
159
- end
160
-
161
- def charoff(offset = nil)
162
- @charoff ||= nil
163
- self.charoff = offset if offset
164
- @charoff
165
- end
166
-
167
- def charoff=(offset)
168
- raise ArgumentError if offset.to_i < 0
169
- @charoff = offset
170
- modify_html("charoff", offset)
171
- end
172
-
173
- # Returns the CSS class. The trailing underscore is necessary in order
174
- # to avoid conflict with the 'class' keyword.
175
- #
176
- def class_(klass = nil)
177
- @class ||= nil
178
- self.class_ = klass if klass
179
- @class
180
- end
181
-
182
- # Returns the CSS class. The trailing underscore is necessary in order
183
- # to avoid conflict with the 'class' keyword.
184
- #
185
- def class_=(klass)
186
- modify_html('class', klass)
187
- @class = klass
188
- end
189
-
190
- def col(num = nil)
191
- @col ||= nil
192
- self.col = num if num
193
- @col
194
- end
195
-
196
- def col=(num)
197
- raise ArgumentError if num.to_i < 0
198
- @col = num.to_i
199
- modify_html("col", @col)
200
- end
201
-
202
- def colspan(span = nil)
203
- @colspan ||= nil
204
- self.colspan = span if span
205
- @colspan
206
- end
207
-
208
- def colspan=(span)
209
- raise ArgumentError if span.to_i < 0
210
- @colspan = span.to_i
211
- modify_html("colspan", @colspan)
212
- end
213
-
214
- # Allows you to configure various attributes by row or row + column.
215
- #
216
- def configure(row, col=nil, &block)
217
- if col
218
- begin
219
- yield self[row][col]
220
- rescue NameError
221
- msg = "No column to configure in a " + self.class.to_s + " class"
222
- raise ArgumentError, msg
223
- end
224
- else
225
- yield self[row]
226
- end
227
- end
228
-
229
- # Returns the HTML content (i.e. text).
230
- #
231
- def content(arg = nil, &block)
232
- case arg
233
- when String
234
- self.content = Table::Content.new(arg, &block)
235
- when Array
236
- arg.each{ |e|
237
- if e.kind_of?(Array)
238
- row = Table::Row.new
239
- e.each{ |element| row.push(Table::Content.new(element, &block)) }
240
- self.push(row)
241
- else
242
- self.content = Table::Content.new(e, &block)
243
- end
244
- }
245
- else
246
- self.content = arg if arg
247
- end
248
- @html_body
249
- end
250
-
251
- alias data content
252
-
253
- def frame(type = nil)
254
- @frame ||= nil
255
- self.frame = type if type
256
- @frame
257
- end
258
-
259
- def frame=(type)
260
- valid = %w/border void above below hsides lhs rhs vsides box/
261
- raise ArgumentError unless valid.include?(type.downcase)
262
- @frame = type
263
- modify_html("frame", @frame)
264
- end
265
-
266
- def height(num = nil)
267
- @height ||= nil
268
- self.height = num if num
269
- @height
270
- end
271
-
272
- def height=(num)
273
- raise ArgumentError if num.to_i < 0
274
- @height = num.to_i
275
- modify_html("height", @height)
276
- end
277
-
278
- def hspace(num = nil)
279
- @hspace ||= nil
280
- self.hspace = num if num
281
- @hspace
282
- end
283
-
284
- def hspace=(num)
285
- raise ArgumentError if num.to_i < 0
286
- @hspace = num.to_i
287
- modify_html("hspace", @hspace)
288
- end
289
-
290
- def nowrap(bool = nil)
291
- @nowrap ||= nil
292
- self.nowrap = bool if bool
293
- @nowrap
294
- end
295
-
296
- def nowrap=(bool)
297
- unless bool.kind_of?(TrueClass) || bool.kind_of?(FalseClass)
298
- raise TypeError
299
- end
300
- @nowrap = bool
301
- modify_html("nowrap", @nowrap)
302
- end
303
-
304
- def rowspan(num = nil)
305
- @rowspan ||= nil
306
- self.rowspan = num if num
307
- @rowspan
308
- end
309
-
310
- def rowspan=(num)
311
- raise ArgumentError if num.to_i < 0
312
- @rowspan = num.to_i
313
- modify_html("rowspan", @rowspan)
314
- end
315
-
316
- def rules(edges = nil)
317
- @rules ||= nil
318
- self.rules = edges if edges
319
- @rules
320
- end
321
-
322
- def rules=(edges)
323
- valid = %w/all groups rows cols none/
324
- raise ArgumentError unless valid.include?(edges.to_s.downcase)
325
- @rules = edges
326
- modify_html("rules", @rules)
327
- end
328
-
329
- def span(num = nil)
330
- @span ||= nil
331
- self.span = num if num
332
- @span
333
- end
334
-
335
- def span=(num)
336
- raise ArgumentError if num.to_i < 0
337
- @span = num.to_i
338
- modify_html("span", @span)
339
- end
340
-
341
- def style(string = nil)
342
- @style ||= nil
343
- self.style = string if string
344
- @style
345
- end
346
-
347
- def style=(string)
348
- @style = string.to_s
349
- modify_html("style", @style)
350
- end
351
-
352
- def summary(string = nil)
353
- @summary ||= nil
354
- self.summary = string if string
355
- @summary
356
- end
357
-
358
- def summary=(string)
359
- @summary = string.to_s
360
- modify_html("summary", @summary)
361
- end
362
-
363
- def valign(position = nil)
364
- @valign ||= nil
365
- self.valign = position if position
366
- @valign
367
- end
368
-
369
- def valign=(position)
370
- valid = %w/top center bottom baseline/
371
- raise ArgumentError unless valid.include?(position.to_s.downcase)
372
- @valign = position
373
- modify_html("valign", @valign)
374
- end
375
-
376
- def vspace(num = nil)
377
- @vspace ||= nil
378
- self.vspace = num if num
379
- @vspace
380
- end
381
-
382
- def vspace=(num)
383
- raise ArgumentError if num.to_i < 0
384
- @vspace = num.to_i
385
- modify_html("vspace", @vspace)
386
- end
387
-
388
- def width(num = nil)
389
- @width ||= nil
390
- self.width = num if num
391
- @width
392
- end
393
-
394
- def width=(num)
395
- if num =~ /%/
396
- @width = num
397
- else
398
- raise ArgumentError if num.to_i < 0
399
- @width = num.to_i
400
- end
401
- modify_html("width", @width)
402
- end
403
- end