Class: Newstile::Element

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

Overview

Represents all elements in the parse tree.

newstile only uses this one class for representing all available elements in a parse tree (paragraphs, headers, emphasis, …). The type of element can be set via the #type accessor.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, value = nil, attr = nil, options = {}) ⇒ Element

Create a new Element object of type type. The optional parameters value, attr and options can also be set in this constructor for convenience.



156
157
158
159
# File 'lib/newstile/document.rb', line 156

def initialize(type, value = nil, attr = nil, options = {})
  @type, @value, @attr, @options = type, value, Utils::OrderedHash.new(attr), options
  @children = []
end

Instance Attribute Details

#attrObject (readonly)

The attributes of the element. Uses an Utils::OrderedHash to retain the insertion order.



140
141
142
# File 'lib/newstile/document.rb', line 140

def attr
  @attr
end

#childrenObject

The child elements of this element.



151
152
153
# File 'lib/newstile/document.rb', line 151

def children
  @children
end

#optionsObject

The options hash for the element. It is used for storing arbitray options as well as the following special contents:

  • Category of the element, either :block or :span, under the :category key. If this key is absent, it can be assumed that the element is in the :span category.



148
149
150
# File 'lib/newstile/document.rb', line 148

def options
  @options
end

#typeObject

A symbol representing the element type. For example, :p or :blockquote.



133
134
135
# File 'lib/newstile/document.rb', line 133

def type
  @type
end

#valueObject

The value of the element. The interpretation of this field depends on the type of the element. Many elements don’t use this field.



137
138
139
# File 'lib/newstile/document.rb', line 137

def value
  @value
end

Instance Method Details

#inspectObject

:nodoc:



161
162
163
# File 'lib/newstile/document.rb', line 161

def inspect #:nodoc:
  "<kd:#{@type}#{@value.nil? ? '' : ' ' + @value.inspect} #{@attr.inspect}#{options.empty? ? '' : ' ' + @options.inspect}#{@children.empty? ? '' : ' ' + @children.inspect}>"
end