Class: Sevgi::Derender::Node
- Inherits:
-
Object
- Object
- Sevgi::Derender::Node
- Defined in:
- lib/sevgi/derender/node.rb
Overview
Node in a derender tree produced from an SVG/XML node.
Constant Summary collapse
- META_NAMESPACE =
Attribute namespace prefix used for Sevgi metadata.
"_:"
Instance Attribute Summary collapse
-
#node ⇒ Nokogiri::XML::Node
readonly
Returns the source XML node.
-
#pres ⇒ Array<String>
readonly
Returns preamble XML lines carried by the root node.
-
#type ⇒ Symbol
readonly
Returns the dispatch type used by derender element strategies.
Instance Method Summary collapse
-
#_ ⇒ Hash{String => String}
(also: #meta)
Returns Sevgi metadata attributes without the metadata namespace prefix.
-
#attributes ⇒ Hash{String => String}
Returns source XML attributes keyed with namespace prefixes when present.
-
#attributes! ⇒ Hash{String => String}
Returns source XML attributes and namespace declarations emitted on this node.
-
#children ⇒ Array<Sevgi::Derender::Node>
Returns non-ignorable child derender nodes.
-
#content ⇒ String
Returns node text content.
-
#derender ⇒ String
Converts this node into formatted Sevgi DSL Ruby source.
-
#element ⇒ String
Returns the Sevgi DSL element name for this node.
-
#evaluate(element, include_current = true) ⇒ Sevgi::Graphics::Element, ...
Evaluates this node under a graphics element.
-
#evaluate_children(element) ⇒ Array<Sevgi::Graphics::Element>
Evaluates only this node's children under a graphics element.
-
#find(arg, by: "id") ⇒ Sevgi::Derender::Node?
Finds the first descendant whose attribute matches a value.
-
#initialize(node, pres = [], namespaces: nil, top: true) ⇒ void
constructor
Builds a derender node.
-
#name ⇒ String
Returns the source XML qualified name, including its namespace prefix when present.
-
#namespaces ⇒ Hash{String => String}
Returns source XML namespace declarations emitted on this node.
-
#root? ⇒ Boolean
Reports whether this node is the SVG root strategy.
Constructor Details
#initialize(node, pres = [], namespaces: nil, top: true) ⇒ void
Builds a derender node.
43 44 45 46 47 48 49 |
# File 'lib/sevgi/derender/node.rb', line 43 def initialize(node, pres = [], namespaces: nil, top: true) @node = node @pres = pres @namespaces = namespaces @top = top @type = dispatch end |
Instance Attribute Details
#node ⇒ Nokogiri::XML::Node (readonly)
Returns the source XML node.
26 27 28 |
# File 'lib/sevgi/derender/node.rb', line 26 def node @node end |
#pres ⇒ Array<String> (readonly)
Returns preamble XML lines carried by the root node.
30 31 32 |
# File 'lib/sevgi/derender/node.rb', line 30 def pres @pres end |
#type ⇒ Symbol (readonly)
Returns the dispatch type used by derender element strategies.
34 35 36 |
# File 'lib/sevgi/derender/node.rb', line 34 def type @type end |
Instance Method Details
#_ ⇒ Hash{String => String} Also known as: meta
Returns Sevgi metadata attributes without the metadata namespace prefix.
56 57 58 59 60 61 62 |
# File 'lib/sevgi/derender/node.rb', line 56 def _ @_ ||= attributes .slice( *attributes.keys.select { |key| key.start_with?(META_NAMESPACE) } ) .transform_keys! { |key| key.delete_prefix(META_NAMESPACE) } end |
#attributes ⇒ Hash{String => String}
Returns source XML attributes keyed with namespace prefixes when present.
68 |
# File 'lib/sevgi/derender/node.rb', line 68 def attributes = @attributes ||= node.attribute_nodes.to_h { [attribute_key(it), it.value] } |
#attributes! ⇒ Hash{String => String}
Returns source XML attributes and namespace declarations emitted on this node.
72 |
# File 'lib/sevgi/derender/node.rb', line 72 def attributes! = {**attributes, **namespaces} |
#children ⇒ Array<Sevgi::Derender::Node>
Returns non-ignorable child derender nodes.
76 |
# File 'lib/sevgi/derender/node.rb', line 76 def children = @children ||= node.children.map { self.class.new(it, top: false) }.reject { ignorable_child?(it) } |
#content ⇒ String
Returns node text content.
xml:space="preserve" keeps content verbatim. Default-space text nodes are stripped for ordinary pretty-printed
content; line-break indentation around mixed-content children is removed while inline boundary spaces and tabs are
kept because they affect rendered SVG text.
84 |
# File 'lib/sevgi/derender/node.rb', line 84 def content = @content ||= preserve_space? ? node.content : normalized_content |
#derender ⇒ String
Foreign namespace elements use the explicit Element DSL path, and nested svg nodes remain elements.
Unsafe bare Ruby names are emitted through the explicit Element DSL word.
Converts this node into formatted Sevgi DSL Ruby source.
91 |
# File 'lib/sevgi/derender/node.rb', line 91 def derender = Ruby.format(decompile(pres).join("\n")) |
#element ⇒ String
Returns the Sevgi DSL element name for this node.
95 |
# File 'lib/sevgi/derender/node.rb', line 95 def element = name |
#evaluate(element, include_current = true) ⇒ Sevgi::Graphics::Element, ...
Namespace declarations, qualified attributes, significant text, and nested svg nodes are preserved.
Evaluates this node under a graphics element.
103 104 105 106 107 |
# File 'lib/sevgi/derender/node.rb', line 103 def evaluate(element, include_current = true) return Evaluator.new(element).append(self) if include_current evaluate_children(element) end |
#evaluate_children(element) ⇒ Array<Sevgi::Graphics::Element>
Evaluates only this node's children under a graphics element.
112 |
# File 'lib/sevgi/derender/node.rb', line 112 def evaluate_children(element) = children.map { it.evaluate(element) }.compact |
#find(arg, by: "id") ⇒ Sevgi::Derender::Node?
Finds the first descendant whose attribute matches a value.
118 119 120 121 122 |
# File 'lib/sevgi/derender/node.rb', line 118 def find(arg, by: "id") return self if attributes[by] == arg children.lazy.map { it.find(arg, by:) }.find(&:itself) end |
#name ⇒ String
Returns the source XML qualified name, including its namespace prefix when present.
126 |
# File 'lib/sevgi/derender/node.rb', line 126 def name = @name ||= [node.namespace&.prefix, node.name].compact.join(":") |
#namespaces ⇒ Hash{String => String}
Returns source XML namespace declarations emitted on this node.
130 |
# File 'lib/sevgi/derender/node.rb', line 130 def namespaces = (@namespaces ||= local_namespaces) |
#root? ⇒ Boolean
Reports whether this node is the SVG root strategy.
134 |
# File 'lib/sevgi/derender/node.rb', line 134 def root? = type == :Root |