Fixed ImageThumbnailerFilter, released 0.4.2.

master
Jakub Stastny aka botanicus 2011-06-30 11:12:44 +02:00
parent f53750a820
commit 3d0aa4d91f
3 changed files with 43 additions and 20 deletions

View File

@ -1,5 +1,6 @@
= Version 0.4 = Version 0.4
* Added project generator (ace-gen). * Added project generator (ace-gen).
* Fixed filter for image thumbnails.
= Version 0.3 = Version 0.3
* Added LazyRendering mixin. * Added LazyRendering mixin.

View File

@ -3,37 +3,59 @@
require "ace/filters" require "ace/filters"
require "nokogiri" require "nokogiri"
# <thumbnail src="assets/img/motivation-sheet.jpg" />
# <thumbnail src="assets/img/motivation-sheet.jpg" size="550" />
# <thumbnail src="assets/img/motivation-sheet.jpg" size="550x20" />
# TODO:
# class Post < Ace::Item
# before Ace::ImageThumbnailerFilter, default_thumb_size: 550
# end
module Ace module Ace
class ImageThumbnailerFilter < Filter class ImageThumbnailerFilter < Filter
def thumb_name(filename) def thumb_path(file_name)
filename.gsub(/\.([^\.]*)$/, '_thumb.\1') @file_name ||= file_name
@thumb_path ||= file_name.gsub(/\.([^\.]*)$/, '_thumb.\1')
end end
def thumbnail_nodeset(filename, doc) def thumb_server_path
a = Nokogiri::XML::Node.new 'a', doc @thumb_path.sub("content", "")
img = Nokogiri::XML::Node.new 'img', doc
a.set_attribute('href', filename)
img.set_attribute('src', thumb_name(filename))
img.parent = a
a
end end
def make_thumbnail_image(filename, size) def original_image_server_path
size ||= 20 # default size @file_name.sub("content", "")
cmd = "convert content#{filename} -resize #{size} content#{thumb_name(filename)}" end
warn "~ make thumbnail with '#{cmd}'"
system(cmd) def thumbnail_nodeset(file_name, doc)
raise "Error when converting image 'content#{filename}'" if $?.to_i != 0 link = Nokogiri::XML::Node.new("a", doc)
image = Nokogiri::XML::Node.new("img", doc)
link.set_attribute("href", original_image_server_path)
image.set_attribute("src", thumb_server_path)
image.parent = link
return link
end end
def call(item, content) def call(item, content)
puts item.inspect puts "~ [THUMB] #{item.original_path}"
doc = Nokogiri::HTML(content) doc = Nokogiri::HTML(content)
doc.css("thumbnail").each do |thumb| doc.css("thumbnail").each do |thumb|
make_thumbnail_image thumb[:src], thumb[:size] original_file = "content/#{thumb[:src]}"
thumb.replace thumbnail_nodeset(thumb[:src], doc) generate_thumbnail(original_file, thumb[:size] || 550)
thumb.replace(thumbnail_nodeset(original_file, doc))
end end
doc.to_s doc.to_s
end end
private
def generate_thumbnail(file_name, size)
unless File.exist?(thumb_path(file_name))
command = "convert #{file_name} -resize #{size} #{thumb_path(file_name)}"
warn "~ $ #{command}"
system(command)
raise "Error when converting image '#{file_name}'" if $?.to_i != 0
else
warn "~ File #{thumb_path(file_name)} already exists."
end
end
end end
end end

View File

@ -1,5 +1,5 @@
# encoding: utf-8 # encoding: utf-8
module Ace module Ace
VERSION = "0.4.1" VERSION = "0.4.2"
end end