Class: Vectory::SvgDocument

Inherits:
Object
  • Object
show all
Defined in:
lib/vectory/svg_document.rb

Constant Summary collapse

SVG_NS =
"http://www.w3.org/2000/svg".freeze

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ SvgDocument

Returns a new instance of SvgDocument.



7
8
9
# File 'lib/vectory/svg_document.rb', line 7

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

Instance Method Details

#contentObject



11
12
13
# File 'lib/vectory/svg_document.rb', line 11

def content
  @document.root.to_xml
end

#namespace(suffix, links, xpath_to_remove) ⇒ Object



15
16
17
18
19
# File 'lib/vectory/svg_document.rb', line 15

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


21
22
23
24
25
26
27
28
29
30
# File 'lib/vectory/svg_document.rb', line 21

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

#remove_xpath(xpath) ⇒ Object



42
43
44
45
46
# File 'lib/vectory/svg_document.rb', line 42

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

  self
end

#suffix_ids(suffix) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/vectory/svg_document.rb', line 32

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

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

  self
end