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(props) ⇒ ImgBuilder

Returns a new instance of ImgBuilder.



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

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

Instance Attribute Details

#sourceObject (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_figcaptionObject



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

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

#generate_imgObject



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

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

#generate_pictureObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/img_builder.rb', line 43

def generate_picture
  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_captionObject



57
58
59
60
61
62
63
# File 'lib/img_builder.rb', line 57

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

#generate_url_wrapperObject



65
66
67
68
69
70
71
# File 'lib/img_builder.rb', line 65

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_wrapperObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/img_builder.rb', line 73

def generate_wrapper
  classes = "imgWrapper #{@props.img_display} #{@props.align} #{@props.attr_size_class} #{@props.wrapper_class}".squish
  <<~END_HTML.remove_blank_lines
    <div class='#{classes}' style='#{@props.attr_width_style} #{@props.wrapper_style}'>
      #{"<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_sObject



86
87
88
89
# File 'lib/img_builder.rb', line 86

def to_s
  @props.compute_dependant_properties
  generate_wrapper
end