Module: Jekyll::DistorteD::Molecule::Font

Includes:
Cooltrainer::DistorteD::Font
Defined in:
lib/distorted-jekyll/molecule/font.rb

Instance Method Summary collapse

Instance Method Details

#fallback_imgObject

Return the filename of the most-compatible output image for use as the fallback <img> tag inside our <picture>.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/distorted-jekyll/molecule/font.rb', line 46

def fallback_img
  best_ver = nil
  files.keep_if{|f| f.key?(:type) && f&.dig(:type)&.media_type == 'image'.freeze}.each{ |f|
    # PNG > WebP
    if f&.dig(:type)&.sub_type == 'png'.freeze || best_ver.nil?
      best_ver = f
    end
  }
  # Return the filename of the biggest matched variation,
  # otherwise use the original filename.
  best_ver&.dig(:name) || @name
end

#render_to_output_buffer(context, output) ⇒ Object



13
14
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
40
41
42
# File 'lib/distorted-jekyll/molecule/font.rb', line 13

def render_to_output_buffer(context, output)
  super
  begin
    filez = files.keep_if{ |f|
      # Strip out all non-displayable media-types, e.g. the actual text/whatever.
      f.key?(:type) && f&.dig(:type)&.media_type == 'image'.freeze
    }.keep_if{ |f|
      # Strip out full-size images (will have `nil`) — only display thumbnail vers
      f.key?(:width) or f.key?(:height)
    }.map{ |f|
      # Stringify to make Liquid happy
      f.transform_values(&:to_s).transform_keys(&:to_s)
    }
    output << parse_template.render({
      'name' => @name,
      'path' => @relative_dest,
      'alt' => abstract(:alt),
      'title' => abstract(:title),
      'sources' => filez,
      'fallback_img' => fallback_img,
    })
  rescue Liquid::SyntaxError => l
    unless Jekyll.env == 'production'.freeze
      output << parse_template(name: 'error_code'.freeze).render({
        'message' => l.message,
      })
    end
  end
  output
end