Fix the thumbnailer.
This commit is contained in:
parent
58321ef82b
commit
96fa313ccc
3 changed files with 30 additions and 33 deletions
5
bin/ace
5
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
|
if File.binread(file).match(/^-{3,5}\s*$/) # TODO: this should be a filter or lazy-loaded
|
||||||
puts "~ Read #{file} with parse"
|
puts "~ Read #{file} with parse"
|
||||||
raw_item = Ace::RawItem.new(file).tap(&: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
|
else
|
||||||
puts "~ Read #{file} without parse"
|
puts "~ Read #{file} without parse"
|
||||||
item = klass.create(Hash.new, File.read(file))
|
item = klass.create(Hash.new, File.read(file), file)
|
||||||
end
|
end
|
||||||
item.original_path = file
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
12
lib/ace.rb
12
lib/ace.rb
|
@ -37,6 +37,11 @@ module Ace
|
||||||
self.content = pieces[2..-1].join.strip
|
self.content = pieces[2..-1].join.strip
|
||||||
end
|
end
|
||||||
|
|
||||||
|
set_timestamps_in_metadata
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def set_timestamps_in_metadata
|
||||||
self.metadata[:created_at] ||= File.ctime(self.path)
|
self.metadata[:created_at] ||= File.ctime(self.path)
|
||||||
self.metadata[:updated_at] ||= File.mtime(self.path)
|
self.metadata[:updated_at] ||= File.mtime(self.path)
|
||||||
end
|
end
|
||||||
|
@ -89,16 +94,17 @@ module Ace
|
||||||
self.after_filters << filter.new(*args)
|
self.after_filters << filter.new(*args)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.create(metadata, content)
|
def self.create(*args)
|
||||||
self.new(metadata, content).tap(&:register)
|
self.new(*args).tap(&:register)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Content can be anything, not just a string.
|
# Content can be anything, not just a string.
|
||||||
attr_accessor :metadata, :content
|
attr_accessor :metadata, :content
|
||||||
attr_accessor :original_path
|
attr_accessor :original_path
|
||||||
def initialize(metadata, content)
|
def initialize(metadata, content, original_path)
|
||||||
@metadata = metadata
|
@metadata = metadata
|
||||||
@content = content
|
@content = content
|
||||||
|
@original_path = original_path
|
||||||
end
|
end
|
||||||
|
|
||||||
def config
|
def config
|
||||||
|
|
|
@ -3,9 +3,9 @@
|
||||||
require "ace/filters"
|
require "ace/filters"
|
||||||
require "nokogiri"
|
require "nokogiri"
|
||||||
|
|
||||||
# <thumbnail src="assets/img/motivation-sheet.jpg" />
|
# <thumbnail src="/assets/img/motivation-sheet.jpg" />
|
||||||
# <thumbnail src="assets/img/motivation-sheet.jpg" size="550" />
|
# <thumbnail src="/assets/img/motivation-sheet.jpg" size="550" />
|
||||||
# <thumbnail src="assets/img/motivation-sheet.jpg" size="550x20" />
|
# <thumbnail src="/assets/img/motivation-sheet.jpg" size="550x20" />
|
||||||
|
|
||||||
# TODO:
|
# TODO:
|
||||||
# class Post < Ace::Item
|
# class Post < Ace::Item
|
||||||
|
@ -14,24 +14,15 @@ require "nokogiri"
|
||||||
|
|
||||||
module Ace
|
module Ace
|
||||||
class ImageThumbnailerFilter < Filter
|
class ImageThumbnailerFilter < Filter
|
||||||
def thumb_path(file_name)
|
def to_thumb(path)
|
||||||
@file_name ||= file_name
|
path.to_s.sub(/\.(\w+)$/, '_thumb.\1')
|
||||||
@thumb_path ||= file_name.gsub(/content\/(.+)\.([^\.]*)$/, 'output/\1_thumb.\2')
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def thumb_server_path
|
def thumbnail_nodeset(link, doc)
|
||||||
@thumb_path.sub("content", "")
|
|
||||||
end
|
|
||||||
|
|
||||||
def original_image_server_path
|
|
||||||
@file_name.sub("content", "")
|
|
||||||
end
|
|
||||||
|
|
||||||
def thumbnail_nodeset(file_name, doc)
|
|
||||||
link = Nokogiri::XML::Node.new("a", doc)
|
link = Nokogiri::XML::Node.new("a", doc)
|
||||||
image = Nokogiri::XML::Node.new("img", doc)
|
image = Nokogiri::XML::Node.new("img", doc)
|
||||||
link.set_attribute("href", original_image_server_path)
|
link.set_attribute("href", link)
|
||||||
image.set_attribute("src", thumb_server_path)
|
image.set_attribute("src", to_thumb(link))
|
||||||
image.parent = link
|
image.parent = link
|
||||||
return link
|
return link
|
||||||
end
|
end
|
||||||
|
@ -40,22 +31,23 @@ module Ace
|
||||||
puts "~ [THUMB] #{item.original_path}"
|
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|
|
||||||
original_file = "content/#{thumb[:src]}"
|
original_image_path = "content" + thumb[:src]
|
||||||
generate_thumbnail(original_file, thumb[:size] || 550)
|
thumbnail_path = to_thumb("output" + thumb[:src])
|
||||||
thumb.replace(thumbnail_nodeset(original_file, doc))
|
generate_thumbnail(original_image_path, thumbnail_path, thumb[:src], thumb[:size] || 550)
|
||||||
|
thumb.replace(thumbnail_nodeset(thumb[:src], doc))
|
||||||
end
|
end
|
||||||
doc.to_s
|
doc.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def generate_thumbnail(file_name, size)
|
def generate_thumbnail(original_path, thumbnail_path, link, size)
|
||||||
unless File.exist?(thumb_path(file_name))
|
unless File.exist?(thumbnail_path)
|
||||||
command = "convert #{file_name} -resize #{size} #{thumb_path(file_name)}"
|
command = "convert #{original_path} -resize #{size} #{thumbnail_path}"
|
||||||
warn "~ $ #{command}"
|
warn "~ $ #{command}"
|
||||||
system(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
|
else
|
||||||
warn "~ File #{thumb_path(file_name)} already exists."
|
warn "~ File #{thumbnail_path} already exists."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue