Awesome
SvgOptimizer
Some small optimizations for SVG files.
Installation
gem install svg_optimizer
Or add the following line to your project's Gemfile:
gem "svg_optimizer"
Usage
# Optimize an existing String.
xml = File.read("file.svg")
optimized = SvgOptimizer.optimize(xml)
# Optimize a file - it will override the original file.
SvgOptimizer.optimize_file("file.svg")
# Optimize a file - it saves a copy to "optimized/file.svg".
SvgOptimizer.optimize_file("file.svg", "optimized/file.svg")
You may have a need to optimize a trusted SVG document with entities that need to be
expanded. Only if you are sure the file is trusted, you may enable this additional entity
processing by passing the trusted:
keyword argument:
# Optimize an existing trusted String which requires entity expansion.
xml = File.read("file.svg")
optimized = SvgOptimizer.optimize(xml, trusted: true)
# Optimize a trusted file which requires entity expansion - it will override the original file.
SvgOptimizer.optimize_file("file.svg", trusted: true)
You can specify the plugins you want to enable. The method signature is:
SvgOptimizer.optimize(xml, plugins)
SvgOptimizer.optimize_file(input, output, plugins)
where plugins
is an array of classes that implement the following contract:
class MyPlugin < SvgOptimizer::Plugins::Base
def process
xml.xpath("//comment()").remove
end
end
The default list of plugins is stored at SvgOptimizer::DEFAULT_PLUGINS
. To use
your new plugin, just do something like this:
SvgOptimizer.optimize(xml, SvgOptimizer::DEFAULT_PLUGINS + [MyPlugin])
Maintainer
Contributors
Contributing
For more details about how to contribute, please read https://github.com/fnando/svg_optimizer/blob/main/CONTRIBUTING.md.
License
The gem is available as open source under the terms of the MIT License. A copy of the license can be found at https://github.com/fnando/svg_optimizer/blob/main/LICENSE.md.
Code of Conduct
Everyone interacting in the svg_optimizer project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.