svg-inline-file-extractor 0.1.1 → 0.2.0

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: 86bd56dfb132d0e44d1f43e90b90d3820d80bd94
4
- data.tar.gz: 4b3ccf10067ce9769e6b9e77ad84865a2e284bdf
3
+ metadata.gz: e57054a34848ede24adfa1a96da697af6c73772e
4
+ data.tar.gz: 1be1f8c90e3ca2ea72ff156c9f672fb5f626a7ff
5
5
  SHA512:
6
- metadata.gz: 7a393389a63bd08ce0909649d68b8ae3ef59e952df8ae86da881f09a2f005a2bb325869d77e2543490e429b9081c82747f19757d2891f03692548cd4eda02964
7
- data.tar.gz: a026ad5434c85b87ea2f9a7be413acd49686bd597bf3446660e8d658509534101220fe450fb39879f9d27ed42314d3c6421661ddda19bb0f046bb6af1d991ff4
6
+ metadata.gz: 00fbda34a7c2e73661a6b98127e0912f20ceaaa5158d9e88ddda20fd7d52ab4dc2498f02160e9ea457ab86d7f80c80c405e316a8f8b627e24b4bc70383f00d16
7
+ data.tar.gz: 853e0003ecc94aecf6faa5da82c16020823f3f78bd4ac4c22da007e366f3adb44e185bb6eb2acca1b80604ac471102b32373a4942ab32a1418c4fa47b0663976
data/README.md CHANGED
@@ -6,17 +6,20 @@
6
6
 
7
7
  The purpose of this gem is to extract inline SVG base64 encoded PNG images. This is based in part from ideas in https://gist.github.com/r10r/2822884
8
8
 
9
- This is a work in progress.
9
+ This is a work in progress. You should specify a specific version of this gem when
10
+ using, as the interface is subject to wild change in this early phase of development.
10
11
 
11
12
  ## Roadmap
12
13
 
13
- * Add ability to replace an inline image with a URI after extraction
14
- * Extract svg_file image handling to a separate class
15
- * Work with more than just PNG files
14
+ * ~~Add ability to replace an inline image with a URI after extraction~~ Done
15
+ * ~~Extract svg_file image handling to a separate class~~ Done
16
+ * ~~Work with more than just PNG files~~ Done
16
17
  * Query images for their type, possibly other meta
17
18
 
18
19
  ## Installation
19
20
 
21
+ This gem is tested against Ruby 2.2.0 and 2.3.0
22
+
20
23
  Add this line to your application's Gemfile:
21
24
 
22
25
  ```ruby
@@ -33,21 +36,27 @@ Or install it yourself as:
33
36
 
34
37
  ## Usage
35
38
 
39
+ ### Example
40
+
41
+ from `bin/console`
42
+
36
43
  ```ruby
37
44
  require 'svg_inline_file_extractor'
38
45
 
39
46
  # open a file
40
47
  # set path to an SVG file (example file in the gem under spec/fixtures)
41
- # path = <PATH TO SOME SVG FILE WITH INLINE IMAGES>
42
- files = SvgInlineFileExtractor.binary_images(path: path)
43
-
44
- # Write FIRST image to a tempfile
45
- tf = Tempfile.new(['file','.png'])
46
- tf.write(files.first)
47
- tf.close
48
-
49
- #open the file
50
- `open #{tf.path}`
48
+ # file = File.read(<PATH TO SOME SVG FILE WITH INLINE IMAGES>)
49
+ files = SvgInlineFileExtractor.binary_images(file)
50
+
51
+ # Open all the extracted files:
52
+ files.each do |file|
53
+ tf = Tempfile.new(['file','.png'])
54
+ tf.write(file)
55
+ tf.close
56
+
57
+ #open the file with system open
58
+ `open #{tf.path}`
59
+ end
51
60
 
52
61
  ```
53
62
 
@@ -1,8 +1,20 @@
1
1
  require "svg_inline_file_extractor/version"
2
2
  require "svg_inline_file_extractor/svg_file"
3
+ require "svg_inline_file_extractor/inline_image"
3
4
 
4
5
  module SvgInlineFileExtractor
5
- def self.binary_images(string)
6
- SvgFile.binary_images(string)
6
+
7
+ # @param [ String ] xml_string should be a full raw SVG file
8
+ # @return [ Array<String> ] an array of binary strings containing the images in the SVG file
9
+ def self.binary_images(xml_string)
10
+ SvgFile.binary_images(xml_string)
11
+ end
12
+
13
+ # @param [ String ] xml_string should be a full raw SVG file
14
+ # @return [ Array<InlineImage> ] an array of the inline images found in the document
15
+ # @see InlineImage
16
+ def self.inline_images(xml_string)
17
+ SvgFile.inline_images(xml_string)
7
18
  end
19
+
8
20
  end
@@ -0,0 +1,68 @@
1
+ require 'base64'
2
+
3
+ module SvgInlineFileExtractor
4
+ class InlineImage
5
+ class UnableToDetermineImageTypeError < StandardError; end
6
+
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
+ DATA_IMAGE_HEADER_PATTERN = /data:image\/(?<type>.+);base64\,/.freeze
9
+
10
+ # @attr [ String ] href_contents The value extracted from the nokogiri_element (href). May be binary.
11
+ # @attr [ Nokogiri::XML::Attr ] nokogiri_element the href node from the SVG file
12
+ attr_accessor :href_contents, :nokogiri_element
13
+
14
+ # @param [ Nokogiri::XML::Attr ] nokogiri_element the href node from the SVG file
15
+ def initialize(nokogiri_element)
16
+ self.nokogiri_element = nokogiri_element
17
+ self.href_contents = nokogiri_element.value
18
+ end
19
+
20
+ # @return [ String ] The image type according to the DATA_IMAGE_HEADER_PATTERN
21
+ # @raise [ UnableToDetermineImageTypeError ] will raise if regex doesn't match.
22
+ def declared_image_type
23
+ @declared_image_type ||= begin
24
+ if (match = href_contents.match(DATA_IMAGE_HEADER_PATTERN))
25
+ match[:type]
26
+ else
27
+ raise UnableToDetermineImageTypeError, "Unable to match header to #{DATA_IMAGE_HEADER_PATTERN}"
28
+ end
29
+ end
30
+ end
31
+
32
+ # @return [ String | nil ] the value of the id attribute in the parent element
33
+ def element_id
34
+ @element_id ||= get_attribute('id')
35
+ end
36
+
37
+ # @return [ String | nil ] the value of the class attribute in the parent element
38
+ def element_class
39
+ @element_class ||= get_attribute('class')
40
+ end
41
+
42
+ # @param [ String ] attribute an attribute to retreive from the parent element (the entity that this href came from)
43
+ # @return [ String | Nil ] the value in the specified attribute, or nil if not set
44
+ def get_attribute(attribute)
45
+ attribute = nokogiri_element.parent.attribute(attribute)
46
+ attribute.value if attribute
47
+ end
48
+
49
+ # Updates the contents of the href that this inline image came from
50
+ # @param [ String ] value the value to set the href contents to
51
+ def svg_href_contents=(value)
52
+ nokogiri_element.value = value
53
+ end
54
+
55
+ # @return [ String ] Base64 decoded binary image from href
56
+ # @note I'm not sure if memoization is the best here, let's see what happens in the wild.
57
+ def binary_image
58
+ @binary_image ||= Base64.decode64(without_header)
59
+ end
60
+
61
+ private
62
+
63
+ def without_header
64
+ @without_header ||= href_contents.gsub(DATA_IMAGE_HEADER_PATTERN, '')
65
+ end
66
+
67
+ end
68
+ end
@@ -5,21 +5,30 @@ module SvgInlineFileExtractor
5
5
  class SvgFile
6
6
 
7
7
  class << self
8
- def binary_images(string)
9
- new(string).binary_images
8
+ # @param [ String ] xml_string should be a full raw SVG file
9
+ def binary_images(xml_string)
10
+ new(xml_string).binary_images
10
11
  end
11
12
 
12
- def strip_header_from(string)
13
- string.gsub(/data:image\/...;base64\,/, '')
13
+ # @param [ String ] xml_string should be a full raw SVG file
14
+ def inline_images(xml_string)
15
+ new(xml_string).inline_images
14
16
  end
15
17
  end
16
18
 
17
19
  attr_accessor :xml_string
18
20
 
21
+ # @param [ String ] xml_string should be a full raw SVG file
19
22
  def initialize(xml_string)
20
23
  self.xml_string = xml_string
21
24
  end
22
25
 
26
+ # @return [ Array<String> ] an array of binary strings containing the images in the SVG file
27
+ def binary_images
28
+ inline_images.map(&:binary_image)
29
+ end
30
+
31
+ # @return [ Nokogiri::XML ] a nokogiri XML document wrapper
23
32
  def nokogiri_document
24
33
  @nokogiri_document ||= begin
25
34
  doc = Nokogiri::XML(xml_string)
@@ -28,11 +37,11 @@ module SvgInlineFileExtractor
28
37
  end
29
38
  end
30
39
 
31
- def binary_images
40
+ # @return [ Array<InlineImage> ] an array of the inline images found in the document
41
+ # @see InlineImage
42
+ def inline_images
32
43
  image_elements.map do |element|
33
- image_string = element.value
34
- image_string = self.class.strip_header_from(image_string)
35
- Base64.decode64(image_string)
44
+ InlineImage.new(element)
36
45
  end
37
46
  end
38
47
 
@@ -1,3 +1,3 @@
1
1
  module SvgInlineFileExtractor
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -17,10 +17,13 @@ Gem::Specification.new do |spec|
17
17
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
18
  spec.require_paths = ["lib"]
19
19
 
20
+ spec.required_ruby_version = '>= 2.2.0'
21
+
20
22
  spec.add_dependency "nokogiri", "~> 1.6"
21
23
 
22
24
  spec.add_development_dependency "bundler", "~> 1.11"
23
25
  spec.add_development_dependency "rake", "~> 10.0"
24
26
  spec.add_development_dependency "rspec", "~> 3.0"
25
27
  spec.add_development_dependency "pry", "~> 0"
28
+ spec.add_development_dependency "simplecov", "~> 0.12"
26
29
  end
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.1.1
4
+ version: 0.2.0
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-09-21 00:00:00.000000000 Z
11
+ date: 2016-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.12'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.12'
83
97
  description:
84
98
  email:
85
99
  - boie0025@gmail.com
@@ -98,6 +112,7 @@ files:
98
112
  - bin/console
99
113
  - bin/setup
100
114
  - lib/svg_inline_file_extractor.rb
115
+ - lib/svg_inline_file_extractor/inline_image.rb
101
116
  - lib/svg_inline_file_extractor/svg_file.rb
102
117
  - lib/svg_inline_file_extractor/version.rb
103
118
  - svg_inline_file_extractor.gemspec
@@ -112,7 +127,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
112
127
  requirements:
113
128
  - - ">="
114
129
  - !ruby/object:Gem::Version
115
- version: '0'
130
+ version: 2.2.0
116
131
  required_rubygems_version: !ruby/object:Gem::Requirement
117
132
  requirements:
118
133
  - - ">="