Class: Utopia::Content::Document::State

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/content/document.rb

Overview

The state of a single tag being rendered within a document instance.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, tag, node, attributes = tag.to_hash) ⇒ State

Returns a new instance of State.



213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/utopia/content/document.rb', line 213

def initialize(parent, tag, node, attributes = tag.to_hash)
	@parent = parent
	@tag = tag
	@node = node
	@attributes = attributes
	
	@buffer = XRB::MarkupString.new.force_encoding(Encoding::UTF_8)
	@content = nil
	
	@deferred = []
	
	@tags = []
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



228
229
230
# File 'lib/utopia/content/document.rb', line 228

def attributes
  @attributes
end

#contentObject (readonly)

Returns the value of attribute content.



229
230
231
# File 'lib/utopia/content/document.rb', line 229

def content
  @content
end

#deferredObject (readonly)

Returns the value of attribute deferred.



235
236
237
# File 'lib/utopia/content/document.rb', line 235

def deferred
  @deferred
end

#nodeObject (readonly)

Returns the value of attribute node.



230
231
232
# File 'lib/utopia/content/document.rb', line 230

def node
  @node
end

#parentObject (readonly)

Returns the value of attribute parent.



227
228
229
# File 'lib/utopia/content/document.rb', line 227

def parent
  @parent
end

#tagsObject (readonly)

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



233
234
235
# File 'lib/utopia/content/document.rb', line 233

def tags
  @tags
end

Instance Method Details

#[](key) ⇒ Object



243
244
245
# File 'lib/utopia/content/document.rb', line 243

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

#call(document) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/utopia/content/document.rb', line 247

def call(document)
	@content = @buffer
	@buffer = XRB::MarkupString.new.force_encoding(Encoding::UTF_8)
	
	if node.respond_to? :call
		node.call(document, self)
	else
		document.parse_markup(@content)
	end
	
	return @buffer
end

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



237
238
239
240
241
# File 'lib/utopia/content/document.rb', line 237

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

#empty?Boolean

Whether this state has any nested tags.

Returns:

  • (Boolean)


273
274
275
# File 'lib/utopia/content/document.rb', line 273

def empty?
	@tags.empty?
end

#tag_begin(tag) ⇒ Object



277
278
279
280
281
282
# File 'lib/utopia/content/document.rb', line 277

def tag_begin(tag)
	# raise ArgumentError.new("tag_begin: #{tag} is tag.self_closed?") if tag.self_closed?
	
	@tags << tag
	tag.write_opening_tag(@buffer)
end

#tag_complete(tag) ⇒ Object



268
269
270
# File 'lib/utopia/content/document.rb', line 268

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

#tag_end(tag) ⇒ Object

Raises:



284
285
286
287
# File 'lib/utopia/content/document.rb', line 284

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

#text(string) ⇒ Object



264
265
266
# File 'lib/utopia/content/document.rb', line 264

def text(string)
	XRB::Markup.append(@buffer, string)
end

#write(string) ⇒ Object



260
261
262
# File 'lib/utopia/content/document.rb', line 260

def write(string)
	@buffer << string
end