# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/util-iconv"
require "qwik/htree-format-xml"
require "qwik/htree-generator"

module Qwik
  class Action
    def plg_atom
      [:span, {:class=>"attribute"},
	[:a, {:href=>"atom.xml"}, "ATOM"]]
    end

    def plg_rss
      [:span, {:class=>"attribute"},
	[:a, {:href=>@site.sitename+".rss"}, "RSS"]]
    end

    def ext_xml
      raise unless @req.base == "atom"
      @res["Content-Type"] = "application/xml"
      gen = AtomGenerator.new(@config, @site)
      @res.body = gen.generate
    end

    def ext_rss
      c_require_base_is_sitename

      @res["Content-Type"] = "application/xml"
     #rssgen = RSS10Generator.new(@config, @site)
      rssgen = RSS091Generator.new(@config, @site)
      xml = rssgen.generate
      str = xml.format_xml(0)
      str = str.sjistou8
      @res.body = str
    end
  end

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

    def get_list
      @site.date_list
    end

    def generate
      xml = generate_feed
      str = xml.format_xml
      str = str.sjistou8
      str = '<?xml version="1.0" encoding="utf-8"?>'+str
      str
    end

    def generate_feed
      feed = []
      feed << [:tilte, @site.title]
      feed << [:link, {:rel=>"alternate", :type=>"text/html", :href=>@site.url}]
#     feed << [:modified, @site.get_pages.last_page_time.rfc_date+"Z"]
      feed << [:modified, @site.last_page_time.rfc_date+"Z"]

      get_list.each {|page|
	entry = []
	entry << [:title, page.get_title]
	entry << [:link,
	  {:rel=>"alternate", :type=>"text/html", :href=>page.url}]
	str = page.mtime.rfc_date
#	entry << [:id, "tag:diveintomark.org,2003:3.2397"]
#	entry << [:issued, str]
	entry << [:modified, str+"Z"]
	feed << entry
      }

      [:feed, {:version=>"0.3", :xmlns=>"http://purl.org/atom/ns#"}, feed]
    end
  end

  class RSSGenerator
    def initialize(config, site)
      @config, @site = config, site
      @g = HTree::Generator.new # not HtmlGenerator
    end

    def get_list
      @site.date_list
    end
  end

  class RSS091Generator < RSSGenerator
    def generate
      channel = []
      channel << @g.title{@site.title}
      channel << @g.link{@site.url}
      channel << @g.description{@site.title}
      channel << @g.language{"ja"}
      get_list.each {|page|
	channel << @g.item{[
	    @g.title{page.key},
	    @g.link{@site.url(page.key)},
	    @g.description{page.mtime.to_s},
	]}
      }

      doc = []
      doc << HTree::XMLDecl.new("1.0", "UTF-8")
      doc << HTree::DocType.new("rss", "-//Netscape Communications//DTD RSS 0.91//EN", "http://my.netscape.com/publish/formats/rss-0.91.dtd")
      doc << @g.rss(:version=>"0.91"){@g.channel{channel}}
      HTree::Doc.new(doc)
    end
  end

  class RSS10Generator < RSSGenerator
    def generate
      ar = []
      ar << @g.title{@site.title}
      ar << @g.link{@site.url}
      ar << @g.description{@site.title}
      ar << @g.items{
	@g.make("rdf:Seq"){
	  get_list.map {|page|
	    @g.make("rdf:li", {"rdf:resource"=>page.url})
	  }
	}
      }
      channel = @g.channel("rdf:about"=>@site.url){ar}

      rdf_ar = get_list.map {|page|
	@g.item("rdf:about"=>page.url){[
	    @g.title{page.get_title},
	    @g.link{page.url},
	    @g.make("dc:date"){page.mtime.rfc_date}
	  ]}
      }
      rdf_ar << channel

      rdf = @g.make("rdf:RDF",
		    {"xmlns:rdf"=>"http://www.w3.org/1999/02/22-rdf-syntax-ns#",
		      "xmlns"=>"http://purl.org/rss/1.0/",
		      "xmlns:dc"=>"http://purl.org/dc/elements/1.1/"}){rdf_ar}
      HTree::Doc.new(rdf)
    end
  end
end

if $0 == __FILE__
  require "t/test"
  $debug = true
end

if $debug
  class TC_ActAtom < Test::Unit::TestCase
    include TestSession

    def test_all
      # test_plg_atom
      assert_wiki([:p, [:a, {:href=>"atom.xml"}, "atom.xml"]], "[[atom.xml]]")
      assert_wiki([:p, [:a, {:href=>"atom.xml"}, "a"]], "[[a|atom.xml]]")
      assert_wiki([:span, {:class=>"attribute"},
		    [:a, {:href=>"atom.xml"}, "ATOM"]], "{{atom}}")

      # test_act_xml
      session("/test/atom.xml")
      assert_equal("application/xml", @res["Content-Type"])
      #    p @res.body
    end
  end

  class TC_ActRss < Test::Unit::TestCase
    include TestSession

    def test_all
      # test_plg_rss
      assert_wiki([:span, {:class=>"attribute"},
		    [:a, {:href=>"test.rss"}, "RSS"]], "{{rss}}")
      assert_wiki([:p, [:a, {:href=>"test.rss"}, "test.rss"]], "[[test.rss]]")

      # test_get_rss
      t_add_user
      page = @site.create_new
      page.store("* ‚ ")
      session("/test/test.rss")
      assert_equal("application/xml", @res["Content-Type"])

      session("/test/test.rss") {|req|
	req.cookies.clear
      }
      assert_equal("application/xml", @res["Content-Type"])
    end
  end

  class TC_AtomGenerator < Test::Unit::TestCase
    include TestSession

    def test_get_rss
      session

      page = @site.create_new
      page.store("* ‚ ")
      gen = Qwik::AtomGenerator.new(@config, @site)
      @res.body = gen.generate_feed
      str = @res.body.format_xml
    end
  end

  class TC_RssGenerator < Test::Unit::TestCase
    include TestSession

    def test_rss091
      session

      page = @site.create("1")
      page.store("* ‚ ")
      rssgen = Qwik::RSS091Generator.new(@config, @site)
      @res.body = rssgen.generate
      str = @res.body.format_xml
      assert(str.include?("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"))
      assert(str.include?("<!DOCTYPE rss PUBLIC \"-//Netscape Communications//DTD RSS 0.91//EN\" \"http://my.netscape.com/publish/formats/rss-0.91.dtd\">"))
    end

    def test_rss10
      session

      page = @site.create("1")
      page.store("* ‚ ")
      rssgen = Qwik::RSS10Generator.new(@config, @site)
      @res.body = rssgen.generate
      str = @res.body.format_xml
      assert(str.include?("<rdf:RDF xmlns=\"http://purl.org/rss/1.0/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">"))
    end
  end
end
