Added Ace::LazyRendering mixin for lazy rendering.
* Just include it and redefine #source_files method.
This commit is contained in:
parent
c2de7c4253
commit
cc36b411c7
2 changed files with 36 additions and 21 deletions
26
lib/ace.rb
26
lib/ace.rb
|
@ -144,29 +144,13 @@ module Ace
|
|||
end
|
||||
end
|
||||
|
||||
def compare_mtime(one, others)
|
||||
File.exist?(one) && File.mtime(one) > others.map { |post| File.mtime(post) }.max
|
||||
end
|
||||
|
||||
def source_files
|
||||
[self.original_path]
|
||||
end
|
||||
|
||||
def fresh?
|
||||
@fresh ||= compare_mtime(self.output_path, self.source_files)
|
||||
end
|
||||
|
||||
def save!
|
||||
if self.fresh?
|
||||
puts "~ [IGNORE] #{self.output_path}"
|
||||
else
|
||||
puts "~ [UPDATE] #{self.output_path}"
|
||||
content = self.render # so filters can influence output_path
|
||||
content = self.render # so filters can influence output_path
|
||||
puts "~ [RENDER] #{self.output_path}"
|
||||
|
||||
FileUtils.mkdir_p File.dirname(self.output_path)
|
||||
File.open(self.output_path, "w") do |file|
|
||||
file.puts(content)
|
||||
end
|
||||
FileUtils.mkdir_p File.dirname(self.output_path)
|
||||
File.open(self.output_path, "w") do |file|
|
||||
file.puts(content)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
31
lib/ace/mixins/lazy.rb
Normal file
31
lib/ace/mixins/lazy.rb
Normal file
|
@ -0,0 +1,31 @@
|
|||
# encoding: utf-8
|
||||
|
||||
# Sometimes rendering is taking too long, so you want to skip
|
||||
# it in case that nothing changed in the files which influence
|
||||
# the output. For example syntax highlighting via Pygments takes
|
||||
# a while, so it's better to include this mixin and redefine
|
||||
# #source_files to include every file which influence the output.
|
||||
module Ace
|
||||
module LazyRendering
|
||||
def compare_mtime(one, others)
|
||||
File.exist?(one) && File.mtime(one) > others.map { |post| File.mtime(post) }.max
|
||||
end
|
||||
|
||||
# @api public
|
||||
def source_files
|
||||
[self.original_path]
|
||||
end
|
||||
|
||||
def fresh?
|
||||
@fresh ||= compare_mtime(self.output_path, self.source_files)
|
||||
end
|
||||
|
||||
def save!
|
||||
if self.fresh?
|
||||
puts "~ [IGNORE] #{self.output_path}"
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue