public
Description: Some more or less useful rake tasks. Includes tasks to work with git-cvs, convert an Atom collection to a blog, post to an AtomPub server and more.
Homepage:
Clone URL: git://github.com/sr/tasks.git
tasks / blogify.rake
100644 46 lines (42 sloc) 1.979 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
namespace :atompub do
  desc 'Convert Atom collection located at URI (use LOGIN and PASS if necessary) using TEMPLATE
and writes generated files to PUB_PATH'
  task :blogify => ['blogify:entries', 'blogify:index']
  namespace :blogify do
    %w(rubygems rake atom/collection atom/http haml).each { |l| require l }
    PUB_PATH = File.expand_path(ENV['PUB_PATH'] || ENV['PWD'])
    TEMPLATE = File.expand_path(ENV['TEMPLATE'] || File.dirname(__FILE__) + '/template.haml')
 
    task :entries => :retrieve_entries do
      template = Haml::Engine.new(File.read(TEMPLATE))
      ENTRIES.each do |month, entries|
        FileUtils.mkdir_p("#{PUB_PATH}/#{entries.first.updated.year}")
        file = "#{PUB_PATH}/#{month}.html"
        unless File.exists?(file) && "#{Time.now.year}/#{Time.now.strftime('%m')}" != month
          File.open(file, 'w') { |f| f << template.render(binding) }
        end
      end
    end
 
    task :index => :entries do
      cp PUB_PATH + "/#{ENTRIES.keys.sort.pop}.html", PUB_PATH + '/index.html'
      archives = '<div id="archives"><ul><li><a href="/2007/12.html">2007/12</a></li>%s</ul></div>' %
        ENTRIES.keys.map{|m|"<li><a href=\"#{m}.html\">#{m}</a></li>"}.join("\n")
      content = File.read(PUB_PATH + '/index.html')
      File.open(PUB_PATH + '/index.html', 'w'){|f| f << content.gsub('</h1>', "</h1>\n"+archives)}
    end
 
    task :retrieve_entries do
      http = Atom::HTTP.new
      if ENV['LOGIN'] && ENV['PASS']
        http.user = ENV['LOGIN']
        http.pass = ENV['PASS']
      end
      collection = Atom::Collection.new(ENV['URI'], http)
      collection.update!
      raise 'collection is empty' if collection.entries.empty?
      collection.entries.sort_by { |e| e.updated }
      ENTRIES = collection.entries.inject({}) do |hash, entry|
        key = "#{entry.updated.year}/#{entry.updated.strftime('%m')}"
        hash.merge({key => (hash[key] || []) << entry})
      end unless defined?(ENTRIES)
    end
  end
end