Class: Utopia::Content::Tag

Inherits:
Struct
  • Object
show all
Includes:
Trenni::Markup
Defined in:
lib/utopia/content/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

Instance Attribute Details

#attributesObject Also known as: to_hash

Returns the value of attribute attributes

Returns:

  • (Object)

    the current value of attributes



26
27
28
# File 'lib/utopia/content/tag.rb', line 26

def attributes
  @attributes
end

#closedObject

Returns the value of attribute closed

Returns:

  • (Object)

    the current value of closed



26
27
28
# File 'lib/utopia/content/tag.rb', line 26

def closed
  @closed
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



26
27
28
# File 'lib/utopia/content/tag.rb', line 26

def name
  @name
end

Class Method Details

.closed(name, **attributes) ⇒ Object



29
30
31
# File 'lib/utopia/content/tag.rb', line 29

def self.closed(name, **attributes)
	self.new(name, true, attributes)
end

.opened(name, **attributes) ⇒ Object



33
34
35
# File 'lib/utopia/content/tag.rb', line 33

def self.opened(name, **attributes)
	self.new(name, false, attributes)
end

Instance Method Details

#[](key) ⇒ Object



37
38
39
# File 'lib/utopia/content/tag.rb', line 37

def [] key
	attributes[key]
end

#self_closed?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/utopia/content/tag.rb', line 53

def self_closed?
	closed
end

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



43
44
45
46
47
48
49
# File 'lib/utopia/content/tag.rb', line 43

def to_s(content = nil)
	buffer = String.new.force_encoding(name.encoding)
	
	write(buffer, content)
	
	return buffer
end

#write(buffer, content = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/utopia/content/tag.rb', line 79

def write(buffer, content = nil)
	if closed and content.nil?
		write_opening_tag(buffer, true)
	else
		write_opening_tag(buffer)
		buffer << content if content
		write_closing_tag(buffer)
	end
end

#write_closing_tag(buffer) ⇒ Object



75
76
77
# File 'lib/utopia/content/tag.rb', line 75

def write_closing_tag(buffer)
	buffer << "</#{name}>"
end

#write_opening_tag(buffer, self_closing = false) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/utopia/content/tag.rb', line 57

def write_opening_tag(buffer, self_closing = false)
	buffer << "<#{name}"

	attributes.each do |key, value|
		if value
			buffer << " #{key}=\"#{value}\""
		else
			buffer << " #{key}"
		end
	end
	
	if self_closing
		buffer << "/>"
	else
		buffer << ">"
	end
end