Class: SVGGVS::File
- Inherits:
-
Object
- Object
- SVGGVS::File
- Defined in:
- lib/svggvs/file.rb
Instance Method Summary collapse
- #clear_targets! ⇒ Object
- #defs ⇒ Object
- #doc ⇒ Object
- #dup_with_only_last_target ⇒ Object
-
#initialize(path_or_doc) ⇒ File
constructor
A new instance of File.
- #original_defs ⇒ Object
- #reset_defs! ⇒ Object
- #root ⇒ Object
- #save(file) ⇒ Object
- #source ⇒ Object
- #target ⇒ Object
- #with_new_target {|target_obj| ... } ⇒ Object
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
57 58 59 |
# File 'lib/svggvs/file.rb', line 57 def clear_targets! target.children.each(&:remove) end |
#defs ⇒ Object
32 33 34 |
# File 'lib/svggvs/file.rb', line 32 def defs @defs ||= doc.at_css('defs') end |
#doc ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/svggvs/file.rb', line 46 def doc return @doc if @doc @doc = Nokogiri::XML(::File.read(@path)) clear_targets! original_defs @doc end |
#dup_with_only_last_target ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/svggvs/file.rb', line 84 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 |
#original_defs ⇒ Object
36 37 38 |
# File 'lib/svggvs/file.rb', line 36 def original_defs @original_defs ||= defs.dup end |
#reset_defs! ⇒ Object
40 41 42 43 44 |
# File 'lib/svggvs/file.rb', line 40 def reset_defs! defs.children.each(&:remove) defs << original_defs.children end |
#root ⇒ Object
24 25 26 |
# File 'lib/svggvs/file.rb', line 24 def root source.parent end |
#save(file) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/svggvs/file.rb', line 101 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 |
#source ⇒ Object
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 |
#target ⇒ Object
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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/svggvs/file.rb', line 61 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) reset_defs! yield target_obj target_obj.replaced target_obj.unclone target << target_obj.target target_obj.injected_defs.values.each do |v| defs << v end @instance += 1 end |