Class: Shi::Jekyll::Images::WebPFile

Inherits:
Jekyll::StaticFile
  • Object
show all
Extended by:
Config
Includes:
Config
Defined in:
lib/shi/jekyll/images/files.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Config

config, get_value, path_by_page, site, site_config

Constructor Details

#initialize(path, source, bounds, crop) ⇒ WebPFile

Returns a new instance of WebPFile.



100
101
102
103
104
105
106
# File 'lib/shi/jekyll/images/files.rb', line 100

def initialize path, source, bounds, crop
  @wp_path = path
  @wp_source = source
  @wp_bounds = bounds
  @wp_crop = crop
  super site, site.source, File.dirname(path), File.basename(path)
end

Class Method Details

.create(page, source, bounds, crop) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/shi/jekyll/images/files.rb', line 79

def create page, source, bounds, crop
  bounds ||= default_bounds(page)
  return source if source.write? && (['SOURCE', 'ORIGINAL', 'ORIGIN', 'NONE'].include?(bounds.upcase)) && crop.nil?

  @@created ||= {}
  key = make_key source, bounds, crop
  result = @@created[key]
  if result == nil
    result = new path(page, source, bounds, crop), source, bounds, crop
    @@created[key] = result
  end
  if !site.static_files.include?(result)
    site.static_files << result
  end
  result
end

.default_bounds(page) ⇒ Object



75
76
77
# File 'lib/shi/jekyll/images/files.rb', line 75

def default_bounds page
  get_value(page, 'image_bounds') || 'ORIGINAL'
end

.make_key(source, bounds, crop) ⇒ Object



68
69
70
71
72
73
# File 'lib/shi/jekyll/images/files.rb', line 68

def make_key source, bounds, crop
  result = source.relative_path
  result += "@#{bounds}" if bounds
  result += "@#{crop}" if crop
  Jekyll::PathManager::join '', result
end

.name_by_source(source, bounds, crop) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/shi/jekyll/images/files.rb', line 56

def name_by_source source, bounds, crop
  result = source.basename
  result += "-#{bounds}" if bounds && !['SOURCE', 'ORIGINAL', 'ORIGIN', 'NONE'].include?(bounds.upcase)
  result += "-#{Jekyll::Utils::slugify(crop)}" if crop
  result += '.webp'
  result
end

.path(page, source, bounds, crop) ⇒ Object



64
65
66
# File 'lib/shi/jekyll/images/files.rb', line 64

def path page, source, bounds, crop
  Jekyll::PathManager::join path_by_page(page), name_by_source(source, bounds, crop)
end

Instance Method Details

#modified?Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
# File 'lib/shi/jekyll/images/files.rb', line 108

def modified?
  src_path = File.join(site.source, @wp_source.relative_path)
  tgt_path = destination site.dest
  return true unless File.exist?(tgt_path) && File.exist?(src_path)
  File.mtime(src_path) > File.mtime(tgt_path)
end

#write(dest) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/shi/jekyll/images/files.rb', line 155

def write dest
  return true if !write?

  tgt_path = destination dest
  src_path = File::join site.source, @wp_source.relative_path

  return false unless File.exist?(src_path)

  FileUtils::mkdir_p(File.dirname(tgt_path))
  FileUtils::rm(tgt_path) if File.exist?(tgt_path)
  cmd = "convert '#{src_path}' #{cropping(@wp_crop)} #{resizing(@wp_bounds)} #{qualiting} '#{tgt_path}'"
  system(cmd, exception: true, err: '/dev/null')

  true
end

#write?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/shi/jekyll/images/files.rb', line 115

def write?
  modified?
end