Class: Sevgi::Derender::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/sevgi/derender/node.rb

Overview

Immutable conversion result for one SVG/XML node.

Attributes, namespaces, content, and descendants are owned snapshots; parser objects and dispatch strategies remain internal to Derender. Attributes omitted during decompilation are absent throughout the captured subtree.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, pres = [], namespaces: nil, omit: nil, top: true) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Builds an owned derender result.

Parameters:

  • node (Nokogiri::XML::Node)

    source XML node

  • pres (Array<String>) (defaults to: [])

    preamble XML lines carried by the root node

  • namespaces (Hash{String => String}, nil) (defaults to: nil)

    namespace declarations to emit on this node

  • omit (Hash{String => Boolean}, nil) (defaults to: nil)

    normalized attribute omission set shared by the captured subtree

  • top (Boolean) (defaults to: true)

    true when this node is the root of the current conversion



114
115
116
117
118
119
120
121
122
123
# File 'lib/sevgi/derender/node.rb', line 114

def initialize(node, pres = [], namespaces: nil, omit: nil, top: true)
  @node = node
  @omit = omit || {}.freeze
  @top = top
  capture_context(pres, namespaces)
  capture_values
  capture_children
  capture_identity
  freeze
end

Instance Attribute Details

#attributesHash{String => String} (readonly)

Returns immutable XML attributes.

Returns:

  • (Hash{String => String})

    owned attribute snapshot



86
87
88
# File 'lib/sevgi/derender/node.rb', line 86

def attributes
  @attributes
end

#childrenArray<Sevgi::Derender::Node> (readonly)

Returns immutable child nodes.

Returns:



90
91
92
# File 'lib/sevgi/derender/node.rb', line 90

def children
  @children
end

#contentString (readonly)

Returns immutable normalized text content. xml:space="preserve" and single-line mixed text retain their exact text. Other text trims surrounding whitespace; multiline mixed text removes only its outer indentation lines.

Returns:

  • (String)

    frozen owned text snapshot



95
96
97
# File 'lib/sevgi/derender/node.rb', line 95

def content
  @content
end

#nameString (readonly)

Returns the immutable qualified element name.

Returns:

  • (String)

    owned element name



99
100
101
# File 'lib/sevgi/derender/node.rb', line 99

def name
  @name
end

#namespacesHash{String => String} (readonly)

Returns immutable namespace declarations emitted for this node. A conversion root owns its local declarations; a separately selected node owns all declarations in scope; descendant snapshots own their local declarations.

Returns:

  • (Hash{String => String})

    frozen owned namespace snapshot



104
105
106
# File 'lib/sevgi/derender/node.rb', line 104

def namespaces
  @namespaces
end

Instance Method Details

#_Hash{String => String} Also known as: meta

Returns Sevgi metadata attributes without the metadata namespace prefix.

Returns:

  • (Hash{String => String})

    immutable metadata snapshot



127
# File 'lib/sevgi/derender/node.rb', line 127

def _ = @meta

#derenderString

Note:

Foreign namespace elements use the explicit Element DSL path, and nested svg nodes remain elements.

Note:

Unsafe bare Ruby names are emitted through the explicit Element DSL word.

Converts this node into formatted Sevgi DSL Ruby source.

Returns:

  • (String)

    formatted Sevgi DSL source

Raises:

  • (Sevgi::PanicError)

    when generated Ruby source cannot be formatted



136
# File 'lib/sevgi/derender/node.rb', line 136

def derender = Ruby.format(decompile(@pres).join("\n"))

#evaluate(element) ⇒ Sevgi::Graphics::Element?

Note:

Namespace declarations, qualified attributes, significant text, and nested svg nodes are preserved.

Evaluates this node under a graphics element.

Parameters:

  • element (Sevgi::Graphics::Element)

    target graphics element

Returns:

  • (Sevgi::Graphics::Element, nil)

    included current element, or nil when it produces no graphics output



142
# File 'lib/sevgi/derender/node.rb', line 142

def evaluate(element) = Evaluator.new(element).append(self)

#evaluate_children(element) ⇒ Array<Sevgi::Graphics::Element>

Evaluates only this node's children under a graphics element.

Parameters:

  • element (Sevgi::Graphics::Element)

    target graphics element

Returns:

  • (Array<Sevgi::Graphics::Element>)

    immutable included-child snapshot



147
# File 'lib/sevgi/derender/node.rb', line 147

def evaluate_children(element) = children.filter_map { it.evaluate(element) }.freeze

#find(arg, by: "id") ⇒ Sevgi::Derender::Node?

Note:

Attributes omitted during decompilation are unavailable for later searches.

Finds this node or the first descendant whose attribute matches a value.

Parameters:

  • arg (String)

    attribute value to find

  • by (String) (defaults to: "id")

    attribute name used for lookup

Returns:



154
155
156
157
158
# File 'lib/sevgi/derender/node.rb', line 154

def find(arg, by: "id")
  return self if attributes[by] == arg

  children.lazy.map { it.find(arg, by:) }.find(&:itself)
end

#root?Boolean

Reports whether this node represents an SVG document root.

Returns:

  • (Boolean)

    true for the conversion root strategy



162
# File 'lib/sevgi/derender/node.rb', line 162

def root? = @type == :Root