Module: PictureTag::Utils

Defined in:
lib/jekyll-4-picture-tag/utils.rb

Overview

This is a little module to hold logic that doesn’t fit other places. If it starts getting big, refactor.

Class Method Summary collapse

Class Method Details

.biggest_sourceObject

Returns the widest source image



63
64
65
# File 'lib/jekyll-4-picture-tag/utils.rb', line 63

def self.biggest_source
  PictureTag.source_images.values.max_by(&:width)
end

.count_srcsetsObject

Used for auto markup configuration and such



46
47
48
49
50
51
# File 'lib/jekyll-4-picture-tag/utils.rb', line 46

def self.count_srcsets
  formats = PictureTag.preset['formats'].length
  source_images = PictureTag.source_images.length

  formats * source_images
end

.keep_filesObject

Configure Jekyll to keep our generated files



6
7
8
9
10
11
12
13
14
15
# File 'lib/jekyll-4-picture-tag/utils.rb', line 6

def self.keep_files
  dest_dir = PictureTag.config['picture']['output']

  # Chop a slash off the end, if it's there. Doesn't work otherwise.
  dest_dir = dest_dir[0..-2] if dest_dir =~ %r{/\z}

  return if PictureTag.site.config['keep_files'].include?(dest_dir)

  PictureTag.site.config['keep_files'] << dest_dir
end

.liquid_lookup(params) ⇒ Object

Parse a liquid template; allows liquid variables to be included as tag params.



26
27
28
29
30
31
32
33
34
# File 'lib/jekyll-4-picture-tag/utils.rb', line 26

def self.liquid_lookup(params)
  Liquid::Template.parse(params).render(PictureTag.context)

  # This gsub allows people to include template code for javascript
  # libraries such as handlebar.js. It adds complication and I'm not sure
  # it has much value now, so I'm commenting it out. If someone has a use
  # case for it we can add it back in.
  # .gsub(/\\\{\\\{|\\\{\\%/, '\{\{' => '{{', '\{\%' => '{%')
end

.markdown_page?Boolean

Returns whether or not the current page is a markdown file.

Returns:

  • (Boolean)


54
55
56
57
58
59
60
# File 'lib/jekyll-4-picture-tag/utils.rb', line 54

def self.markdown_page?
  page_name = PictureTag.page['name']
  page_ext = PictureTag.page['ext']
  ext = page_ext ? page_ext : File.extname(page_name)

  ext.casecmp('.md').zero? || ext.casecmp('.markdown').zero?
end

.process_format(format, media) ⇒ Object

Allows us to use ‘original’ as a format name.



37
38
39
40
41
42
43
# File 'lib/jekyll-4-picture-tag/utils.rb', line 37

def self.process_format(format, media)
  if format.casecmp('original').zero?
    PictureTag.source_images[media].ext
  else
    format.downcase
  end
end

.warning(message) ⇒ Object

Print a warning to the console



18
19
20
21
22
# File 'lib/jekyll-4-picture-tag/utils.rb', line 18

def self.warning(message)
  return if PictureTag.config['picture']['suppress_warnings']

  warn 'Jekyll Picture Tag Warning: '.yellow + message
end