Class: JekyllImgFlow::PathResolver

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

Overview

PathResolver - centralized path management for all image operations

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ PathResolver

Returns a new instance of PathResolver.



9
10
11
# File 'lib/jekyll-imgflow/path_resolver.rb', line 9

def initialize(config)
  @config = config
end

Instance Method Details

#build_url(relative_path, site) ⇒ String

Build public URL for image

Parameters:

  • relative_path (String)

    Relative path from site root

  • site (Jekyll::Site)

    Jekyll site object

Returns:

  • (String)

    Public URL



110
111
112
113
114
# File 'lib/jekyll-imgflow/path_resolver.rb', line 110

def build_url(relative_path, site)
  base_url = site.config["url"] || "http://localhost:4000"
  baseurl = site.config["baseurl"] || ""
  "#{base_url}#{baseurl}/#{relative_path}"
end

#cli_path(image_name) ⇒ Object

Get CLI path (convenience method)



25
26
27
# File 'lib/jekyll-imgflow/path_resolver.rb', line 25

def cli_path(image_name)
  paths_for(image_name)[:cli]
end

#http_path(image_name) ⇒ Object

Get HTTP path (convenience method)



30
31
32
# File 'lib/jekyll-imgflow/path_resolver.rb', line 30

def http_path(image_name)
  paths_for(image_name)[:http]
end

#paths_for(image_name) ⇒ Hash

Get path information for an image

Parameters:

  • image_name (String)

    Name of the image file

Returns:

  • (Hash)

    Path information for different use cases



16
17
18
19
20
21
22
# File 'lib/jekyll-imgflow/path_resolver.rb', line 16

def paths_for(image_name)
  {
    cli: File.join(@config.site.source, @config.originals, image_name), # Full filesystem path
    http: File.join(@config.originals, image_name), # Relative path for URLs
    name: image_name # Just the filename
  }
end

#relative_from_dest(full_path, site) ⇒ String

Get relative path from site destination

Parameters:

  • full_path (String)

    Full filesystem path

  • site (Jekyll::Site)

    Jekyll site object

Returns:

  • (String)

    Relative path



120
121
122
123
124
125
126
127
128
# File 'lib/jekyll-imgflow/path_resolver.rb', line 120

def relative_from_dest(full_path, site)
  # Simple path manipulation - remove site.dest prefix and leading slash
  if full_path.start_with?(site.dest)
    relative = full_path[site.dest.length..]
    relative.start_with?("/") ? relative[1..] : relative
  else
    full_path
  end
end

#resolve_original_path(image_name) ⇒ String

Resolve original path with validation

Parameters:

  • image_name (String)

    Name of the image file

Returns:

  • (String)

    Full path to original image file

Raises:

  • (ArgumentError)

    If image cannot be resolved or doesn't exist



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/jekyll-imgflow/path_resolver.rb', line 38

def resolve_original_path(image_name)
  full_path = cli_path(image_name)

  # Validate path exists and is readable
  raise ArgumentError, "Cannot resolve image path: #{image_name} (not found: #{full_path})" unless File.exist?(full_path)

  raise ArgumentError, "Image path is not a file: #{image_name} (path: #{full_path})" unless File.file?(full_path)

  raise ArgumentError, "Image path not readable: #{image_name} (path: #{full_path})" unless File.readable?(full_path)

  # Validate input file format against config
  file_extension = File.extname(full_path).delete(".").downcase
  input_formats = @config.input_formats

  raise ArgumentError, "No input_formats configured in unified config" unless input_formats

  unless input_formats.include?(file_extension)
    raise ArgumentError,
          "Invalid input format: '#{file_extension}'. Must be one of: #{input_formats.join(', ')}."
  end

  full_path
end

#resolve_output_path(filename) ⇒ String

Resolve output path for generated filename

Parameters:

  • filename (String)

    Generated filename

Returns:

  • (String)

    Full path to output file in _site (for specialized versions during rendering)



65
66
67
68
# File 'lib/jekyll-imgflow/path_resolver.rb', line 65

def resolve_output_path(filename)
  # Resolve output path relative to site destination
  File.join(@config.site.dest, @config.output, filename)
end

#resolve_relative_output_path(filename) ⇒ String

Resolve relative output path for manifest storage

Parameters:

  • filename (String)

    Generated filename

Returns:

  • (String)

    Relative path from site root



81
82
83
84
# File 'lib/jekyll-imgflow/path_resolver.rb', line 81

def resolve_relative_output_path(filename)
  # Return path relative to site root (without leading /)
  File.join(@config.output, filename)
end

#resolve_source_output_path(filename) ⇒ String

Resolve output path to source directory for default images

Parameters:

  • filename (String)

    Generated filename

Returns:

  • (String)

    Full path to output file in source (for default versions during pre_render)



73
74
75
76
# File 'lib/jekyll-imgflow/path_resolver.rb', line 73

def resolve_source_output_path(filename)
  # Default images go to source directory so Jekyll can copy them to _site
  File.join(@config.site.source, @config.output, filename)
end

#site_destString

Get site destination directory

Returns:

  • (String)

    Site destination path



88
89
90
# File 'lib/jekyll-imgflow/path_resolver.rb', line 88

def site_dest
  @config.site.dest
end

#temp_input_path(extension = "tmp") ⇒ String

Generate temporary input path

Parameters:

  • extension (String) (defaults to: "tmp")

    File extension

Returns:

  • (String)

    Path to temporary file



102
103
104
# File 'lib/jekyll-imgflow/path_resolver.rb', line 102

def temp_input_path(extension = "tmp")
  File.join(Dir.tmpdir, "imgflow-in-#{SecureRandom.hex}.#{extension}")
end

#temp_output_path(format) ⇒ String

Generate temporary output path

Parameters:

  • format (String)

    File format extension

Returns:

  • (String)

    Path to temporary file



95
96
97
# File 'lib/jekyll-imgflow/path_resolver.rb', line 95

def temp_output_path(format)
  File.join(Dir.tmpdir, "imgflow-out-#{SecureRandom.hex}.#{format}")
end