diff --git a/bin/ace b/bin/ace index 7c080f9..9b84756 100755 --- a/bin/ace +++ b/bin/ace @@ -43,12 +43,11 @@ rules.rules.each do |klass, files| if File.binread(file).match(/^-{3,5}\s*$/) # TODO: this should be a filter or lazy-loaded puts "~ Read #{file} with parse" raw_item = Ace::RawItem.new(file).tap(&:parse) - item = klass.create(raw_item.metadata, raw_item.content) + item = klass.create(raw_item.metadata, raw_item.content, file) else puts "~ Read #{file} without parse" - item = klass.create(Hash.new, File.read(file)) + item = klass.create(Hash.new, File.read(file), file) end - item.original_path = file end end diff --git a/lib/ace.rb b/lib/ace.rb index a506ca8..da0bf65 100644 --- a/lib/ace.rb +++ b/lib/ace.rb @@ -37,6 +37,11 @@ module Ace self.content = pieces[2..-1].join.strip end + set_timestamps_in_metadata + end + + private + def set_timestamps_in_metadata self.metadata[:created_at] ||= File.ctime(self.path) self.metadata[:updated_at] ||= File.mtime(self.path) end @@ -89,16 +94,17 @@ module Ace self.after_filters << filter.new(*args) end - def self.create(metadata, content) - self.new(metadata, content).tap(&:register) + def self.create(*args) + self.new(*args).tap(&:register) end # Content can be anything, not just a string. attr_accessor :metadata, :content attr_accessor :original_path - def initialize(metadata, content) - @metadata = metadata - @content = content + def initialize(metadata, content, original_path) + @metadata = metadata + @content = content + @original_path = original_path end def config diff --git a/lib/ace/filters/image_thumbnailer.rb b/lib/ace/filters/image_thumbnailer.rb index 1f049e4..9b66317 100644 --- a/lib/ace/filters/image_thumbnailer.rb +++ b/lib/ace/filters/image_thumbnailer.rb @@ -3,9 +3,9 @@ require "ace/filters" require "nokogiri" -# -# -# +# +# +# # TODO: # class Post < Ace::Item @@ -14,24 +14,15 @@ require "nokogiri" module Ace class ImageThumbnailerFilter < Filter - def thumb_path(file_name) - @file_name ||= file_name - @thumb_path ||= file_name.gsub(/content\/(.+)\.([^\.]*)$/, 'output/\1_thumb.\2') + def to_thumb(path) + path.to_s.sub(/\.(\w+)$/, '_thumb.\1') end - def thumb_server_path - @thumb_path.sub("content", "") - end - - def original_image_server_path - @file_name.sub("content", "") - end - - def thumbnail_nodeset(file_name, doc) + def thumbnail_nodeset(link, 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) + link.set_attribute("href", link) + image.set_attribute("src", to_thumb(link)) image.parent = link return link end @@ -40,22 +31,23 @@ module Ace puts "~ [THUMB] #{item.original_path}" doc = Nokogiri::HTML(content) doc.css("thumbnail").each do |thumb| - original_file = "content/#{thumb[:src]}" - generate_thumbnail(original_file, thumb[:size] || 550) - thumb.replace(thumbnail_nodeset(original_file, doc)) + original_image_path = "content" + thumb[:src] + thumbnail_path = to_thumb("output" + thumb[:src]) + generate_thumbnail(original_image_path, thumbnail_path, thumb[:src], thumb[:size] || 550) + thumb.replace(thumbnail_nodeset(thumb[:src], 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)}" + def generate_thumbnail(original_path, thumbnail_path, link, size) + unless File.exist?(thumbnail_path) + command = "convert #{original_path} -resize #{size} #{thumbnail_path}" warn "~ $ #{command}" system(command) - raise "Error when converting image '#{file_name}'" if $?.to_i != 0 + raise "Error when converting image '#{original_path}'" if $?.to_i != 0 else - warn "~ File #{thumb_path(file_name)} already exists." + warn "~ File #{thumbnail_path} already exists." end end end