vectory 0.7.3 → 0.7.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d7e5cf52ef253c6c347b08de4933cec1766c28c018bfc7db7cec90389d136f84
4
- data.tar.gz: 88874bb762defd6629019da70230e1bcad31a8421bd5687d90b6bae5f456ffc6
3
+ metadata.gz: aef4319c091897a2bc5971c65ef3d87332655aa1d76516ffbcd66fc9541465da
4
+ data.tar.gz: ca405f2071d56b523ec6203b6dcf98c390fa420c2c9a58102fdb766a179d9577
5
5
  SHA512:
6
- metadata.gz: e25ab6e9f1e7ff9f61f4f525e4116b31d1f5a7649579abcd304efff9dd1e6b31afcd57d1b317ef8bec884359d201ea3f99b8aa6157707c6586e46729a80dc94f
7
- data.tar.gz: ed47e2375f1f01140b88a014b1ec03f73571cf461f957793b35d6d34b52d4dd4cc82701186e016482e27fd5d2807e36893354d8a7c81e97d1763fab8a6b87fed
6
+ metadata.gz: 101b0bbf1881240b6a84516450e8e73670c726339d16d1bb7b15e3efd806422f237a95dcc5b1e7145c951df4326cbda679d7f89c0523909822e0d3a28ecf2c73
7
+ data.tar.gz: 4a5b4495f7a8335e8514c2dd76fc332ce6f46afe070132f9aee427317aaeedcbfd4922c23fb7efff8c429f89a2cff74077cfdb5d3e4134279867556b9e38a614
data/lib/vectory/emf.rb CHANGED
@@ -29,7 +29,7 @@ module Vectory
29
29
 
30
30
  def to_svg
31
31
  with_file("emf") do |input_path|
32
- content = Emf2svg.from_file(input_path).sub(/<\?[^>]+>/, "")
32
+ content = Emf2svg.from_file(input_path)
33
33
 
34
34
  Svg.from_content(content)
35
35
  end
data/lib/vectory/svg.rb CHANGED
@@ -25,8 +25,10 @@ module Vectory
25
25
  from_path(uri)
26
26
  end
27
27
 
28
- def content
29
- @document&.to_xml || @content
28
+ def initialize(content = nil, initial_path = nil)
29
+ super
30
+
31
+ self.content = content
30
32
  end
31
33
 
32
34
  def to_emf
@@ -41,81 +43,17 @@ module Vectory
41
43
  convert_with_inkscape("--export-type=ps", Ps)
42
44
  end
43
45
 
44
- def namespace(suffix, links, xpath_to_remove)
45
- remap_links(links)
46
- suffix_ids(suffix)
47
- remove_xpath(xpath_to_remove)
48
- end
49
-
50
- def remap_links(map)
51
- document.xpath(".//m:a", "m" => SVG_NS).each do |a|
52
- href_attrs = ["xlink:href", "href"]
53
- href_attrs.each do |p|
54
- a[p] and x = map[File.expand_path(a[p])] and a[p] = x
55
- end
56
- end
57
-
58
- self
59
- end
60
-
61
- def suffix_ids(suffix)
62
- ids = collect_ids
63
- return if ids.empty?
64
-
65
- update_ids_attrs(ids, suffix)
66
- update_ids_css(ids, suffix)
67
-
68
- self
69
- end
70
-
71
- def remove_xpath(xpath)
72
- document.xpath(xpath).remove
73
-
74
- self
75
- end
76
-
77
46
  private
78
47
 
79
48
  def content=(content)
80
- if @document
81
- @document = Nokogiri::XML(content)
82
- else
83
- @content = content
84
- end
85
- end
86
-
87
- def document
88
- @document ||= begin
89
- doc = Nokogiri::XML(@content)
90
- @content = nil
91
- doc
92
- end
93
- end
94
-
95
- def collect_ids
96
- document.xpath("./@id | .//@id").map(&:value)
97
- end
98
-
99
- def update_ids_attrs(ids, suffix)
100
- document.xpath(". | .//*[@*]").each do |a|
101
- a.attribute_nodes.each do |x|
102
- ids.include?(x.value) and x.value += sprintf("_%09d", suffix)
103
- end
49
+ # non-root node inserts the xml tag which breaks markup when placed in
50
+ # another xml document
51
+ document = Nokogiri::XML(content).root
52
+ unless document
53
+ raise ParsingError, "Could not parse '#{content&.slice(0, 30)}'"
104
54
  end
105
- end
106
-
107
- def update_ids_css(ids, suffix)
108
- document.xpath("//m:style", "m" => SVG_NS).each do |s|
109
- c = s.children.to_xml
110
- ids.each do |i|
111
- c = c.gsub(%r[##{i}\b],
112
- sprintf("#%<id>s_%<suffix>09d", id: i, suffix: suffix))
113
- .gsub(%r(\[id\s*=\s*['"]?#{i}['"]?\]),
114
- sprintf("[id='%<id>s_%<suffix>09d']", id: i, suffix: suffix))
115
- end
116
55
 
117
- s.children = c
118
- end
56
+ @content = document.to_xml
119
57
  end
120
58
  end
121
59
  end
@@ -0,0 +1,76 @@
1
+ require "nokogiri"
2
+
3
+ module Vectory
4
+ class SvgDocument
5
+ SVG_NS = "http://www.w3.org/2000/svg".freeze
6
+
7
+ def initialize(content)
8
+ @document = Nokogiri::XML(content)
9
+ end
10
+
11
+ def content
12
+ @document.root.to_xml
13
+ end
14
+
15
+ def namespace(suffix, links, xpath_to_remove)
16
+ remap_links(links)
17
+ suffix_ids(suffix)
18
+ remove_xpath(xpath_to_remove)
19
+ end
20
+
21
+ def remap_links(map)
22
+ @document.xpath(".//m:a", "m" => SVG_NS).each do |a|
23
+ href_attrs = ["xlink:href", "href"]
24
+ href_attrs.each do |p|
25
+ a[p] and x = map[File.expand_path(a[p])] and a[p] = x
26
+ end
27
+ end
28
+
29
+ self
30
+ end
31
+
32
+ def suffix_ids(suffix)
33
+ ids = collect_ids
34
+ return if ids.empty?
35
+
36
+ update_ids_attrs(ids, suffix)
37
+ update_ids_css(ids, suffix)
38
+
39
+ self
40
+ end
41
+
42
+ def remove_xpath(xpath)
43
+ @document.xpath(xpath).remove
44
+
45
+ self
46
+ end
47
+
48
+ private
49
+
50
+ def collect_ids
51
+ @document.xpath("./@id | .//@id").map(&:value)
52
+ end
53
+
54
+ def update_ids_attrs(ids, suffix)
55
+ @document.xpath(". | .//*[@*]").each do |a|
56
+ a.attribute_nodes.each do |x|
57
+ ids.include?(x.value) and x.value += sprintf("_%09d", suffix)
58
+ end
59
+ end
60
+ end
61
+
62
+ def update_ids_css(ids, suffix)
63
+ @document.xpath("//m:style", "m" => SVG_NS).each do |s|
64
+ c = s.children.to_xml
65
+ ids.each do |i|
66
+ c = c.gsub(%r[##{i}\b],
67
+ sprintf("#%<id>s_%<suffix>09d", id: i, suffix: suffix))
68
+ .gsub(%r(\[id\s*=\s*['"]?#{i}['"]?\]),
69
+ sprintf("[id='%<id>s_%<suffix>09d']", id: i, suffix: suffix))
70
+ end
71
+
72
+ s.children = c
73
+ end
74
+ end
75
+ end
76
+ end
@@ -1,4 +1,5 @@
1
1
  require_relative "svg"
2
+ require_relative "svg_document"
2
3
 
3
4
  module Vectory
4
5
  class SvgMapping
@@ -70,13 +71,19 @@ module Vectory
70
71
  end
71
72
 
72
73
  def generate_content(image, svgmap, suffix)
73
- vector = build_vector(image)
74
- return unless vector
74
+ document = build_svg_document(image)
75
+ return unless document
75
76
 
76
77
  links_map = from_targets_to_links_map(svgmap)
77
- vector.namespace(suffix, links_map, PROCESSING_XPATH)
78
+ document.namespace(suffix, links_map, PROCESSING_XPATH)
79
+
80
+ document.content
81
+ end
82
+
83
+ def build_svg_document(image)
84
+ vector = build_vector(image)
78
85
 
79
- vector.content
86
+ SvgDocument.new(vector.content)
80
87
  end
81
88
 
82
89
  def build_vector(image)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Vectory
4
- VERSION = "0.7.3"
4
+ VERSION = "0.7.4"
5
5
  end
data/lib/vectory.rb CHANGED
@@ -28,6 +28,8 @@ module Vectory
28
28
 
29
29
  class NotWrittenToDiskError < Error; end
30
30
 
31
+ class ParsingError < Error; end
32
+
31
33
  def self.ui
32
34
  @ui ||= Logger.new(STDOUT).tap do |logger|
33
35
  logger.level = ENV['VECTORY_LOG'] || Logger::WARN
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vectory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.3
4
+ version: 0.7.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-03-27 00:00:00.000000000 Z
11
+ date: 2024-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: emf2svg
@@ -112,6 +112,7 @@ files:
112
112
  - lib/vectory/inkscape_converter.rb
113
113
  - lib/vectory/ps.rb
114
114
  - lib/vectory/svg.rb
115
+ - lib/vectory/svg_document.rb
115
116
  - lib/vectory/svg_mapping.rb
116
117
  - lib/vectory/system_call.rb
117
118
  - lib/vectory/utils.rb