Class: SVGGVS::File

Inherits:
Object
  • Object
show all
Defined in:
lib/svggvs/file.rb

Instance Method Summary collapse

Constructor Details

#initialize(path_or_doc) ⇒ File

Returns a new instance of File.



5
6
7
8
9
10
11
12
13
14
# File 'lib/svggvs/file.rb', line 5

def initialize(path_or_doc)
  @instance = 0

  case path_or_doc
  when String
    @path = path_or_doc
  else
    @doc = path_or_doc
  end
end

Instance Method Details

#clear_targets!Object



41
42
43
# File 'lib/svggvs/file.rb', line 41

def clear_targets!
  target.children.each(&:remove)
end

#docObject



32
33
34
35
36
37
38
39
# File 'lib/svggvs/file.rb', line 32

def doc
  return @doc if @doc

  @doc = Nokogiri::XML(::File.read(@path))
  clear_targets!

  @doc
end

#dup_with_only_last_targetObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/svggvs/file.rb', line 62

def dup_with_only_last_target
  dupe = self.class.new(doc.dup)

  target = dupe.target.children.last.dup
  target[:style] = ''

  dupe.target.remove

  dupe.root.children.each do |child|
    child[:style] = 'display:none'
  end

  dupe.root << target

  dupe
end

#rootObject



24
25
26
# File 'lib/svggvs/file.rb', line 24

def root
  source.parent
end

#save(file) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/svggvs/file.rb', line 79

def save(file)
  for_save_doc = doc.dup
  for_save_doc.css('g[inkscape|label]').each do |group|
    if (group[:style] || '').include?('display:none')
      if !(group['inkscape:label'] || '').include?('(protect)')
        group.remove
      end
    end
  end

  ::File.open(file, 'w') { |fh| fh.print for_save_doc.to_xml }
end

#sourceObject



16
17
18
19
20
21
22
# File 'lib/svggvs/file.rb', line 16

def source
  return @source if @source

  @source = doc.at_css('g[inkscape|label="Source"]')
  @source['style'] = 'display:none'
  @source
end

#targetObject



28
29
30
# File 'lib/svggvs/file.rb', line 28

def target
  @target ||= doc.at_css('g[inkscape|label="Target"]')
end

#with_new_target {|target_obj| ... } ⇒ Object

Yields:

  • (target_obj)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/svggvs/file.rb', line 45

def with_new_target
  new_target = source.dup
  new_target[:id] = new_target[:id] + "_#{@instance}"
  new_target['inkscape:label'] = new_target['inkscape:label'] + "_#{@instance}"

  target_obj = Target.new(new_target)

  yield target_obj

  target_obj.replaced
  target_obj.unclone

  target << target_obj.target

  @instance += 1
end