| Class | RSS::Converter |
| In: |
rss/converter.rb
|
| Parent: | Object |
# File rss/converter.rb, line 9 def initialize(to_enc, from_enc=nil) normalized_to_enc = to_enc.downcase.gsub(/-/, '_') from_enc ||= 'utf-8' normalized_from_enc = from_enc.downcase.gsub(/-/, '_') if normalized_to_enc == normalized_from_enc def_same_enc() else def_diff_enc = "def_to_#{normalized_to_enc}_from_#{normalized_from_enc}" if respond_to?(def_diff_enc) __send__(def_diff_enc) else def_else_enc(to_enc, from_enc) end end end
# File rss/converter.rb, line 29 def def_convert(depth=0) instance_eval("def convert(value)\nif value.kind_of?(String)\n\#{yield('value')}\nelse\nvalue\nend\nend\n", *get_file_and_line_from_caller(depth)) end
# File rss/converter.rb, line 61 def def_else_enc(to_enc, from_enc) def_iconv_convert(to_enc, from_enc, 0) end
# File rss/converter.rb, line 42 def def_iconv_convert(to_enc, from_enc, depth=0) begin require "iconv" @iconv = Iconv.new(to_enc, from_enc) def_convert(depth+1) do |value| "begin\n@iconv.iconv(\#{value})\nrescue Iconv::Failure\nraise ConversionError.new(\#{value}, \"\#{to_enc}\", \"\#{from_enc}\")\nend\n" end rescue LoadError, ArgumentError, SystemCallError raise UnknownConversionMethodError.new(to_enc, from_enc) end end
# File rss/converter.rb, line 122 def def_to_euc_jp_from_iso_2022_jp require "nkf" def_convert do |value| "NKF.nkf('-Je', #{value})" end end
# File rss/converter.rb, line 108 def def_to_euc_jp_from_shift_jis require "nkf" def_convert do |value| "NKF.nkf('-Se', #{value})" end end
# File rss/converter.rb, line 92 def def_to_euc_jp_from_utf_8 def_uconv_convert_if_can('u8toeuc', 'EUC-JP', 'UTF-8', '-We') end
# File rss/converter.rb, line 129 def def_to_iso_2022_jp_from_euc_jp require "nkf" def_convert do |value| "NKF.nkf('-Ej', #{value})" end end
# File rss/converter.rb, line 142 def def_to_iso_8859_1_from_utf_8 def_convert do |value| "array_utf8 = \#{value}.unpack('U*')\narray_enc = []\narray_utf8.each do |num|\nif num <= 0xFF\narray_enc << num\nelse\narray_enc.concat \"&\\#\\\#{num};\".unpack('C*')\nend\nend\narray_enc.pack('C*')\n" end end
# File rss/converter.rb, line 115 def def_to_shift_jis_from_euc_jp require "nkf" def_convert do |value| "NKF.nkf('-Es', #{value})" end end
# File rss/converter.rb, line 100 def def_to_shift_jis_from_utf_8 def_uconv_convert_if_can('u8tosjis', 'Shift_JIS', 'UTF-8', '-Ws') end
# File rss/converter.rb, line 96 def def_to_utf_8_from_euc_jp def_uconv_convert_if_can('euctou8', 'UTF-8', 'EUC-JP', '-Ew') end
# File rss/converter.rb, line 136 def def_to_utf_8_from_iso_8859_1 def_convert do |value| "#{value}.unpack('C*').pack('U*')" end end
# File rss/converter.rb, line 104 def def_to_utf_8_from_shift_jis def_uconv_convert_if_can('sjistou8', 'UTF-8', 'Shift_JIS', '-Sw') end
# File rss/converter.rb, line 71 def def_uconv_convert_if_can(meth, to_enc, from_enc, nkf_arg) begin require "uconv" def_convert(1) do |value| "begin\nUconv.\#{meth}(\#{value})\nrescue Uconv::Error\nraise ConversionError.new(\#{value}, \"\#{to_enc}\", \"\#{from_enc}\")\nend\n" end rescue LoadError require 'nkf' def_convert(1) do |value| "NKF.nkf(#{nkf_arg.dump}, #{value})" end end end
ruby-doc.org is hosted and run by James Britt and Happy Camper Studios, a Ruby application development company in Phoenix, Arizona. Ruby-doc.org was created in 2002 to promote the Ruby language and to help other Ruby hackers.
Documentation content on ruby-doc.org is provided by remarkable members of the Ruby community.
For more information on the Ruby programming language, visit ruby-lang.org.
Want to help improve Ruby's API docs? See Ruby Documentation Guidelines.