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

#prefixed_attributes(attributes, prefix) ⇒ Object



30
31
32
# File 'lib/jekyll-imgflow/picture_tag_adaptor.rb', line 30

def prefixed_attributes(attributes, prefix)
  attributes.to_h.map { |name, value| "#{prefix}-#{name}:\"#{value}\"" }
end

#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
# 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?

  "{% imgflow #{[result[:markup], *translated_attributes(result[:attributes]), '%}'].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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/jekyll-imgflow/picture_tag_adaptor.rb', line 37

def translate_to_imgflow(picture_markup)
  # Extract content between {% picture ... %} without a backtracking regex
  content = extract_picture_content(picture_markup)
  return { markup: "", attributes: {} } unless content

  content = content.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]

  imgflow_parts = translated_parts(categorized)
  {
    markup: imgflow_parts.join(" "),
    attributes: categorized[:html_attributes],
    markup_format: categorized[:markup_format]
  }
end

#translated_attributes(attributes) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/jekyll-imgflow/picture_tag_adaptor.rb', line 22

def translated_attributes(attributes)
  parts = []
  parts << "alt:\"#{attributes[:alt]}\"" if attributes[:alt]
  parts.concat(prefixed_attributes(attributes[:img], "img"))
  parts.concat(prefixed_attributes(attributes[:picture], "picture"))
  parts
end