Class: JekyllImgFlow::PictureTagAdaptor

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

Overview

Adaptor that translates Jekyll Picture Tag syntax to ImgFlow syntax Simple tag-to-tag translation with values

Instance Method Summary collapse

Constructor Details

#initialize(site, config) ⇒ PictureTagAdaptor

Returns a new instance of PictureTagAdaptor.



7
8
9
10
# File 'lib/jekyll-imgflow/picture_tag_adaptor.rb', line 7

def initialize(site, config)
  @site = site
  @config = config
end

Instance Method Details

#to_imgflow_tag(picture_markup) ⇒ String

Convert Picture Tag to ImgFlow tag markup that can be used in templates

Parameters:

  • picture_markup (String)

    Picture Tag markup

Returns:

  • (String)

    ImgFlow tag markup with attributes



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jekyll-imgflow/picture_tag_adaptor.rb', line 15

def to_imgflow_tag(picture_markup)
  result = translate_to_imgflow(picture_markup)
  return "" if result[:markup].empty?

  # Build ImgFlow tag with attributes
  tag_parts = ["{% imgflow", result[:markup]]

  # Add HTML attributes
  tag_parts << "alt:\"#{result[:attributes][:alt]}\"" if result[:attributes][:alt]

  if result[:attributes][:img]&.any?
    result[:attributes][:img].each do |name, value|
      tag_parts << "img-#{name}:\"#{value}\""
    end
  end

  if result[:attributes][:picture]&.any?
    result[:attributes][:picture].each do |name, value|
      tag_parts << "picture-#{name}:\"#{value}\""
    end
  end

  tag_parts << "%}"
  tag_parts.join(" ")
end

#translate_to_imgflow(picture_markup) ⇒ Hash

Convert Picture Tag markup to ImgFlow markup with HTML attributes

Parameters:

  • picture_markup (String)

    Picture Tag markup like "picture hero image.jpg 16:9 --alt Text %"

Returns:

  • (Hash)

    Translation result with markup and attributes



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jekyll-imgflow/picture_tag_adaptor.rb', line 44

def translate_to_imgflow(picture_markup)
  # Extract content between {% picture ... %}
  match = picture_markup.match(/\{%\s*picture\s+(.+?)\s*%}/)
  return { markup: "", attributes: {} } unless match

  content = match[1].strip
  return { markup: "", attributes: {} } if content.empty?

  # Parse arguments
  args = parse_arguments(content)
  return { markup: "", attributes: {} } if args.empty?

  # Step 1: Categorize arguments by type
  categorized = categorize_arguments(args)
  return { markup: "", attributes: {} } unless categorized[:image]

  # Step 2: Translate each category to ImgFlow syntax
  imgflow_parts = []
  imgflow_parts << categorized[:image]
  imgflow_parts += translate_media_queries(categorized[:media_queries])
  imgflow_parts += translate_operations(categorized[:operations])
  imgflow_parts << "formats:webp,jpg" unless formats?(imgflow_parts)
  imgflow_parts << "markup:#{categorized[:markup_format]}" if categorized[:markup_format] && categorized[:markup_format] != "auto"

  # Step 3: Assemble result
  {
    markup: imgflow_parts.compact.join(" "),
    attributes: categorized[:html_attributes],
    markup_format: categorized[:markup_format]
  }
end