# $Id: ext_charset.rb,v 1.1 2004/04/03 05:24:26 eto Exp $
# Copyright (C) 2003-2004 Kouichirou Eto, All rights reserved.
# This is free software with ABSOLUTELY NO WARRANTY.
# You can redistribute it and/or modify it under the terms of the GNU GPL2.

# $use_charset = true
if $use_charset

$LOAD_PATH.unshift('../../../chise/ruby/lib')
require 'chise'
require 'chise/ids'
include CHISE

module Qwik
  class Plugin
    def define_char()
      str = yield
      h = {}
      mode = nil
      i = nil
      ts = str.w.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 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 = @g.img(:src=>"#{url}#{pngfile}", :style=>"width:1em;")
      s
    end

  end
end

end
