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

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

module Qwik
  class Action
    def plg_interwiki(arg, *d)
      wiki, pagename = arg.split(":")
      return if pagename.nil?
      text = yield
      text = wiki+":"+pagename if text.nil? || text == ""
      iw = @site.interwiki
      href = iw.href(wiki, pagename)

      return [:span, {"class"=>"interwiki"}, text] if href.nil?
      [:a, {"href"=>href, "class"=>"interwiki"}, text]
    end
  end

  class InterWiki
    def initialize(config, site)
      @site = site
      @db = nil
    end

    def href(wiki, pagename)
      iw = db[wiki]
      return nil if iw.nil?
      url, encoding = iw
      case encoding
      when "sjis" then pagename = pagename.escape
      when "euc"  then pagename = pagename.sjistoeuc.escape
      when "utf8" then pagename = pagename.sjistou8.escape
      end
      return url.sub(/\$1/, pagename) if url.index("$1")
      url+pagename
    end

    private

    def db
      return @db if @db
      page = get_page
      @db = page.wikidb
      @db
    end

    def get_page
      k = "InterWikiName"
      return @site[k] if @site.exist?(k)
      k = "_"+k
      return @site[k] if @site.exist?(k)
      return @site.create(k)
    end
  end
end

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

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

    def test_all
      page = @site.create("_InterWikiName")

      # test_interwiki
      page.store(",Test,http://example.com/?q=,sjis")
      assert_wiki([:p, [:a, {"href"=>"http://example.com/?q=t",
			"class"=>"interwiki"}, "Test:t"]], "[[Test:t]]")
      assert_wiki([:a, {"href"=>"http://example.com/?q=t",
		      "class"=>"interwiki"}, "Test:t"], "{{interwiki(Test:t)}}")
      assert_wiki([:a, {"href"=>"http://example.com/?q=t",
		      "class"=>"interwiki"}, "content"],
		  "{{interwiki(Test:t)\ncontent\n}}")
      assert_wiki([:p, [:a, {"href"=>"http://example.com/?q=t",
			"class"=>"interwiki"}, "text"]], "[[text|Test:t]]")
      assert_wiki([:p, [:span, {"class"=>"interwiki"}, "nosuchwiki:t"]],
		  "[[nosuchwiki:t]]")

      # test_interwiki_kanji
      page.store(<<"EOM")
,SJIS,http://example.com/?q=,sjis
,EUC,http://example.com/?q=,euc
,UTF8,http://example.com/?q=,utf8
EOM
      assert_wiki([:p, [:a, {"href"=>"http://example.com/?q=%8E%9A",
			"class"=>"interwiki"}, "SJIS:Žš"]], "[[SJIS:Žš]]")
      assert_wiki([:p, [:a, {"href"=>"http://example.com/?q=%BB%FA",
			"class"=>"interwiki"}, "EUC:Žš"]], "[[EUC:Žš]]")
      assert_wiki([:p, [:a, {"href"=>"http://example.com/?q=%E5%AD%97",
			"class"=>"interwiki"},  "UTF8:Žš"]], "[[UTF8:Žš]]")

      # test_interwiki_realuse
      page.store(<<"EOM")
,google,http://www.google.com/search?num=50&lr=lang_ja&q=,utf8
,isbn,http://www.amazon.co.jp/exec/obidos/ASIN/$1/ref=nosim/q02-22,raw
,amazon,http://www.amazon.co.jp/exec/obidos/external-search?tag=q02-22&keyword=$1&mode=blended,utf8
,hiki,http://www.namaraii.com/hiki/hiki.cgi?,euc
,yukiwiki,http://www.hyuki.com/yukiwiki/wiki.cgi?,euc
EOM
      assert_wiki([:p, [:a, {"href"=>"http://www.google.com/search?num=50&lr=lang_ja&q=%E5%AD%97", "class"=>"interwiki"}, "google:Žš"]], "[[google:Žš]]")
      assert_wiki([:p, [:a, {"href"=>"http://www.google.com/search?num=50&lr=lang_ja&q=%3C", "class"=>"interwiki"}, "google:<"]], "[[google:<]]")
      assert_wiki([:p, [:a, {"href"=>"http://www.amazon.co.jp/exec/obidos/ASIN/4797318325/ref=nosim/q02-22", "class"=>"interwiki"}, "isbn:4797318325"]], "[[isbn:4797318325]]")
      assert_wiki([:p, [:a, {"href"=>"http://www.amazon.co.jp/exec/obidos/external-search?tag=q02-22&keyword=%E5%AD%97&mode=blended", "class"=>"interwiki"}, "amazon:Žš"]], "[[amazon:Žš]]")
      assert_wiki([:p, [:a, {"href"=>"http://www.namaraii.com/hiki/hiki.cgi?%BB%FA", "class"=>"interwiki"}, "hiki:Žš"]], "[[hiki:Žš]]")
      assert_wiki([:p, [:a, {"href"=>"http://www.hyuki.com/yukiwiki/wiki.cgi?%BB%FA", "class"=>"interwiki"}, "yukiwiki:Žš"]], "[[yukiwiki:Žš]]")

      # test_interwiki_error
      assert_wiki([:p, [:span, {"class"=>"interwiki"}, "a:b"]], "[[a:b]]")

      assert_wiki([:p, [:span, {:class=>"new"}, "\"D_R\", \"/v/w\""],
		    [:a, {:href=>".new?t=%22D_R%22%2C+%22%2Fv%2Fw%22",
			:class=>"new"},
		      [:img, {:src=>"/.theme/i/new.png", :class=>"new",
			  :alt=>"create"}]]], '[["D_R", "/v/w"]]')
      assert_wiki([:p], '[["H_R", "h://e.c/"]]')
    end
  end
end
