|
Revision 14973, 1.3 kB
(checked in by hsbt, 5 months ago)
|
|
add migrate_to_utf8.
|
| Line | |
|---|
| 1 | # |
|---|
| 2 | # convert utf-8 in pstore. |
|---|
| 3 | # |
|---|
| 4 | # usage: convert_pstore.rb file1 |
|---|
| 5 | # |
|---|
| 6 | |
|---|
| 7 | $KCODE = 'u' |
|---|
| 8 | |
|---|
| 9 | require 'nkf' |
|---|
| 10 | require "pstore" |
|---|
| 11 | begin |
|---|
| 12 | require "iconv" |
|---|
| 13 | rescue LoadError |
|---|
| 14 | end |
|---|
| 15 | |
|---|
| 16 | def convert_pstore( file ) |
|---|
| 17 | db = PStore.new( file ) |
|---|
| 18 | begin |
|---|
| 19 | roots = db.transaction{ db.roots } |
|---|
| 20 | rescue ArgumentError |
|---|
| 21 | if /\Aundefined class\/module (.+?)(::)?\z/ =~ $!.message |
|---|
| 22 | klass = $1 |
|---|
| 23 | if /EmptdiaryString\z/ =~ klass |
|---|
| 24 | eval( "class #{klass} < String; end" ) |
|---|
| 25 | else |
|---|
| 26 | eval( "class #{ klass}; end" ) |
|---|
| 27 | end |
|---|
| 28 | retry |
|---|
| 29 | end |
|---|
| 30 | end |
|---|
| 31 | db.transaction do |
|---|
| 32 | roots.each do |root| |
|---|
| 33 | convert_element( db[root] ) |
|---|
| 34 | end |
|---|
| 35 | end |
|---|
| 36 | end |
|---|
| 37 | |
|---|
| 38 | def convert_element( data ) |
|---|
| 39 | case data |
|---|
| 40 | when Hash, Array |
|---|
| 41 | data.each_with_index do |e, i| |
|---|
| 42 | if String === e |
|---|
| 43 | data[i] = migrate_to_utf8( e ) |
|---|
| 44 | else |
|---|
| 45 | convert_element( e ) |
|---|
| 46 | end |
|---|
| 47 | end |
|---|
| 48 | else |
|---|
| 49 | data.instance_variables.each do |e| |
|---|
| 50 | var = data.instance_variable_get( e ) |
|---|
| 51 | if String === var |
|---|
| 52 | data.instance_variable_set( e, migrate_to_utf8( var ) ) |
|---|
| 53 | else |
|---|
| 54 | convert_element( var ) |
|---|
| 55 | end |
|---|
| 56 | end |
|---|
| 57 | end |
|---|
| 58 | end |
|---|
| 59 | |
|---|
| 60 | def migrate_to_utf8( str ) |
|---|
| 61 | to_native( str, 'EUC-JP' ) |
|---|
| 62 | end |
|---|
| 63 | |
|---|
| 64 | def to_native( str, charset = nil ) |
|---|
| 65 | begin |
|---|
| 66 | Iconv.conv('utf-8', charset || 'utf-8', str) |
|---|
| 67 | rescue |
|---|
| 68 | from = case charset |
|---|
| 69 | when /^utf-8$/i |
|---|
| 70 | 'W' |
|---|
| 71 | when /^shift_jis/i |
|---|
| 72 | 'S' |
|---|
| 73 | when /^EUC-JP/i |
|---|
| 74 | 'E' |
|---|
| 75 | else |
|---|
| 76 | '' |
|---|
| 77 | end |
|---|
| 78 | NKF::nkf("-m0 -#{from}w", str) |
|---|
| 79 | end |
|---|
| 80 | end |
|---|
| 81 | |
|---|
| 82 | convert_pstore( ARGV[0] ) |
|---|