Class: Trenni::Tag

Inherits:
Struct
  • Object
show all
Includes:
Markup
Defined in:
lib/trenni/tag.rb

Overview

This represents an individual SGML tag, e.g. <a>, </a> or <a />, with attributes. Attribute values must be escaped.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Markup

append, escape_string, raw

Instance Attribute Details

#attributesObject Also known as: to_hash

Returns the value of attribute attributes

Returns:

  • (Object)

    the current value of attributes



27
28
29
# File 'lib/trenni/tag.rb', line 27

def attributes
  @attributes
end

#closedObject

Returns the value of attribute closed

Returns:

  • (Object)

    the current value of closed



27
28
29
# File 'lib/trenni/tag.rb', line 27

def closed
  @closed
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



27
28
29
# File 'lib/trenni/tag.rb', line 27

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 <tag>.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/trenni/tag.rb', line 109

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
			self.append_attributes(buffer, value, attribute_key)
		when TrueClass
			buffer << ' ' << attribute_key.to_s
		else
			buffer << ' ' << attribute_key.to_s << '="'
			Markup.append(buffer, value)
			buffer << '"'
		end
	end
	
	return nil
end

.append_tag(buffer, name, attributes, content) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/trenni/tag.rb', line 90

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
			Markup.append(buffer, content)
		end
		buffer << '</' << name.to_s << '>'
	end
	
	return nil
end

.closed(name, attributes = {}) ⇒ Object



38
39
40
# File 'lib/trenni/tag.rb', line 38

def self.closed(name, attributes = {})
	self.new(name, true, attributes)
end

.format_tag(name, attributes, content) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/trenni/tag.rb', line 82

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



42
43
44
# File 'lib/trenni/tag.rb', line 42

def self.opened(name, attributes = {})
	self.new(name, false, attributes)
end

.split(qualified_name) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/trenni/tag.rb', line 30

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



46
47
48
# File 'lib/trenni/tag.rb', line 46

def [] key
	attributes[key]
end

#self_closed?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/trenni/tag.rb', line 58

def self_closed?
	closed
end

#to_s(content = nil) ⇒ Object Also known as: to_str



52
53
54
# File 'lib/trenni/tag.rb', line 52

def to_s(content = nil)
	self.class.format_tag(name, attributes, content || !closed)
end

#write(buffer, content = nil) ⇒ Object



78
79
80
# File 'lib/trenni/tag.rb', line 78

def write(buffer, content = nil)
	self.class.append_tag(buffer, name, attributes, content || !closed)
end

#write_closing_tag(buffer) ⇒ Object



74
75
76
# File 'lib/trenni/tag.rb', line 74

def write_closing_tag(buffer)
	buffer << '</' << name << '>'
end

#write_opening_tag(buffer) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/trenni/tag.rb', line 62

def write_opening_tag(buffer)
	buffer << '<' << name
	
	self.class.append_attributes(buffer, attributes, nil)
	
	if self_closed?
		buffer << '/>'
	else
		buffer << '>'
	end
end