Class: SVGGVS::Target

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Target

Returns a new instance of Target.



7
8
9
# File 'lib/svggvs/target.rb', line 7

def initialize(target)
  @target = target
end

Instance Attribute Details

#targetObject (readonly)

Returns the value of attribute target.



5
6
7
# File 'lib/svggvs/target.rb', line 5

def target
  @target
end

Instance Method Details

#__getobj__Object



11
12
13
# File 'lib/svggvs/target.rb', line 11

def __getobj__
  @target
end

#active_layers=(layers) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/svggvs/target.rb', line 15

def active_layers=(layers)
  css("g[inkscape|groupmode='layer']").each do |layer|
    if layers.include?(layer['inkscape:label'])
      layer['style'] = ''

      current_parent = layer.parent

      while current_parent && current_parent.name == "g"
        current_parent['style'] = ''

        current_parent = current_parent.parent
      end
    else
      layer['style'] = if layer['inkscape:label'].include?('(visible)')
                         ''
                       else
                         'display:none'
                       end
    end
  end
end

#replaced(node = @target) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/svggvs/target.rb', line 41

def replaced(node = @target)
  if !!@replacements
    node.children.each do |child|
      if child.text?
        if match = child.content[%r{\{% ([^ ]+) %\}}, 1]
          child.content = @replacements[match] || ''
        end
      else
        if label = child['inkscape:label']
          if !!@replacements[label]
            if flow_para = child.css('svg|flowPara').first
              flow_para.content = @replacements[label] || ''
            end

            if span = child.css('svg|tspan').first
              span.content = @replacements[label] || ''
            end

            if child.name == "image" && !!@replacements[label]
              child['xlink:href'] = ::File.expand_path(@replacements[label])
            end
          end
        end

        replaced(child)
      end
    end
  end
end

#replacements=(replacements) ⇒ Object



37
38
39
# File 'lib/svggvs/target.rb', line 37

def replacements=(replacements)
  @replacements = replacements
end

#uncloneObject

only uncloning text



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/svggvs/target.rb', line 72

def unclone
  css('svg|use').each do |clone|
    if source = css(clone['xlink:href']).first
      if source.name == 'flowRoot' || source.name == 'text'
        new_group = clone.add_next_sibling("<g />").first

        clone.attributes.each do |key, attribute|
          new_group[attribute.name] = attribute.value
        end

        new_group << source.dup

        clone.remove
      end
    end
  end
end