pwim-emoticon 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,74 @@
1
+ require "emoticon/conversion_table"
2
+ require 'scanf'
3
+ require "kconv"
4
+ require "singleton"
5
+
6
+ module Emoticon
7
+ class Transcoder
8
+ include Emoticon::ConversionTable
9
+ include Singleton
10
+
11
+ def unicodecr_to_external(str)
12
+ str.gsub(/&#x([0-9a-f]{4});/i) do |match|
13
+ unicode = $1.scanf("%x").first
14
+ if conversion_table
15
+ converted = conversion_table[unicode] # キャリア間変換
16
+ else
17
+ converted = unicode # 変換しない
18
+ end
19
+
20
+ # 携帯側エンコーディングに変換する
21
+ case converted
22
+ when Integer
23
+ # 変換先がUnicodeで指定されている。つまり対応する絵文字がある。
24
+ if sjis = UNICODE_TO_SJIS[converted]
25
+ [sjis].pack('n')
26
+ elsif webcode = SOFTBANK_UNICODE_TO_WEBCODE[converted-0x1000]
27
+ "\x1b\x24#{webcode}\x0f"
28
+ else
29
+ # キャリア変換テーブルに指定されていたUnicodeに対応する
30
+ # 携帯側エンコーディングが見つからない(変換テーブルの不備の可能性あり)。
31
+ match
32
+ end
33
+ when String
34
+ # 変換先がUnicodeで指定されている。
35
+ to_sjis ? Kconv::kconv(converted, Kconv::SJIS, Kconv::UTF8) : converted
36
+ when nil
37
+ # 変換先が定義されていない。
38
+ match
39
+ end
40
+ end
41
+ end
42
+
43
+ def utf8_to_unicodecr(str)
44
+ str.gsub(UTF8_REGEXP) do |match|
45
+ "&#x%04x;" % match.unpack('U').first
46
+ end
47
+ end
48
+
49
+ def unicodecr_to_utf8(str)
50
+ str.gsub(/&#x([0-9a-f]{4});/i) do |match|
51
+ unicode = $1.scanf("%x").first
52
+ if UNICODE_TO_SJIS[unicode] || SOFTBANK_UNICODE_TO_WEBCODE[unicode-0x1000]
53
+ [unicode].pack('U')
54
+ else
55
+ match
56
+ end
57
+ end
58
+ end
59
+
60
+ def internal_to_external(s)
61
+ unicodecr_to_external(utf8_to_unicodecr(s))
62
+ end
63
+
64
+ private
65
+
66
+ def to_sjis
67
+ false
68
+ end
69
+
70
+ def conversion_table
71
+ nil
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,22 @@
1
+ require File.join(File.dirname(File.dirname(__FILE__)), "transcoder")
2
+
3
+ class Emoticon::Transcoder::Au < Emoticon::Transcoder
4
+ # +str+ のなかでDoCoMo絵文字をUnicode数値文字参照に置換した文字列を返す。
5
+ def external_to_unicodecr(str)
6
+ str.gsub(AU_SJIS_REGEXP) do |match|
7
+ sjis = match.unpack('n').first
8
+ unicode = AU_SJIS_TO_UNICODE[sjis]
9
+ unicode ? ("&#x%04x;"%unicode) : match
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def to_sjis
16
+ true
17
+ end
18
+
19
+ def conversion_table
20
+ CONVERSION_TABLE_TO_AU
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ require File.join(File.dirname(File.dirname(__FILE__)), "transcoder")
2
+
3
+ class Emoticon::Transcoder::Docomo < Emoticon::Transcoder
4
+
5
+ # +str+ のなかでDoCoMo絵文字をUnicode数値文字参照に置換した文字列を返す。
6
+ def external_to_unicodecr(str)
7
+ str.gsub(SJIS_REGEXP) do |match|
8
+ sjis = match.unpack('n').first
9
+ unicode = SJIS_TO_UNICODE[sjis]
10
+ unicode ? ("&#x%04x;"%unicode) : match
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def to_sjis
17
+ true
18
+ end
19
+
20
+ def conversion_table
21
+ CONVERSION_TABLE_TO_DOCOMO
22
+ end
23
+ end
@@ -0,0 +1,25 @@
1
+ require File.join(File.dirname(File.dirname(__FILE__)), "transcoder", "softbank")
2
+
3
+ class Emoticon::Transcoder::Jphone < Emoticon::Transcoder::Softbank
4
+ # +str+のなかでWebcodeのSoftBank絵文字を(+0x1000だけシフトして)Unicode数値文字参照に変換した文字列を返す。
5
+ def external_to_unicodecr(str)
6
+ # SoftBank Webcode
7
+ s = str.clone
8
+ # 連続したエスケープコードが省略されている場合は切りはなす。
9
+ s.gsub!(/\x1b\x24(.)(.+?)\x0f/) do |match|
10
+ a = $1
11
+ $2.split(//).map{|x| "\x1b\x24#{a}#{x}\x0f"}.join('')
12
+ end
13
+ # Webcodeを変換
14
+ s.gsub(SOFTBANK_WEBCODE_REGEXP) do |match|
15
+ unicode = SOFTBANK_WEBCODE_TO_UNICODE[match[2,2]] + 0x1000
16
+ unicode ? ("&#x%04x;"%unicode) : match
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def to_sjis
23
+ true
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ require File.join(File.dirname(File.dirname(__FILE__)), "transcoder")
2
+
3
+ class Emoticon::Transcoder::Null < Emoticon::Transcoder
4
+
5
+ # 対応する変換メソッドが定義されていない場合は素通し
6
+ def external_to_unicodecr(str)
7
+ str
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ require File.join(File.dirname(File.dirname(__FILE__)), "transcoder")
2
+
3
+ class Emoticon::Transcoder::Softbank < Emoticon::Transcoder
4
+
5
+ # +str+ のなかでDoCoMo絵文字をUnicode数値文字参照に置換した文字列を返す。
6
+ def external_to_unicodecr(str)
7
+ # SoftBank Unicode
8
+ str.gsub(SOFTBANK_UNICODE_REGEXP) do |match|
9
+ unicode = match.unpack('U').first
10
+ "&#x%04x;" % (unicode+0x1000)
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def conversion_table
17
+ CONVERSION_TABLE_TO_SOFTBANK
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ require File.join(File.dirname(File.dirname(__FILE__)), "transcoder", "softbank")
2
+
3
+ Emoticon::Transcoder::Vodafone = Emoticon::Transcoder::Softbank
@@ -0,0 +1,20 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../../test_helper'
3
+ require "emoticon/transcoder/au"
4
+
5
+
6
+ class AuTest < Test::Unit::TestCase
7
+ def setup
8
+ @transcoder = Emoticon::Transcoder::Au.new
9
+ end
10
+
11
+ def test_internal_to_external
12
+ assert_equal "\xf6\x60", @transcoder.internal_to_external(DOCOMO_CR)
13
+ assert_equal "\xf6\x60", @transcoder.internal_to_external(DOCOMO_UTF8)
14
+ assert_equal "[ドコモポイント]".tosjis, @transcoder.internal_to_external(DOCOMO_DOCOMO_POINT)
15
+ assert_equal "\xf6\x60", @transcoder.internal_to_external(AU_CR)
16
+ assert_equal "\xf6\x60", @transcoder.internal_to_external(AU_UTF8)
17
+ assert_equal "\xf6\x60", @transcoder.internal_to_external(SOFTBANK_CR)
18
+ assert_equal "\xf6\x60", @transcoder.internal_to_external(SOFTBANK_UTF8)
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../../test_helper'
3
+ require "emoticon/transcoder/docomo"
4
+
5
+
6
+ class DocomoTest < Test::Unit::TestCase
7
+ def setup
8
+ @transcoder = Emoticon::Transcoder::Docomo.new
9
+ end
10
+
11
+ def test_internal_to_external
12
+ assert_equal "\xf8\x9f", @transcoder.internal_to_external(DOCOMO_CR)
13
+ assert_equal "\xf8\x9f", @transcoder.internal_to_external(DOCOMO_UTF8)
14
+ assert_equal "\xf9\x79", @transcoder.internal_to_external(DOCOMO_DOCOMO_POINT)
15
+ assert_equal "\xf8\x9f", @transcoder.internal_to_external(AU_CR)
16
+ assert_equal "\xf8\x9f", @transcoder.internal_to_external(AU_UTF8)
17
+ assert_equal "\xf8\x9f", @transcoder.internal_to_external(SOFTBANK_CR)
18
+ assert_equal "\xf8\x9f", @transcoder.internal_to_external(SOFTBANK_UTF8)
19
+ end
20
+ end
@@ -0,0 +1,18 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../../test_helper'
3
+ require "emoticon/transcoder/jphone"
4
+
5
+
6
+ class JphoneTest < Test::Unit::TestCase
7
+ def setup
8
+ @transcoder = Emoticon::Transcoder::Jphone.new
9
+ end
10
+
11
+ def test_internal_to_external
12
+ assert_equal "\e$Gj\x0f", @transcoder.internal_to_external(DOCOMO_CR)
13
+ assert_equal "\e$Gj\x0f", @transcoder.internal_to_external(DOCOMO_UTF8)
14
+ assert_equal "[ドコモポイント]".tosjis, @transcoder.internal_to_external(DOCOMO_DOCOMO_POINT)
15
+ assert_equal "\e$Gj\x0f", @transcoder.internal_to_external(SOFTBANK_CR)
16
+ assert_equal "\e$Gj\x0f", @transcoder.internal_to_external(SOFTBANK_UTF8)
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../../test_helper'
3
+ require "emoticon/transcoder/softbank"
4
+
5
+
6
+ class SoftbankTest < Test::Unit::TestCase
7
+ def setup
8
+ @transcoder = Emoticon::Transcoder::Softbank.new
9
+ end
10
+
11
+ def test_internal_to_external
12
+ assert_equal "\e$Gj\x0f", @transcoder.internal_to_external(DOCOMO_CR)
13
+ assert_equal "\e$Gj\x0f", @transcoder.internal_to_external(DOCOMO_UTF8)
14
+ assert_equal "[ドコモポイント]", @transcoder.internal_to_external(DOCOMO_DOCOMO_POINT)
15
+ assert_equal "\e$Gj\x0f", @transcoder.internal_to_external(AU_CR)
16
+ assert_equal "\e$Gj\x0f", @transcoder.internal_to_external(AU_UTF8)
17
+ assert_equal "\e$Gj\x0f", @transcoder.internal_to_external(SOFTBANK_CR)
18
+ assert_equal "\e$Gj\x0f", @transcoder.internal_to_external(SOFTBANK_UTF8)
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../../test_helper'
3
+ require "emoticon/transcoder/vodafone"
4
+
5
+
6
+ class VodafoneTest < Test::Unit::TestCase
7
+ def setup
8
+ @transcoder = Emoticon::Transcoder::Vodafone.new
9
+ end
10
+
11
+ def test_internal_to_external
12
+ assert_equal "\e$Gj\x0f", @transcoder.internal_to_external(DOCOMO_CR)
13
+ assert_equal "\e$Gj\x0f", @transcoder.internal_to_external(DOCOMO_UTF8)
14
+ assert_equal "[ドコモポイント]", @transcoder.internal_to_external(DOCOMO_DOCOMO_POINT)
15
+ assert_equal "\e$Gj\x0f", @transcoder.internal_to_external(AU_CR)
16
+ assert_equal "\e$Gj\x0f", @transcoder.internal_to_external(AU_UTF8)
17
+ assert_equal "\e$Gj\x0f", @transcoder.internal_to_external(SOFTBANK_CR)
18
+ assert_equal "\e$Gj\x0f", @transcoder.internal_to_external(SOFTBANK_UTF8)
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+ require 'emoticon'
3
+
4
+ class EmoticonTest < Test::Unit::TestCase
5
+ def test_transcoder_for_carrier
6
+ assert_instance_of(Emoticon::Transcoder::Docomo, Emoticon.transcoder_for_carrier("docomo"))
7
+ assert_instance_of(Emoticon::Transcoder::Au, Emoticon.transcoder_for_carrier("au"))
8
+ assert_instance_of(Emoticon::Transcoder::Softbank, Emoticon.transcoder_for_carrier("softbank"))
9
+ assert_instance_of(Emoticon::Transcoder::Softbank, Emoticon.transcoder_for_carrier("vodafone"))
10
+ assert_instance_of(Emoticon::Transcoder::Jphone, Emoticon.transcoder_for_carrier("jphone"))
11
+ assert_instance_of(Emoticon::Transcoder::Null, Emoticon.transcoder_for_carrier("emobile"))
12
+ assert_instance_of(Emoticon::Transcoder::Null, Emoticon.transcoder_for_carrier("willcom"))
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ require 'test/unit'
2
+ $:.unshift File.dirname(__FILE__) + '/../lib'
3
+
4
+ DOCOMO_CR = "&#xE63E;"
5
+ DOCOMO_UTF8 = [0xe63e].pack("U")
6
+ DOCOMO_DOCOMO_POINT = "&#xE6D5;"
7
+ AU_CR = "&#xE488;"
8
+ AU_UTF8 = [0xe488].pack("U")
9
+ SOFTBANK_CR = "&#xF04A;"
10
+ SOFTBANK_UTF8 = [0xf04a].pack("U")
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pwim-emoticon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Paul McMahon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-12 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Emoticon is a Ruby library for transcoding emoticons across different carriers for Japanese mobile phones. It is derived from Jpmobile, a rails plugin for building mobile sites for Japanese mobiles created by Yohji Shidara
17
+ email: paul@tokyodev.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README
26
+ - MIT-LICENSE
27
+ - emoticon.gemspec
28
+ - lib/emoticon.rb
29
+ - lib/emoticon/conversion_table.rb
30
+ - lib/emoticon/transcoder.rb
31
+ - lib/emoticon/conversion_table/au.rb
32
+ - lib/emoticon/conversion_table/docomo.rb
33
+ - lib/emoticon/conversion_table/softbank.rb
34
+ - lib/emoticon/transcoder/au.rb
35
+ - lib/emoticon/transcoder/docomo.rb
36
+ - lib/emoticon/transcoder/jphone.rb
37
+ - lib/emoticon/transcoder/null.rb
38
+ - lib/emoticon/transcoder/softbank.rb
39
+ - lib/emoticon/transcoder/vodafone.rb
40
+ has_rdoc: false
41
+ homepage: http://github.com/pwim/emoticon
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.2.0
63
+ signing_key:
64
+ specification_version: 2
65
+ summary: Emoticon (emoji) handling for Japanese mobile phones
66
+ test_files:
67
+ - test/emoticon_test.rb
68
+ - test/test_helper.rb
69
+ - test/emoticon/transcoder/au_test.rb
70
+ - test/emoticon/transcoder/docomo_test.rb
71
+ - test/emoticon/transcoder/jphone_test.rb
72
+ - test/emoticon/transcoder/softbank_test.rb
73
+ - test/emoticon/transcoder/vodafone_test.rb