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.
@@ -1,120 +1,120 @@
1
- module HtmlHandler
2
-
3
- $upper = false
4
-
5
- # Used on HTML attributes. It creates proper HTML text based on the argument
6
- # type. A string looks like "attr='text'", a number looks like "attr=1",
7
- # while a true value simply looks like "attr" (no equal sign).
8
- #--
9
- # This is private method.
10
- #
11
- def modify_html(attribute,arg=nil)
12
- if @html_begin.scan(/\b#{attribute}\b/).empty?
13
- if arg.kind_of?(Fixnum)
14
- @html_begin << " #{attribute}=#{arg}"
15
- elsif arg.kind_of?(TrueClass)
16
- @html_begin << " #{attribute}"
17
- else
18
- @html_begin << " #{attribute}='#{arg}'"
19
- end
20
- else
21
- if arg.kind_of?(Fixnum)
22
- @html_begin.gsub!(/#{attribute}=\d+/,"#{attribute}=#{arg}")
23
- elsif arg.kind_of?(FalseClass)
24
- @html_begin.gsub!(/#{attribute}/,'')
25
- else
26
- @html_begin.gsub!(/#{attribute}=['\w\.]+/,"#{attribute}='#{arg}'")
27
- end
28
- end
29
- end
30
-
31
- # Returns the HTML text for the current object. Indentation and end tag
32
- # options are optional, based on the settings of the classes themselves.
33
- #
34
- # If +formatting+ is false, then formatting and whitespace is not applied
35
- # and you will get a single, very long string. Note that case is still
36
- # honored.
37
- #
38
- def html(formatting = true)
39
- if self.class.respond_to?(:html_case)
40
- $upper = true if self.class.html_case == "upper"
41
- end
42
-
43
- if $upper
44
- @html_begin.upcase!
45
- @html_end.upcase!
46
- end
47
-
48
- ilevel = 0
49
-
50
- if formatting && self.class.respond_to?(:indent_level)
51
- ilevel = self.class.indent_level
52
- end
53
-
54
- html = ' ' * ilevel + @html_begin[0..-1]
55
- len = html.length
56
- html[len,len] = '>'
57
-
58
- if self.kind_of?(Array)
59
- if formatting
60
- html << self.map{ |e| "\n" + e.html(formatting).to_s }.join
61
- else
62
- html << self.map{ |e| e.html(formatting).to_s }.join
63
- end
64
- else
65
- html << @html_body
66
- end
67
-
68
- #####################################################################
69
- # Add end tags, or not, depending on whether the class supports the
70
- # end_tags class method. Those that don't have an end_tags class
71
- # method necessarily means that the end tag must be included.
72
- #
73
- # The Table.global_end_tags method overrides the individual class
74
- # preferences with regards to end tags.
75
- #####################################################################
76
- if self.kind_of?(Array)
77
- if HTML::Table.global_end_tags?
78
- if self.class.respond_to?(:end_tags?)
79
- if formatting
80
- if self.class.end_tags?
81
- html << "\n" + (' ' * ilevel) + @html_end
82
- end
83
- else
84
- html << (' ' * ilevel) + @html_end if self.class.end_tags?
85
- end
86
- else
87
- if formatting
88
- html << "\n" + (' ' * ilevel) + @html_end
89
- else
90
- html << (' ' * ilevel) + @html_end
91
- end
92
- end
93
- else
94
- unless self.class.respond_to?(:end_tags?)
95
- if formatting
96
- html << "\n" + (' ' * ilevel) + @html_end
97
- else
98
- html << (' ' * ilevel) + @html_end
99
- end
100
- end
101
- end
102
- else
103
- if HTML::Table.global_end_tags?
104
- if self.class.respond_to?(:end_tags?)
105
- html << @html_end if self.class.end_tags?
106
- else
107
- html << @html_end
108
- end
109
- else
110
- unless self.class.respond_to?(:end_tags?)
111
- html << @html_end
112
- end
113
- end
114
- end
115
-
116
- return html
117
- end
118
-
119
- private :modify_html
120
- end
1
+ module HtmlHandler
2
+
3
+ $upper = false
4
+
5
+ # Used on HTML attributes. It creates proper HTML text based on the argument
6
+ # type. A string looks like "attr='text'", a number looks like "attr=1",
7
+ # while a true value simply looks like "attr" (no equal sign).
8
+ #--
9
+ # This is private method.
10
+ #
11
+ def modify_html(attribute,arg=nil)
12
+ if @html_begin.scan(/\b#{attribute}\b/).empty?
13
+ if arg.kind_of?(Fixnum)
14
+ @html_begin << " #{attribute}=#{arg}"
15
+ elsif arg.kind_of?(TrueClass)
16
+ @html_begin << " #{attribute}"
17
+ else
18
+ @html_begin << " #{attribute}='#{arg}'"
19
+ end
20
+ else
21
+ if arg.kind_of?(Fixnum)
22
+ @html_begin.gsub!(/#{attribute}=\d+/,"#{attribute}=#{arg}")
23
+ elsif arg.kind_of?(FalseClass)
24
+ @html_begin.gsub!(/#{attribute}/,'')
25
+ else
26
+ @html_begin.gsub!(/#{attribute}=['\w\.]+/,"#{attribute}='#{arg}'")
27
+ end
28
+ end
29
+ end
30
+
31
+ # Returns the HTML text for the current object. Indentation and end tag
32
+ # options are optional, based on the settings of the classes themselves.
33
+ #
34
+ # If +formatting+ is false, then formatting and whitespace is not applied
35
+ # and you will get a single, very long string. Note that case is still
36
+ # honored.
37
+ #
38
+ def html(formatting = true)
39
+ if self.class.respond_to?(:html_case)
40
+ $upper = true if self.class.html_case == "upper"
41
+ end
42
+
43
+ if $upper
44
+ @html_begin.upcase!
45
+ @html_end.upcase!
46
+ end
47
+
48
+ ilevel = 0
49
+
50
+ if formatting && self.class.respond_to?(:indent_level)
51
+ ilevel = self.class.indent_level
52
+ end
53
+
54
+ html = ' ' * ilevel + @html_begin[0..-1]
55
+ len = html.length
56
+ html[len,len] = '>'
57
+
58
+ if self.kind_of?(Array)
59
+ if formatting
60
+ html << self.map{ |e| "\n" + e.html(formatting).to_s }.join
61
+ else
62
+ html << self.map{ |e| e.html(formatting).to_s }.join
63
+ end
64
+ else
65
+ html << @html_body
66
+ end
67
+
68
+ #####################################################################
69
+ # Add end tags, or not, depending on whether the class supports the
70
+ # end_tags class method. Those that don't have an end_tags class
71
+ # method necessarily means that the end tag must be included.
72
+ #
73
+ # The Table.global_end_tags method overrides the individual class
74
+ # preferences with regards to end tags.
75
+ #####################################################################
76
+ if self.kind_of?(Array)
77
+ if HTML::Table.global_end_tags?
78
+ if self.class.respond_to?(:end_tags?)
79
+ if formatting
80
+ if self.class.end_tags?
81
+ html << "\n" + (' ' * ilevel) + @html_end
82
+ end
83
+ else
84
+ html << (' ' * ilevel) + @html_end if self.class.end_tags?
85
+ end
86
+ else
87
+ if formatting
88
+ html << "\n" + (' ' * ilevel) + @html_end
89
+ else
90
+ html << (' ' * ilevel) + @html_end
91
+ end
92
+ end
93
+ else
94
+ unless self.class.respond_to?(:end_tags?)
95
+ if formatting
96
+ html << "\n" + (' ' * ilevel) + @html_end
97
+ else
98
+ html << (' ' * ilevel) + @html_end
99
+ end
100
+ end
101
+ end
102
+ else
103
+ if HTML::Table.global_end_tags?
104
+ if self.class.respond_to?(:end_tags?)
105
+ html << @html_end if self.class.end_tags?
106
+ else
107
+ html << @html_end
108
+ end
109
+ else
110
+ unless self.class.respond_to?(:end_tags?)
111
+ html << @html_end
112
+ end
113
+ end
114
+ end
115
+
116
+ return html
117
+ end
118
+
119
+ private :modify_html
120
+ end
data/lib/html/row.rb CHANGED
@@ -1,185 +1,188 @@
1
- module HTML
2
- class Table::Row < Array
3
- include AttributeHandler
4
- include HtmlHandler
5
-
6
- @indent_level = 3
7
- @end_tags = true
8
-
9
- # Returns a new Table::Row object. Optionally takes a block. If +arg+
10
- # is provided it is treated as content. If +header+ is false, that
11
- # content is transformed into a Row::Data object. Otherwise, it is
12
- # converted into a Row::Header object.
13
- #
14
- # See the # Table::Row#content= method for more information.
15
- #--
16
- # Note that, despite the name, Row is a subclass of Array, not Table.
17
- #
18
- def initialize(arg = nil, header = false, &block)
19
- @html_begin = '<tr'
20
- @html_end = '</tr>'
21
-
22
- @header = header
23
-
24
- instance_eval(&block) if block_given?
25
- self.content = arg if arg
26
- end
27
-
28
- # Returns whether or not content is converted into a Row::Header object
29
- # (as opposed to a Row::Data object).
30
- #
31
- def header?
32
- @header
33
- end
34
-
35
- # Sets whether or not content is converted into a Row::Header object
36
- # or a Row::Data object.
37
- #
38
- def header=(bool)
39
- @header = bool
40
- end
41
-
42
- # Adds content to the Row object.
43
- #
44
- # Because a Row object doesn't store any of its own content, the
45
- # arguments to this method must be a Row::Data object, a Row::Header
46
- # object, or a String (or an array of any of these). In the latter case,
47
- # a single Row::Data object is created for each string.
48
- #
49
- # Examples (with whitespace and newlines removed):
50
- #
51
- # row = Table::Row.new
52
- #
53
- # # Same as Table::Row.new('foo')
54
- # row.content = 'foo'
55
- # row.html => <tr><td>foo</td></tr>
56
- #
57
- # row.content = [['foo,'bar']]
58
- # row.html => <tr><td>foo</td><td>bar</td></tr>
59
- #
60
- # row.content = Table::Row::Data.new('foo')
61
- # row.html => <tr><td>foo</td></tr>
62
- #
63
- # row.content = Table::Row::Header.new('foo')
64
- # row.html => <tr><th>foo</th></tr>
65
- #
66
- def content=(arg)
67
- case arg
68
- when String
69
- if @header
70
- self.push(Table::Row::Header.new(arg))
71
- else
72
- self.push(Table::Row::Data.new(arg))
73
- end
74
- when Array
75
- arg.each{ |e|
76
- if e.kind_of?(Table::Row::Data) || e.kind_of?(Table::Row::Header)
77
- self.push(e)
78
- else
79
- if @header
80
- self.push(Table::Row::Header.new(e))
81
- else
82
- self.push(Table::Row::Data.new(e))
83
- end
84
- end
85
- }
86
- else
87
- self.push(arg)
88
- end
89
- end
90
-
91
- # Returns the number of spaces that tags for this class are indented.
92
- # For the Row class, the indention level defaults to 3.
93
- #
94
- def self.indent_level
95
- @indent_level
96
- end
97
-
98
- # Sets the number of spaces that tags for this class are indented.
99
- #
100
- def self.indent_level=(num)
101
- expect(num, Integer)
102
- raise ArgumentError if num < 0
103
- @indent_level = num
104
- end
105
-
106
- # Returns true or false, depending on whether or not the end tags for
107
- # this class, </tr>, are included for each row or not. By default, this
108
- # is set to true.
109
- #
110
- def self.end_tags?
111
- @end_tags
112
- end
113
-
114
- # Sets the behavior for whether or not the end tags for this class,
115
- # </tr>, are included for each row or not. Only true and false are
116
- # valid arguments.
117
- #
118
- def self.end_tags=(bool)
119
- expect(bool,[TrueClass,FalseClass])
120
- @end_tags = bool
121
- end
122
-
123
- # This method has been redefined to only allow certain classes to be
124
- # accepted as arguments. Specifically, they are Data and Header. An
125
- # Array is also valid, but only if it only includes Data or Header
126
- # objects.
127
- #
128
- def []=(index, obj)
129
- if obj.kind_of?(Array)
130
- obj.each{ |o| expect(o, [Data, Header]) }
131
- else
132
- expect(obj, [Data, Header])
133
- end
134
- super
135
- end
136
-
137
- # This method has been redefined to only allow certain classes to be
138
- # accepted as arguments. Specifically, they are String, Fixnum,
139
- # Data and Header.
140
- #
141
- # A plain string or number pushed onto a Row is automatically
142
- # converted to a Data object.
143
- #
144
- def push(*args)
145
- args.each do |obj|
146
- if obj.kind_of?(String) || obj.kind_of?(Fixnum)
147
- td = Table::Row::Data.new(obj.to_s)
148
- super(td)
149
- next
150
- else
151
- expect(obj, [Data, Header])
152
- end
153
- super(obj)
154
- end
155
- end
156
-
157
- # This method has been redefined to only allow certain classes to be
158
- # accepted as arguments. The rules are the same as they are for
159
- # Row#push.
160
- #
161
- def <<(obj)
162
- if obj.kind_of?(String) || obj.kind_of?(Fixnum)
163
- td = Table::Row::Data.new(obj.to_s)
164
- super(td)
165
- else
166
- expect(obj, [Data, Header])
167
- end
168
- super(obj)
169
- end
170
-
171
- # This method has been redefined to only allow certain classes to be
172
- # accepted as arguments. The rules are the same as they are for
173
- # Row#push.
174
- #
175
- def unshift(obj)
176
- if obj.kind_of?(String) || obj.kind_of?(Fixnum)
177
- td = Table::Row::Data.new(obj.to_s)
178
- super(td)
179
- else
180
- expect(obj,[Data,Header])
181
- end
182
- super(obj)
183
- end
184
- end
185
- end
1
+ module HTML
2
+ class Table::Row < Array
3
+ include AttributeHandler
4
+ include HtmlHandler
5
+
6
+ @indent_level = 3
7
+ @end_tags = true
8
+
9
+ # Returns a new Table::Row object. Optionally takes a block. If +arg+
10
+ # is provided it is treated as content. If +header+ is false, that
11
+ # content is transformed into a Row::Data object. Otherwise, it is
12
+ # converted into a Row::Header object.
13
+ #
14
+ # See the # Table::Row#content= method for more information.
15
+ #--
16
+ # Note that, despite the name, Row is a subclass of Array, not Table.
17
+ #
18
+ def initialize(arg = nil, header = false, &block)
19
+ @html_begin = '<tr'
20
+ @html_end = '</tr>'
21
+
22
+ @header = header
23
+
24
+ instance_eval(&block) if block_given?
25
+ self.content = arg if arg
26
+ end
27
+
28
+ # Returns whether or not content is converted into a Row::Header object
29
+ # (as opposed to a Row::Data object).
30
+ #
31
+ def header?
32
+ @header
33
+ end
34
+
35
+ # Sets whether or not content is converted into a Row::Header object
36
+ # or a Row::Data object.
37
+ #
38
+ def header=(bool)
39
+ @header = bool
40
+ end
41
+
42
+ # Adds content to the Row object.
43
+ #
44
+ # Because a Row object doesn't store any of its own content, the
45
+ # arguments to this method must be a Row::Data object, a Row::Header
46
+ # object, or a String (or an array of any of these). In the latter case,
47
+ # a single Row::Data object is created for each string.
48
+ #
49
+ # Examples (with whitespace and newlines removed):
50
+ #
51
+ # row = Table::Row.new
52
+ #
53
+ # # Same as Table::Row.new('foo')
54
+ # row.content = 'foo'
55
+ # row.html => <tr><td>foo</td></tr>
56
+ #
57
+ # row.content = [['foo,'bar']]
58
+ # row.html => <tr><td>foo</td><td>bar</td></tr>
59
+ #
60
+ # row.content = Table::Row::Data.new('foo')
61
+ # row.html => <tr><td>foo</td></tr>
62
+ #
63
+ # row.content = Table::Row::Header.new('foo')
64
+ # row.html => <tr><th>foo</th></tr>
65
+ #
66
+ def content=(arg)
67
+ case arg
68
+ when String
69
+ if @header
70
+ self.push(Table::Row::Header.new(arg))
71
+ else
72
+ self.push(Table::Row::Data.new(arg))
73
+ end
74
+ when Array
75
+ arg.each{ |e|
76
+ if e.kind_of?(Table::Row::Data) || e.kind_of?(Table::Row::Header)
77
+ self.push(e)
78
+ else
79
+ if @header
80
+ self.push(Table::Row::Header.new(e))
81
+ else
82
+ self.push(Table::Row::Data.new(e))
83
+ end
84
+ end
85
+ }
86
+ else
87
+ self.push(arg)
88
+ end
89
+ end
90
+
91
+ # Returns the number of spaces that tags for this class are indented.
92
+ # For the Row class, the indention level defaults to 3.
93
+ #
94
+ def self.indent_level
95
+ @indent_level
96
+ end
97
+
98
+ # Sets the number of spaces that tags for this class are indented.
99
+ #
100
+ def self.indent_level=(num)
101
+ expect(num, Integer)
102
+ raise ArgumentError if num < 0
103
+ @indent_level = num
104
+ end
105
+
106
+ # Returns true or false, depending on whether or not the end tags for
107
+ # this class, </tr>, are included for each row or not. By default, this
108
+ # is set to true.
109
+ #
110
+ def self.end_tags?
111
+ @end_tags
112
+ end
113
+
114
+ # Sets the behavior for whether or not the end tags for this class,
115
+ # </tr>, are included for each row or not. Only true and false are
116
+ # valid arguments.
117
+ #
118
+ def self.end_tags=(bool)
119
+ expect(bool,[TrueClass,FalseClass])
120
+ @end_tags = bool
121
+ end
122
+
123
+ # This method has been redefined to only allow certain classes to be
124
+ # accepted as arguments. Specifically, they are Data and Header. An
125
+ # Array is also valid, but only if it only includes Data or Header
126
+ # objects.
127
+ #
128
+ def []=(index, obj)
129
+ if obj.kind_of?(Array)
130
+ obj.each{ |o| expect(o, [Data, Header]) }
131
+ else
132
+ expect(obj, [Data, Header])
133
+ end
134
+ super
135
+ end
136
+
137
+ # This method has been redefined to only allow certain classes to be
138
+ # accepted as arguments. Specifically, they are String, Fixnum,
139
+ # Data and Header.
140
+ #
141
+ # A plain string or number pushed onto a Row is automatically
142
+ # converted to a Data object.
143
+ #
144
+ def push(*args)
145
+ args.each do |obj|
146
+ if obj.kind_of?(String) || obj.kind_of?(Fixnum)
147
+ td = Table::Row::Data.new(obj.to_s)
148
+ super(td)
149
+ next
150
+ else
151
+ expect(obj, [Data, Header])
152
+ end
153
+ super(obj)
154
+ end
155
+ end
156
+
157
+ # This method has been redefined to only allow certain classes to be
158
+ # accepted as arguments. The rules are the same as they are for
159
+ # Row#push.
160
+ #
161
+ def <<(obj)
162
+ if obj.kind_of?(String) || obj.kind_of?(Fixnum)
163
+ td = Table::Row::Data.new(obj.to_s)
164
+ super(td)
165
+ else
166
+ expect(obj, [Data, Header])
167
+ end
168
+ super(obj)
169
+ end
170
+
171
+ # This method has been redefined to only allow certain classes to be
172
+ # accepted as arguments. The rules are the same as they are for
173
+ # Row#push.
174
+ #
175
+ def unshift(obj)
176
+ if obj.kind_of?(String) || obj.kind_of?(Fixnum)
177
+ td = Table::Row::Data.new(obj.to_s)
178
+ super(td)
179
+ else
180
+ expect(obj,[Data,Header])
181
+ end
182
+ super(obj)
183
+ end
184
+
185
+ alias to_s html
186
+ alias to_str html
187
+ end
188
+ end