Class: JekyllImgFlow::OperationProcessor
- Inherits:
-
Object
- Object
- JekyllImgFlow::OperationProcessor
- Defined in:
- lib/jekyll-imgflow/operation_processor.rb
Overview
OperationProcessor - processes image operations using providers Handles both single operations and batch operations
Instance Method Summary collapse
- #add_static_file(site, relative) ⇒ Object
-
#build_operation_from_params(params) ⇒ Hash
Build operation structure from params hash Combines all params into a single operation with flat params.
-
#build_operations_from_params(tag_params) ⇒ Array<Hash>
Build operations array from tag parameters.
-
#determine_version_type(params) ⇒ Symbol
Determine if operations represent default or specialized version.
-
#initialize(provider, path_resolver, manifest = nil, config = nil) ⇒ OperationProcessor
constructor
A new instance of OperationProcessor.
-
#needs_processing?(input_path, output_path, operations) ⇒ Boolean
Check if operation needs to be processed (cache check).
-
#process_batch_operations(operations, input_path, final_output_path) ⇒ String
Process multiple operations on an image in sequence (batch).
-
#process_operation(original_name, operation, input_path, page_path = nil) ⇒ String
Process an operation and return the output path.
-
#process_single_operation(operation_type, input_path, output_path, params) ⇒ String
Process a single operation on an image.
-
#register_jekyll_static_file(file_path) ⇒ Object
Register a generated file as a Jekyll::StaticFile so Jekyll copies it to _site during the write phase.
- #static_file_registered?(site, relative) ⇒ Boolean
Constructor Details
#initialize(provider, path_resolver, manifest = nil, config = nil) ⇒ OperationProcessor
Returns a new instance of OperationProcessor.
9 10 11 12 13 14 15 |
# File 'lib/jekyll-imgflow/operation_processor.rb', line 9 def initialize(provider, path_resolver, manifest = nil, config = nil) @provider = provider @path_resolver = path_resolver @filename_generator = FilenameGenerator.new @manifest = manifest @config = config end |
Instance Method Details
#add_static_file(site, relative) ⇒ Object
122 123 124 125 126 |
# File 'lib/jekyll-imgflow/operation_processor.rb', line 122 def add_static_file(site, relative) site.static_files << Jekyll::StaticFile.new( site, site.source, File.dirname(relative), File.basename(relative) ) end |
#build_operation_from_params(params) ⇒ Hash
Build operation structure from params hash Combines all params into a single operation with flat params
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/jekyll-imgflow/operation_processor.rb', line 199 def build_operation_from_params(params) # Determine primary operation type type = if params[:width] || params[:height] :resize elsif params[:crop] :crop elsif params[:watermark] :watermark else :format end # Return unified operation structure with all params flattened { type: type, params: params.compact } end |
#build_operations_from_params(tag_params) ⇒ Array<Hash>
Build operations array from tag parameters
221 222 223 |
# File 'lib/jekyll-imgflow/operation_processor.rb', line 221 def build_operations_from_params(tag_params) [build_operation_from_params(tag_params)] end |
#determine_version_type(params) ⇒ Symbol
Determine if operations represent default or specialized version
228 229 230 |
# File 'lib/jekyll-imgflow/operation_processor.rb', line 228 def determine_version_type(params) @config&.determine_version_type(params) || :specialized end |
#needs_processing?(input_path, output_path, operations) ⇒ Boolean
Check if operation needs to be processed (cache check)
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/jekyll-imgflow/operation_processor.rb', line 173 def needs_processing?(input_path, output_path, operations) # If output doesn't exist, needs processing return true unless File.exist?(output_path) # If input is newer than output, needs processing return true if File.mtime(input_path) > File.mtime(output_path) # Check if operations changed by comparing cache key # stored alongside the output file cache_key = @filename_generator.generate_cache_key(operations) cache_file = "#{output_path}.cache_key" return true unless File.exist?(cache_file) stored_key = File.read(cache_file).strip return true if stored_key != cache_key # No cache key file - needs processing to create it false end |
#process_batch_operations(operations, input_path, final_output_path) ⇒ String
Process multiple operations on an image in sequence (batch)
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/jekyll-imgflow/operation_processor.rb', line 133 def process_batch_operations(operations, input_path, final_output_path) return input_path if operations.empty? current_input = input_path temp_files = [] begin # Process each operation in sequence operations.each_with_index do |operation, index| operation_type = operation[:type] params = operation[:params] || {} # Determine output path if index == operations.length - 1 # Last operation - use final output path output_path = final_output_path else # Intermediate operation - use temp file format = params[:format] || File.extname(input_path).delete(".") output_path = @path_resolver.temp_output_path(format) temp_files << output_path end # Process the operation current_input = process_single_operation(operation_type, current_input, output_path, params) end current_input ensure # Cleanup temp files even on failure temp_files.each { |f| FileUtils.rm_f(f) } end end |
#process_operation(original_name, operation, input_path, page_path = nil) ⇒ String
Process an operation and return the output path
41 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/jekyll-imgflow/operation_processor.rb', line 41 def process_operation(original_name, operation, input_path, page_path = nil) type = operation[:type] params = operation[:params] # Determine version type version_type = determine_version_type(params) # Preserve original directory structure under output subdir = File.dirname(original_name) subdir = nil if subdir == "." # Generate filename using FilenameGenerator (JPT compatible) filename = @filename_generator.generate_filename(input_path, params) # Write to source directory so Jekyll copies files to _site during the write phase actual_output_path = @path_resolver.resolve_source_output_path(filename, subdir) # Ensure output directory exists before processing FileUtils.mkdir_p(File.dirname(actual_output_path)) # Animated GIFs must not be resized or converted — the operation # would destroy the animation. Copy the original file as-is so the # manifest can track it and HTML references stay valid. if AnimatedGifDetector.animated?(input_path) Jekyll.logger.warn "🖼️ ImgFlow: Skipping resize for animated GIF " \ "'#{original_name}' — copying original as-is to " \ "preserve animation." FileUtils.cp(input_path, actual_output_path) else # Process the operation (create the image) process_single_operation(type, input_path, actual_output_path, params) end # Register in manifest if @manifest # Store relative path (with leading /) for manifest storage relative_path = "/#{@path_resolver.resolve_relative_output_path(filename, subdir)}" provider_name = @provider.class.provider_name @manifest.register_version( original_name, relative_path, params, version_type, page_path, nil, # file_digest provider_name ) end # Register as Jekyll static file so Jekyll copies it to _site during # the write phase. Without this, files created during pre_render (or # during render via imgflow tags) are not picked up by Jekyll's static # file reader, which runs before pre_render. This would leave _site # without optimized images until a second build. register_jekyll_static_file(actual_output_path) actual_output_path end |
#process_single_operation(operation_type, input_path, output_path, params) ⇒ String
Process a single operation on an image
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/jekyll-imgflow/operation_processor.rb', line 23 def process_single_operation(operation_type, input_path, output_path, params) # Get the appropriate tag class for validation tag_class = JekyllImgFlow::Tags::TagRegistry.get_tag(operation_type) raise "Unknown operation: #{operation_type}" unless tag_class # Create tag instance and process tag = tag_class.new(@provider) tag.process(input_path, output_path, params) output_path end |
#register_jekyll_static_file(file_path) ⇒ Object
Register a generated file as a Jekyll::StaticFile so Jekyll copies it to _site during the write phase. No-op for mock/test sites.
104 105 106 107 108 109 110 111 112 |
# File 'lib/jekyll-imgflow/operation_processor.rb', line 104 def register_jekyll_static_file(file_path) site = @config&.site return unless site.is_a?(Jekyll::Site) relative = file_path.delete_prefix("#{site.source}/") return if relative == file_path # not under site source add_static_file(site, relative) unless static_file_registered?(site, relative) end |
#static_file_registered?(site, relative) ⇒ Boolean
116 117 118 |
# File 'lib/jekyll-imgflow/operation_processor.rb', line 116 def static_file_registered?(site, relative) site.static_files.any? { |sf| sf.relative_path == "/#{relative}" } end |