Class: ImgBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/img_builder.rb

Overview

Constructs HTML img tag from properties

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(img, props) ⇒ ImgBuilder

Returns a new instance of ImgBuilder.



16
17
18
19
20
21
# File 'lib/img_builder.rb', line 16

def initialize(img, props)
  @img = img
  @props = props
  @props.compute_dependant_properties
  @source = Source.new @props.src
end

Instance Attribute Details

#img ⇒ Object (readonly)

Returns the value of attribute img.



14
15
16
# File 'lib/img_builder.rb', line 14

def img
  @img
end

#props ⇒ Object (readonly)

Returns the value of attribute props.



14
15
16
# File 'lib/img_builder.rb', line 14

def props
  @props
end

#source ⇒ Object (readonly)

Returns the value of attribute source.



14
15
16
# File 'lib/img_builder.rb', line 14

def source
  @source
end

Instance Method Details

#generate_figcaption ⇒ Object



23
24
25
26
27
28
29
# File 'lib/img_builder.rb', line 23

def generate_figcaption
  <<~END_CAPTION
    <figcaption class='imgFigCaption #{@props.attr_size_class}'>
      #{@props.url ? generate_url_caption : @props.caption}
    </figcaption>
  END_CAPTION
end

#generate_img ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/img_builder.rb', line 31

def generate_img
  img_classes = @props.classes || 'rounded shadow'
  <<~END_IMG
    <img #{@props.attr_alt}
      class="imgImg #{img_classes.squish}"
      src="#{@props.src}"
      #{@props.attr_style_img}
      #{@props.attr_title}
      #{@props.lazy}
      #{@props.priority}
    />
  END_IMG
end

#generate_picture ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/img_builder.rb', line 46

def generate_picture
  if @props.lazy && (@props.size || @props.max_width)
    @img.logger.warn do
      <<~END_MSG.squish
        Warning: lazy loading was specified, but the size or max_width attribute was specified for the href tag
        on line #{@img.line_number} (after front matter) of #{@img.page['path']}.
        Specify dimensions via style or class attributes instead.
      END_MSG
    end
  end

  return generate_img if @props.src.start_with? 'http'

  # avif is not well supported yet
  # <source srcset="#{@props.src_any 'avif'}" type="image/avif">
  result = <<~END_IMG
    <picture#{@props.attr_id} class='imgPicture'>
      #{@source.generate.join("\n  ")}
      #{generate_img}
    </picture>
  END_IMG
  result.strip
end

#generate_url_caption ⇒ Object



70
71
72
73
74
75
76
# File 'lib/img_builder.rb', line 70

def generate_url_caption
  <<~END_URL
    <a href="#{@props.url}"#{@props.attr_target}#{@props.attr_nofollow}>
      #{@props.caption}
    </a>
  END_URL
end

#generate_url_wrapper ⇒ Object



78
79
80
81
82
83
84
# File 'lib/img_builder.rb', line 78

def generate_url_wrapper
  <<~END_HTML
    <a href='#{@props.url}'#{@props.attr_target}#{@props.attr_nofollow} class='imgImgUrl'>
      #{generate_picture}
    </a>
  END_HTML
end

#generate_wrapper ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/img_builder.rb', line 86

def generate_wrapper
  classes = "imgWrapper #{@props.img_display} #{@props.align} #{@props.attr_size_class} " \
            "#{@props.attr_max_width_class} #{@props.wrapper_class}"
  classes = classes.squish
  styles = "#{@props.attr_width_style} #{@props.attr_max_width_style} #{@props.wrapper_style}".squish
  <<~END_HTML.remove_blank_lines
    <div class='#{classes}' style='#{styles}'>
      #{"<figure>\n" if @props.caption}
        #{@props.url ? generate_url_wrapper : generate_picture}
        #{generate_figcaption if @props.caption}
      #{"</figure>\n" if @props.caption}
      #{@props.attribute if @props.attribution}
    </div>
  END_HTML
end

#to_s ⇒ Object



102
103
104
105
# File 'lib/img_builder.rb', line 102

def to_s
  @props.compute_dependant_properties
  generate_wrapper
end