Module: Shi::Jekyll::Images::File

Defined in:
lib/shi/jekyll/images/files.rb

Class Method Summary collapse

Class Method Details

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



8
9
10
11
12
13
14
15
# File 'lib/shi/jekyll/images/files.rb', line 8

def create page, source, bounds, crop
  type = File.extname source.relative_path
  if type.downcase == '.svg'
    Shi::Jekyll::Images::SVGFile::create page, source
  else
    Shi::Jekyll::Images::WebPFile::create page, source, bounds, crop
  end
end

.restore_generated_files(site) ⇒ Object

Восстанавливаем ранее сгенерированные файлы в site.static_files



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/shi/jekyll/images/files.rb', line 18

def restore_generated_files(site)
  target_root = site.config['shi_images']&.[]('target_root') || 'img'
  generated_files_dir = File.join(site.dest, target_root)
  return unless File.directory?(generated_files_dir)

  Dir.glob(File.join(generated_files_dir, '**', '*.{webp,svg}')).each do |file_path|
    relative_path = file_path.sub(/^#{site.dest}\//, '')
    unless site.static_files.any? { |f| f.relative_path == relative_path }
      # Создаём фиктивный StaticFile, чтобы Jekyll не удалял файл
      source_file = site.static_files.find { |f| f.relative_path == File.basename(relative_path).sub(/\.webp$/, '') }
      source_file ||= Jekyll::StaticFile.new(site, site.source, File.dirname(relative_path), File.basename(relative_path))
      
      if File.extname(file_path).downcase == '.svg'
        static_file = Shi::Jekyll::Images::SVGFile.new(relative_path, source_file)
      else
        # Для WebP используем dummy bounds/crop, так как точные значения неизвестны
        static_file = Shi::Jekyll::Images::WebPFile.new(relative_path, source_file, nil, nil)
      end
      site.static_files << static_file unless site.static_files.include?(static_file)
    end
  end
end