svg_optimizer 0.2.5 → 0.3.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
- SHA1:
3
- metadata.gz: 252e3ad0e74992fe85def8ef8d749d2f55975b6a
4
- data.tar.gz: 0ab2451d8cf7ee5ec69a0cf6462adcba953575e2
2
+ SHA256:
3
+ metadata.gz: ec1327e57cc38ef5060b4e20afa3bce438abb6e967b8eb2b1691c571871fe293
4
+ data.tar.gz: 3945086145b7e192f93adb176b19d970c8707e6981ed8e7f14dcdc5ffbb0c723
5
5
  SHA512:
6
- metadata.gz: bfd401c30de6a280c6daddc788678f8635f0e179b7119ddc9ead993dbfbd0d08d70c30cf61153a6a6e7ff224dbdfd8b336c0027c65cbb916b8d115e262d966e6
7
- data.tar.gz: 0db71a133efeaa39992777d7a34fd4c1c309afafb4367fa1b9741fa73af26f4755cdb834ee3f2e8f80948794bf03f3190397e0a24e9e34e6e8395fffca9292a2
6
+ metadata.gz: '099effe7f458ba87feef6323f0df44caefbc9fec401cf626b6d4452fccbb60ec9f9c2eb8ea407eab470278ffb2f9084413dafde9dbea736acf6541bafddcd0d9'
7
+ data.tar.gz: 6e8831f708e90f74176342ba2a681894099e4dbaaa0122fb12959b395addf79638326dd629efe61d082a7b262a7671b1cb31013bfaff760529ed59a9f8d707a9
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # SvgOptimizer
2
2
 
3
- [![Travis-CI](https://travis-ci.org/fnando/svg_optimizer.svg)](https://travis-ci.org/fnando/svg_optimizer)
4
- [![Code Climate](https://codeclimate.com/github/fnando/svg_optimizer/badges/gpa.svg)](https://codeclimate.com/github/fnando/svg_optimizer)
5
- [![Test Coverage](https://codeclimate.com/github/fnando/svg_optimizer/badges/coverage.svg)](https://codeclimate.com/github/fnando/svg_optimizer/coverage)
3
+ [![Tests](https://github.com/fnando/svg_optimizer/workflows/ruby-tests/badge.svg)](https://github.com/fnando/svg_optimizer)
6
4
  [![Gem](https://img.shields.io/gem/v/svg_optimizer.svg)](https://rubygems.org/gems/svg_optimizer)
7
5
  [![Gem](https://img.shields.io/gem/dt/svg_optimizer.svg)](https://rubygems.org/gems/svg_optimizer)
8
6
 
@@ -10,17 +8,15 @@ Some small optimizations for SVG files.
10
8
 
11
9
  ## Installation
12
10
 
13
- Add this line to your application's Gemfile:
14
-
15
- gem 'svg_optimizer'
16
-
17
- And then execute:
18
-
19
- $ bundle
11
+ ```bash
12
+ gem install svg_optimizer
13
+ ```
20
14
 
21
- Or install it yourself as:
15
+ Or add the following line to your project's Gemfile:
22
16
 
23
- $ gem install svg_optimizer
17
+ ```ruby
18
+ gem "svg_optimizer"
19
+ ```
24
20
 
25
21
  ## Usage
26
22
 
@@ -36,6 +32,20 @@ SvgOptimizer.optimize_file("file.svg")
36
32
  SvgOptimizer.optimize_file("file.svg", "optimized/file.svg")
37
33
  ```
38
34
 
35
+ You may have a need to optimize a *trusted* SVG document with entities that need to be
36
+ expanded. **Only if you are sure the file is trusted**, you may enable this additional entity
37
+ processing by passing the `trusted:` keyword argument:
38
+
39
+ ``` ruby
40
+ # Optimize an existing trusted String which requires entity expansion.
41
+ xml = File.read("file.svg")
42
+ optimized = SvgOptimizer.optimize(xml, trusted: true)
43
+
44
+ # Optimize a trusted file which requires entity expansion - it will override the original file.
45
+ SvgOptimizer.optimize_file("file.svg", trusted: true)
46
+ ```
47
+
48
+
39
49
  You can specify the plugins you want to enable. The method signature is:
40
50
 
41
51
  ```ruby
@@ -53,16 +63,34 @@ class MyPlugin < SvgOptimizer::Plugins::Base
53
63
  end
54
64
  ```
55
65
 
56
- The default list of plugins is stored at `SvgOptimizer::DEFAULT_PLUGINS`. To use your new plugin, just do something like this:
66
+ The default list of plugins is stored at `SvgOptimizer::DEFAULT_PLUGINS`. To use
67
+ your new plugin, just do something like this:
57
68
 
58
69
  ```ruby
59
70
  SvgOptimizer.optimize(xml, SvgOptimizer::DEFAULT_PLUGINS + [MyPlugin])
60
71
  ```
61
72
 
73
+ ## Maintainer
74
+
75
+ - [Nando Vieira](https://github.com/fnando)
76
+
77
+ ## Contributors
78
+
79
+ - https://github.com/fnando/svg_optimizer/contributors
80
+
62
81
  ## Contributing
63
82
 
64
- 1. Fork it
65
- 2. Create your feature branch (`git checkout -b my-new-feature`)
66
- 3. Commit your changes (`git commit -am 'Add some feature'`)
67
- 4. Push to the branch (`git push origin my-new-feature`)
68
- 5. Create new Pull Request
83
+ For more details about how to contribute, please read
84
+ https://github.com/fnando/svg_optimizer/blob/main/CONTRIBUTING.md.
85
+
86
+ ## License
87
+
88
+ The gem is available as open source under the terms of the
89
+ [MIT License](https://opensource.org/licenses/MIT). A copy of the license can be
90
+ found at https://github.com/fnando/svg_optimizer/blob/main/LICENSE.md.
91
+
92
+ ## Code of Conduct
93
+
94
+ Everyone interacting in the svg_optimizer project's codebases, issue trackers,
95
+ chat rooms and mailing lists is expected to follow the
96
+ [code of conduct](https://github.com/fnando/svg_optimizer/blob/main/CODE_OF_CONDUCT.md).
@@ -9,7 +9,7 @@ module SvgOptimizer
9
9
  ].freeze
10
10
 
11
11
  IDS = LETTERS.dup.concat(LETTERS.combination(2).to_a).freeze
12
- XLINK_NAMESPACE = "http://www.w3.org/1999/xlink".freeze
12
+ XLINK_NAMESPACE = "http://www.w3.org/1999/xlink"
13
13
 
14
14
  def ids
15
15
  @ids ||= IDS.dup
@@ -50,6 +50,7 @@ module SvgOptimizer
50
50
  def remove_matching_attribute(node, name)
51
51
  node.attributes.each do |_, attr|
52
52
  next unless attr.namespace
53
+
53
54
  attr.remove if attr.namespace.prefix == name
54
55
  end
55
56
  end
@@ -11,9 +11,7 @@ module SvgOptimizer
11
11
  end
12
12
  end
13
13
 
14
- private
15
-
16
- def remove_if_empty(node, attr)
14
+ private def remove_if_empty(node, attr)
17
15
  node.remove_attribute(attr) if node[attr].empty?
18
16
  end
19
17
  end
@@ -3,7 +3,17 @@
3
3
  module SvgOptimizer
4
4
  module Plugins
5
5
  class RemoveEmptyContainer < Base
6
- ELEMENTS = %w[a defs g marker mask missing-glyph pattern switch symbol].freeze
6
+ ELEMENTS = %w[
7
+ a
8
+ defs
9
+ g
10
+ marker
11
+ mask
12
+ missing-glyph
13
+ pattern
14
+ switch
15
+ symbol
16
+ ].freeze
7
17
  SELECTOR = ELEMENTS.map {|element| %[#{element}:empty] }.join(", ")
8
18
 
9
19
  def process
@@ -12,9 +22,7 @@ module SvgOptimizer
12
22
  end
13
23
  end
14
24
 
15
- private
16
-
17
- def remove_node(node)
25
+ private def remove_node(node)
18
26
  node.children.empty? && node.remove
19
27
  remove_node(node.parent) if node.parent
20
28
  end
@@ -7,9 +7,7 @@ module SvgOptimizer
7
7
  xml.xpath("//text()").each(&method(:remove_if_empty))
8
8
  end
9
9
 
10
- private
11
-
12
- def remove_if_empty(node)
10
+ private def remove_if_empty(node)
13
11
  node.remove if node.text.gsub(/\s/, "").empty?
14
12
  end
15
13
  end
@@ -4,7 +4,7 @@ module SvgOptimizer
4
4
  module Plugins
5
5
  class RemoveTitle < Base
6
6
  def process
7
- xml.xpath("//title").remove
7
+ xml.css("title").remove
8
8
  end
9
9
  end
10
10
  end
@@ -4,7 +4,12 @@ module SvgOptimizer
4
4
  module Plugins
5
5
  class RemoveUselessStrokeAndFill < Base
6
6
  SELECTOR = %w[circle ellipse line path polygon polyline rect].join(",")
7
- STROKE_ATTRS = %w[stroke stroke-opacity stroke-width stroke-dashoffset].freeze
7
+ STROKE_ATTRS = %w[
8
+ stroke
9
+ stroke-opacity
10
+ stroke-width
11
+ stroke-dashoffset
12
+ ].freeze
8
13
  FILL_ATTRS = %w[fill-opacity fill-rule].freeze
9
14
 
10
15
  def process
@@ -15,40 +20,39 @@ module SvgOptimizer
15
20
  end
16
21
  end
17
22
 
18
- private
19
-
20
- def remove_useless_attributes(node)
23
+ private def remove_useless_attributes(node)
21
24
  return if inherited_attribute(node, "id")
25
+
22
26
  remove_stroke(node) if remove_stroke?(node)
23
27
  remove_fill(node) if remove_fill?(node)
24
28
  end
25
29
 
26
- def remove_stroke(node)
30
+ private def remove_stroke(node)
27
31
  STROKE_ATTRS.each {|attr| node.delete(attr) }
28
32
  node["stroke"] = "none" if decline_inherited_stroke?(node)
29
33
  end
30
34
 
31
- def remove_fill(node)
35
+ private def remove_fill(node)
32
36
  FILL_ATTRS.each {|attr| node.delete(attr) }
33
37
  node["fill"] = "none" if decline_inherited_fill?(node)
34
38
  end
35
39
 
36
- def decline_inherited_stroke?(node)
40
+ private def decline_inherited_stroke?(node)
37
41
  (inherited_attribute(node.parent, "stroke") || "none") != "none"
38
42
  end
39
43
 
40
- def decline_inherited_fill?(node)
44
+ private def decline_inherited_fill?(node)
41
45
  !inherited_attribute(node, "fill") || node.has_attribute?("fill")
42
46
  end
43
47
 
44
- def remove_stroke?(node)
48
+ private def remove_stroke?(node)
45
49
  return true if (inherited_attribute(node, "stroke") || "none") == "none"
46
50
  return true if inherited_attribute(node, "stroke-opacity") == "0"
47
51
 
48
52
  inherited_attribute(node, "stroke-width") == "0"
49
53
  end
50
54
 
51
- def remove_fill?(node)
55
+ private def remove_fill?(node)
52
56
  fill = inherited_attribute(node, "fill")
53
57
 
54
58
  return true if fill == "none"
@@ -57,7 +61,7 @@ module SvgOptimizer
57
61
  !fill
58
62
  end
59
63
 
60
- def inherited_attribute(node, attr)
64
+ private def inherited_attribute(node, attr)
61
65
  return if node.nil? || node.document?
62
66
  return node[attr] if node.has_attribute?(attr)
63
67
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SvgOptimizer
4
- VERSION = "0.2.5".freeze
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/svg_optimizer.rb CHANGED
@@ -37,14 +37,23 @@ module SvgOptimizer
37
37
  RemoveEmptyContainer
38
38
  ].map {|name| Plugins.const_get(name) }
39
39
 
40
- def self.optimize(contents, plugins = DEFAULT_PLUGINS)
41
- xml = Nokogiri::XML(contents)
40
+ def self.optimize(contents, plugins = DEFAULT_PLUGINS, trusted: false)
41
+ xml = Nokogiri::XML(contents) do |config|
42
+ config.recover.noent if trusted
43
+ end
44
+
42
45
  plugins.each {|plugin| plugin.new(xml).process }
46
+
43
47
  xml.root.to_xml
44
48
  end
45
49
 
46
- def self.optimize_file(path, target = path, plugins = DEFAULT_PLUGINS)
47
- contents = optimize(File.read(path), plugins)
50
+ def self.optimize_file(
51
+ path,
52
+ target = path,
53
+ plugins = DEFAULT_PLUGINS,
54
+ trusted: false
55
+ )
56
+ contents = optimize(File.read(path), plugins, trusted: trusted)
48
57
  File.open(target, "w") {|file| file << contents }
49
58
  true
50
59
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svg_optimizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-04 00:00:00.000000000 Z
11
+ date: 2023-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rake
42
+ name: minitest-utils
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -67,7 +67,35 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: minitest-utils
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-fnando
71
99
  requirement: !ruby/object:Gem::Requirement
72
100
  requirements:
73
101
  - - ">="
@@ -101,7 +129,7 @@ executables: []
101
129
  extensions: []
102
130
  extra_rdoc_files: []
103
131
  files:
104
- - LICENSE.txt
132
+ - LICENSE.md
105
133
  - README.md
106
134
  - lib/svg_optimizer.rb
107
135
  - lib/svg_optimizer/plugins/base.rb
@@ -123,8 +151,9 @@ files:
123
151
  homepage: https://github.com/fnando/svg_optimizer
124
152
  licenses:
125
153
  - MIT
126
- metadata: {}
127
- post_install_message:
154
+ metadata:
155
+ rubygems_mfa_required: 'true'
156
+ post_install_message:
128
157
  rdoc_options: []
129
158
  require_paths:
130
159
  - lib
@@ -132,16 +161,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
132
161
  requirements:
133
162
  - - ">="
134
163
  - !ruby/object:Gem::Version
135
- version: '0'
164
+ version: '2.7'
136
165
  required_rubygems_version: !ruby/object:Gem::Requirement
137
166
  requirements:
138
167
  - - ">="
139
168
  - !ruby/object:Gem::Version
140
169
  version: '0'
141
170
  requirements: []
142
- rubyforge_project:
143
- rubygems_version: 2.6.13
144
- signing_key:
171
+ rubygems_version: 3.4.6
172
+ signing_key:
145
173
  specification_version: 4
146
174
  summary: SVG optimization based on Node's SVGO
147
175
  test_files: []
File without changes