class Builder
A builder for rendering Utopia content that extends XRB::Builder with Utopia-specific functionality.
Definitions
def initialize(parent, tag, node, attributes = tag.to_hash, **options)
Initialize rendering state for a content node.
Signature
-
parameter
parentBuilder | Nil The enclosing builder state.
-
parameter
tagXRB::Tag | Nil The tag that opened this state.
-
parameter
nodeUtopia::Content::Node The content node.
-
parameter
attributesHash The attributes.
Implementation
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
def defer(value = nil, &block)
Insert a deferred-content marker and retain its rendering block.
Signature
-
parameter
valueObject | Nil An unused compatibility argument.
-
yields
{|document| ...} The deferred rendering operation.
-
returns
String The closed deferred-content tag.
Implementation
def defer(value = nil, &block)
@deferred << block
XRB::Tag.closed(DEFERRED_TAG_NAME, :id => @deferred.size - 1)
end
def [](key)
Fetch an attribute for the current content node.
Signature
-
parameter
keyString | Symbol The lookup key.
-
returns
Object | Nil The attribute value.
Implementation
def [](key)
@attributes[key]
end
def call(document)
Render this node's captured content into a document.
Signature
-
parameter
documentUtopia::Content::Document The content document.
-
returns
String The rendered output buffer.
Implementation
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
def write(string)
Override write to directly append to output
Implementation
def write(string)
@output << string
end
def text(content)
Override text to handle build_markup protocol
Implementation
def text(content)
return unless content
if content.respond_to?(:build_markup)
content.build_markup(self)
else
XRB::Markup.append(@output, content)
end
end
def tag_complete(tag)
Write a complete tag to the output.
Signature
-
parameter
tagXRB::Tag The tag.
-
returns
String The output buffer.
Implementation
def tag_complete(tag)
tag.write(@output)
end
def empty?
Whether this state has any nested tags.
Implementation
def empty?
@tags.empty?
end
def tag_begin(tag)
Tag begin.
Signature
-
parameter
tagXRB::Tag The opening tag.
-
returns
String The output buffer.
Implementation
def tag_begin(tag)
@tags << tag
tag.write_opening_tag(@output)
end
def tag_end(tag)
Tag end.
Signature
-
parameter
tagXRB::Tag The closing tag.
-
returns
String The output buffer.
-
raises
UnbalancedTagError If the closing tag does not match the most recent opening tag.
Implementation
def tag_end(tag)
raise UnbalancedTagError.new(tag) unless @tags.pop.name == tag.name
tag.write_closing_tag(@output)
end