Module: PictureTag::Utils

Defined in:
lib/jekyll_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.

Constant Summary collapse

GRAVITIES =

These are valid ImageMagick gravity arguments (relevant to our use case):

%w[center
north
northeast
east
southeast
south
southwest
west
northwest].freeze
GEOMETRY_REGEX =

This is an attempt to recognize valid imagemagick geometry arguments with regex. It only tries to match values which don’t preserve aspect ratio, as they’re the ones people might actually need here.

/\A\d*%?[x:]?\d*[%!]?([+-]\d+){,2}\Z/i.freeze

Class Method Summary collapse

Class Method Details

.count_srcsetsObject

Used for auto markup configuration and such



50
51
52
# File 'lib/jekyll_picture_tag/utils.rb', line 50

def count_srcsets
  PictureTag.formats.length * PictureTag.source_images.length
end

.interpolate(xvals, yvals, xval) ⇒ Object

Linear interpolator. Pass it 2 values in the x array, 2 values in the y array, and an x value, returns a y value.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/jekyll_picture_tag/utils.rb', line 69

def interpolate(xvals, yvals, xval)
  xvals.map!(&:to_f)
  yvals.map!(&:to_f)

  # Slope
  m = (yvals.last - yvals.first) / (xvals.last - xvals.first)
  # Value of y when x=0
  b = yvals.first - (m * xvals.first)
  # y = mx + b
  (m * xval) + b
end

.keep_filesObject

Configure Jekyll to keep our generated files



25
26
27
28
29
30
31
32
33
34
# File 'lib/jekyll_picture_tag/utils.rb', line 25

def 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.



45
46
47
# File 'lib/jekyll_picture_tag/utils.rb', line 45

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

.markdown_page?Boolean

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

Returns:

  • (Boolean)


55
56
57
58
59
60
61
# File 'lib/jekyll_picture_tag/utils.rb', line 55

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

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

.titleize(input) ⇒ Object



63
64
65
# File 'lib/jekyll_picture_tag/utils.rb', line 63

def titleize(input)
  input.split('_').map(&:capitalize).join
end

.warning(message) ⇒ Object

Print a warning to the console



37
38
39
40
41
# File 'lib/jekyll_picture_tag/utils.rb', line 37

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

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