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

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")

module Qwik
  class Action
    def plg_date(time=nil)
      time_str = ""
      return unless time.is_a? String
      if /\A[0-9]+\z/ =~ time
	t = Time.at(time.to_i)
	return [:span, {:class=>"date"}, t.strftime("%b #{t.day}, %Y")]
      elsif /\A[-0-9]+\z/ =~ time # 2005-04-19
	return [:span, {:class=>"date"}, time]
      end
      return
    end

    def plg_mail(from="", time=nil)
      str = yield
      h = ""
      h = "{{date("+time.to_s+")}}" if time
      h += " From: "+MailAddress.new(from).obfuscate
      str = MailAddress.obfuscate(str)
      msg = "* "+h+"\n"+str
      c_pre_text(msg)
    end

    def plg_include(pagename)
      page = @site[pagename.to_s]
      return "" if page.nil?
      str = page.load

      org_base = @req.base
      @req.base = pagename

      w = c_res(str)
      tres = TDiaryResolver.new(@config, @site, self)
      w = tres.resolve(w)

      @req.base = org_base
      w
    end

    def plg_text(str="")
      str = "" if str.nil?
      if block_given?
	str2 = yield
	str += str2 if str2
      end
      c_res(str)
    end

  end
end

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

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

    def test_date
      assert_wiki("<span class=\"date\">2001-01-01</span>",
		  "{{date(2001-01-01)}}")
      assert_wiki("<span class=\"date\">Jan 1, 1970</span>",
		  "{{date(0)}}")
      assert_wiki("<span class=\"date\">2001-01-01</span>",
		  "{{date(2001-01-01)}}")
      assert_wiki("", "{{date(a)}}")
    end
    def assert_day(e, w, user=DEFAULT_USER)
      assert_path(e, w, user, "//div[@class='day']")
    end

    def assert_body_main(e, w, user=DEFAULT_USER)
     #assert_path(e, w, user, "//div[@id='body']")
      assert_path(e, w, user, "//div[@class='body_main']")
    end

    def assert(e, wikitext)
      assert_body_main(e, wikitext)
    end

    def test_text
      assert_day([[:h2, {:id=>"8f03c3a6dbec1d0f1a5af60947b7b052"}, "궇"],
		   [:div, {:class=>"body"}, [:div, {:class=>"section"}, []]]],
		 "{{text(* 궇)}}")
      assert_day([[:h2, {:id=>"7657b04993b557a2ee9b36bf280a3ec4"}, "궋"],
		   [:div, {:class=>"body"}, [:div, {:class=>"section"}, []]]],
		 "{{text\n* 궋\n}}")
      assert_wiki("<p>&lt;</p>", "{{text(<)}}")
      @site["1"].delete
      assert_wiki("<p>&lt;</p>", "{{text\n<\n}}")
      @site["1"].delete
      @site.erase_all
    end

    def test_mail
      assert([:div, {:class=>"day"}, [:h2, {:id=>"From"}, "From:"],
	       [:div, {:class=>"body"},
		 [:div, {:class=>"section"}, [[:p, "a"]]]]],
	     "{{mail\na\n}}")
      assert([:div, {:class=>"day"}, [:h2, {:id=>"From"}, "From:"],
	       [:div, {:class=>"body"},
		 [:div, {:class=>"section"}, [[:p, "긡긚긣궳궥갃"]]]]],
	     "* 긡긚긣\n{{mail\n긡긚긣궳궥갃\n\n}}")
      assert([:div, {:class=>"day"},
	       [:h2, {:id=>"979dff15c789fca0315256fc8af6fa64"}, "From: t@e..."],
	       [:div, {:class=>"body"},
		 [:div, {:class=>"section"}, [[:p, "긚"]]]]],
	     "* 긡\n{{mail(t@e.com)\n긚\n\n}}")
      assert([:div, {:class=>"day"},
	       [:h2, {:id=>"979dff15c789fca0315256fc8af6fa64"},
		 [:span, {:class=>"date"}, "Jan 1, 1970"], " From: t@e..."],
	       [:div, {:class=>"body"},
		 [:div, {:class=>"section"}, [[:p, "긚"]]]]],
	     "* 긡\n{{mail(t@e.com,0)\n긚\n\n}}")
      assert([:div, {:class=>"day"},
	       [:h2, {:id=>"979dff15c789fca0315256fc8af6fa64"}, "From: t@e..."],
	       [:div, {:class=>"body"},
		 [:div, {:class=>"section"}, [[:p, "t@e..."]]]]],
	     "* 긡\n{{mail(t@e.com)\nt@e.com\n\n}}")

      teardown

      # test_include
      page = @site.create_new
      page.store("* t")
      page2 = @site.create_new
      page2.store("* t2")
      key = page2.key
      assert_day(["",
		   [:div, {:class=>"body"},
		     [:div, {:class=>"section"},
		       [[:div,
			   {:class=>"day"},
			   [:h2, {:id=>"t2"}, "t2"],
			   [:div, {:class=>"body"},
			     [:div, {:class=>"section"}, []]]]]]]],
		 "{{include(#{key})}}")

      assert_body_main([:div,
		       {:class=>"day"},
		       "",
		       [:div,
			 {:class=>"body"},
			 [:div,
			   {:class=>"section"},
			   [[:div,
			       {:class=>"day"},
			       [:h2, {:id=>"t2"}, "t2"],
			       [:div, {:class=>"body"},
				 [:div, {:class=>"section"}, []]]]]]]],
		     "{{include(#{key})}}")

      config = @site["_SiteConfig"]
      config.store(":titlelink:true")
      assert_body_main("<div class=\"day\"><div class=\"body\"><div class=\"section\"><div class=\"day\"><h2><a class=\"label\" href=\"2.html#t2\" name=\"t2\">걾</a>t2</h2><div class=\"body\"><div class=\"section\"></div><!--section--></div><!--body--></div><!--day--></div><!--section--></div><!--body--></div><!--day-->",
		     "{{include(#{key})}}")

      config.store(":emode_titlelink:true")
      assert_body_main("<div class=\"day\"><div class=\"body\"><div class=\"section\"><div class=\"day\"><h2><a class=\"label\" href=\"2.html#t2\" name=\"t2\">걾</a>t2</h2><div class=\"body\"><div class=\"section\"></div><!--section--></div><!--body--></div><!--day--></div><!--section--></div><!--body--></div><!--day-->",
		     "{{include(#{key})}}")

      config.store("")

      # test_include_with_titlelink
      t_add_user

      page = @site.create("t1")
      page.store("* t1\n{{include(t2)}}")
      page2 = @site.create("t2")
      page2.store("* t2!")

      session("/test/t1.html")
      assert_text("t1", "title")
      assert_xml("t2!", "//div[@class='day']/h2")

      config = @site["_SiteConfig"]
      config.store(":titlelink:true")
      session("/test/t1.html")
      assert_xml([[:a, {:href=>"t2.html#6b9e49fa28900683969f489ac35161e6",
		       :name=>"6b9e49fa28900683969f489ac35161e6",
		       :class=>"label"}, "걾"], "t2!"],
		 "//div[@class='day']/h2")

      config = @site["_SiteConfig"]
      config.store(":emode_titlelink:true")
      session("/test/t1.html")
      assert_xml([[:a, {:href=>"t2.html#Xb_M6_lBLMaGGH88jpd-rQ",
		       :name=>"Xb_M6_lBLMaGGH88jpd-rQ",
		       :class=>"label"}, "걾"], "t2!"],
		 "//div[@class='day']/h2")
    end
  end
end
