summaryrefslogtreecommitdiff
path: root/lib/sitegen.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sitegen.rb')
-rw-r--r--lib/sitegen.rb49
1 files changed, 48 insertions, 1 deletions
diff --git a/lib/sitegen.rb b/lib/sitegen.rb
index c6e89e9..765cfbd 100644
--- a/lib/sitegen.rb
+++ b/lib/sitegen.rb
@@ -1,5 +1,6 @@
# coding: utf-8
require 'date'
+require 'fileutils'
require 'set'
module Sitegen
@@ -60,6 +61,52 @@ module Sitegen
end
def self.generate(target)
- # TODO
+ case
+ when @mk[target].nil?
+ raise "No rule to make target '#{target}'. Stop."
+ when target.end_with?(".atom")
+ write_ifchanged(target) do |file|
+ file.puts('<?xml version="1.0" encoding="utf-8"?>')
+ file.print(@mk[target].atom)
+ end
+ when target.end_with?(".html")
+ write_ifchanged(target) do |file|
+ file.print(@mk[target].html)
+ end
+ else
+ raise "No rule to make target '#{target}'. Stop."
+ end
+ end
+
+ def self.write_ifchanged(outfilename)
+ tmpfilename = "#{File::dirname(outfilename)}/.tmp#{File::basename(outfilename)}"
+
+ # Write our stuff to tmpfile
+ FileUtils::mkdir_p(File::dirname(tmpfilename))
+ tmpfile = File::new(tmpfilename, 'wb')
+ begin
+ yield tmpfile
+ rescue Exception => e
+ tmpfile.close
+ File::unlink(tmpfilename)
+ raise e
+ end
+ tmpfile.close
+
+ # Now see if we should replace outfile with tmpfile
+ same = false
+ begin
+ if FileUtils::compare_file(tmpfilename, outfilename)
+ same = true
+ end
+ rescue Errno::ENOENT
+ end
+
+ # And actually do so
+ if same
+ File::unlink(tmpfilename)
+ else
+ File::rename(tmpfilename, outfilename)
+ end
end
end