2004-01-27 22:46:13 -05:00
|
|
|
require "rss/utils"
|
|
|
|
|
|
|
|
module RSS
|
|
|
|
|
|
|
|
class Converter
|
|
|
|
|
|
|
|
include Utils
|
|
|
|
|
|
|
|
def initialize(to_enc, from_enc=nil)
|
|
|
|
to_enc = to_enc.downcase.gsub(/-/, '_')
|
|
|
|
from_enc ||= 'utf-8'
|
|
|
|
from_enc = from_enc.downcase.gsub(/-/, '_')
|
|
|
|
if to_enc == from_enc
|
|
|
|
def_same_enc()
|
|
|
|
else
|
|
|
|
if respond_to?("def_to_#{to_enc}_from_#{from_enc}")
|
|
|
|
send("def_to_#{to_enc}_from_#{from_enc}")
|
|
|
|
else
|
|
|
|
def_else_enc(to_enc, from_enc)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def convert(value)
|
|
|
|
value
|
|
|
|
end
|
|
|
|
|
|
|
|
def def_convert()
|
|
|
|
instance_eval(<<-EOC, *get_file_and_line_from_caller(0))
|
|
|
|
def convert(value)
|
|
|
|
if value.kind_of?(String)
|
|
|
|
#{yield('value')}
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
EOC
|
|
|
|
end
|
|
|
|
|
|
|
|
def def_iconv_convert(to_enc, from_enc)
|
|
|
|
begin
|
|
|
|
require "iconv"
|
|
|
|
def_convert do |value|
|
|
|
|
<<-EOC
|
|
|
|
@iconv ||= Iconv.new("#{to_enc}", "#{from_enc}")
|
|
|
|
begin
|
|
|
|
@iconv.iconv(#{value})
|
|
|
|
rescue Iconv::Failure
|
|
|
|
raise ConversionError.new(#{value}, "#{to_enc}", "#{from_enc}")
|
|
|
|
end
|
2004-03-02 11:21:11 -05:00
|
|
|
EOC
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
rescue LoadError, ArgumentError, SystemCallError
|
|
|
|
raise UnknownConversionMethodError.new(to_enc, from_enc)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def def_else_enc(to_enc, from_enc)
|
|
|
|
raise UnknownConversionMethodError.new(to_enc, from_enc)
|
|
|
|
end
|
|
|
|
|
|
|
|
def def_same_enc()
|
|
|
|
def_convert do |value|
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-03-02 11:21:11 -05:00
|
|
|
def def_uconv_convert_if_can(meth, to_enc, from_enc)
|
2004-01-27 22:46:13 -05:00
|
|
|
begin
|
|
|
|
require "uconv"
|
|
|
|
def_convert do |value|
|
2004-03-02 11:21:11 -05:00
|
|
|
<<-EOC
|
|
|
|
begin
|
|
|
|
Uconv.#{meth}(#{value})
|
|
|
|
rescue Uconv::Error
|
|
|
|
raise ConversionError.new(#{value}, "#{to_enc}", "#{from_enc}")
|
|
|
|
end
|
|
|
|
EOC
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
rescue LoadError
|
2004-03-02 11:21:11 -05:00
|
|
|
def_iconv_convert(to_enc, from_enc)
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
end
|
2004-03-02 11:21:11 -05:00
|
|
|
|
|
|
|
def def_to_euc_jp_from_utf_8
|
|
|
|
def_uconv_convert_if_can('u8toeuc', 'EUC-JP', 'UTF-8')
|
|
|
|
end
|
2004-01-27 22:46:13 -05:00
|
|
|
|
|
|
|
def def_to_utf_8_from_euc_jp
|
2004-03-02 11:21:11 -05:00
|
|
|
def_uconv_convert_if_can('euctou8', 'UTF-8', 'EUC-JP')
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def def_to_shift_jis_from_utf_8
|
2004-03-02 11:21:11 -05:00
|
|
|
def_uconv_convert_if_can('u8tosjis', 'Shift_JIS', 'UTF-8')
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def def_to_utf_8_from_shift_jis
|
2004-03-02 11:21:11 -05:00
|
|
|
def_uconv_convert_if_can('sjistou8', 'UTF-8', 'Shift_JIS')
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def def_to_euc_jp_from_shift_jis
|
2004-02-03 09:01:42 -05:00
|
|
|
require "nkf"
|
2004-01-27 22:46:13 -05:00
|
|
|
def_convert do |value|
|
|
|
|
"NKF.nkf('-Se', #{value})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def def_to_shift_jis_from_euc_jp
|
2004-02-03 09:01:42 -05:00
|
|
|
require "nkf"
|
2004-01-27 22:46:13 -05:00
|
|
|
def_convert do |value|
|
|
|
|
"NKF.nkf('-Es', #{value})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def def_to_euc_jp_from_iso_2022_jp
|
2004-02-03 09:01:42 -05:00
|
|
|
require "nkf"
|
2004-01-27 22:46:13 -05:00
|
|
|
def_convert do |value|
|
|
|
|
"NKF.nkf('-Je', #{value})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def def_to_iso_2022_jp_from_euc_jp
|
2004-02-03 09:01:42 -05:00
|
|
|
require "nkf"
|
2004-01-27 22:46:13 -05:00
|
|
|
def_convert do |value|
|
|
|
|
"NKF.nkf('-Ej', #{value})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def def_to_utf_8_from_iso_8859_1
|
|
|
|
def_convert do |value|
|
|
|
|
"#{value}.unpack('C*').pack('U*')"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def def_to_iso_8859_1_from_utf_8
|
|
|
|
def_convert do |value|
|
|
|
|
<<-EOC
|
|
|
|
array_utf8 = #{value}.unpack('U*')
|
|
|
|
array_enc = []
|
|
|
|
array_utf8.each do |num|
|
|
|
|
if num <= 0xFF
|
|
|
|
array_enc << num
|
|
|
|
else
|
|
|
|
array_enc.concat "&\#\#{num};".unpack('C*')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
array_enc.pack('C*')
|
2004-03-02 11:21:11 -05:00
|
|
|
EOC
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|