Class: XRB::Tag
- Inherits:
-
Struct
- Object
- Struct
- XRB::Tag
- Defined in:
- lib/xrb/tag.rb
Overview
This represents an individual SGML tag, e.g. , or , with attributes. Attribute values must be escaped.
Instance Attribute Summary collapse
-
#attributes ⇒ Object
(also: #to_hash)
Returns the value of attribute attributes.
-
#closed ⇒ Object
Returns the value of attribute closed.
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
-
.append_attributes(buffer, attributes, prefix) ⇒ Object
Convert a set of attributes into a string suitable for use within a
. -
.append_tag(buffer, name, attributes, content) ⇒ Object
Append a complete tag to an output buffer.
-
.closed(name, attributes = {}) ⇒ Object
Create a self-closing tag.
-
.format_tag(name, attributes, content) ⇒ Object
Format a tag as a new string.
-
.opened(name, attributes = {}) ⇒ Object
Create a tag with separate opening and closing markup.
-
.split(qualified_name) ⇒ Object
Split a qualified tag name into its namespace and local name.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Fetch an attribute value by key.
-
#append_markup(output) ⇒ Object
Append the tag to an output buffer.
-
#build_markup(builder) ⇒ Object
Append the tag using a markup builder.
-
#self_closed? ⇒ Boolean
Determine whether the tag is self-closing.
-
#to_s(content = nil) ⇒ Object
(also: #to_str)
Format the tag and optional content as markup.
-
#write(buffer, content = nil) ⇒ Object
Write the complete tag and optional content to a buffer.
-
#write_closing_tag(buffer) ⇒ Object
Write the closing tag to a buffer.
-
#write_opening_tag(buffer) ⇒ Object
Write the opening or self-closing tag to a buffer.
Instance Attribute Details
#attributes ⇒ Object Also known as: to_hash
Returns the value of attribute attributes
10 11 12 |
# File 'lib/xrb/tag.rb', line 10 def attributes @attributes end |
#closed ⇒ Object
Returns the value of attribute closed
10 11 12 |
# File 'lib/xrb/tag.rb', line 10 def closed @closed end |
#name ⇒ Object
Returns the value of attribute name
10 11 12 |
# File 'lib/xrb/tag.rb', line 10 def name @name end |
Class Method Details
.append_attributes(buffer, attributes, prefix) ⇒ Object
Convert a set of attributes into a string suitable for use within a
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 |
# File 'lib/xrb/tag.rb', line 144 def self.append_attributes(buffer, attributes, prefix) attributes.each do |key, value| next unless value attribute_key = prefix ? "#{prefix}-#{key}" : key case value when Hash self.append_attributes(buffer, value, attribute_key) when Array value.each do |attribute| raise TypeError, "expected array of key-value pairs" unless attribute.is_a?(Array) and attribute.size == 2 self.append_attributes(buffer, [attribute], attribute_key) end when TrueClass buffer << " " << attribute_key.to_s else buffer << " " << attribute_key.to_s << '="' value.append_markup(buffer) buffer << '"' end end return nil end |
.append_tag(buffer, name, attributes, content) ⇒ Object
Append a complete tag to an output buffer.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/xrb/tag.rb', line 125 def self.append_tag(buffer, name, attributes, content) buffer << "<" << name.to_s self.append_attributes(buffer, attributes, nil) if !content buffer << "/>" else buffer << ">" unless content == true content.append_markup(buffer) end buffer << "</" << name.to_s << ">" end return nil end |
.closed(name, attributes = {}) ⇒ Object
Create a self-closing tag.
26 27 28 |
# File 'lib/xrb/tag.rb', line 26 def self.closed(name, attributes = {}) self.new(name, true, attributes) end |
.format_tag(name, attributes, content) ⇒ Object
Format a tag as a new string.
111 112 113 114 115 116 117 |
# File 'lib/xrb/tag.rb', line 111 def self.format_tag(name, attributes, content) buffer = String.new.force_encoding(name.encoding) self.append_tag(buffer, name, attributes, content) return buffer end |
.opened(name, attributes = {}) ⇒ Object
Create a tag with separate opening and closing markup.
34 35 36 |
# File 'lib/xrb/tag.rb', line 34 def self.opened(name, attributes = {}) self.new(name, false, attributes) end |
.split(qualified_name) ⇒ Object
Split a qualified tag name into its namespace and local name.
14 15 16 17 18 19 20 |
# File 'lib/xrb/tag.rb', line 14 def self.split(qualified_name) if i = qualified_name.index(":") return qualified_name.slice(0...i), qualified_name.slice(i+1..-1) else return nil, qualified_name end end |
Instance Method Details
#[](key) ⇒ Object
Fetch an attribute value by key.
55 56 57 |
# File 'lib/xrb/tag.rb', line 55 def [] key attributes[key] end |
#append_markup(output) ⇒ Object
Append the tag to an output buffer.
41 42 43 |
# File 'lib/xrb/tag.rb', line 41 def append_markup(output) self.write(output) end |
#build_markup(builder) ⇒ Object
Append the tag using a markup builder.
48 49 50 |
# File 'lib/xrb/tag.rb', line 48 def build_markup(builder) self.append_markup(builder.output) end |
#self_closed? ⇒ Boolean
Determine whether the tag is self-closing.
72 73 74 |
# File 'lib/xrb/tag.rb', line 72 def self_closed? closed end |
#to_s(content = nil) ⇒ Object Also known as: to_str
Format the tag and optional content as markup.
64 65 66 |
# File 'lib/xrb/tag.rb', line 64 def to_s(content = nil) self.class.format_tag(name, attributes, content || !closed) end |
#write(buffer, content = nil) ⇒ Object
Write the complete tag and optional content to a buffer.
102 103 104 |
# File 'lib/xrb/tag.rb', line 102 def write(buffer, content = nil) self.class.append_tag(buffer, name, attributes, content || !closed) end |
#write_closing_tag(buffer) ⇒ Object
Write the closing tag to a buffer.
94 95 96 |
# File 'lib/xrb/tag.rb', line 94 def write_closing_tag(buffer) buffer << "</" << name << ">" end |
#write_opening_tag(buffer) ⇒ Object
Write the opening or self-closing tag to a buffer.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/xrb/tag.rb', line 79 def write_opening_tag(buffer) buffer << "<" << name self.class.append_attributes(buffer, attributes, nil) if self_closed? buffer << "/>" else buffer << ">" end end |