Class: Utopia::Content::Builder

Inherits:
XRB::Builder
  • Object
show all
Defined in:
lib/utopia/content/builder.rb

Overview

A builder for rendering Utopia content that extends XRB::Builder with Utopia-specific functionality.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, tag, node, attributes = tag.to_hash, **options) ⇒ Builder

Initialize rendering state for a content node.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/utopia/content/builder.rb', line 19

def initialize(parent, tag, node, attributes = tag.to_hash, **options)
	super(**options)
	
	@parent = parent
	@tag = tag
	@node = node
	@attributes = attributes
	
	@content = nil
	@deferred = []
	@tags = []
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



34
35
36
# File 'lib/utopia/content/builder.rb', line 34

def attributes
  @attributes
end

#contentObject (readonly)

Returns the value of attribute content.



35
36
37
# File 'lib/utopia/content/builder.rb', line 35

def content
  @content
end

#deferredObject (readonly)

Returns the value of attribute deferred.



41
42
43
# File 'lib/utopia/content/builder.rb', line 41

def deferred
  @deferred
end

#nodeObject (readonly)

Returns the value of attribute node.



36
37
38
# File 'lib/utopia/content/builder.rb', line 36

def node
  @node
end

#parentObject (readonly)

Returns the value of attribute parent.



32
33
34
# File 'lib/utopia/content/builder.rb', line 32

def parent
  @parent
end

#tagObject (readonly)

Returns the value of attribute tag.



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

def tag
  @tag
end

#tagsObject (readonly)

A list of all tags in order of rendering them, which have not been finished yet.



39
40
41
# File 'lib/utopia/content/builder.rb', line 39

def tags
  @tags
end

Instance Method Details

#[](key) ⇒ Object

Fetch an attribute for the current content node.



56
57
58
# File 'lib/utopia/content/builder.rb', line 56

def [](key)
	@attributes[key]
end

#call(document) ⇒ Object

Render this node's captured content into a document.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/utopia/content/builder.rb', line 63

def call(document)
	@content = @output.dup
	@output.clear
	
	if node.respond_to? :call
		node.call(document, self)
	else
		document.parse_markup(@content)
	end
	
	return @output
end

#defer(value = nil, &block) ⇒ Object

Insert a deferred-content marker and retain its rendering block.



47
48
49
50
51
# File 'lib/utopia/content/builder.rb', line 47

def defer(value = nil, &block)
	@deferred << block
	
	XRB::Tag.closed(DEFERRED_TAG_NAME, :id => @deferred.size - 1)
end

#empty?Boolean

Whether this state has any nested tags.

Returns:

  • (Boolean)


100
101
102
# File 'lib/utopia/content/builder.rb', line 100

def empty?
	@tags.empty?
end

#tag_begin(tag) ⇒ Object

Tag begin.



107
108
109
110
# File 'lib/utopia/content/builder.rb', line 107

def tag_begin(tag)
	@tags << tag
	tag.write_opening_tag(@output)
end

#tag_complete(tag) ⇒ Object

Write a complete tag to the output.



95
96
97
# File 'lib/utopia/content/builder.rb', line 95

def tag_complete(tag)
	tag.write(@output)
end

#tag_end(tag) ⇒ Object

Tag end.

Raises:



116
117
118
119
# File 'lib/utopia/content/builder.rb', line 116

def tag_end(tag)
	raise UnbalancedTagError.new(tag) unless @tags.pop.name == tag.name
	tag.write_closing_tag(@output)
end

#text(content) ⇒ Object

Override text to handle build_markup protocol



82
83
84
85
86
87
88
89
90
# File 'lib/utopia/content/builder.rb', line 82

def text(content)
	return unless content
	
	if content.respond_to?(:build_markup)
		content.build_markup(self)
	else
		XRB::Markup.append(@output, content)
	end
end

#write(string) ⇒ Object

Override write to directly append to output



77
78
79
# File 'lib/utopia/content/builder.rb', line 77

def write(string)
	@output << string
end