svg-inline-file-extractor 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d00a790a3ec3da8c4479e4fe41d6dbc671dd8c4
|
4
|
+
data.tar.gz: 8c847625e736043a775a29d36eca99c0cfd598c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f7f7dd525f41ad3851b229a1fdb0c1ba98e84b4faa8a63214116daf24502ba26d2441e75a74974a298b1db0213e5bc4ee3c6ce5fbec81b79611c9481eaad7dc
|
7
|
+
data.tar.gz: ffd6c4b4ff0aa005f59224788266e2d6f6d50c75564d91a96735d7223a404f5655a45de055298a1362f65567e1d0192d281c0c1322972d5159a5aea312203974
|
@@ -3,6 +3,7 @@ require "svg_inline_file_extractor/svg_file"
|
|
3
3
|
require "svg_inline_file_extractor/inline_image"
|
4
4
|
|
5
5
|
module SvgInlineFileExtractor
|
6
|
+
class MiniMagickMissing < StandardError; end
|
6
7
|
|
7
8
|
# @param [ String ] xml_string should be a full raw SVG file
|
8
9
|
# @return [ Array<String> ] an array of binary strings containing the images in the SVG file
|
@@ -16,5 +17,34 @@ module SvgInlineFileExtractor
|
|
16
17
|
def self.inline_images(xml_string)
|
17
18
|
SvgFile.inline_images(xml_string)
|
18
19
|
end
|
19
|
-
|
20
|
+
|
21
|
+
# @param [ String ] uri a URI to open and identify.
|
22
|
+
# @return [ String | Nil ] image type determined by MiniMagick.
|
23
|
+
# @note will return nil if MiniMagick is not present
|
24
|
+
def self.identify_image(uri)
|
25
|
+
if use_mini_magick?
|
26
|
+
return with_temp_image(uri) do |temp_image|
|
27
|
+
MiniMagick::Image.open(temp_image).type.downcase
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# @return [ Boolean ] true if MiniMagick is found, false if not.
|
33
|
+
def self.use_mini_magick?
|
34
|
+
Object.const_defined?('MiniMagick::Image')
|
35
|
+
end
|
36
|
+
|
37
|
+
# do something with a temporary copy of uri in a block.
|
38
|
+
# @param [ String ] uri - the location of the file to open into a temp file.
|
39
|
+
def self.with_temp_image(uri)
|
40
|
+
temp = Tempfile.new
|
41
|
+
temp.binmode
|
42
|
+
puts "opening #{uri}"
|
43
|
+
temp.write open(uri).binmode.read
|
44
|
+
temp.close
|
45
|
+
results = yield temp.path
|
46
|
+
temp.delete
|
47
|
+
results
|
48
|
+
end
|
49
|
+
|
20
50
|
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'base64'
|
2
|
+
require 'open-uri'
|
2
3
|
|
3
4
|
module SvgInlineFileExtractor
|
4
5
|
class InlineImage
|
5
|
-
class UnableToDetermineImageTypeError < StandardError; end
|
6
6
|
|
7
7
|
# Regex pattern to match against the beginning of the href string. the type, if found, will be the raw string, eg: jpeg, jpg, png, etc.
|
8
8
|
DATA_IMAGE_HEADER_PATTERN = /data:image\/(?<type>.+);base64\,/.freeze
|
@@ -17,8 +17,7 @@ module SvgInlineFileExtractor
|
|
17
17
|
self.href_contents = nokogiri_element.value
|
18
18
|
end
|
19
19
|
|
20
|
-
# @return [ String ] The image type according to the DATA_IMAGE_HEADER_PATTERN
|
21
|
-
# @raise [ UnableToDetermineImageTypeError ] will raise if regex doesn't match.
|
20
|
+
# @return [ String|Nil ] The image type according to the DATA_IMAGE_HEADER_PATTERN, nil if pattern not matched.
|
22
21
|
def declared_image_type
|
23
22
|
@declared_image_type ||= begin
|
24
23
|
if (match = href_contents.match(DATA_IMAGE_HEADER_PATTERN))
|
@@ -27,6 +26,7 @@ module SvgInlineFileExtractor
|
|
27
26
|
end
|
28
27
|
end
|
29
28
|
|
29
|
+
# @return [ Boolean ] true if declared_image_type is not nil
|
30
30
|
def inline_image?
|
31
31
|
!!declared_image_type
|
32
32
|
end
|
@@ -48,9 +48,11 @@ module SvgInlineFileExtractor
|
|
48
48
|
attribute.value if attribute
|
49
49
|
end
|
50
50
|
|
51
|
-
# Updates the contents of the href that this inline image came from
|
51
|
+
# Updates the contents of the href that this inline image came from,
|
52
|
+
# and #href_contents
|
52
53
|
# @param [ String ] value the value to set the href contents to
|
53
|
-
def
|
54
|
+
def href_contents=(value)
|
55
|
+
@href_contents = value
|
54
56
|
nokogiri_element.value = value
|
55
57
|
end
|
56
58
|
|
@@ -60,8 +62,28 @@ module SvgInlineFileExtractor
|
|
60
62
|
@binary_image ||= Base64.decode64(without_header)
|
61
63
|
end
|
62
64
|
|
65
|
+
# If the image href is set to a URI, attempt to download/open and convert to an
|
66
|
+
# base64 encoded inline image
|
67
|
+
# @return [ Boolean ] true
|
68
|
+
# @raise [ SvgInlineFileExtractor::MiniMagickMissing ] if MiniMagick is not installed/required.
|
69
|
+
def set_binary_image_from_uri!
|
70
|
+
unless SvgInlineFileExtractor.use_mini_magick?
|
71
|
+
raise MiniMagickMissing, '#set_binary_image_from_uri! requires the MiniMagick gem to be installed.'
|
72
|
+
end
|
73
|
+
SvgInlineFileExtractor.with_temp_image(nokogiri_element.value) do |temp_image|
|
74
|
+
format = SvgInlineFileExtractor.identify_image(temp_image).to_s.downcase
|
75
|
+
nokogiri_element.value = "data:image/#{format};base64,#{encode(temp_image)}"
|
76
|
+
nokogiri_element.name = 'xlink:href'
|
77
|
+
end
|
78
|
+
true
|
79
|
+
end
|
80
|
+
|
63
81
|
private
|
64
82
|
|
83
|
+
def encode(image_path)
|
84
|
+
Base64.encode64(File.open(image_path.to_s).binmode.read)
|
85
|
+
end
|
86
|
+
|
65
87
|
def without_header
|
66
88
|
@without_header ||= href_contents.gsub(DATA_IMAGE_HEADER_PATTERN, '')
|
67
89
|
end
|
@@ -37,16 +37,25 @@ module SvgInlineFileExtractor
|
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
+
# @param [ Boolean ] false returns only inline base64 encoded images, true returns all image tags
|
40
41
|
# @return [ Array<InlineImage> ] an array of the inline images found in the document
|
41
42
|
# @see InlineImage
|
42
|
-
def inline_images
|
43
|
+
def inline_images(all = false)
|
43
44
|
image_elements.map do |element|
|
44
|
-
|
45
|
+
image = InlineImage.new(element)
|
46
|
+
if all || image.inline_image?
|
45
47
|
image
|
46
48
|
end
|
47
49
|
end.compact
|
48
50
|
end
|
49
51
|
|
52
|
+
# @return the rendered nokogiri_document.
|
53
|
+
# @note text " " will be replaced with newlines to allow newlines in Base64
|
54
|
+
# encoded strings.
|
55
|
+
def rendered_svg
|
56
|
+
nokogiri_document.to_s.gsub(' ', "\n")
|
57
|
+
end
|
58
|
+
|
50
59
|
private
|
51
60
|
|
52
61
|
def image_elements
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: svg-inline-file-extractor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nic Boie
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|