Class: Ykutils::NKFUTIL::Assoc

Inherits:
Object
  • Object
show all
Defined in:
lib/ykutils/nkfutil.rb,
lib/ykutils/nkfutil19.rb,
lib/ykutils/nkfutil20.rb

Class Method Summary collapse

Class Method Details

.auto_config_from_inner(dest_enc, _misc_option = nil) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/ykutils/nkfutil.rb', line 64

def self.auto_config_from_inner(dest_enc, misc_option = nil)
  dest_encoding = Assoc.to_nkf_encoding_format(dest_enc)
  inner_encoding = Assoc.to_nkf_encoding_format(Assoc.get_inner_encoding)
  return unless inner_encoding != "A"

  @config = "-#{dest_encoding.downcase}#{inner_encoding.upcase}"
  @config += " #{misc_option}" unless misc_option.nil?
end

.auto_config_to_inner(str, _misc_option = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ykutils/nkfutil.rb', line 48

def self.auto_config_to_inner(str, misc_option = nil)
  src_encoding =
    if str
      Assoc.to_nkf_encoding_format(NKFUTIL.guess_encoding(str))
    else
      "A"
    end

  inner_encoding = Assoc.to_nkf_encoding_format(Assoc.get_inner_encoding)
  if inner_encoding != "A"
    @config = "-#{inner_encoding.downcase}#{src_encoding.upcase}"
    @config += " #{misc_option}" unless misc_option.nil?
  end
  src_encoding
end

.config(_src_encoding, dest_encoding, _misc_option = nil) ⇒ Object



43
44
45
46
# File 'lib/ykutils/nkfutil.rb', line 43

def self.config(src_encoding, dest_encoding, misc_option = nil)
  @config = "-#{dest_encoding.to_s[0, 1].downcase}#{src_encoding.to_s[0, 1].upcase}"
  @config += " #{misc_option}" unless misc_option.nil?
end

.convert(str) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ykutils/nkfutil.rb', line 73

def self.convert(str)
  nstr = nil
  unless str.nil?
    if @config.nil?
      nstr = str
    else
      begin
        nstr = NKF.nkf(@config, str)
      rescue StandardError => e
        puts e
        puts "========="
        pp caller(0)
      end
    end
  end
  nstr
end

.get(key) ⇒ Object



30
31
32
# File 'lib/ykutils/nkfutil.rb', line 30

def self.get(key)
  @hs[key]
end

.inner_encodingObject



91
92
93
# File 'lib/ykutils/nkfutil.rb', line 91

def self.inner_encoding
  @inner_encoding = $KCODE == "NONE" ? "ASCII" : $KCODE
end

.set(key, value) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/ykutils/nkfutil.rb', line 22

def self.set(key, value)
  @hs[key] = if value
               Assoc.convert(value)
             else
               value
             end
end

.to_nkf_encoding_format(encoding) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/ykutils/nkfutil.rb', line 34

def self.to_nkf_encoding_format(encoding)
  case encoding
  when "UTF8"
    "w"
  else
    encoding[0, 1]
  end
end