Module: Sumi::Derender

Defined in:
derender/lib/sumi/derender.rb,
derender/lib/sumi/derender/node.rb,
derender/lib/sumi/derender/version.rb,
derender/lib/sumi/derender/document.rb,
derender/lib/sumi/derender/elements.rb,
derender/lib/sumi/derender/internal.rb,
derender/lib/sumi/derender/evaluator.rb,
derender/lib/sumi/derender/attributes.rb,
derender/lib/sumi/derender/elements/any.rb,
derender/lib/sumi/derender/elements/root.rb,
derender/lib/sumi/derender/elements/text.rb,
derender/lib/sumi/derender/elements/style.rb,
derender/lib/sumi/derender/elements/comment.rb,
derender/lib/sumi/derender/elements/instruction.rb

Overview

Brings editor-authored SVG/XML into programmatic Sumi workflows.

Some vector geometry is easier to create in a visual editor than to reconstruct as Ruby. Examples include a Bezier-heavy logo and a hand-adjusted illustration. Derender preserves that SVG/XML tree as inspectable data, or graphics elements under an existing document. This lets editor-authored geometry participate in programmatic composition, styling, layout, and output. Use it when SVG/XML is a real input artifact, not as an intermediate authoring format for ordinary Sumi drawing code.

Generated source uses bare DSL calls only for recognized element names that cannot dispatch to an existing Ruby or Sumi method. Other XML names use the explicit Element DSL word so executing generated source preserves the XML tree without invoking same-named Ruby methods.

Evaluation APIs treat SVG/XML as data: they build graphics element trees directly and do not execute generated Ruby source. The String returned by source-generation APIs is ordinary Ruby source. Review and integrate it statically rather than passing it to Ruby's raw dynamic evaluation methods. Malformed, rootless, or unmatched input is rejected with ArgumentError. Processing instructions retain their target, data, and order as inert XML markup. Custom entity references in the selected subtree are rejected before inclusion changes a target. Predefined and numeric references remain valid. Whole-document conversion rejects nodes after the root. Explicit id selection ignores unrelated document siblings. Input uses the XML encoding declaration or byte-order mark. Generated source and rendered XML use UTF-8. The XML declaration retains its version and standalone flag, with any encoding field changed to UTF-8.

Namespace dispatch treats qualified and foreign elements as ordinary XML nodes. Their element identity, namespace declarations, qualified attributes, significant text, and nested svg elements survive source generation and direct evaluation. CSS specialization applies only to unqualified style elements in no namespace or the default SVG namespace. The document-root strategy also requires an unqualified svg at the root of the conversion. Simple CSS rules use the readable css({...}) DSL form. At-rules, duplicate declarations, and other CSS that cannot be represented losslessly as a Hash remain owned raw style content.

Attribute omission uses exact, case-sensitive names across the selected subtree. ID selection happens first. namespace declarations remain intact, and omitting the style attribute does not omit style elements.

Examples:

Inspect, select, and convert an immutable result

result = Sumi::Derender.decompile('<svg><rect id="mark" width="10"/></svg>')
mark = result.find("mark")
mark.attributes #=> {"id"=>"mark", "width"=>"10"}
mark.derender   #=> "rect id: \"mark\", width: 10\n"

Preserve an at-rule as raw style content

Sumi::Derender.derender("<style>@media print { rect { fill: black; } }</style>")
#=> "style Sumi::Graphics::Content.cdata(\"@media print { rect { fill: black; } }\")\n"

Select a node while omitting editor-only attributes

source = '<svg><g id="mark" style="fill: red"><rect/></g></svg>'
Sumi::Derender.derender(source, id: "mark", omit: %i[id style])
#=> "g do\n  rect\nend\n"

See Also:

Defined Under Namespace

Classes: Node

Constant Summary collapse

VERSION =

Current version of the Sumi derender gem.

"1.0.0"

Class Method Summary collapse

Class Method Details

.decompile(content, id: nil, omit: nil) ⇒ Sumi::Derender::Node

Converts SVG/XML content into an immutable derender result.

Examples:

Inspect a selected immutable node

root = Sumi::Derender.decompile('<svg><g id="mark"><rect width="4"/></g></svg>')
mark = root.find("mark")
mark.name                  #=> "g"
mark.children.first.name  #=> "rect"
mark.attributes["id"]     #=> "mark"

Parameters:

  • content (String) —

    SVG/XML source content

  • id (String, Symbol, nil) (defaults to: nil) —

    optional SVG id selecting a node inside the source

  • omit (String, Symbol, Array<String, Symbol>, nil) (defaults to: nil) —

    exact attribute name or names omitted from the selected subtree after id selection

Returns:

Raises:

See Also:



77
# File 'derender/lib/sumi/derender.rb', line 77

def self.decompile(content, id: nil, omit: nil) = Document.new(content).decompile(id, omit:)

.decompile_file(file, id: nil, omit: nil) ⇒ Sumi::Derender::Node

Converts an SVG/XML file into an immutable derender result.

Parameters:

  • file (String) —

    path to the source SVG/XML file

  • id (String, Symbol, nil) (defaults to: nil) —

    optional SVG id selecting a node inside the source

  • omit (String, Symbol, Array<String, Symbol>, nil) (defaults to: nil) —

    exact attribute name or names omitted from the selected subtree after id selection

Returns:

Raises:

  • (Sumi::ArgumentError) —

    when the file cannot be found, file content is malformed or rootless, or the id is absent

  • (SystemCallError) —

    when the file cannot be read

See Also:



90
# File 'derender/lib/sumi/derender.rb', line 90

def self.decompile_file(file, id: nil, omit: nil) = Document.load_file(file).decompile(id, omit:)

.derender(content, id: nil, omit: nil) ⇒ String

Note:

Namespace-aware dispatch preserves ordinary foreign/qualified nodes and nested SVG elements.

Note:

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

Converts SVG/XML content into Sumi DSL Ruby source. The returned String is ordinary Ruby source. Review and integrate it statically rather than using Ruby's raw dynamic evaluation methods.

Examples:

Select first, then omit attributes from generated source

xml = '<svg><g id="mark" style="fill: red"><rect id="part"/></g></svg>'
Sumi::Derender.derender(xml, id: :mark, omit: [:id, "style"])
#=> "g do\n  rect\nend\n"

Parameters:

  • content (String) —

    SVG/XML source content

  • id (String, Symbol, nil) (defaults to: nil) —

    optional SVG id selecting a node inside the source

  • omit (String, Symbol, Array<String, Symbol>, nil) (defaults to: nil) —

    exact attribute name or names omitted from the selected subtree after id selection

Returns:

  • (String) —

    formatted Sumi DSL source

Raises:

See Also:



109
# File 'derender/lib/sumi/derender.rb', line 109

def self.derender(content, id: nil, omit: nil) = Document.new(content).decompile(id, omit:).derender

.derender_file(file, id: nil, omit: nil) ⇒ String

Note:

Namespace-aware dispatch preserves ordinary foreign/qualified nodes and nested SVG elements.

Note:

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

Converts an SVG/XML file into Sumi DSL Ruby source. The returned String is ordinary Ruby source. Review and integrate it statically rather than using Ruby's raw dynamic evaluation methods.

Parameters:

  • file (String) —

    path to the source SVG/XML file

  • id (String, Symbol, nil) (defaults to: nil) —

    optional SVG id selecting a node inside the source

  • omit (String, Symbol, Array<String, Symbol>, nil) (defaults to: nil) —

    exact attribute name or names omitted from the selected subtree after id selection

Returns:

  • (String) —

    formatted Sumi DSL source

Raises:

  • (Sumi::ArgumentError) —

    when the file cannot be found, file content is malformed or rootless, or the id is absent

  • (Sumi::PanicError) —

    when generated Ruby source cannot be formatted

  • (SystemCallError) —

    when the file cannot be read

See Also:



126
# File 'derender/lib/sumi/derender.rb', line 126

def self.derender_file(file, id: nil, omit: nil) = Document.load_file(file).decompile(id, omit:).derender

.evaluate(content, element, id: nil, omit: nil) ⇒ Sumi::Graphics::Element?

Note:

Namespace-aware dispatch preserves ordinary foreign/qualified nodes and nested SVG elements.

Evaluates SVG/XML content under a graphics element, including the selected node.

Examples:

Include the selected node under an existing element

drawing = Sumi::Graphics.SVG(:minimal)
included = Sumi::Derender.evaluate('<circle id="mark" r="4"/>', drawing)
included.name   #=> :circle
included[:id]   #=> "mark"

Parameters:

  • content (String) —

    SVG/XML source content

  • element (Sumi::Graphics::Element) —

    target graphics element

  • id (String, Symbol, nil) (defaults to: nil) —

    optional SVG id selecting a node inside the source

  • omit (String, Symbol, Array<String, Symbol>, nil) (defaults to: nil) —

    exact attribute name or names omitted from the selected subtree after id selection

Returns:

  • (Sumi::Graphics::Element, nil) —

    included selected/root graphics element, or nil when the selected node produces no graphics output

Raises:

See Also:



145
146
147
# File 'derender/lib/sumi/derender.rb', line 145

def self.evaluate(content, element, id: nil, omit: nil)
  Document.new(content).decompile(id, omit:).evaluate(element)
end

.evaluate_children(content, element, id: nil, omit: nil) ⇒ Array<Sumi::Graphics::Element>

Note:

Namespace-aware dispatch preserves ordinary foreign/qualified nodes and nested SVG elements.

Evaluates only the selected node's children under a graphics element.

Examples:

Include only children and retain an immutable result snapshot

drawing = Sumi::Graphics.SVG(:minimal)
children = Sumi::Derender.evaluate_children('<g><rect/><circle/></g>', drawing)
children.map(&:name) #=> [:rect, :circle]
children.frozen?     #=> true

Parameters:

  • content (String) —

    SVG/XML source content

  • element (Sumi::Graphics::Element) —

    target graphics element

  • id (String, Symbol, nil) (defaults to: nil) —

    optional SVG id selecting a node inside the source

  • omit (String, Symbol, Array<String, Symbol>, nil) (defaults to: nil) —

    exact attribute name or names omitted from the selected subtree after id selection

Returns:

Raises:

See Also:



165
166
167
# File 'derender/lib/sumi/derender.rb', line 165

def self.evaluate_children(content, element, id: nil, omit: nil)
  Document.new(content).decompile(id, omit:).evaluate_children(element)
end

.evaluate_children_file(file, element, id: nil, omit: nil) ⇒ Array<Sumi::Graphics::Element>

Note:

Namespace-aware dispatch preserves ordinary foreign/qualified nodes and nested SVG elements.

Evaluates only the selected node's children from an SVG/XML file under a graphics element.

Parameters:

  • file (String) —

    path to the source SVG/XML file

  • element (Sumi::Graphics::Element) —

    target graphics element

  • id (String, Symbol, nil) (defaults to: nil) —

    optional SVG id selecting a node inside the source

  • omit (String, Symbol, Array<String, Symbol>, nil) (defaults to: nil) —

    exact attribute name or names omitted from the selected subtree after id selection

Returns:

Raises:

  • (Sumi::ArgumentError) —

    when the file cannot be found, file content is malformed or rootless, or the id is absent

  • (SystemCallError) —

    when the file cannot be read

See Also:



200
201
202
# File 'derender/lib/sumi/derender.rb', line 200

def self.evaluate_children_file(file, element, id: nil, omit: nil)
  Document.load_file(file).decompile(id, omit:).evaluate_children(element)
end

.evaluate_file(file, element, id: nil, omit: nil) ⇒ Sumi::Graphics::Element?

Note:

Namespace-aware dispatch preserves ordinary foreign/qualified nodes and nested SVG elements.

Evaluates an SVG/XML file under a graphics element, including the selected node.

Parameters:

  • file (String) —

    path to the source SVG/XML file

  • element (Sumi::Graphics::Element) —

    target graphics element

  • id (String, Symbol, nil) (defaults to: nil) —

    optional SVG id selecting a node inside the source

  • omit (String, Symbol, Array<String, Symbol>, nil) (defaults to: nil) —

    exact attribute name or names omitted from the selected subtree after id selection

Returns:

  • (Sumi::Graphics::Element, nil) —

    included selected/root graphics element, or nil when the selected node produces no graphics output

Raises:

  • (Sumi::ArgumentError) —

    when the file cannot be found, file content is malformed or rootless, or the id is absent

  • (SystemCallError) —

    when the file cannot be read

See Also:



183
184
185
# File 'derender/lib/sumi/derender.rb', line 183

def self.evaluate_file(file, element, id: nil, omit: nil)
  Document.load_file(file).decompile(id, omit:).evaluate(element)
end