Sha256: 66ac2aeb07447effc44a0d76aea056570e17f6733f53ea765a7ce49a46b1f168

Contents?: true

Size: 845 Bytes

Versions: 2

Compression:

Stored size: 845 Bytes

Contents

module Svgeez
  class SymbolElement
    def initialize(file_path, file_id)
      @file_path = file_path
      @file_id = file_id
    end

    def build
      IO.read(@file_path).match(%r{^<svg\s*?(?<attributes>.*?)>(?<content>.*?)</svg>}m) do |matches|
        %(<symbol #{element_attributes(matches[:attributes]).sort.join(' ')}>#{element_contents(matches[:content])}</symbol>)
      end
    end

    private

    def element_attributes(attributes)
      attrs = attributes.scan(/(?:viewBox|xmlns:.+?)=".*?"/m)

      attrs << %(id="#{@file_id}-#{File.basename(@file_path, '.svg').gsub(/['"\s]/, '-')}")
    end

    def element_contents(content)
      content.scan(/\sid="(.+?)"/).flatten.each do |value|
        content.gsub!(/\s(id|xlink:href)="(#?#{value})"/m, %( \\1="\\2-#{SecureRandom.uuid}"))
      end

      content
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
svgeez-2.0.0 lib/svgeez/symbol_element.rb
svgeez-1.0.3 lib/svgeez/symbol_element.rb