1 # Nick's web site: scour filter. Run scour to reduce the size of SVG images.
3 # Copyright © 2021 Nick Bowler
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
18 class ScourFilter < Nanoc::Filter
21 def run(content, params = {})
22 defaults = { quiet: true, id_stripping: true, shorten_ids: true }
24 defaults.merge(params).each do |key, val|
27 # Normalize some argument names for consistency
29 when :comment_stripping, :viewboxing, :id_stripping
31 when :simplify_colors, :style_to_xml, :group_collapsing,
34 when :renderer_workaround, :line_breaks
35 prefix = :no if not val
38 # scour does not generally have inverse options, so drop
39 # some nonsense combinations.
40 next if val and prefix == :disable
41 next if val and key == :line_breaks
45 if prefix then arg << "#{prefix}-" end
46 arg << key.to_s.gsub("_", "-")
47 if val != true then arg << "=#{val}" end
52 content, status = Open3.capture2("scour", *args, stdin_data: content)
53 raise "scour failed" if status != 0