Class: Sumi::Graphics::Content Abstract

Inherits:
Object
  • Object
show all
Defined in:
graphics/lib/sumi/graphics/auxiliary/content.rb

Overview

This class is abstract.

Subclasses implement #render and expose their own construction API.

Extensible protocol for renderable text-like content inside an SVG element. Use Content.cdata, Content.css, Content.encoded, or Content.verbatim for built-in content, or subclass Content and implement #render for a custom serialization. Content owns an immutable deep snapshot: strings and containers are copied, mutable leaf objects are stringified once during construction, and #content returns caller-owned copies. Later mutations cannot change rendering or invalidate the construction-time XML checks.

Examples:

Choose a built-in content channel

Sumi::Graphics.SVG(:minimal) do
  text "A & B" # ordinary String arguments are encoded automatically
  text Sumi::Graphics::Content.encoded("A & B")
  style Sumi::Graphics::Content.cdata(".note { fill: red; }")
  style Sumi::Graphics::Content.css(".note" => {fill: "red"})
  g Sumi::Graphics::Content.verbatim("<title>trusted markup</title>")
end

Define custom content that emits an SVG tspan

class Emphasis < Sumi::Graphics::Content
  def self.[](content) = send(:new, content)

  def render(output, depth)
    text = Sumi::Graphics::Content.encoded(to_s).to_s
    output.append(depth + 1, %(<tspan font-style="italic">#{text}</tspan>))
  end
end

Sumi::Graphics.SVG(:minimal) { text Emphasis["important & safe"] }.Render

# The renderer ignores Emphasis#render's return value. Custom content must escape inserted data itself.

See Also:

Defined Under Namespace

Classes: CData, CSS, Encoded, Verbatim

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cdata(content) ⇒ Sumi::Graphics::Content::CData

Builds CDATA content. Mutable objects are stringified during construction, and embedded CDATA terminators are split safely.

Parameters:

  • content (Object) —

    wrapped content

Returns:

Raises:

  • (Sumi::ArgumentError) —

    when content cannot be stringified, contains invalid encoding or illegal XML 1.0 characters, contains cycles, or has keys that collide after stringification



44
# File 'graphics/lib/sumi/graphics/auxiliary/content.rb', line 44

def self.cdata(...) = CData.send(:new, ...)

.css(content) ⇒ Sumi::Graphics::Content::CSS

Builds CSS content.

Parameters:

  • content (Hash) —

    CSS rules

Returns:

Raises:

  • (Sumi::ArgumentError) —

    when content is not a hash, contains a malformed style, cannot be stringified, contains invalid encoding or illegal XML 1.0 characters, contains cycles, or has keys that collide after stringification



53
# File 'graphics/lib/sumi/graphics/auxiliary/content.rb', line 53

def self.css(...) = CSS.send(:new, ...)

.encoded(content) ⇒ Sumi::Graphics::Content::Encoded

Builds XML text-encoded content. Mutable objects are stringified during construction before XML text escaping.

Parameters:

  • content (Object) —

    wrapped content

Returns:

Raises:

  • (Sumi::ArgumentError) —

    when content cannot be stringified, contains invalid encoding or illegal XML 1.0 characters, contains cycles, or has keys that collide after stringification



62
# File 'graphics/lib/sumi/graphics/auxiliary/content.rb', line 62

def self.encoded(...) = Encoded.send(:new, ...)

.verbatim(content) ⇒ Sumi::Graphics::Content::Verbatim

Builds verbatim content. Mutable objects are stringified during construction.

Parameters:

  • content (Object) —

    wrapped content

Returns:

Raises:

  • (Sumi::ArgumentError) —

    when content cannot be stringified, contains invalid encoding or illegal XML 1.0 characters, contains cycles, or has keys that collide after stringification



71
# File 'graphics/lib/sumi/graphics/auxiliary/content.rb', line 71

def self.verbatim(...) = Verbatim.send(:new, ...)

Instance Method Details

#content ⇒ Object

Returns a recursively independent, caller-owned payload snapshot.

Returns:

  • (Object) —

    wrapped content snapshot



75
# File 'graphics/lib/sumi/graphics/auxiliary/content.rb', line 75

def content = Snapshot.copy(@content)

#empty? ⇒ Boolean

Reports whether the captured payload is empty.

Returns:

  • (Boolean)


91
# File 'graphics/lib/sumi/graphics/auxiliary/content.rb', line 91

def empty? = payload.respond_to?(:empty?) ? payload.empty? : to_s.empty?

#eql?(other) ⇒ Boolean Also known as: ==

Reports strict content equality by concrete class and captured payload.

Parameters:

  • other (Object) —

    comparison target

Returns:

  • (Boolean)


96
# File 'graphics/lib/sumi/graphics/auxiliary/content.rb', line 96

def eql?(other) = self.class == other.class && payload.eql?(other.send(:payload))

#hash ⇒ Integer

Returns a hash compatible with strict equality.

Returns:

  • (Integer)


100
# File 'graphics/lib/sumi/graphics/auxiliary/content.rb', line 100

def hash = [self.class, payload].hash

#render(_output, _depth) ⇒ Object

This method is abstract.

Subclasses implement content rendering.

Appends this content's serialized XML lines to rendering output. The output collaborator responds to append(depth, *lines), where depth is an Integer or nil and every line is a String containing valid serialized XML text. The rendering engine ignores both append and render return values. Custom content is responsible for escaping data that it inserts into markup.

Parameters:

  • _output (#append) —

    rendering output collaborator

  • _depth (Integer) —

    current render depth

Returns:

  • (Object) —

    ignored by the rendering engine

Raises:



113
# File 'graphics/lib/sumi/graphics/auxiliary/content.rb', line 113

def render(_output, _depth) = PanicError.("#{self.class}#render must be implemented")

#to_s ⇒ String

Returns content as a string.

Returns:

  • (String)


117
# File 'graphics/lib/sumi/graphics/auxiliary/content.rb', line 117

def to_s = XML.text(payload)