Sha256: 6598ee25c96747078cf93484e0fe585d76befa3c393bc24788553515765ab451

Contents?: true

Size: 1.32 KB

Versions: 52

Compression:

Stored size: 1.32 KB

Contents

#
# convert utf-8 in pstore.
#
# usage: convert_pstore.rb file1
#

$KCODE = 'u'

require 'nkf'
require "pstore"
begin
	require "iconv"
rescue LoadError
end

def convert_pstore( file )
	db = PStore.new( file )
	begin
		roots = db.transaction{ db.roots }
	rescue ArgumentError
		if /\Aundefined class\/module (.+?)(::)?\z/ =~ $!.message
			klass = $1
			if /EmptdiaryString\z/ =~ klass
				eval( "class #{klass} < String; end" )
			else
				eval( "class #{ klass}; end" )
			end
			retry
		end
	end
	db.transaction do
		roots.each do |root|
			convert_element( db[root] )
		end
	end
end

def convert_element( data )
	case data
	when Hash, Array
		data.each_with_index do |e, i|
			if String === e
				data[i] = migrate_to_utf8( e )
			else
				convert_element( e )
			end
		end
	else
		data.instance_variables.each do |e|
			var = data.instance_variable_get( e )
			if String === var
				data.instance_variable_set( e, migrate_to_utf8( var ) )
			else
				convert_element( var )
			end
		end
	end
end

def migrate_to_utf8( str )
	to_native( str, 'EUC-JP' )
end

def to_native( str, charset = nil )
	begin
		Iconv.conv('utf-8', charset || 'utf-8', str)
	rescue
		from = case charset
			when /^utf-8$/i
				'W'
			when /^shift_jis/i
				'S'
			when /^EUC-JP/i
				'E'
			else
				''
		end
		NKF::nkf("-m0 -#{from}w", str)
	end
end

convert_pstore( ARGV[0] )

Version data entries

52 entries across 46 versions & 2 rubygems

Version Path
tdiary-contrib-5.3.0 util/convert_pstore.rb
tdiary-contrib-5.2.4 util/convert_pstore.rb
tdiary-contrib-5.2.3 util/convert_pstore.rb
tdiary-contrib-5.2.2 util/convert_pstore.rb
tdiary-contrib-5.2.1 util/convert_pstore.rb
tdiary-contrib-5.2.0 util/convert_pstore.rb
tdiary-contrib-5.1.7 util/convert_pstore.rb
tdiary-contrib-5.1.6 util/convert_pstore.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/tdiary-contrib-5.1.4/util/convert_pstore.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/bundler/gems/tdiary-contrib-d1e41204db13/util/convert_pstore.rb
tdiary-contrib-5.1.5 util/convert_pstore.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/tdiary-contrib-5.1.4/util/convert_pstore.rb
tdiary-contrib-5.1.4 util/convert_pstore.rb
tdiary-contrib-5.1.3 util/convert_pstore.rb
tdiary-contrib-5.1.2 util/convert_pstore.rb
tdiary-contrib-5.1.1 util/convert_pstore.rb
tdiary-contrib-5.1.0 util/convert_pstore.rb
tdiary-contrib-5.0.13 util/convert_pstore.rb
tdiary-contrib-5.0.12 util/convert_pstore.rb
tdiary-contrib-5.0.11 util/convert_pstore.rb