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

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/template"
require "qwik/wabisabi-get"
require "qwik/generator-module"

module Qwik
  class PlainPageGenerator
    include GeneratorModule

    def self.generate(template, title, msg)
      w = template.get_tag("head")

      # insert title
      w.insert(1, [:title, title])

      # insert JavaScript
      js = GeneratorModule.generate_js("/")
      w.insert(w.length, *js)

      # insert meta
      w << [:meta, {:name=>"ROBOTS", :content=>"NOINDEX,NOFOLLOW"}]

      # insert h1
      w = template.get_tag("h1")
      w << title

      # insert main
      w = template.get_by_class("main")
      w << msg

      template
    end
  end
end

if $0 == __FILE__
  require "pp"
  require "test/unit"
  require "qwik/config"
  require "qwik/memory"
  $debug = true
end

if $debug
  class TC_PlainPageGenerator < Test::Unit::TestCase
    def test_all
      # setup config and memory
      @config = defined?($test_config) ? $test_config : Qwik::Config.new(true)
      @memory = defined?($test_memory) ? $test_memory :
	Qwik::ServerMemory.new(@config)

      template = @memory.template.get("plain")
      assert_equal([:h1], template.get_tag("h1"))
      assert_equal([:div, {:class=>"main"}],
		   template.get_by_class("main"))

      res = Qwik::PlainPageGenerator.generate(template, "title", "msg")
      assert_equal([:title, "title"], res.get_tag("title"))
      assert_equal([:script, {:src=>"/.theme/js/base.js",
		       :type=>"text/javascript"}, ""], res.get_tag("script"))
      assert_equal([:meta, {:content=>"NOINDEX,NOFOLLOW", :name=>"ROBOTS"}],
		   res.get_tag("meta"))
      assert_equal([:h1, "title"], res.get_tag("h1"))
      assert_equal([:div, {:class=>"main"}, "msg"],
		   res.get_path("//div[@class='main']"))
      assert_equal(nil, res.get_tag("meta[2]")) # not redirected
    end
  end
end
