Class: Utopia::Tags::Gallery

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/tags/gallery.rb

Defined Under Namespace

Modules: Processes Classes: ImageMetadata, ImagePath

Constant Summary collapse

CACHE_DIR =
"_cache"
PROCESSES =
{
	:pdf_thumbnail => Processes::DocumentThumbnail.new([300, 300]),
	:photo_thumbnail => Processes::PhotoThumbnail.new([300, 300]),
	:large => Processes::Thumbnail.new([800, 800]),
	:square_thumbnail => Processes::CropThumbnail.new([300, 300]),
	:thumbnail => Processes::Thumbnail.new([300, 300])
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, path) ⇒ Gallery

Returns a new instance of Gallery.



165
166
167
168
# File 'lib/utopia/tags/gallery.rb', line 165

def initialize(node, path)
	@node = node
	@path = path
end

Class Method Details

.call(transaction, state) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/utopia/tags/gallery.rb', line 242

def self.call(transaction, state)
	gallery = new(transaction.end_tags[-2].node, Utopia::Path.create(state["path"] || "./"))
	 = gallery.
	.default = {}

	tag_name = state["tag"] || "img"
	gallery_class = state["class"] || "gallery"

	options = {}
	options[:process] = state["process"]
	options[:filter] = Regexp.new("(#{state["filetypes"]})$", "i") if state["filetypes"]
	
	filter = Regexp.new(state["filter"], Regexp::IGNORECASE) if state["filter"]

	transaction.tag("div", "class" => gallery_class) do |node|
		images = gallery.images(options).sort do |a, b|
			if ([a.original.basename]["order"] && [b.original.basename]["order"])
				[a.original.basename]["order"] <=> [b.original.basename]["order"]
			else
				a.original.basename <=> b.original.basename
			end
		end

		images.each do |path|
			next if filter and !filter.match(path.original.basename)
			
			alt = ImageMetadata.new([path.original.basename])
			transaction.tag(tag_name, "src" => path, "alt" => alt)
		end
	end
end

Instance Method Details

#images(options = {}) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/utopia/tags/gallery.rb', line 180

def images(options = {})
	options[:filter] ||= /(jpg|png)$/i

	paths = []
	local_path = @node.local_path(@path)

	Dir.entries(local_path).each do |filename|
		next unless filename.match(options[:filter])

		fullpath = File.join(local_path, filename)

		paths << ImagePath.new(@path + filename)
	end

	if options[:process]
		paths.each do |path|
			processed_image(path, options[:process])
		end
	end

	return paths
end

#metadataObject



170
171
172
173
174
175
176
177
178
# File 'lib/utopia/tags/gallery.rb', line 170

def 
	 = @node.local_path(@path + "gallery.yaml")
	
	if File.exist? 
		return YAML::load(File.read())
	else
		return {}
	end
end

#processed_image(image_path, processes) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/utopia/tags/gallery.rb', line 203

def processed_image(image_path, processes)
	# Create the local cache directory if it doesn't exist already
	local_cache_path = @node.local_path(image_path.cache_root)
	
	unless File.exist? local_cache_path
		FileUtils.mkdir local_cache_path
	end
	
	# Calculate the new name for the processed image
	local_original_path = @node.local_path(image_path.original)
	
	if processes.kind_of? String
		processes = processes.split(",").collect{|p| p.split(":")}
	end
	
	processes.each do |process_name, extension|
		process_name = process_name.to_sym
		
		process = PROCESSES[process_name]
		extension ||= process.default_extension(image_path)
		
		image_path.extensions[process_name] = extension if extension
		
		local_processed_path = @node.local_path(image_path.processed(process_name))
		
		unless File.exists? local_processed_path
			image = Magick::ImageList.new(local_original_path)
			image.scene = 0
			
			processed_image = process.call(image)
			processed_image.write(local_processed_path)
			
			# Run GC to free up any memory.
			processed_image = nil
			GC.start if defined? GC
		end
	end
end