Commit 8412fd099e772bbd846c919aeb461b84e7c183e7
- Diff rendering mode:
- inline
- side by side
lib/sup/util.rb
(13 / 8)
|   | |||
| 664 | 664 | end | |
| 665 | 665 | ||
| 666 | 666 | class Iconv | |
| 667 | def self.easy_decode target, charset, text | ||
| 668 | return text if charset =~ /^(x-unknown|unknown[-_ ]?8bit|ascii[-_ ]?7[-_ ]?bit)$/i | ||
| 669 | charset = case charset | ||
| 667 | def self.easy_decode target, orig_charset, text | ||
| 668 | if text.respond_to? :force_encoding | ||
| 669 | text = text.dup | ||
| 670 | text.force_encoding Encoding::BINARY | ||
| 671 | end | ||
| 672 | charset = case orig_charset | ||
| 670 | 673 | when /UTF[-_ ]?8/i then "utf-8" | |
| 671 | 674 | when /(iso[-_ ])?latin[-_ ]?1$/i then "ISO-8859-1" | |
| 672 | 675 | when /iso[-_ ]?8859[-_ ]?15/i then 'ISO-8859-15' | |
| 673 | 676 | when /unicode[-_ ]1[-_ ]1[-_ ]utf[-_]7/i then "utf-7" | |
| 674 | else charset | ||
| 677 | when /^euc$/i then 'EUC-JP' # XXX try them all? | ||
| 678 | when /^(x-unknown|unknown[-_ ]?8bit|ascii[-_ ]?7[-_ ]?bit)$/i then 'ASCII' | ||
| 679 | else orig_charset | ||
| 675 | 680 | end | |
| 676 | 681 | ||
| 677 | 682 | begin | |
| 678 | Iconv.iconv(target + "//IGNORE", charset, text + " ").join[0 .. -2] | ||
| 679 | rescue Errno::EINVAL, Iconv::InvalidEncoding, Iconv::InvalidCharacter, Iconv::IllegalSequence => e | ||
| 680 | warn "couldn't transcode text from #{charset} to #{target} (\"#{text[0 ... 20]}\"...) (got #{e.message}); using original as is" | ||
| 681 | text | ||
| 683 | returning(Iconv.iconv(target, charset, text + " ").join[0 .. -2]) { |str| str.check } | ||
| 684 | rescue Errno::EINVAL, Iconv::InvalidEncoding, Iconv::InvalidCharacter, Iconv::IllegalSequence, String::CheckError | ||
| 685 | warn "couldn't transcode text from #{orig_charset} (#{charset}) to #{target}) (#{text[0 ... 20].inspect}...) (got #{$!.message} (#{$!.class}))" | ||
| 686 | text.ascii | ||
| 682 | 687 | end | |
| 683 | 688 | end | |
| 684 | 689 | end |

