lib/ykutils/nkfutil19.rb in ykutils-0.1.0 vs lib/ykutils/nkfutil19.rb in ykutils-0.1.1

- old
+ new

@@ -1,165 +1,169 @@ # -*- coding utf-8 -*- -require 'nkf' +require "nkf" module Ykutils module NKFUTIL - #ASCII-8BIT - #US-ASCII - CODE_TO_NAME = Hash.new("ASCII") - CODE_TO_NAME["US-ASCII"] = "ASCII" - CODE_TO_NAME["stateless-ISO-2022-JP"] = "JIS" - CODE_TO_NAME["ISO-2022-JP"] = "JIS" - CODE_TO_NAME["ISO-2022-JP-2"] = "JIS" - CODE_TO_NAME["EUC-JP"] = "EUC" - CODE_TO_NAME["eucJP-ms"] = "EUC" - CODE_TO_NAME["Shift_JIS"] = "SJIS" - CODE_TO_NAME["Windows-31J"] = "SJIS" - CODE_TO_NAME["ASCII-8BIT"] = "BINARY" - CODE_TO_NAME["UTF-8"] = "UTF8" + # ASCII-8BIT + # US-ASCII + unless CODE_TO_NAME + CODE_TO_NAME = Hash.new("ASCII") + CODE_TO_NAME["US-ASCII"] = "ASCII" + CODE_TO_NAME["stateless-ISO-2022-JP"] = "JIS" + CODE_TO_NAME["ISO-2022-JP"] = "JIS" + CODE_TO_NAME["ISO-2022-JP-2"] = "JIS" + CODE_TO_NAME["EUC-JP"] = "EUC" + CODE_TO_NAME["eucJP-ms"] = "EUC" + CODE_TO_NAME["Shift_JIS"] = "SJIS" + CODE_TO_NAME["Windows-31J"] = "SJIS" + CODE_TO_NAME["ASCII-8BIT"] = "BINARY" + CODE_TO_NAME["UTF-8"] = "UTF8" + end - NAME_TO_ENCODING = Hash.new("US-ASCII") - NAME_TO_ENCODING["UTF8"] = "UTF-8" - NAME_TO_ENCODING["ASCII"] = "US-ASCII" - NAME_TO_ENCODING["JIS"] = "ISO-2022-JP" - NAME_TO_ENCODING["EUC"] = "EUC-JP" - NAME_TO_ENCODING["SJIS"] = "Winodws-31J" - NAME_TO_ENCODING["BINARY"] = "ASCII-8BIT" + begin + NAME_TO_ENCODING + rescue => ex + NAME_TO_ENCODING = Hash.new("US-ASCII") + NAME_TO_ENCODING["UTF8"] = "UTF-8" + NAME_TO_ENCODING["ASCII"] = "US-ASCII" + NAME_TO_ENCODING["JIS"] = "ISO-2022-JP" + NAME_TO_ENCODING["EUC"] = "EUC-JP" + NAME_TO_ENCODING["SJIS"] = "Winodws-31J" + NAME_TO_ENCODING["BINARY"] = "ASCII-8BIT" + end - def NKFUTIL.guess_encoding(str) + def self.guess_encoding(str) puts "**#{str.encoding}" - CODE_TO_NAME[ str.encoding.to_s ] + CODE_TO_NAME[str.encoding.to_s] end - def NKFUTIL.config( src_encoding, dest_encoding , misc_option = nil ) - end + def self.config(src_encoding, dest_encoding, misc_option = nil); end class Assoc @@hs = {} @@config = nil - def Assoc.set( key, value ) - if value - @@hs[key] = Assoc.convert( value ) - else - @@hs[key] = value - end + def self.set(key, value) + @@hs[key] = if value + Assoc.convert(value) + else + value + end end - def Assoc.get( key ) + def self.get(key) @@hs[key] end - def Assoc.to_nkf_encoding_format( encoding ) - NAME_TO_ENCODING[ encoding ] + def self.to_nkf_encoding_format(encoding) + NAME_TO_ENCODING[encoding] end - - def Assoc.config( src_encoding, dest_encoding , misc_option = nil ) + def self.config(_src_encoding, dest_encoding, _misc_option = nil) @@config = dest_encoding end - def Assoc.auto_config_to_inner( str , misc_option = nil ) - if str - src_encoding = Assoc.to_nkf_encoding_format( NKFUTIL.guess_encoding( str ) ) - else - src_encoding = "US-ASCII" - end + def self.auto_config_to_inner(str, _misc_option = nil) + src_encoding = if str + Assoc.to_nkf_encoding_format(NKFUTIL.guess_encoding(str)) + else + "US-ASCII" + end # inner_encoding = Assoc.to_nkf_encoding_format( Assoc.get_inner_encoding ) # if inner_encoding != "US-ASCII" # @@config = inner_encoding @@config = nil # end src_encoding end - def Assoc.auto_config_from_inner( dest_enc , misc_option = nil ) - dest_encoding = Assoc.to_nkf_encoding_format( dest_enc ) + 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 ) # if inner_encoding != "US-ASCII" # @@config = dest_encoding.downcase @@config = dest_encoding # end end - def Assoc.convert( str ) + def self.convert(str) nstr = nil - if str != nil - if @@config != nil + unless str.nil? + if @@config.nil? + nstr = str + else begin # nstr = NKF.nkf( @@config , str ) - nstr = str.encode( @@config ) - rescue => ex - puts ex + nstr = str.encode(@@config) + rescue StandardError => e + puts e puts "=========" pp caller(0) end - else - nstr = str end end nstr end # def Assoc.get_inner_encoding # @@inner_encoding = ($KCODE != "NONE") ? $KCODE : "ASCII" # end end - def NKFUTIL.set( key, value ) - Assoc.set( key , value ) + def self.set(key, value) + Assoc.set(key, value) end - def NKFUTIL.get( key ) - Assoc.get( key ) + def self.get(key) + Assoc.get(key) end - def NKFUTIL.convert( str ) - if str != nil - Assoc.convert( str ) - else + def self.convert(str) + if str.nil? "" + else + Assoc.convert(str) end end - def NKFUTIL.assoc_equal( target , key ) - target == key || target == Assoc.get( key ) + def self.assoc_equal(target, key) + target == key || target == Assoc.get(key) end - def NKFUTIL.config( src_encoding, dest_encoding , misc_option = nil ) - Assoc.config( src_encoding, dest_encoding , misc_option ) - end + def self.config(src_encoding, dest_encoding, misc_option = nil) + Assoc.config(src_encoding, dest_encoding, misc_option) + end - def NKFUTIL.auto_config_to_inner( str, misc_option = nil ) - Assoc.auto_config_to_inner( str , misc_option ) + def self.auto_config_to_inner(str, misc_option = nil) + Assoc.auto_config_to_inner(str, misc_option) end - def NKFUTIL.auto_config_from_inner( dest_encoding , misc_option = nil ) - Assoc.auto_config_to_inner( dest_encoding , misc_option ) - end + def self.auto_config_from_inner(dest_encoding, misc_option = nil) + Assoc.auto_config_to_inner(dest_encoding, misc_option) + end - def puts_sj( line ) - puts line.encod( NAME_TO_ENCODING["SJIS"] ) + def puts_sj(line) + puts line.encod(NAME_TO_ENCODING["SJIS"]) # puts NKF.nkf( "-Ws -m0" , line ) end - def puts_u( line ) - puts line.encod( NAME_TO_ENCODING["UTF8"] ) - puts NKF.nkf( "-Sw -m0" , line ) + def puts_u(line) + puts line.encod(NAME_TO_ENCODING["UTF8"]) + puts NKF.nkf("-Sw -m0", line) end - def nkf_utf8_file( infname , outfname ) - File.open( outfname ){|outf| - File.open( infname ){|file| + def nkf_utf8_file(infname, outfname) + File.open(outfname) do |outf| + File.open(infname) do |file| while line = file.gets line.chomp! # oline = NKF.nkf( "-w -m0" , line ) - oline = line.encode( NAME_TO_ENCODING["UTF8"] ) - outf.printf("%s\n" , oline ) + oline = line.encode(NAME_TO_ENCODING["UTF8"]) + outf.printf("%s\n", oline) end - } - } + end + end end end end