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.

Class Method Summary collapse

Class Method Details

.aspect_float(width, height) ⇒ Object



67
68
69
# File 'lib/jekyll_picture_tag/utils.rb', line 67

def aspect_float(width, height)
  width.to_f / height
end

.count_srcsetsObject

Used for auto markup configuration and such



32
33
34
# File 'lib/jekyll_picture_tag/utils.rb', line 32

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.



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

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



7
8
9
10
11
12
13
14
15
16
# File 'lib/jekyll_picture_tag/utils.rb', line 7

def keep_files
  dest_dir = PictureTag.pconfig['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.



27
28
29
# File 'lib/jekyll_picture_tag/utils.rb', line 27

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)


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

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

.snakeize(input) ⇒ Object



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

def snakeize(input)
  input.scan(/[A-Z][a-z]+/).map(&:downcase).join('_')
end

.titleize(input) ⇒ Object



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

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

.warning(message) ⇒ Object

Print a warning to the console



19
20
21
22
23
# File 'lib/jekyll_picture_tag/utils.rb', line 19

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

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