Class: Sevgi::Graphics::Content

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

Overview

Renderable text-like content inside an SVG element. 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.

Defined Under Namespace

Classes: CData, CSS, Encoded, Verbatim

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ void

Creates immutable content from a deep payload snapshot. Strings and containers are copied recursively; mutable non-container objects are stringified once during construction. The caller's objects are never retained.

Parameters:

  • content (Object)

    wrapped content

Raises:

  • (Sevgi::ArgumentError)

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



87
88
89
90
# File 'lib/sevgi/graphics/auxiliary/content.rb', line 87

def initialize(content)
  @content = Snapshot.capture(content)
  XML.validate(@content)
end

Class Method Details

.cdata(content) ⇒ Sevgi::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:

  • (Sevgi::ArgumentError)

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



124
# File 'lib/sevgi/graphics/auxiliary/content.rb', line 124

def self.cdata(...) = CData.new(...)

.contents(*args) ⇒ Array<Sevgi::Graphics::Content>

Wraps content arguments, encoding non-content values. Mutable non-content values are stringified by encoded content during construction, before XML text escaping.

Parameters:

  • args (Array<Object>)

    content arguments

Returns:

Raises:

  • (Sevgi::ArgumentError)

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



132
# File 'lib/sevgi/graphics/auxiliary/content.rb', line 132

def self.contents(*args) = args.map { it.is_a?(Content) ? it : encoded(it) }

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

Builds CSS content.

Parameters:

  • content (Hash)

    CSS rules

Returns:

Raises:

  • (Sevgi::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



141
# File 'lib/sevgi/graphics/auxiliary/content.rb', line 141

def self.css(...) = CSS.new(...)

.encoded(content) ⇒ Sevgi::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:

  • (Sevgi::ArgumentError)

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



150
# File 'lib/sevgi/graphics/auxiliary/content.rb', line 150

def self.encoded(...) = Encoded.new(...)

.text(contents) ⇒ String

Joins content lines with newlines.

Parameters:

  • contents (Object, Array<Object>)

    content lines

Returns:

  • (String)


155
# File 'lib/sevgi/graphics/auxiliary/content.rb', line 155

def self.text(contents) = Array(contents).join("\n")

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

Builds verbatim content. Mutable objects are stringified during construction.

Parameters:

  • content (Object)

    wrapped content

Returns:

Raises:

  • (Sevgi::ArgumentError)

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



164
# File 'lib/sevgi/graphics/auxiliary/content.rb', line 164

def self.verbatim(...) = Verbatim.new(...)

Instance Method Details

#contentObject

Returns a recursively independent, caller-owned payload snapshot.

Returns:

  • (Object)

    wrapped content snapshot



79
# File 'lib/sevgi/graphics/auxiliary/content.rb', line 79

def content = Snapshot.copy(@content)

#initialize_copy(original) ⇒ 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.

This method returns an undefined value.

Copies content payload ownership for duplicated element trees.

Parameters:



96
97
98
99
# File 'lib/sevgi/graphics/auxiliary/content.rb', line 96

def initialize_copy(original)
  @content = Snapshot.capture(original.content)
  super
end

#render(_renderer, _depth) ⇒ void

This method is abstract.

Subclasses implement content rendering.

This method returns an undefined value.

Renders content with a renderer.

Parameters:

  • _renderer (Object)

    renderer receiving output

  • _depth (Integer)

    current render depth

Raises:

  • (Sevgi::PanicError)

    when a subclass does not implement render



107
# File 'lib/sevgi/graphics/auxiliary/content.rb', line 107

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

#to_sString

Returns content as a string.

Returns:

  • (String)


111
# File 'lib/sevgi/graphics/auxiliary/content.rb', line 111

def to_s = XML.text(payload)