Class: ImgBuilder
- Inherits:
-
Object
- Object
- ImgBuilder
- Defined in:
- lib/img_builder.rb
Overview
Constructs HTML img tag from properties
Instance Attribute Summary collapse
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #generate_figcaption ⇒ Object
- #generate_img ⇒ Object
- #generate_picture ⇒ Object
- #generate_url_caption ⇒ Object
- #generate_url_wrapper ⇒ Object
- #generate_wrapper ⇒ Object
-
#initialize(props) ⇒ ImgBuilder
constructor
A new instance of ImgBuilder.
- #to_s ⇒ Object
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
#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
22 23 24 25 26 27 28 |
# File 'lib/img_builder.rb', line 22 def <<~END_CAPTION <figcaption class='imgFigCaption #{@props.attr_size_class}'> #{@props.url ? : @props.} </figcaption> END_CAPTION end |
#generate_img ⇒ Object
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_picture ⇒ Object
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_caption ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/img_builder.rb', line 57 def <<~END_URL <a href="#{@props.url}"#{@props.attr_target}#{@props.attr_nofollow}> #{@props.} </a> END_URL end |
#generate_url_wrapper ⇒ Object
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_wrapper ⇒ Object
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.} #{@props.url ? generate_url_wrapper : generate_picture} #{ if @props.} #{"</figure>\n" if @props.} #{@props.attribute if @props.attribution} </div> END_HTML end |
#to_s ⇒ Object
86 87 88 89 |
# File 'lib/img_builder.rb', line 86 def to_s @props.compute_dependant_properties generate_wrapper end |