]> git.draconx.ca Git - homepage.git/blobdiff - lib/scour.rb
Add filter to shrink SVG icons using scour.
[homepage.git] / lib / scour.rb
diff --git a/lib/scour.rb b/lib/scour.rb
new file mode 100644 (file)
index 0000000..c84104e
--- /dev/null
@@ -0,0 +1,57 @@
+# Nick's web site: scour filter.  Run scour to reduce the size of SVG images.
+#
+# Copyright © 2021 Nick Bowler
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+class ScourFilter < Nanoc::Filter
+    identifier :scour
+
+    def run(content, params = {})
+        defaults = { quiet: true, id_stripping: true, shorten_ids: true }
+        args = []
+        defaults.merge(params).each do |key, val|
+            prefix = nil
+
+            # Normalize some argument names for consistency
+            case key
+            when :comment_stripping, :viewboxing, :id_stripping
+                prefix = :enable
+            when :simplify_colors, :style_to_xml, :group_collapsing,
+                 :embed_rasters
+                prefix = :disable
+            when :renderer_workaround, :line_breaks
+                prefix = :no if not val
+            end
+
+            # scour does not generally have inverse options, so drop
+            # some nonsense combinations.
+            next if val and prefix == :disable
+            next if val and key == :line_breaks
+            next if !val
+
+            arg = "--"
+            if prefix then arg << "#{prefix}-" end
+            arg << key.to_s.gsub("_", "-")
+            if val != true then arg << "=#{val}" end
+
+            args << arg
+        end
+
+        content, status = Open3.capture2("scour", *args, stdin_data: content)
+        raise "scour failed" if status != 0
+
+        return content
+    end
+end