lib/ace.rb in ace-0.3.1 vs lib/ace.rb in ace-0.3.2
- old
+ new
@@ -8,10 +8,12 @@
# 5) match the routes, write the files
require "yaml"
require "fileutils"
require "ace/filters/sass"
+require "digest/sha1"
+require "date"
module Ace
module Helpers
# include your helpers here
end
@@ -20,20 +22,28 @@
attr_accessor :path, :metadata, :content
def initialize(path)
@data = File.read(path)
end
+ def check_metadata_created_at(path)
+ if self.metadata[:title]
+ year, month, day = File.basename(path).slice(0,10).split('-')
+ self.metadata[:created_at] ||= Date.new(year.to_i, month.to_i, day.to_i)
+ end
+ end
+
def parse
pieces = @data.split(/^-{3,5}\s*$/)
# if pieces.size < 3
# raise RuntimeError.new(
# "The file '#{path}' appears to start with a metadata section (three or five dashes at the top) but it does not seem to be in the correct format."
# )
# end
# Parse
self.metadata = YAML.load(pieces[1]).inject(Hash.new) { |metadata, pair| metadata.merge(pair[0].to_sym => pair[1]) } || Hash.new
+ # TODO: check metadata[:created_at] and supply it from filename
self.content = pieces[2..-1].join.strip
end
end
# This class represents the items which will be
@@ -133,25 +143,47 @@
end
"#{self.base_url}#{self.server_path}"
end
+ def digest(data)
+ Digest::SHA1.hexdigest(data)
+ end
+
+ def feeds
+ @feeds ||= begin
+ RSSFeed.subclasses.map(&:new)
+ end
+ end
+
attr_writer :output_path
def output_path
@output_path ||= begin
unless self.original_path.nil?
self.original_path.sub("content", "output")
end
end
end
def save!
- content = self.render # so filters can influence output_path
puts "~ [RENDER] #{self.output_path}"
+ content = self.render # so filters can influence output_path
- FileUtils.mkdir_p File.dirname(self.output_path)
- File.open(self.output_path, "w") do |file|
- file.puts(content)
+ begin
+ old_content = File.open(self.output_path, "rb") { |f| f.read }
+ rescue
+ old_content = ''
+ end
+
+ if self.digest(content) != self.digest(old_content)
+ warn "~ CRC isn't same, save new content into #{self.output_path}"
+ # puts old_content.inspect
+ # puts content.inspect
+
+ FileUtils.mkdir_p File.dirname(self.output_path)
+ File.open(self.output_path, "w") do |file|
+ file.puts(content)
+ end
end
end
end
class Asset < Item