class Victor::CLI::SVGNode

Attributes

layout[R]
node[R]

Public Class Methods

load(svg_string, layout: nil) click to toggle source

Returns a new instance from raw SVG string

# File lib/victor/cli/svg_node.rb, line 17
def self.load(svg_string, layout: nil)
  doc = Nokogiri::XML svg_string
  root = doc.children.last
  new root, layout: layout
end
load_file(path, layout: nil) click to toggle source

Returns a new instance from an SVG file

# File lib/victor/cli/svg_node.rb, line 12
def self.load_file(path, layout: nil)
  load File.read(path), layout: layout
end
new(node, layout: nil) click to toggle source

Initializes with a Nokogiri XML node

# File lib/victor/cli/svg_node.rb, line 24
def initialize(node, layout: nil)
  @node = node
  @layout = layout || :cli
end

Public Instance Methods

attributes() click to toggle source

Returns a hash of attributes

# File lib/victor/cli/svg_node.rb, line 51
def attributes
  @attributes ||= attributes!
end
children() click to toggle source

Returns an array of children elements (SVGNode)

# File lib/victor/cli/svg_node.rb, line 61
def children
  @children ||= children!
end
content() click to toggle source

Returns the content (body) of the lement

# File lib/victor/cli/svg_node.rb, line 56
def content
  @content ||= content!
end
inspect() click to toggle source
# File lib/victor/cli/svg_node.rb, line 36
def inspect
  "#<#{self.class} name=#{name}, type=#{type}>"
end
name() click to toggle source

Returns the tag name

# File lib/victor/cli/svg_node.rb, line 41
def name
  node.name
end
render() click to toggle source

Returns formatted Ruby code

# File lib/victor/cli/svg_node.rb, line 30
def render
  Rufo::Formatter.format ruby_code
rescue Rufo::SyntaxError => e
  raise ruby_code
end
ruby_code() click to toggle source

Returns the ruby code, unformatted

# File lib/victor/cli/svg_node.rb, line 66
def ruby_code
  erb erb_template
end
type() click to toggle source

Returns one of our internal types (symbol)

# File lib/victor/cli/svg_node.rb, line 46
def type
  @type ||= type!
end

Private Instance Methods

attributes!() click to toggle source

Returns a hash of attributes

# File lib/victor/cli/svg_node.rb, line 116
def attributes!
  node.attribute_nodes.map do |attr|
    name = attr.name
    value = attr.value
    key = attr.respond_to?(:prefix) ? "#{attr.prefix}:#{name}" : name
    
    [key, value]
  end.to_h
end
children!() click to toggle source

Returns a filtered list of SVGNode children

# File lib/victor/cli/svg_node.rb, line 109
def children!
  node.children.map do |child|
    SVGNode.new child
  end.reject(&:rejected?)
end
content!() click to toggle source
# File lib/victor/cli/svg_node.rb, line 126
def content!
  if type == :css
    CSSData.new(node.content).to_h
  elsif node.content.is_a? String
    node.content.strip
  else
    # TODO: do we need this?
    # :nocov:
    node.content
    # :nocov:
  end
end
erb(code) click to toggle source

Renders ERB code

# File lib/victor/cli/svg_node.rb, line 78
def erb(code)
  ERB.new(code, nil, "%-").result(binding)
end
erb_template() click to toggle source

Returns the content of the appropriate ERB tempalte, based on type

# File lib/victor/cli/svg_node.rb, line 83
def erb_template
  @erb_template ||= File.read(erb_template_file)
end
erb_template_file() click to toggle source

Returns the path to the appropriate ERB template, based on type

# File lib/victor/cli/svg_node.rb, line 88
def erb_template_file
  file = type == :root ? "root_#{layout}" : type
  path = File.expand_path "templates/nodes/#{file}.erb", __dir__
end
rejected?() click to toggle source

Returns true if the element should be ignored

# File lib/victor/cli/svg_node.rb, line 73
def rejected?
  (node.text? and node.text.strip.empty?) or type == :junk
end
type!() click to toggle source

Returns the internal element type

# File lib/victor/cli/svg_node.rb, line 94
def type!
  if node.text?
    :text
  elsif node.comment?
    :junk
  elsif node.name == 'style'
    :css
  elsif node.name == 'svg'
    :root
  else
    :element
  end
end