Fixed ImageThumbnailerFilter, released 0.4.2.
This commit is contained in:
parent
f53750a820
commit
3d0aa4d91f
3 changed files with 43 additions and 20 deletions
|
@ -1,5 +1,6 @@
|
|||
= Version 0.4
|
||||
* Added project generator (ace-gen).
|
||||
* Fixed filter for image thumbnails.
|
||||
|
||||
= Version 0.3
|
||||
* Added LazyRendering mixin.
|
||||
|
|
|
@ -3,37 +3,59 @@
|
|||
require "ace/filters"
|
||||
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
|
||||
class ImageThumbnailerFilter < Filter
|
||||
def thumb_name(filename)
|
||||
filename.gsub(/\.([^\.]*)$/, '_thumb.\1')
|
||||
def thumb_path(file_name)
|
||||
@file_name ||= file_name
|
||||
@thumb_path ||= file_name.gsub(/\.([^\.]*)$/, '_thumb.\1')
|
||||
end
|
||||
|
||||
def thumbnail_nodeset(filename, doc)
|
||||
a = Nokogiri::XML::Node.new 'a', doc
|
||||
img = Nokogiri::XML::Node.new 'img', doc
|
||||
a.set_attribute('href', filename)
|
||||
img.set_attribute('src', thumb_name(filename))
|
||||
img.parent = a
|
||||
a
|
||||
def thumb_server_path
|
||||
@thumb_path.sub("content", "")
|
||||
end
|
||||
|
||||
def make_thumbnail_image(filename, size)
|
||||
size ||= 20 # default size
|
||||
cmd = "convert content#{filename} -resize #{size} content#{thumb_name(filename)}"
|
||||
warn "~ make thumbnail with '#{cmd}'"
|
||||
system(cmd)
|
||||
raise "Error when converting image 'content#{filename}'" if $?.to_i != 0
|
||||
def original_image_server_path
|
||||
@file_name.sub("content", "")
|
||||
end
|
||||
|
||||
def thumbnail_nodeset(file_name, doc)
|
||||
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
|
||||
|
||||
def call(item, content)
|
||||
puts item.inspect
|
||||
puts "~ [THUMB] #{item.original_path}"
|
||||
doc = Nokogiri::HTML(content)
|
||||
doc.css("thumbnail").each do |thumb|
|
||||
make_thumbnail_image thumb[:src], thumb[:size]
|
||||
thumb.replace thumbnail_nodeset(thumb[:src], doc)
|
||||
original_file = "content/#{thumb[:src]}"
|
||||
generate_thumbnail(original_file, thumb[:size] || 550)
|
||||
thumb.replace(thumbnail_nodeset(original_file, doc))
|
||||
end
|
||||
doc.to_s
|
||||
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
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
# encoding: utf-8
|
||||
|
||||
module Ace
|
||||
VERSION = "0.4.1"
|
||||
VERSION = "0.4.2"
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue