html-table 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,34 @@
1
+ module HTML
2
+
3
+ # This class represents an HTML table body (<tbody>). It is a
4
+ # subclass of Table::TableSection.
5
+ class Table::Body < Table::TableSection
6
+ @indent_level = 3
7
+ @end_tags = true
8
+
9
+ # Returns a new Table::Body object. Optionally takes a block. If
10
+ # an argument is provided, it is treated as content.
11
+ def initialize(arg=nil)
12
+ @html_begin = "<tbody"
13
+ @html_end = "</tbody>"
14
+ yield self if block_given?
15
+ self.content = arg if arg
16
+ end
17
+
18
+ # Returns a boolean indicating whether or not end tags, </tbody>, are
19
+ # included for each Body object in the final HTML output. The
20
+ # default is true.
21
+ def self.end_tags?
22
+ @end_tags
23
+ end
24
+
25
+ # Sets whether or not end tags are included for each Body object in
26
+ # the final HTML output. The default is true. Only true or false are
27
+ # valid arguments.
28
+ def self.end_tags=(bool)
29
+ expect(bool,[TrueClass,FalseClass])
30
+ @end_tags = bool
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,40 @@
1
+ module HTML
2
+
3
+ # This class represents an HTML Caption. Despite the name, it is not
4
+ # a subclass of Table. Note that end tags for this class are mandatory.
5
+ class Table::Caption
6
+ include AttributeHandler
7
+ include HtmlHandler
8
+ undef_method :configure
9
+ @indent_level = 3
10
+
11
+ # Returns a new Table::Caption object. Optionally takes a block. If
12
+ # an argument is provided it is treated as content.
13
+ def initialize(arg=nil)
14
+ @html_begin = '<caption'
15
+ @html_body = ''
16
+ @html_end = '</caption>'
17
+ yield self if block_given?
18
+ self.content = arg if arg
19
+ end
20
+
21
+ # Adds content to the Table::Caption object.
22
+ def content=(arg)
23
+ @html_body = arg.to_s
24
+ end
25
+
26
+ # Returns the number of spaces that tags for this class are indented.
27
+ # For the Table::Caption class, the indention level defaults to 3.
28
+ def self.indent_level
29
+ @indent_level
30
+ end
31
+
32
+ # Sets the number of spaces that tags for this class are indented.
33
+ def self.indent_level=(num)
34
+ expect(num,Integer)
35
+ raise ArgumentError, "indent level must be >= 0" if num < 0
36
+ @indent_level = num
37
+ end
38
+ end
39
+
40
+ end
@@ -0,0 +1,33 @@
1
+ module HTML
2
+ # This class represents an HTML ColGroup column (<col>). Despite the
3
+ # name, it is not a subclass of ColGroup or Table.
4
+ class Table::ColGroup::Col
5
+ include AttributeHandler
6
+ include HtmlHandler
7
+ undef_method :configure, :content
8
+ @indent_level = 6
9
+
10
+ # Creates and returns a new ColGroup object. Optionally takes a block.
11
+ # Note that it does not accept an argument - col tags do not have content
12
+ def initialize
13
+ @html_begin = '<col'
14
+ @html_body = ''
15
+ @html_end = ''
16
+ yield self if block_given?
17
+ end
18
+
19
+ # Returns the indentation level for the tags of this class. The
20
+ # default is 6.
21
+ def self.indent_level
22
+ @indent_level
23
+ end
24
+
25
+ # Sets the indentation level for the tags of this class. The default
26
+ # is 6.
27
+ def self.indent_level=(num)
28
+ expect(num,Integer)
29
+ raise ArgumentError, "num must be >= 0" if num < 0
30
+ @indent_level = num
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,99 @@
1
+ module HTML
2
+ # This class represents an HTML column group (<colgroup>). It is a
3
+ # subclass of Array. The only elements it may contain are instances
4
+ # of the ColGroup::Col class.
5
+ class Table::ColGroup < Array
6
+ include AttributeHandler
7
+ include HtmlHandler
8
+ @indent_level = 3
9
+ @end_tags = true
10
+
11
+ undef_method :content
12
+
13
+ # Returns a new ColGroup object. Optionally takes a block. If an
14
+ # argument is provided, it is treated as content.
15
+ def initialize(arg = nil)
16
+ @html_begin = '<colgroup'
17
+ @html_body = ''
18
+ @html_end = '</colgroup>'
19
+ yield self if block_given?
20
+ self.push(arg) if arg
21
+ end
22
+
23
+ # Returns the indentation level for the tags of this class. The
24
+ # default is 3.
25
+ def self.indent_level
26
+ @indent_level
27
+ end
28
+
29
+ # Sets the indentation level for the tags of this class. The default
30
+ # is 3.
31
+ def self.indent_level=(num)
32
+ expect(num,Integer)
33
+ raise ArgumentError,"indent_level must be >= 0" if num < 0
34
+ @indent_level = num
35
+ end
36
+
37
+ # This method has been redefined to only allow ColGroup::Col objects
38
+ # to be assigned.
39
+ def []=(index,obj)
40
+ if obj.kind_of?(Array)
41
+ expect(obj.first,Col) # In case of 0 length Array
42
+ obj.each{ |o|
43
+ expect(o,Col)
44
+ }
45
+ else
46
+ expect(obj,Col)
47
+ end
48
+ super
49
+ end
50
+
51
+ # This method has been redefined to only allow ColGroup::Col objects
52
+ # to be pushed onto a ColGroup instance.
53
+ def push(*args)
54
+ args.each do |obj|
55
+ unless obj.kind_of?(Table::ColGroup::Col)
56
+ msg = "Can only assign Col objects to ColGroup class"
57
+ msg += ": " + obj.class.to_s
58
+ raise TypeError, msg
59
+ end
60
+ super(obj)
61
+ end
62
+ end
63
+
64
+ # This method has been redefined to only allow ColGroup::Col objects
65
+ # to be pushed onto a ColGroup instance.
66
+ def <<(obj)
67
+ unless obj.kind_of?(Table::ColGroup::Col)
68
+ msg = "Can only assign Col objects to ColGroup class"
69
+ msg += ": " + obj.class.to_s
70
+ raise TypeError, msg
71
+ end
72
+ super(obj)
73
+ end
74
+
75
+ # This method has been redefined to only allow ColGroup::Col objects
76
+ # to be unshifted onto a ColGroup instance.
77
+ def unshift(obj)
78
+ unless obj.kind_of?(Table::ColGroup::Col)
79
+ msg = "Can only assign Data and Header objects to Row class"
80
+ raise TypeError, msg
81
+ end
82
+ super
83
+ end
84
+
85
+ # Returns a boolean indicating whether or not end tags are included for
86
+ # each ColGroup object in the final HTML output. The default is true.
87
+ def self.end_tags?
88
+ @end_tags
89
+ end
90
+
91
+ # Sets whether or not end tags are included for each ColGroup object in
92
+ # the final HTML output. The default is true. Only true or false are
93
+ # valid arguments.
94
+ def self.end_tags=(bool)
95
+ expect(bool,[TrueClass,FalseClass])
96
+ @end_tags = bool
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,56 @@
1
+ module HTML
2
+
3
+ # This class represents HTML table data, <td>. Despite the name
4
+ # it is not a subclass of Table::Row or Table.
5
+ class Table::Row::Data
6
+ include AttributeHandler
7
+ include HtmlHandler
8
+ undef_method :configure
9
+ @indent_level = 6
10
+ @end_tags = true
11
+
12
+ # Creates and returns a Data object. Optionally takes a block. If
13
+ # an argument is provided, it is treated as content.
14
+ def initialize(arg = nil)
15
+ @html_begin = '<td'
16
+ @html_body = ''
17
+ @html_end = '</td>'
18
+ yield self if block_given?
19
+ self.content = arg if arg
20
+ end
21
+
22
+ # Adds content to the Table::Row::Data object.
23
+ def content=(arg)
24
+ @html_body = arg.to_s
25
+ end
26
+
27
+ # Returns the indentation level for the tags of this class. The
28
+ # default is 6.
29
+ def self.indent_level
30
+ @indent_level
31
+ end
32
+
33
+ # Sets the indentation level for the tags of this class. The default
34
+ # is 6.
35
+ def self.indent_level=(num)
36
+ expect(num,Integer)
37
+ raise ArgumentError,"indent_level must be >= 0" if num < 0
38
+ @indent_level = num
39
+ end
40
+
41
+ # Returns a boolean indicating whether or not end tags, </td>, are
42
+ # included for each Data object in the final HTML output. The
43
+ # default is true.
44
+ def self.end_tags?
45
+ @end_tags
46
+ end
47
+
48
+ # Sets whether or not end tags are included for each Data object in
49
+ # the final HTML output. The default is true. Only true or false are
50
+ # valid arguments.
51
+ def self.end_tags=(bool)
52
+ expect(bool,[TrueClass,FalseClass])
53
+ @end_tags = bool
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,43 @@
1
+ module HTML
2
+
3
+ # This class represents an HTML table foot (<tfoot>). It is a
4
+ # subclass of Table::TableSection. It is a singleton class.
5
+ class Table::Foot < Table::TableSection
6
+ private_class_method :new
7
+ @@foot = nil
8
+ @indent_level = 3
9
+ @end_tags = true
10
+
11
+ # This is our constructor for Foot objects because it is a singleton
12
+ # class. Optionally, a block may be provided. If an argument is
13
+ # provided it is treated as content.
14
+ def self.create(arg=nil, &block)
15
+ @@foot = new(arg,&block) unless @@foot
16
+ @@foot
17
+ end
18
+
19
+ # Called by create() instead of new(). This initializes the Foot class.
20
+ def initialize(arg, &block)
21
+ @html_begin = "<tfoot"
22
+ @html_end = "</tfoot>"
23
+ yield self if block_given?
24
+ self.content = arg if arg
25
+ end
26
+
27
+ # Returns a boolean indicating whether or not end tags, </tfoot>, are
28
+ # included for each Foot object in the final HTML output. The
29
+ # default is true.
30
+ def self.end_tags?
31
+ @end_tags
32
+ end
33
+
34
+ # Sets whether or not end tags are included for each Foot object in
35
+ # the final HTML output. The default is true. Only true or false are
36
+ # valid arguments.
37
+ def self.end_tags=(bool)
38
+ expect(bool, [TrueClass,FalseClass])
39
+ @end_tags = bool
40
+ end
41
+ end
42
+
43
+ end
@@ -0,0 +1,42 @@
1
+ module HTML
2
+
3
+ # This class represents an HTML table head (<thead>). It is a
4
+ # subclass of Table::TableSection. It is a singleton class.
5
+ class Table::Head < Table::TableSection
6
+ private_class_method :new
7
+ @@head = nil
8
+ @indent_level = 3
9
+ @end_tags = true
10
+
11
+ # This is our constructor for Head objects because it is a singleton
12
+ # class. Optionally, a block may be provided. If an argument is
13
+ # provided it is treated as content.
14
+ def self.create(arg=nil,&block)
15
+ @@head = new(arg,&block) unless @@head
16
+ @@head
17
+ end
18
+
19
+ # Called by create() instead of new(). This initializes the Head class.
20
+ def initialize(arg,&block)
21
+ @html_begin = "<thead"
22
+ @html_end = "</thead>"
23
+ yield self if block_given?
24
+ self.content = arg if arg
25
+ end
26
+
27
+ # Returns a boolean indicating whether or not end tags, </thead>, are
28
+ # included for each Head object in the final HTML output. The
29
+ # default is true.
30
+ def self.end_tags?
31
+ @end_tags
32
+ end
33
+
34
+ # Sets whether or not end tags are included for each Head object in
35
+ # the final HTML output. The default is true. Only true or false are
36
+ # valid arguments.
37
+ def self.end_tags=(bool)
38
+ expect(bool,[TrueClass,FalseClass])
39
+ @end_tags = bool
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,56 @@
1
+ module HTML
2
+
3
+ # This class represents an HTML table header (<th>). Despite the name
4
+ # it is not a subclass of Table or Table::Row.
5
+ class Table::Row::Header
6
+ include AttributeHandler
7
+ include HtmlHandler
8
+ undef_method :configure
9
+ @indent_level = 6
10
+ @end_tags = true
11
+
12
+ # Creates and returns a new Header object. Optionally takes a block.
13
+ # If an argument is provided, it is treated as content.
14
+ def initialize(arg = nil)
15
+ @html_begin = '<th'
16
+ @html_body = ''
17
+ @html_end = '</th>'
18
+ yield self if block_given?
19
+ self.content = arg if arg
20
+ end
21
+
22
+ # Adds content to the Table::Row::Header object.
23
+ def content=(arg)
24
+ @html_body = arg.to_s
25
+ end
26
+
27
+ # Returns the indentation level for the tags of this class. The
28
+ # default is 6.
29
+ def self.indent_level
30
+ @indent_level
31
+ end
32
+
33
+ # Sets the indentation level for the tags of this class. The default
34
+ # is 6.
35
+ def self.indent_level=(num)
36
+ expect(num,Integer)
37
+ raise ArgumentError,"indent_level must be >= 0" if num < 0
38
+ @indent_level = num
39
+ end
40
+
41
+ # Returns a boolean indicating whether or not end tags are included for
42
+ # each Header object in the final HTML output. The default is true.
43
+ def self.end_tags?
44
+ @end_tags
45
+ end
46
+
47
+ # Sets whether or not end tags are included for each Header object in
48
+ # the final HTML output. The default is true. Only true or false are
49
+ # valid arguments.
50
+ def self.end_tags=(bool)
51
+ expect(bool,[TrueClass,FalseClass])
52
+ @end_tags = bool
53
+ end
54
+ end
55
+
56
+ end
@@ -0,0 +1,94 @@
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
+ def modify_html(attribute,arg=nil)
9
+ if @html_begin.scan(/\b#{attribute}\b/).empty?
10
+ if arg.kind_of?(Fixnum)
11
+ @html_begin << " #{attribute}=#{arg}"
12
+ elsif arg.kind_of?(TrueClass)
13
+ @html_begin << " #{attribute}"
14
+ else
15
+ @html_begin << " #{attribute}='#{arg}'"
16
+ end
17
+ else
18
+ if arg.kind_of?(Fixnum)
19
+ @html_begin.gsub!(/#{attribute}=\d+/,"#{attribute}=#{arg}")
20
+ elsif arg.kind_of?(FalseClass)
21
+ @html_begin.gsub!(/#{attribute}/,'')
22
+ else
23
+ @html_begin.gsub!(/#{attribute}=['\w\.]+/,"#{attribute}='#{arg}'")
24
+ end
25
+ end
26
+ end
27
+
28
+ # Returns the HTML text for the current object. Indentation and end tag
29
+ # options are optional, based on the settings of the classes themselves.
30
+ def html
31
+ if self.class.respond_to?(:html_case)
32
+ $upper = true if self.class.html_case == "upper"
33
+ end
34
+
35
+ if $upper
36
+ @html_begin.upcase!
37
+ @html_end.upcase!
38
+ end
39
+
40
+ ilevel = 0
41
+
42
+ if self.class.respond_to?(:indent_level)
43
+ ilevel = self.class.indent_level
44
+ end
45
+
46
+ html = ' ' * ilevel + @html_begin[0..-1]
47
+ len = html.length
48
+ html[len,len] = '>'
49
+
50
+ if self.kind_of?(Array)
51
+ html << self.map{ |e| "\n" + e.html.to_s }.join
52
+ else
53
+ html << @html_body
54
+ end
55
+
56
+ #####################################################################
57
+ # Add end tags, or not, depending on whether the class supports the
58
+ # end_tags class method. Those that don't have an end_tags class
59
+ # method necessarily means that the end tag must be included.
60
+ #
61
+ # The Table.global_end_tags method overrides the individual class
62
+ # preferences with regards to end tags.
63
+ #####################################################################
64
+ if self.kind_of?(Array)
65
+ if Table.global_end_tags?
66
+ if self.class.respond_to?(:end_tags?)
67
+ html << "\n" + ' ' * ilevel + @html_end if self.class.end_tags?
68
+ else
69
+ html << "\n" + ' ' * ilevel + @html_end
70
+ end
71
+ else
72
+ unless self.class.respond_to?(:end_tags?)
73
+ html << "\n" + ' ' * ilevel + @html_end
74
+ end
75
+ end
76
+ else
77
+ if Table.global_end_tags?
78
+ if self.class.respond_to?(:end_tags?)
79
+ html << @html_end if self.class.end_tags?
80
+ else
81
+ html << @html_end
82
+ end
83
+ else
84
+ unless self.class.respond_to?(:end_tags?)
85
+ html << @html_end
86
+ end
87
+ end
88
+ end
89
+
90
+ return html
91
+ end
92
+
93
+ private :modify_html
94
+ end