Derender

Some vector drawings are easier to create in a visual editor. Examples include a Bezier-heavy logo, a traced illustration, and a hand-adjusted path. Derender brings that SVG or XML into Sumi's element model. The geometry can then participate in Ruby-driven composition, styling, layout, and output.

Use Derender when SVG/XML is a real input artifact, not as a detour for shapes or relationships that are clearer in the Sumi DSL. Convert inline content or a file, generate source, include part of it, or inspect its tree.

The round trip

Derender round tripSVG or XML input becomes an immutable Derender node, then Sumi source, then an evaluated SVG tree.SVG/XML content or fileImmutable Derender nodeSumi sourceEvaluated SVG tree

The conversion keeps element names, attributes, text, comments, CDATA, processing instructions, and child order. It represents the XML tree as Ruby. It cannot recover loops, helper methods, or other higher-level source code.

Processing instructions retain their target and data as XML markup. Sumi does not execute them. Custom entity references in the selected subtree raise Sumi::ArgumentError before inclusion changes the target. Predefined references such as & and numeric references such as A remain valid.

Whole-document conversion rejects comments and processing instructions after the root element. Trailing whitespace is valid. An explicit id: selects only that subtree and ignores unrelated document siblings.

Derender reads the XML encoding declaration or byte-order mark. Generated Ruby and rendered XML use UTF-8. The declaration keeps its version and standalone flag, with any encoding field changed to UTF-8.

Operation familyInline inputFile inputResultExisting target
InspectSVG.DecompileSVG.DecompileFileimmutable Sumi::Derender::Nodeno
Generate sourceSVG.DerenderSVG.DerenderFileformatted Ruby stringno
Include selectionSVG.EvaluateSVG.EvaluateFileincluded element or nilyes
Include childrenSVG.EvaluateChildrenSVG.EvaluateChildrenFilefrozen element snapshotyes

Choose source generation when the converted Ruby becomes the maintained representation. Choose evaluation or Include when the editor file remains the geometry source. Sumi then composes it at runtime.

Library code uses the capitalized facade operations. For example, a consumer can inspect a node and generate only that subtree without adding the script runner's top-level names:

xml = '<svg><g id="mark" style="fill: red"><rect width="4"/></g></svg>'
mark = SVG.Decompile(xml, id: "mark")
source = mark.derender

raise unless mark.name == "g"
raise unless source.include?("rect width: 4")

Generate source

In a .sumi script, Derender converts inline content and returns formatted Ruby:

source = Derender '<path id="mark" d="M 0 0 L 8 3"/>', id: "mark"
puts source

Library code writes the same operation as SVG.Derender(...).

Use DerenderFile when the source already lives on disk:

source = DerenderFile "badge.svg", id: "mark"

The String returned by Derender, DerenderFile, or Node#derender is ordinary Ruby source. Review it and place it in a maintained .sumi or .rb file. Do not pass it to eval, an *_eval method, or an *_exec method. If the SVG file remains authoritative, use Evaluate* or Include*. Sumi then imports it as XML data.

The optional id selects one subtree. Without it, the conversion uses the document root. Pass one attribute name or an array to omit to remove unwanted editor metadata:

source = DerenderFile "badge.svg", id: "mark", omit: "data-editor-label"

Attribute names can be strings or symbols. They match exactly throughout the selected subtree. Selection happens before omission. Removing IDs can break href, url(#...), and CSS references. Keep referenced IDs or update their consumers together. Removing style, transform, or geometry attributes can change the result. Attribute omission preserves namespace declarations and style elements.

Use imus to print the generated Sumi source. You can supply --omit more than once.

imus --omit data-editor-label --omit data-editor-name badge.svg

Omit the file or pass - to read standard input.

imus --omit data-editor-label < badge.svg

With sumie installed, use imus --normalize to print SVG after a round trip through the DSL. The mode accepts repeatable --omit and --require flags.

imus --normalize --omit data-editor-label badge.svg > normalized.svg

This is a structural formatter, not a byte-preserving XML rewrite. Sumi rendering determines declarations, whitespace, attribute spelling, and other serialized details.

Inspect

Decompile stops one step earlier and returns an immutable node. The node owns snapshots of its attributes, namespaces, metadata, content, and descendants:

xml = <<~SVG
  <svg xmlns="http://www.w3.org/2000/svg" xmlns:_="https://sumi.alef.tr/meta">
    <g id="mark" _:role="icon"><rect width="4"/></g>
  </svg>
SVG

root = SVG.Decompile(xml)
mark = root.find("mark")

raise unless root.root?
raise unless root.namespaces["xmlns"] == "http://www.w3.org/2000/svg"
raise unless root.children.first.equal?(mark)
raise unless mark.attributes["id"] == "mark"
raise unless mark.meta["role"] == "icon"
raise unless mark.children.map(&:name) == ["rect"]
raise unless mark.derender.include?("rect width: 4")

The file counterpart is explicit:

node = DecompileFile "badge.svg", id: "mark"

Use these methods when you need to examine a selection, its attributes, or its children without generating source.

Evaluate and include

Evaluate imports inline content directly into an existing SVG tree. EvaluateChildren imports only the selected node's children:

drawing = SVG :minimal
SVG.Evaluate '<circle id="mark" r="4"/>', drawing, id: "mark"
drawing.Render

Use EvaluateFile or EvaluateChildrenFile for file input. All conversion and evaluation forms accept omit. Inside an SVG block, the established Include and IncludeChildren drawing words remain convenient file-oriented forms because their target is already the current element:

SVG do
  Include "badge.svg", "mark"
end

Use IncludeChildren "badge.svg", "mark" inside the drawing block to import the children of the group with ID mark. To import the root's children from a file with no root ID, call SVG.EvaluateChildrenFile "badge.svg", self in that block.

Selection preserves namespace scope. It does not copy ancestor transforms, inherited styles, or definitions outside the selected subtree. Select a self-contained ancestor or include the required context explicitly.

IncludeChildren and EvaluateChildren* omit the selected wrapper. Its transform and style attributes do not reach the imported children. Use the whole-node form when the wrapper affects appearance.

Applications depending only on sumi-derender can use the lowercase API under Sumi::Derender. decompile, derender, evaluate, and evaluate_children accept content. Their _file counterparts accept paths.

All these APIs parse XML as data. They build immutable snapshots or graphics elements without executing generated Ruby. This differs from Sumi.execute, which runs trusted Ruby with the process's authority. Apply normal resource limits when parsing untrusted XML. Parsing alone does not grant the source a Ruby execution path.

The catalog links its Derender entries back here because selection, conversion, and evaluation all use this same mechanism.