Sha256: e938acb33d4b4f10a6e846ba446cb59c40cdf172185acdb70deda2dc40d32855

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

require 'middleman-webp/pathname_matcher'

module Middleman
  module WebP
    class Options
      attr_reader :ignore

      def initialize(options = {})
        @ignore = options[:ignore] || []
        @ignore = [*@ignore].map do |pattern|
          Middleman::WebP::PathnameMatcher.new(pattern)
        end

        @conversion = options[:conversion_options] || {}
        @conversion = @conversion.reduce(Hash.new('')) do |h, (k, v)|
          h[Middleman::WebP::PathnameMatcher.new(k)] = to_args(v)
          h
        end
      end

      # Internal: Generate command line args for cwebp or gif2webp command
      #
      # Find options defined for given file. Selects all options whose
      # glob pattern matches file path and uses the one with longest
      # glob, because it's assumed to be the most precise one.
      def for(file)
        matching = @conversion.select { |m, o| m.matches? file }

        return '' if matching.empty?

        matching.sort { |(ga, oa), (gb, ob)| gb.size <=> ga.size }[0][1]
      end

      private

      def to_args(options)
        options.map do |k, v|
          if v == true
            "-#{k}"
          elsif v != false
            "-#{k} #{v}"
          end
        end.join(' ').gsub(/ +/, ' ')
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
middleman-webp-0.2.5 lib/middleman-webp/options.rb
middleman-webp-0.2.4 lib/middleman-webp/options.rb