Sha256: 97ab97ad04cddc72a6a42d83a09bd97187f3ae9335c9e8a69b9fea83113e4742

Contents?: true

Size: 1.64 KB

Versions: 3

Compression:

Stored size: 1.64 KB

Contents

require "nokogiri"

module Vectory
  class SvgDocument
    SVG_NS = "http://www.w3.org/2000/svg".freeze

    def initialize(content)
      @document = Nokogiri::XML(content)
    end

    def content
      @document.root.to_xml
    end

    def namespace(suffix, links, xpath_to_remove)
      remap_links(links)
      suffix_ids(suffix)
      remove_xpath(xpath_to_remove)
    end

    def remap_links(map)
      @document.xpath(".//m:a", "m" => SVG_NS).each do |a|
        href_attrs = ["xlink:href", "href"]
        href_attrs.each do |p|
          a[p] and x = map[File.expand_path(a[p])] and a[p] = x
        end
      end

      self
    end

    def suffix_ids(suffix)
      ids = collect_ids
      return if ids.empty?

      update_ids_attrs(ids, suffix)
      update_ids_css(ids, suffix)

      self
    end

    def remove_xpath(xpath)
      @document.xpath(xpath).remove

      self
    end

    private

    def collect_ids
      @document.xpath("./@id | .//@id").map(&:value)
    end

    def update_ids_attrs(ids, suffix)
      @document.xpath(". | .//*[@*]").each do |a|
        a.attribute_nodes.each do |x|
          ids.include?(x.value) and x.value += sprintf("_%09d", suffix)
        end
      end
    end

    def update_ids_css(ids, suffix)
      @document.xpath("//m:style", "m" => SVG_NS).each do |s|
        c = s.children.to_xml
        ids.each do |i|
          c = c.gsub(%r[##{i}\b],
                     sprintf("#%<id>s_%<suffix>09d", id: i, suffix: suffix))
            .gsub(%r(\[id\s*=\s*['"]?#{i}['"]?\]),
                  sprintf("[id='%<id>s_%<suffix>09d']", id: i, suffix: suffix))
        end

        s.children = c
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vectory-0.7.7 lib/vectory/svg_document.rb
vectory-0.7.5 lib/vectory/svg_document.rb
vectory-0.7.4 lib/vectory/svg_document.rb