Module: JekyllImgFlow::ModalWrapper

Included in:
HtmlGenerator
Defined in:
lib/jekyll-imgflow/modal_assets.rb

Overview

Modal wrapping logic for HtmlGenerator — extracted to keep HtmlGenerator under the RuboCop class length limit.

Constant Summary collapse

NON_HTML_FORMATS =

Formats that produce raw URLs/srcsets (not wrappable HTML elements)

%w[direct_url naked_srcset].freeze

Instance Method Summary collapse

Instance Method Details

#generated_modal_resultsObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/jekyll-imgflow/modal_assets.rb', line 125

def generated_modal_results
  return [] unless @config && @context&.registers&.key?(:site)

  result = @results.first
  parsed = JekyllImgFlow::FilenameGenerator.new.parse_filename(File.basename(result))
  return [] if parsed.empty?

  site = @context.registers[:site]
  directory = File.join(site.source, File.dirname(result.delete_prefix("/")))
  return [] unless File.directory?(directory)

  max_width = @config.sizes.values.max
  Dir.children(directory).filter_map do |filename|
    candidate = JekyllImgFlow::FilenameGenerator.new.parse_filename(filename)
    next unless candidate[:base_name] == parsed[:base_name]
    next unless candidate[:hash] == parsed[:hash]
    next if max_width && candidate[:width] > max_width

    File.join(File.dirname(result), filename)
  end
rescue Errno::ENOENT
  []
end

#html_element?Boolean

Check if the current markup format produces an HTML element

Returns:

  • (Boolean)


80
81
82
# File 'lib/jekyll-imgflow/modal_assets.rb', line 80

def html_element?
  !NON_HTML_FORMATS.include?(@markup_format)
end

Determine if modal behavior is enabled for this image Per-image modal:true/false overrides config default Modal is skipped when an explicit link is set

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
# File 'lib/jekyll-imgflow/modal_assets.rb', line 87

def modal_enabled?
  return false if @attributes[:link]

  modal_attr = @attributes[:modal]
  return modal_attr.to_s == "true" if modal_attr

  @config&.image_modal ? true : false
end


115
116
117
# File 'lib/jekyll-imgflow/modal_assets.rb', line 115

def modal_fallback_format
  @config&.fallback_format.to_s
end

Pick the largest existing fallback-format variant for the modal.



97
98
99
100
# File 'lib/jekyll-imgflow/modal_assets.rb', line 97

def modal_image_path
  variants = modal_variants
  variants[modal_fallback_format] || @results.last
end


119
120
121
122
123
# File 'lib/jekyll-imgflow/modal_assets.rb', line 119

def modal_max_width
  return unless @config.respond_to?(:sizes)

  @config.sizes.values.max
end


102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/jekyll-imgflow/modal_assets.rb', line 102

def modal_variants
  candidates = @attributes[:modal_results] + @results + generated_modal_results
  max_width = modal_max_width

  candidates.each_with_object({}) do |result, variants|
    parsed = JekyllImgFlow::FilenameGenerator.new.parse_filename(File.basename(result))
    next if parsed.empty? || (max_width && parsed[:width] > max_width)
    next if variants[parsed[:format]] && modal_width(variants[parsed[:format]]) >= parsed[:width]

    variants[parsed[:format]] = result
  end
end


149
150
151
# File 'lib/jekyll-imgflow/modal_assets.rb', line 149

def modal_width(result)
  JekyllImgFlow::FilenameGenerator.new.parse_filename(File.basename(result))[:width] || 0
end

#wrap_with_modal(html) ⇒ Object

Wrap HTML in modal trigger anchor



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/jekyll-imgflow/modal_assets.rb', line 154

def wrap_with_modal(html)
  variants = modal_variants
  modal_href = html_path(variants[modal_fallback_format] || @results.last)
  return html unless modal_href

  source_formats = variants.keys.reject { |format| format == modal_fallback_format }
  data_attrs = " data-imgflow-modal"
  data_attrs += " data-imgflow-modal-formats=\"#{source_formats.join(',')}\"" unless source_formats.empty?
  alt = @attributes[:alt]
  data_attrs += " data-imgflow-alt=\"#{escape_attr_value(alt)}\"" if alt

  "<a href=\"#{modal_href}\"#{data_attrs}>#{html}</a>"
end