metanorma-utils 1.6.5 → 1.7.1

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: c3f777cb3ba87e217a3b56ee8caf1a2cd8f6948a8664e18900c6eb5494d162f9
4
- data.tar.gz: 6a758b7d70a52f55c71d8db3562ea32a0f19de514948d17a48bee811850e0b16
3
+ metadata.gz: e00e0a899351896ba101b0522ec633f60959dd6a8ca8623c5f3b35445a9e8afb
4
+ data.tar.gz: 1b08d89297e4fc0dfff8dde93301389878cf2cb8a24789bab2876e77c059f62a
5
5
  SHA512:
6
- metadata.gz: 2bf9b223b840ea02f05c3fe37cc6d12ab73044da84f5108c5f94323a096a567934deec55ddce269f616382b09e98f2be1bc8d835120b6127549ee3febd00bcaf
7
- data.tar.gz: 5447a68582a1c245e1d8a1763573b956ae5999e7ed3a4b8f057e57b301cdc16d5cf2db99fa64bcdd6cb9f0047114fb75b812232fca7303c49ee9095a72387646
6
+ metadata.gz: a8fc3672c7ab49cc9e6f49405e47e8df92308b6685e1d0b87ad58afc0377f00ece6e5384552d42eafe866bd40ea87d8c7e2fa935c455006d5d9b49e618527860
7
+ data.tar.gz: 344fae6f2c4499b43dfc12e4da4928cea45a727cc879ed49f6aba4f9fd6edf568c2e4beef40c7779b00a18980aba45e796a60b9f1599658a0516cf7647137bda
data/Gemfile CHANGED
@@ -4,12 +4,6 @@ Encoding.default_internal = Encoding::UTF_8
4
4
  source "https://rubygems.org"
5
5
  git_source(:github) { |repo| "https://github.com/#{repo}" }
6
6
 
7
- group :development, :test do
8
- gem "rspec"
9
- end
10
-
11
- if File.exist? "Gemfile.devel"
12
- eval File.read("Gemfile.devel"), nil, "Gemfile.devel" # rubocop:disable Security/Eval
13
- end
14
-
15
7
  gemspec
8
+
9
+ eval_gemfile("Gemfile.devel") rescue nil
data/lib/utils/image.rb CHANGED
@@ -1,188 +1,7 @@
1
- require "tempfile"
2
- require "marcel"
3
- require "base64"
1
+ require_relative "namespace"
4
2
 
5
3
  module Metanorma
4
+ # Image methods were moved to the Vectory gem
6
5
  module Utils
7
- class << self
8
- class Namespace
9
- def initialize(xmldoc)
10
- @namespace = xmldoc.root.namespace
11
- end
12
-
13
- def ns(path)
14
- return path if @namespace.nil?
15
-
16
- path.gsub(%r{/([a-zA-z])}, "/xmlns:\\1")
17
- .gsub(%r{::([a-zA-z])}, "::xmlns:\\1")
18
- .gsub(%r{\[([a-zA-z][a-z0-9A-Z@/]* ?=)}, "[xmlns:\\1")
19
- .gsub(%r{\[([a-zA-z][a-z0-9A-Z@/]*\])}, "[xmlns:\\1")
20
- end
21
- end
22
-
23
- def save_dataimage(uri)
24
- %r{^data:(?:image|application)/(?<imgtype>[^;]+);(?:charset=[^;]+;)?base64,(?<imgdata>.+)$} =~ uri
25
- imgtype.sub!(/\+[a-z0-9]+$/, "") # svg+xml
26
- imgtype = "png" unless /^[a-z0-9]+$/.match? imgtype
27
- Tempfile.open(["image", ".#{imgtype}"]) do |f|
28
- f.binmode
29
- f.write(Base64.strict_decode64(imgdata))
30
- f.path
31
- end
32
- end
33
-
34
- SVG_NS = "http://www.w3.org/2000/svg".freeze
35
-
36
- def svgmap_rewrite(xmldoc, localdirectory = "")
37
- n = Namespace.new(xmldoc)
38
- xmldoc.xpath(n.ns("//svgmap")).each_with_index do |s, i|
39
- next unless svgmap_rewrite0(s, n, localdirectory, i)
40
- next if s.at(n.ns("./target/eref"))
41
-
42
- s.replace(s.at(n.ns("./figure")))
43
- end
44
- end
45
-
46
- def svgmap_rewrite0(svgmap, namespace, localdirectory, idx)
47
- if (i = svgmap.at(namespace.ns(".//image"))) &&
48
- (src = i["src"]) && !src.empty?
49
- path = svgmap_rewrite0_path(src, localdirectory)
50
- File.file?(path) or return false
51
- svg = Nokogiri::XML(File.read(path, encoding: "utf-8"))
52
- i.replace(svgmap_rewrite1(svgmap, svg.root, namespace, idx))
53
- /^data:/.match(src) and i["src"] = datauri(path)
54
- elsif i = svgmap.at(".//m:svg", "m" => SVG_NS)
55
- i.replace(svgmap_rewrite1(svgmap, i, namespace, idx))
56
- else return false
57
- end
58
- true
59
- end
60
-
61
- def svgmap_rewrite0_path(src, localdirectory)
62
- if /^data:/.match?(src)
63
- save_dataimage(src)
64
- else
65
- File.file?(src) ? src : localdirectory + src
66
- end
67
- end
68
-
69
- def svgmap_rewrite1(svgmap, svg, namespace, idx)
70
- svg_update_href(svgmap, svg, namespace)
71
- svg_update_ids(svg, idx)
72
- svg.xpath("processing-instruction()|.//processing-instruction()").remove
73
- svg.to_xml
74
- end
75
-
76
- def svg_update_href(svgmap, svg, namespace)
77
- targ = svgmap_rewrite1_targets(svgmap, namespace)
78
- svg.xpath(".//m:a", "m" => SVG_NS).each do |a|
79
- ["xlink:href", "href"].each do |p|
80
- a[p] and x = targ[File.expand_path(a[p])] and a[p] = x
81
- end
82
- end
83
- end
84
-
85
- def svgmap_rewrite1_targets(svgmap, namespace)
86
- svgmap.xpath(namespace.ns("./target"))
87
- .each_with_object({}) do |t, m|
88
- x = t.at(namespace.ns("./xref")) and
89
- m[File.expand_path(t["href"])] = "##{x['target']}"
90
- x = t.at(namespace.ns("./link")) and
91
- m[File.expand_path(t["href"])] = x["target"]
92
- t.remove if t.at(namespace.ns("./xref | ./link"))
93
- end
94
- end
95
-
96
- def svg_update_ids(svg, idx)
97
- ids = svg.xpath("./@id | .//@id")
98
- .each_with_object([]) { |i, m| m << i.value }
99
- return if ids.empty?
100
-
101
- svg_update_ids_attrs(svg, ids, idx)
102
- svg_update_ids_css(svg, ids, idx)
103
- end
104
-
105
- def svg_update_ids_attrs(svg, ids, idx)
106
- svg.xpath(". | .//*[@*]").each do |a|
107
- a.attribute_nodes.each do |x|
108
- ids.include?(x.value) and x.value += sprintf("_%09d", idx)
109
- end
110
- end
111
- end
112
-
113
- def svg_update_ids_css(svg, ids, idx)
114
- svg.xpath("//m:style", "m" => SVG_NS).each do |s|
115
- c = s.children.to_xml
116
- ids.each do |i|
117
- c = c.gsub(%r[##{i}\b],
118
- sprintf("#%<id>s_%<idx>09d", id: i, idx: idx))
119
- .gsub(%r(\[id\s*=\s*['"]?#{i}['"]?\]),
120
- sprintf("[id='%<id>s_%<idx>09d']", id: i, idx: idx))
121
- end
122
- s.children = c
123
- end
124
- end
125
-
126
- # sources/plantuml/plantuml20200524-90467-1iqek5i.png
127
- # already includes localdir
128
- # Check whether just the local path or the other specified relative path
129
- # works.
130
- def datauri(uri, local_dir = ".")
131
- (datauri?(uri) || url?(uri)) and return uri
132
- options = absolute_path?(uri) ? [uri] : [uri, File.join(local_dir, uri)]
133
- path = options.detect do |p|
134
- File.exist?(p) ? p : nil
135
- end
136
- path and return encode_datauri(path)
137
- warn "Image specified at `#{uri}` does not exist."
138
- uri # Return original provided location
139
- end
140
-
141
- def encode_datauri(path)
142
- return nil unless File.exist?(path)
143
-
144
- type = Marcel::MimeType.for(Pathname.new(path)) ||
145
- 'text/plain; charset="utf-8"'
146
-
147
- bin = File.binread(path)
148
- data = Base64.strict_encode64(bin)
149
- "data:#{type};base64,#{data}"
150
- rescue StandardError
151
- warn "Data-URI encoding of `#{path}` failed."
152
- nil
153
- end
154
-
155
- def datauri?(uri)
156
- /^data:/.match?(uri)
157
- end
158
-
159
- def url?(url)
160
- %r{^[A-Z]{2,}://}i.match?(url)
161
- end
162
-
163
- def absolute_path?(uri)
164
- %r{^/}.match?(uri) || %r{^[A-Z]:/}.match?(uri)
165
- end
166
-
167
- def decode_datauri(uri)
168
- %r{^data:(?<mimetype>[^;]+);base64,(?<mimedata>.+)$} =~ uri
169
- return nil unless mimetype && mimedata
170
-
171
- data = Base64.strict_decode64(mimedata)
172
- {
173
- type_declared: mimetype,
174
- type_detected: Marcel::MimeType.for(data, declared_type: mimetype),
175
- data: data,
176
- }
177
- end
178
-
179
- # FIXME: This method should ONLY return 1 type, remove Array wrapper
180
- def datauri2mime(uri)
181
- output = decode_datauri(uri)
182
- return nil unless output && output[:type_detected]
183
-
184
- [output[:type_detected]]
185
- end
186
- end
187
6
  end
188
7
  end
@@ -0,0 +1,26 @@
1
+ module Metanorma
2
+ module Utils
3
+ class << self
4
+ # NOTE: It was used in methods of an eigenclass of Metanorma::Utils.
5
+ # Not sure if it's still used somewhere but could be.
6
+ class Namespace
7
+ def initialize(xmldoc)
8
+ @namespace = xmldoc.root.namespace
9
+ end
10
+
11
+ def ns(path)
12
+ return path if @namespace.nil?
13
+
14
+ path.gsub(%r{/([a-zA-Z])}, "/xmlns:\\1")
15
+ .gsub(%r{::([a-zA-Z])}, "::xmlns:\\1")
16
+ .gsub(%r{\[([a-zA-Z][a-z0-9A-Z@/]* ?=)}, "[xmlns:\\1")
17
+ .gsub(%r{\[([a-zA-Z][a-z0-9A-Z@/]*\])}, "[xmlns:\\1")
18
+ end
19
+ end
20
+
21
+ def create_namespace(xmldoc)
22
+ Namespace.new(xmldoc)
23
+ end
24
+ end
25
+ end
26
+ end
data/lib/utils/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Metanorma
2
2
  module Utils
3
- VERSION = "1.6.5".freeze
3
+ VERSION = "1.7.1".freeze
4
4
  end
5
5
  end
@@ -30,8 +30,6 @@ Gem::Specification.new do |spec|
30
30
  spec.add_dependency "concurrent-ruby"
31
31
  spec.add_dependency "csv"
32
32
  spec.add_dependency "htmlentities", "~> 4.3.4"
33
- spec.add_dependency "marcel", "~> 1.0.0"
34
- spec.add_dependency "mime-types"
35
33
  spec.add_dependency "nokogiri", ">= 1.11"
36
34
  spec.add_dependency "sterile", "~> 1.0.14"
37
35
  spec.add_dependency "uuidtools"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metanorma-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.5
4
+ version: 1.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-07 00:00:00.000000000 Z
11
+ date: 2024-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciidoctor
@@ -66,34 +66,6 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 4.3.4
69
- - !ruby/object:Gem::Dependency
70
- name: marcel
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: 1.0.0
76
- type: :runtime
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: 1.0.0
83
- - !ruby/object:Gem::Dependency
84
- name: mime-types
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :runtime
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
69
  - !ruby/object:Gem::Dependency
98
70
  name: nokogiri
99
71
  requirement: !ruby/object:Gem::Requirement
@@ -324,6 +296,7 @@ files:
324
296
  - lib/utils/image.rb
325
297
  - lib/utils/log.rb
326
298
  - lib/utils/main.rb
299
+ - lib/utils/namespace.rb
327
300
  - lib/utils/version.rb
328
301
  - lib/utils/xml.rb
329
302
  - metanorma-utils.gemspec