ext/nkf/lib/kconv.rb


DEFINITIONS

This source file includes following functions.


   1  require 'nkf'
   2  
   3  module Kconv
   4    AUTO = NKF::AUTO
   5    JIS = NKF::JIS
   6    EUC = NKF::EUC
   7    SJIS = NKF::SJIS
   8    BINARY = NKF::BINARY
   9    NOCONV = NKF::NOCONV
  10    UNKNOWN = NKF::UNKNOWN
  11    def kconv(str, out_code, in_code = AUTO)
  12      opt = '-'
  13      case in_code
  14      when NKF::JIS
  15        opt << 'J'
  16      when NKF::EUC
  17        opt << 'E'
  18      when NKF::SJIS
  19        opt << 'S'
  20      end
  21  
  22      case out_code
  23      when NKF::JIS
  24        opt << 'j'
  25      when NKF::EUC
  26        opt << 'e'
  27      when NKF::SJIS
  28        opt << 's'
  29      when NKF::NOCONV
  30        return str
  31      end
  32  
  33      opt = '' if opt == '-'
  34  
  35      NKF::nkf(opt, str)
  36    end
  37    module_function :kconv
  38  
  39    def tojis(str)
  40      NKF::nkf('-j', str)
  41    end
  42    module_function :tojis
  43  
  44    def toeuc(str)
  45      NKF::nkf('-e', str)
  46    end
  47    module_function :toeuc
  48  
  49    def tosjis(str)
  50      NKF::nkf('-s', str)
  51    end
  52    module_function :tosjis
  53  
  54    def guess(str)
  55      NKF::guess(str)
  56    end
  57    module_function :guess
  58  end
  59  
  60  class String
  61    def kconv(out_code, in_code=Kconv::AUTO)
  62      Kconv::kconv(self, out_code, in_code)
  63    end
  64    def tojis
  65      NKF::nkf('-j', self)
  66    end
  67    def toeuc
  68      NKF::nkf('-e', self)
  69    end
  70    def tosjis
  71      NKF::nkf('-s', self)
  72    end
  73  end