Class: JekyllImgFlow::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-imgflow/config.rb

Constant Summary collapse

DEFAULT_ORIGINALS =

Sensible defaults — users only need to configure originals and output (or even nothing at all). All other values fall back to these.

"assets/images/originals"
DEFAULT_OUTPUT =
"assets/images/optimized"
DEFAULT_INPUT_FORMATS =
%w[jpg jpeg png webp avif gif tiff tif svg].freeze
DEFAULT_FORMATS =

Output format priority: avif > webp > png > jpg (fallback). The browser picks the first format it supports, so avif is served to modern browsers, webp as a fallback for avif, png for transparency, and jpg as the universal fallback.

%w[avif webp png jpg].freeze
DEFAULT_SIZES =
{ "sm" => 400, "md" => 800, "lg" => 1200, "xl" => 2000 }.freeze
DEFAULT_QUALITY =
85
DEFAULT_BACKEND_PRIORITY =

Ordered by speed (see docs/providers.md benchmark): sharp (14s) → libvips (22s) → imagemagick (31s) → imgproxy (31s) → weserv (30s) → flyimg

%w[sharp libvips imagemagick imgproxy weserv flyimg].freeze
DEFAULT_FALLBACK_FORMAT =

Format used for the fallback in elements. All other formats in formats get tags for browsers that support them.

"jpg"
DEFAULT_IMAGE_MODAL =
true
CORE_CONFIG_DEFAULTS =

Core config keys mapped to their defaults (assigned as @ivar per key).

{
  "originals" => DEFAULT_ORIGINALS,
  "output" => DEFAULT_OUTPUT,
  "input_formats" => DEFAULT_INPUT_FORMATS,
  "sizes" => DEFAULT_SIZES,
  "formats" => DEFAULT_FORMATS,
  "quality" => DEFAULT_QUALITY,
  "backend_priority" => DEFAULT_BACKEND_PRIORITY,
  "fallback_format" => DEFAULT_FALLBACK_FORMAT
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Config

Returns a new instance of Config.



42
43
44
45
46
47
48
49
# File 'lib/jekyll-imgflow/config.rb', line 42

def initialize(site)
  @site = site
  cfg = merged_config
  assign_core_config(cfg)
  assign_provider_config(cfg)
  @optimize_qualities = cfg["optimize_qualities"] || default_optimize_qualities
  validate_backend_priority!
end

Instance Attribute Details

#backend_priority ⇒ Object (readonly)

Returns the value of attribute backend_priority.



37
38
39
# File 'lib/jekyll-imgflow/config.rb', line 37

def backend_priority
  @backend_priority
end

#fallback_format ⇒ Object (readonly)

Returns the value of attribute fallback_format.



37
38
39
# File 'lib/jekyll-imgflow/config.rb', line 37

def fallback_format
  @fallback_format
end

#flyimg_url ⇒ Object (readonly)

Returns the value of attribute flyimg_url.



37
38
39
# File 'lib/jekyll-imgflow/config.rb', line 37

def flyimg_url
  @flyimg_url
end

#formats ⇒ Object (readonly)

Returns the value of attribute formats.



37
38
39
# File 'lib/jekyll-imgflow/config.rb', line 37

def formats
  @formats
end

#image_modal ⇒ Object (readonly)

Returns the value of attribute image_modal.



37
38
39
# File 'lib/jekyll-imgflow/config.rb', line 37

def image_modal
  @image_modal
end

#imgproxy_url ⇒ Object (readonly)

Returns the value of attribute imgproxy_url.



37
38
39
# File 'lib/jekyll-imgflow/config.rb', line 37

def imgproxy_url
  @imgproxy_url
end

#input_formats ⇒ Object (readonly)

Returns the value of attribute input_formats.



37
38
39
# File 'lib/jekyll-imgflow/config.rb', line 37

def input_formats
  @input_formats
end

#optimize_qualities ⇒ Object (readonly)

Returns the value of attribute optimize_qualities.



37
38
39
# File 'lib/jekyll-imgflow/config.rb', line 37

def optimize_qualities
  @optimize_qualities
end

#originals ⇒ Object (readonly)

Returns the value of attribute originals.



37
38
39
# File 'lib/jekyll-imgflow/config.rb', line 37

def originals
  @originals
end

#output ⇒ Object (readonly)

Returns the value of attribute output.



37
38
39
# File 'lib/jekyll-imgflow/config.rb', line 37

def output
  @output
end

#quality ⇒ Object (readonly)

Returns the value of attribute quality.



37
38
39
# File 'lib/jekyll-imgflow/config.rb', line 37

def quality
  @quality
end

#site ⇒ Object (readonly)

Returns the value of attribute site.



37
38
39
# File 'lib/jekyll-imgflow/config.rb', line 37

def site
  @site
end

#sizes ⇒ Object (readonly)

Returns the value of attribute sizes.



37
38
39
# File 'lib/jekyll-imgflow/config.rb', line 37

def sizes
  @sizes
end

#weserv_url ⇒ Object (readonly)

Returns the value of attribute weserv_url.



37
38
39
# File 'lib/jekyll-imgflow/config.rb', line 37

def weserv_url
  @weserv_url
end

Instance Method Details

#assign_core_config(cfg) ⇒ Object



51
52
53
54
55
56
# File 'lib/jekyll-imgflow/config.rb', line 51

def assign_core_config(cfg)
  CORE_CONFIG_DEFAULTS.each do |key, default|
    instance_variable_set("@#{key}", cfg[key] || default)
  end
  @image_modal = cfg.fetch("image_modal", DEFAULT_IMAGE_MODAL)
end

#assign_provider_config(cfg) ⇒ Object



58
59
60
61
62
# File 'lib/jekyll-imgflow/config.rb', line 58

def assign_provider_config(cfg)
  @imgproxy_url = cfg["imgproxy_url"]
  @weserv_url = cfg["weserv_url"]
  @flyimg_url = cfg["flyimg_url"]
end

#cache_dir ⇒ Object



74
75
76
# File 'lib/jekyll-imgflow/config.rb', line 74

def cache_dir
  merged_config["cache_dir"] || ".cache/imgflow"
end

#default_optimize_qualities ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/jekyll-imgflow/config.rb', line 64

def default_optimize_qualities
  {
    "low" => 30,
    "medium" => 50, # Uses default quality
    "high" => 85, # Uses default quality
    "maximum" => 95,
    "default" => 75
  }
end

#determine_version_type(params) ⇒ Symbol

Determine if params represent a default or specialized version

Parameters:

  • params (Hash) —

    Operation parameters

Returns:

  • (Symbol) —

    :default or :specialized



100
101
102
103
104
105
106
107
108
# File 'lib/jekyll-imgflow/config.rb', line 100

def determine_version_type(params)
  if params[:width] && @sizes.value?(params[:width]) &&
     (!params[:format] || @formats.include?(params[:format])) &&
     (!params[:quality] || params[:quality] == @quality)
    :default
  else
    :specialized
  end
end

#docker_config ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/jekyll-imgflow/config.rb', line 78

def docker_config
  {
    enabled: merged_config.key?("imgproxy_url") ||
      merged_config.key?("weserv_url") ||
      merged_config.key?("flyimg_url"),
    imgproxy_url: @imgproxy_url,
    weserv_url: @weserv_url,
    flyimg_url: @flyimg_url
  }
end

#docker_enabled ⇒ Object



89
90
91
# File 'lib/jekyll-imgflow/config.rb', line 89

def docker_enabled
  docker_config[:enabled]
end

#normalized_input_formats ⇒ Object

Get normalized format lists



136
137
138
# File 'lib/jekyll-imgflow/config.rb', line 136

def normalized_input_formats
  @input_formats.map(&:downcase)
end

#normalized_output_formats ⇒ Object



140
141
142
# File 'lib/jekyll-imgflow/config.rb', line 140

def normalized_output_formats
  @formats.map(&:downcase)
end

#sharp_url ⇒ Object



93
94
95
# File 'lib/jekyll-imgflow/config.rb', line 93

def sharp_url
  merged_config["sharp_url"]
end

#supported_input_format?(format) ⇒ Boolean

Format validation methods

Returns:

  • (Boolean)


111
112
113
114
# File 'lib/jekyll-imgflow/config.rb', line 111

def supported_input_format?(format)
  normalized_format = format.to_s.downcase.delete(".")
  @input_formats.map(&:downcase).include?(normalized_format)
end

#supported_output_format?(format) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
119
# File 'lib/jekyll-imgflow/config.rb', line 116

def supported_output_format?(format)
  normalized_format = format.to_s.downcase.delete(".")
  @formats.map(&:downcase).include?(normalized_format)
end

#validate_input_format!(format) ⇒ Object

Raises:

  • (ArgumentError)


121
122
123
124
125
126
# File 'lib/jekyll-imgflow/config.rb', line 121

def validate_input_format!(format)
  return if supported_input_format?(format)

  raise ArgumentError,
        "Unsupported input format '#{format}'. Supported formats: #{@input_formats.join(', ')}"
end

#validate_output_format!(format) ⇒ Object

Raises:

  • (ArgumentError)


128
129
130
131
132
133
# File 'lib/jekyll-imgflow/config.rb', line 128

def validate_output_format!(format)
  return if supported_output_format?(format)

  raise ArgumentError,
        "Unsupported output format '#{format}'. Supported formats: #{@formats.join(', ')}"
end