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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Config

Returns a new instance of Config.



30
31
32
33
34
35
36
37
# File 'lib/jekyll-imgflow/config.rb', line 30

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_priorityObject (readonly)

Returns the value of attribute backend_priority.



25
26
27
# File 'lib/jekyll-imgflow/config.rb', line 25

def backend_priority
  @backend_priority
end

#fallback_formatObject (readonly)

Returns the value of attribute fallback_format.



25
26
27
# File 'lib/jekyll-imgflow/config.rb', line 25

def fallback_format
  @fallback_format
end

#flyimg_urlObject (readonly)

Returns the value of attribute flyimg_url.



25
26
27
# File 'lib/jekyll-imgflow/config.rb', line 25

def flyimg_url
  @flyimg_url
end

#formatsObject (readonly)

Returns the value of attribute formats.



25
26
27
# File 'lib/jekyll-imgflow/config.rb', line 25

def formats
  @formats
end

#image_modalObject (readonly)

Returns the value of attribute image_modal.



25
26
27
# File 'lib/jekyll-imgflow/config.rb', line 25

def image_modal
  @image_modal
end

#imgproxy_urlObject (readonly)

Returns the value of attribute imgproxy_url.



25
26
27
# File 'lib/jekyll-imgflow/config.rb', line 25

def imgproxy_url
  @imgproxy_url
end

#input_formatsObject (readonly)

Returns the value of attribute input_formats.



25
26
27
# File 'lib/jekyll-imgflow/config.rb', line 25

def input_formats
  @input_formats
end

#optimize_qualitiesObject (readonly)

Returns the value of attribute optimize_qualities.



25
26
27
# File 'lib/jekyll-imgflow/config.rb', line 25

def optimize_qualities
  @optimize_qualities
end

#originalsObject (readonly)

Returns the value of attribute originals.



25
26
27
# File 'lib/jekyll-imgflow/config.rb', line 25

def originals
  @originals
end

#outputObject (readonly)

Returns the value of attribute output.



25
26
27
# File 'lib/jekyll-imgflow/config.rb', line 25

def output
  @output
end

#qualityObject (readonly)

Returns the value of attribute quality.



25
26
27
# File 'lib/jekyll-imgflow/config.rb', line 25

def quality
  @quality
end

#siteObject (readonly)

Returns the value of attribute site.



25
26
27
# File 'lib/jekyll-imgflow/config.rb', line 25

def site
  @site
end

#sizesObject (readonly)

Returns the value of attribute sizes.



25
26
27
# File 'lib/jekyll-imgflow/config.rb', line 25

def sizes
  @sizes
end

#weserv_urlObject (readonly)

Returns the value of attribute weserv_url.



25
26
27
# File 'lib/jekyll-imgflow/config.rb', line 25

def weserv_url
  @weserv_url
end

Instance Method Details

#assign_core_config(cfg) ⇒ Object



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

def assign_core_config(cfg)
  @originals = cfg["originals"] || DEFAULT_ORIGINALS
  @output = cfg["output"] || DEFAULT_OUTPUT
  @input_formats = cfg["input_formats"] || DEFAULT_INPUT_FORMATS
  @sizes = cfg["sizes"] || DEFAULT_SIZES
  @formats = cfg["formats"] || DEFAULT_FORMATS
  @quality = cfg["quality"] || DEFAULT_QUALITY
  @backend_priority = cfg["backend_priority"] || DEFAULT_BACKEND_PRIORITY
  @fallback_format = cfg["fallback_format"] || DEFAULT_FALLBACK_FORMAT
  @image_modal = cfg.fetch("image_modal", DEFAULT_IMAGE_MODAL)
end

#assign_provider_config(cfg) ⇒ Object



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

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

#cache_dirObject



67
68
69
# File 'lib/jekyll-imgflow/config.rb', line 67

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

#default_optimize_qualitiesObject



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

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



93
94
95
96
97
98
99
100
101
# File 'lib/jekyll-imgflow/config.rb', line 93

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_configObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/jekyll-imgflow/config.rb', line 71

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_enabledObject



82
83
84
# File 'lib/jekyll-imgflow/config.rb', line 82

def docker_enabled
  docker_config[:enabled]
end

#normalized_input_formatsObject

Get normalized format lists



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

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

#normalized_output_formatsObject



133
134
135
# File 'lib/jekyll-imgflow/config.rb', line 133

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

#sharp_urlObject



86
87
88
# File 'lib/jekyll-imgflow/config.rb', line 86

def sharp_url
  merged_config["sharp_url"]
end

#supported_input_format?(format) ⇒ Boolean

Format validation methods

Returns:

  • (Boolean)


104
105
106
107
# File 'lib/jekyll-imgflow/config.rb', line 104

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)


109
110
111
112
# File 'lib/jekyll-imgflow/config.rb', line 109

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)


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

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)


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

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

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