Class: JekyllImgFlow::FilenameGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-imgflow/filename_generator.rb

Overview

Generates unique filenames for processed images Compatible with Jekyll Picture Tag format

Constant Summary collapse

VECTOR_FORMATS =

Generate filename for processed image (Jekyll Picture Tag compatible) Formats that are valid for output (raster only). SVG and other vector formats are input-only — image providers always produce raster output (webp, avif, jpg, png).

Returns:

  • (String)

    Generated filename (without path)

%w[svg].freeze

Instance Method Summary collapse

Instance Method Details

#file_digest(original_path) ⇒ String

Get file digest for manifest tracking

Parameters:

  • original_path (String)

    Path to original image

Returns:

  • (String)

    SHA256 digest of file



35
36
37
# File 'lib/jekyll-imgflow/filename_generator.rb', line 35

def file_digest(original_path)
  Digest::SHA256.file(original_path).hexdigest
end

#generate_filename(original_name, operations) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jekyll-imgflow/filename_generator.rb', line 18

def generate_filename(original_name, operations)
  base_name = File.basename(original_name, ".*")
  width = operations[:width]
  format = operations[:format] || File.extname(original_name).delete(".")
  # Vector formats (e.g. SVG) are input-only — never use them as
  # output format. Fall back to jpg (universal <img> fallback).
  format = "jpg" if VECTOR_FORMATS.include?(format.to_s.downcase)

  # Generate hash using Jekyll Picture Tag approach
  hash = generate_jpt_hash(original_name, operations)

  "#{base_name}-#{width}-#{hash}.#{format}"
end

#parse_filename(filename) ⇒ Hash

Parse filename to extract width and hash

Parameters:

  • filename (String)

    Generated filename

Returns:

  • (Hash)

    Parsed components width, hash, format



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/jekyll-imgflow/filename_generator.rb', line 42

def parse_filename(filename)
  # Parse pattern: basename-width-hash.format - simple string operations
  # Find last dot to separate format
  last_dot = filename.rindex(".")
  return {} unless last_dot

  format = filename[(last_dot + 1)..]
  name_part = filename[0...last_dot]

  # Find last dash to separate hash
  last_dash = name_part.rindex("-")
  return {} unless last_dash

  hash = name_part[(last_dash + 1)..]
  name_without_hash = name_part[0...last_dash]

  # Find second-to-last dash to separate width
  second_last_dash = name_without_hash.rindex("-")
  return {} unless second_last_dash

  width_str = name_without_hash[(second_last_dash + 1)..]
  base_name = name_without_hash[0...second_last_dash]

  # Validate components
  return {} unless hash.match?(/^[a-f0-9]+$/)
  return {} unless width_str.match?(/^\d+$/)

  {
    base_name: base_name,
    width: width_str.to_i,
    hash: hash,
    format: format
  }
end