Module: Md2site::Nkfguess

Defined in:
lib/md2site/nkfguess.rb

Overview

NKF利用のエンコーディング推測モジュール

Class Method Summary collapse

Class Method Details

.guess_file(fname, mes) ⇒ String

エンコーディング推測

Parameters:

  • fname (String)

    エンコーディング推測対象ファイル名

  • mes (Messagex)

    Messagexクラスのインスタンス

Returns:

  • (String)

    “ISO-2022-JP”, “eucJP”, “CP932”, “UTF-8”, “ASCII-8BIT”



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/md2site/nkfguess.rb', line 13

def self.guess_file(fname, mes)
  str = nil
  mes.exc_file_read(fname) { File.open(fname) {|f| str = f.gets(nil) } }
  if str.nil?
    encoding = nil
  else
    case NKF.guess(str)
    when NKF::JIS
      encoding = "ISO-2022-JP"
    when NKF::EUC
      encoding = "eucJP"
    when NKF::SJIS
      encoding = "CP932"
    when NKF::UTF8
      encoding = "UTF-8"
    when NKF::BINARY
      encoding = "ASCII-8BIT"
    when NKF::ASCII
      encoding = "ASCII-8BIT"
    when NKF::UNKNOWN
      encoding = "ASCII-8BIT"
    else
      encoding = "ASCII-8BIT"
    end
  end
  encoding
end