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

# $use_charset = true
if (defined? $use_charset) && $use_charset

# $LOAD_PATH.unshift("../../../chise/ruby/lib")
CHISE_DIR = "/chise"
$LOAD_PATH.unshift(CHISE_DIR+"/ruby/lib") unless $LOAD_PATH.include?(CHISE_DIR+"/ruby/lib")
require "chise"
require "chise/ids"
include CHISE

module Qwik
  class Action
    def plg_define_char
      str = yield
      h = {}
      mode = nil
      i = nil
      ts = WikiText.new(str).tokens
      ts.each {|t|
	e = t[:e]
	case e
	when :definition_term_open, :definition_desc_open
	  mode = e
	when :definition_term_close, :definition_desc_close
	  mode = nil
	when :normal_text
	  s = t[:s]
	  if mode == :definition_term_open
	    i = s
	  elsif mode == :definition_desc_open
	    h[i] = s
	  end
	end
      }

      if h["my"]
	er ="&my-"+h["my"]+";"
	c = er.de_er.char
	h.each {|k, v|
	  c[k] = v
	}
      end
      ""
    end

    def my_char_ids(arg)
      arg = arg.to_s
      return "" if arg !~ /^[0-9]+$/
      er ="&my-"+arg+";"
      c = er.de_er.char
      ids = c.ids
      ids.to_s
    end

    def my_char_kage_url
      "http://127.0.0.1:5100/"
    end

    def plg_my_char(arg)
      ids = my_char_ids(arg).sjistou8
      return "" if ids.length == 0
      ids = ids.de_er

      n = ids.gsub(/#0/, IDC_LR).
	gsub(/#1/, IDC_AB).
	gsub(/#2/, IDC_LM).
	gsub(/#3/, IDC_AM).
	gsub(/#4/, IDC_FS).
	gsub(/#5/, IDC_FA).
	gsub(/#6/, IDC_FB).
	gsub(/#7/, IDC_FL).
	gsub(/#8/, IDC_UL).
	gsub(/#9/, IDC_UR).
	gsub(/#a/i, IDC_LL).
	gsub(/#b/i, IDC_OV)

      ids = n
      
      ar = []
      ids.to_a.each {|ch|
	ar << sprintf("u%04x",ch.ucs)
      }
      pngfile = ar.join("")+".gothic.png"
      url = my_char_kage_url
      s = [:img, {:src=>(url+pngfile), :style=>"width:1em;", :alt=>pngfile}]
      s
    end

  end
end

if $0 == __FILE__
  $LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
  require "t/test"
  $debug = true
end

if $debug
# $use_charset = false
#class TC_ActCharset < Test::Unit::TestCase
class TC_ActCharset
  include TestSession

  def test_charset
    n = "CharTest"
    page = @site[n]
    page = @site.create(n) if page.nil?
    page.store("test")
    assert_equal(true, @site.exist?(n))
    assert_wiki("test", "test")
    assert_equal("test", page.load)
    assert_equal("test", @site[n].load)
    assert_wiki("test", page.load)

    page.store(<<"EOT")
{{define_char
:my:1
:ids:#0木森
}}

{{define_char
:my:2
:ids:#0森森
}}
EOT
    assert_wiki("", page.load)
    assert_equal("#0木森", "&my-1;".de_er.char.ids)
    assert_equal("#0森森", "&my-2;".de_er.char.ids)

    url = @action.my_char_kage_url
    page.store("{{my_char(1)}}")
    assert_wiki("<img src='#{url}u2ff0u6728u68ee.gothic.png' style='width:1em;'/>", page.load)
    page.store("&my-1;")
    assert_wiki("<img src='#{url}u2ff0u6728u68ee.gothic.png' style='width:1em;'/>", page.load)
    page.store("{{my_char(2)}}")
    assert_wiki("<img src='#{url}u2ff0u68eeu68ee.gothic.png' style='width:1em;'/>", page.load)

    page.store("{{my_char(a)}}")
    assert_wiki("", page.load)

#    page.store("&my-3;") #そんなの存在していない
#    assert_wiki("<p></p>\n", page.load) #やっぱり中は空と。

    page.store("{{my_char_ids(1)}}")
    assert_wiki("<p>#0木森</p>\n", page.load)
    page.store("{{my_char_ids(2)}}")
    assert_wiki("<p>#0森森</p>\n", page.load)

    @site.delete(n)
    assert_equal(false, @site.exist?(n))
  end

  def test_charset2
    n = "CharTest"
    page = @site[n]
    page = @site.create(n) if page.nil?
    page.store(<<"EOT")
{{define_char
:my:1
:ids:#0木森
}}

{{define_char
:my:2
:ids:#0森森
}}

{{define_char
:my:3
:ids:#1&U-6728;森
}}
EOT
    assert_wiki("", page.load) # eval the _chartest here.
    assert_equal("#0木森", "&my-1;".de_er.char.ids)
    assert_equal("#0森森", "&my-2;".de_er.char.ids)
    assert_equal("木", "&U-6728;".de_er)
#    assert_equal("#1木森", "&my-3;".de_er.char.ids)
#    assert_equal("OV行重", "&my-2;".de_er.char.ids)
    page.store("&my-1;")
    url = @action.my_char_kage_url
    assert_wiki("<img src='#{url}u2ff0u6728u68ee.gothic.png' style='width:1em;'/>", page.load)

    @site.delete(n)
    assert_equal(false, @site.exist?(n))
  end
end

#class TC_ChiseBasic < Test::Unit::TestCase
class TC_ChiseBasic
  include TestSession

  def test_utf8
    @char = "字".su.char
    assert_equal("\273\372", "字".se)
    assert_equal("\345\255\227", "字".su)
    assert_equal("\273\372", "字".su.ue)
    assert_equal("\216\232", "字")
    assert_equal("\273\372", "字".se)
    assert_equal("\345\255\227", "字".su)
    assert_equal("\216\232", "字".su.us)
  end

  def test_er
    @char = "字".su.char
    assert_equal(23383, "字".su.ucs)
    assert_equal(@char, Character.get("&J90-3B7A;"))
    assert_equal(@char, Character.get("&MCS-00005B57;"))
    assert_equal(@char, Character.get("&M-06942;"))
    assert_equal("字", "&J90-3B7A;".de_er.us)
    assert_equal("字", "&U5B57;".de_er.us)
    assert_equal("字", "&U-5B57;".de_er.us)
    assert_equal("字", "&U+5B57;".de_er.us)
    assert_equal("字", "&#x5B57;".de_er.us)
    assert_equal("字", "&#23383;".de_er.us)
  end

  def test_my
    @char = "字".su.char
    # private use area: 0xe000〜0xf8ff
    k = Character.get(0xe001)
    assert_equal(0xe001, k.ucs)
    assert_equal("<\356\200\201,\#xe001>", k.inspect)

    k = "&#xe001;".de_er.char
    assert_equal(0xe001, k.ucs)

    k = "&my-0001;".de_er.char
    assert_equal(0xe001, k.ucs)
    k.ids = IDC_LR+"木森"
    assert_equal(IDC_LR+"木森", k.ids)
    assert_equal(IDC_LR+"木森", "&my-0001;".de_er.ids)
    u = "http://home.fonts.jp:5100/"
    k.kage_url = u
    assert_equal(u, "&my-0001;".de_er.kage_url)

    k = "&my-0002;".de_er.char
    assert_equal(0xe002, k.ucs)
    k.ids = IDC_LR+"森森"
    assert_equal(IDC_LR+"森森", k.ids)
    assert_equal(IDC_LR+"森森", "&my-0002;".de_er.ids)

    "字".eu.mydepth = 1
    assert_equal(1, "字".eu.mydepth)
  end
end

end

end
