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



25
26
27
# File 'lib/trenni/tag.rb', line 25

def attributes
  @attributes
end

#closedObject

Returns the value of attribute closed

Returns:

  • (Object)

    the current value of closed



25
26
27
# File 'lib/trenni/tag.rb', line 25

def closed
  @closed
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



25
26
27
# File 'lib/trenni/tag.rb', line 25

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



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

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



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

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



36
37
38
# File 'lib/trenni/tag.rb', line 36

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

.format_tag(name, attributes, content) ⇒ Object



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

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



40
41
42
# File 'lib/trenni/tag.rb', line 40

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

.split(qualified_name) ⇒ Object



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

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



44
45
46
# File 'lib/trenni/tag.rb', line 44

def [] key
	attributes[key]
end

#self_closed?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/trenni/tag.rb', line 56

def self_closed?
	closed
end

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



50
51
52
# File 'lib/trenni/tag.rb', line 50

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

#write(buffer, content = nil) ⇒ Object



76
77
78
# File 'lib/trenni/tag.rb', line 76

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

#write_closing_tag(buffer) ⇒ Object



72
73
74
# File 'lib/trenni/tag.rb', line 72

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

#write_opening_tag(buffer) ⇒ Object



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

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