Class: Utopia::Tags::Gallery

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

Defined Under Namespace

Modules: Processes Classes: 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.



144
145
146
147
# File 'lib/utopia/tags/gallery.rb', line 144

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

Class Method Details

.call(transaction, state) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/utopia/tags/gallery.rb', line 221

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

	tag_name = state["tag"] || "img"

	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") 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_text = [path.original.basename]["caption"]
			transaction.tag(tag_name, "src" => path, "alt" => alt_text)
		end
	end
end

Instance Method Details

#images(options = {}) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/utopia/tags/gallery.rb', line 159

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



149
150
151
152
153
154
155
156
157
# File 'lib/utopia/tags/gallery.rb', line 149

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



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/utopia/tags/gallery.rb', line 182

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