# $Id: ext_rss.rb,v 1.5 2004/04/19 03:39:55 eto Exp $
# Copyright (C) 2003-2004 Kouichirou Eto, All rights reserved.
# This is free software with ABSOLUTELY NO WARRANTY.
# You can redistribute it and/or modify it under the terms of the GNU GPL2.

require 'qwik/page'
require 'qwik/qxml'

module Qwik
  class Plugin
    def rss
      @g.a(@site.sitename+".rss"){"RSS"}
    end
  end

  class RSSGenerator
    def initialize(config, site)
      @config, @site = config, site
    end

    def get_list()
      @site.pages.date_list()
    end
  end

  class RSS091Generator < RSSGenerator
    def generate
      g = Qxml.new
      channel = g.channel{[
	  g.title{@site.title},
	  g.link{@site.url},
	  g.description{@site.title},
	  g.language{'ja'}
	]}
      get_list.each {|page|
	channel << g.item{[
	    g.title{page.get_title},
	    g.link{@site.url(page.key)},
	    g.description{page.mtime.to_s},
	]}
      }
      doc = REXML::Document.new
      doc << g.rss(:version=>"0.91"){channel}
      doc << REXML::XMLDecl.new(1.0, 'UTF-8')
      doc << REXML::DocType.new('rss', 'PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN" "http://my.netscape.com/publish/formats/rss-0.91.dtd"')
      doc
    end
  end

  class RSS10Generator < RSSGenerator
    def generate
      g = Qxml.new
      rdf = g.make('rdf:RDF')
      rdf.add_attribute("xmlns:rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
      rdf.add_attribute("xmlns", "http://purl.org/rss/1.0/")
      rdf.add_attribute("xmlns:dc", "http://purl.org/dc/elements/1.1/")

      channel = g.channel("rdf:about"=>@site.url){[
	  g.title{@site.title},
	  g.link{@site.url},
	  g.description{@site.title},
	]}
      seq = g.make("rdf:Seq")
      list = get_list
      list.each {|page|
	li = g.make("rdf:li")
	li.add_attribute("rdf:resource", page.url)
	seq << li
      }
      channel << g.items{seq}
      rdf << channel
      list.each {|page|
	item = g.item
	item.add_attribute("rdf:about", page.url)
	item << g.title{page.get_title}
	item << g.link{page.url}
	item << g.make("dc:date"){page.mtime.rfc_date}
	rdf << item
      }
      doc = REXML::Document.new
      doc << rdf
      doc << REXML::XMLDecl.new(1.0, 'UTF-8')
      doc
    end
  end
end
