sawarineko 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a3f2a14c9be7b8aa29c25f511c70cb89cdb5b3a5
4
- data.tar.gz: 68a1c98dca7f5c6b75b82e07358428df92b6c405
3
+ metadata.gz: 9557a8ea93741177f356bdc4ca74b19074ed759b
4
+ data.tar.gz: 1209522799b9aa7a0e00602491eb88898775bf2e
5
5
  SHA512:
6
- metadata.gz: 69a20a207b87bf97cb7f86c0ce65173e673b00839b90f625acf5812dd89c2e66e90b5a8f7ed3f3f81f04d4ab6ac628c385edec15ddf04880ab504ea78ec9b593
7
- data.tar.gz: d432ce8bbe7830a7f475f7ef627ca6390ccd2fa2518f7718b7f510a6893ebfbc6a29f4d3c695cd826c359c35d187b9620cae1fda6c0e2ee9f4e263961600363c
6
+ metadata.gz: fbd6e46d892abadc48d28f03440ec7c781ddca89768aa7336a4adc585be71b5791a482489eeab34f1e1bee9f83598ecafe5ba2ba33f2d8fa81f7e9633560b0d9
7
+ data.tar.gz: cdad60808e1ee4b80663882874e1d51a35fb3534b50900ba806983718a313307f57452457387e6f18b77ac89ca11d9d48507085f64022b77a19f9c7af4cb6b08
@@ -1,8 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.2.0 (2014-10-29)
4
+
5
+ - [#2](https://github.com/yous/sawarineko/issues/2): Add encoding support to Converter (UTF-8, UTF-16BE, UTF-16LE, EUC-JP, Shift_JIS, EUC-KR, CP949)
6
+ - [#2](https://github.com/yous/sawarineko/issues/2): Add `-e/--encoding` option to CLI
7
+
3
8
  ## 1.1.0 (2014-10-28)
4
9
 
5
- - Add `Sawarineko#nya`
10
+ - Add `Sawarineko.nya`
6
11
  - Make `Converter#convert` to get the source as argument
7
12
 
8
13
  ## 1.0.0 (2014-10-27)
data/README.md CHANGED
@@ -57,10 +57,11 @@ For additional command-line options:
57
57
  sawarineko -h
58
58
  ```
59
59
 
60
- Command flag | Description
61
- ----------------|--------------------
62
- `-h, --help` | Print this message.
63
- `-v, --version` | Print version.
60
+ Command flag | Description
61
+ --------------------------|-------------------------------
62
+ `-e, --encoding ENCODING` | Specify the encoding of input.
63
+ `-h, --help` | Print this message.
64
+ `-v, --version` | Print version.
64
65
 
65
66
  ## Contributing
66
67
 
@@ -9,10 +9,11 @@ require 'sawarineko/cli'
9
9
  module Sawarineko
10
10
  # Make the source to Sawarineko. See Converter.
11
11
  #
12
- # source - The String source to convert.
12
+ # source - The String source to convert.
13
+ # encoding - The Encoding of the source (default: Encoding::UTF_8).
13
14
  #
14
15
  # Returns the String converted to Sawarineko.
15
- def self.nya(source)
16
- Converter.new.convert(source)
16
+ def self.nya(source, encoding = Encoding::UTF_8)
17
+ Converter.new(encoding).convert(source)
17
18
  end
18
19
  end
@@ -19,9 +19,9 @@ module Sawarineko
19
19
  source = if paths.empty?
20
20
  $stdin.read
21
21
  else
22
- IO.read(paths[0])
22
+ IO.read(paths[0], encoding: @options[:encoding])
23
23
  end
24
- converter = Converter.new
24
+ converter = Converter.new(@options[:encoding])
25
25
  puts converter.convert(source)
26
26
  0
27
27
  end
@@ -3,16 +3,56 @@
3
3
  module Sawarineko
4
4
  # Convert plain text to Sawarineko text.
5
5
  class Converter
6
+ # Array of convertible character code on EUC-KR encoding.
7
+ CONVERTIBLE_EUC_KR =
8
+ %w(나 낙 난 날 남 낭).map { |ch| ch.encode(Encoding::EUC_KR).ord }.freeze
9
+ private_constant :CONVERTIBLE_EUC_KR
10
+
11
+ # Initialize a Converter. Get the encoding of source. Initialize Regexps for
12
+ # conversion.
13
+ #
14
+ # encoding - The Encoding of source (default: Encoding::UTF_8).
15
+ def initialize(encoding = Encoding::UTF_8)
16
+ @encoding = Encoding.find(encoding)
17
+ @hiragana_regex = Regexp.new('な'.encode(@encoding)).freeze
18
+ @katakana_regex = Regexp.new('ナ'.encode(@encoding)).freeze
19
+ @hangul_regex = case @encoding
20
+ when Encoding::UTF_8, Encoding::UTF_16BE,
21
+ Encoding::UTF_16LE, Encoding::EUC_KR
22
+ Regexp.new('[나-낳]'.encode(@encoding)).freeze
23
+ when Encoding::CP949
24
+ Regexp.new('[나-낳낛-낤낥-낲]'.encode(@encoding)).freeze
25
+ end
26
+ end
27
+
6
28
  # Convert the source.
7
29
  #
8
30
  # source - The String source to convert.
9
31
  #
10
32
  # Returns the String converted to Sawarineko.
11
33
  def convert(source)
12
- source
13
- .gsub(/な/, 'にゃ')
14
- .gsub(/ナ/, 'ニャ')
15
- .gsub(/[나-낳]/) { |ch| (ch.ord + 56).chr(Encoding::UTF_8) }
34
+ new_source = source.gsub(@hiragana_regex, 'にゃ'.encode(@encoding).freeze)
35
+ .gsub(@katakana_regex, 'ニャ'.encode(@encoding).freeze)
36
+ if @hangul_regex
37
+ new_source.gsub(@hangul_regex) { |ch| convert_hangul(ch) }
38
+ else
39
+ new_source
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ # Convert a hangul character to Sawarineko.
46
+ #
47
+ # ch - A hangul character to convert.
48
+ #
49
+ # Returns the converted character.
50
+ def convert_hangul(ch)
51
+ if @encoding == Encoding::EUC_KR && !CONVERTIBLE_EUC_KR.include?(ch.ord)
52
+ return ch
53
+ end
54
+ (ch.encode(Encoding::UTF_8).ord + 56).chr(Encoding::UTF_8)
55
+ .encode(@encoding)
16
56
  end
17
57
  end
18
58
  end
@@ -7,7 +7,7 @@ module Sawarineko
7
7
  class Option
8
8
  # Initialize a Option.
9
9
  def initialize
10
- @options = {}
10
+ @options = { encoding: Encoding::UTF_8 }
11
11
  end
12
12
 
13
13
  # Parse the passed arguments to a Hash.
@@ -21,24 +21,37 @@ module Sawarineko
21
21
  opts.banner = 'Usage: sawarineko [options] [source]'
22
22
 
23
23
  add_options(opts)
24
+ add_options_on_tail(opts)
24
25
  end.parse!(args)
25
26
  [@options, args]
26
27
  end
27
28
 
28
29
  private
29
30
 
30
- # Add command line options to OptionParser.
31
+ # Add command line options.
31
32
  #
32
33
  # opts - An OptionParser object to add options.
33
34
  #
34
35
  # Returns nothing.
35
36
  def add_options(opts)
36
- opts.on('-h', '--help', 'Print this message.') do
37
+ opts.on('-e', '--encoding ENCODING',
38
+ 'Specify the encoding of input.') do |encoding|
39
+ @options[:encoding] = encoding
40
+ end
41
+ end
42
+
43
+ # Add command line options printed at tail.
44
+ #
45
+ # opts - An OptionParser object to add options.
46
+ #
47
+ # Returns nothing.
48
+ def add_options_on_tail(opts)
49
+ opts.on_tail('-h', '--help', 'Print this message.') do
37
50
  puts opts
38
51
  exit 0
39
52
  end
40
53
 
41
- opts.on('-v', '--version', 'Print version.') do
54
+ opts.on_tail('-v', '--version', 'Print version.') do
42
55
  puts Version::STRING
43
56
  exit 0
44
57
  end
@@ -3,6 +3,6 @@
3
3
  module Sawarineko
4
4
  # Hold Sawarineko's version information.
5
5
  module Version
6
- STRING = '1.1.0'
6
+ STRING = '1.2.0'
7
7
  end
8
8
  end
@@ -36,4 +36,20 @@ RSpec.describe Sawarineko::CLI do
36
36
  'にゃにゃはんにゃにゃだいにゃんにゃく' \
37
37
  "にゃらべてにゃがにゃがめ\n")
38
38
  end
39
+
40
+ describe '-e/--encoding' do
41
+ it 'converts text file with given encoding' do
42
+ create_file('sawarineko.txt',
43
+ ['ななめななじゅうななどの' \
44
+ 'ならびでなくなくいななく' \
45
+ 'ななはんななだいなんなく' \
46
+ 'ならべてながながめ'.encode(Encoding::CP949)])
47
+ expect(cli.run(['--encoding', 'cp949',
48
+ 'sawarineko.txt'])).to be(0)
49
+ expect($stdout.string).to eq('にゃにゃめにゃにゃじゅうにゃにゃどの' \
50
+ 'にゃらびでにゃくにゃくいにゃにゃく' \
51
+ 'にゃにゃはんにゃにゃだいにゃんにゃく' \
52
+ "にゃらべてにゃがにゃがめ\n")
53
+ end
54
+ end
39
55
  end
@@ -3,44 +3,108 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  RSpec.describe Sawarineko::Converter do
6
- subject(:converter) { described_class.new }
7
-
8
- it 'converts hiragana な to にゃ' do
9
- expect(converter.convert('な')).to eq('にゃ')
10
- expect(converter.convert('ななめななじゅうななどのならびで' \
11
- 'なくなくいななくななはんななだい' \
12
- 'なんなくならべてながながめ'))
13
- .to eq('にゃにゃめにゃにゃじゅうにゃにゃどのにゃらびで' \
14
- 'にゃくにゃくいにゃにゃくにゃにゃはんにゃにゃだい' \
15
- 'にゃんにゃくにゃらべてにゃがにゃがめ')
16
- end
17
-
18
- it 'converts katakana ナ to ニャ' do
19
- expect(converter.convert('ナ')).to eq('ニャ')
20
- expect(converter.convert('ナナメナナジュウナナドノナラビデ' \
21
- 'ナクナクイナナクナナハンナナダイ' \
22
- 'ナンナクナラベテナガナガメ'))
23
- .to eq('ニャニャメニャニャジュウニャニャドノニャラビデ' \
24
- 'ニャクニャクイニャニャクニャニャハンニャニャダイ' \
25
- 'ニャンニャクニャラベテニャガニャガメ')
26
- end
27
-
28
- it 'converts hangul 나-낳 to 냐-냫' do
29
- ('나'..'낳').zip('냐'..'냫').each do |na, nya|
30
- expect(converter.convert(na)).to eq(nya)
6
+ include ConverterHelper
7
+
8
+ describe '#initialize' do
9
+ subject(:converter) { described_class.new }
10
+
11
+ it 'stores default encoding' do
12
+ encoding = converter.instance_variable_get(:@encoding)
13
+ expect(encoding).to be(Encoding::UTF_8)
14
+ end
15
+
16
+ context 'with Encoding object' do
17
+ subject(:converter) { described_class.new(encoding) }
18
+ let(:encoding) { Encoding::UTF_16LE }
19
+
20
+ it 'stores given encoding' do
21
+ encoding = converter.instance_variable_get(:@encoding)
22
+ expect(encoding).to be(Encoding::UTF_16LE)
23
+ end
24
+ end
25
+
26
+ context 'with string encoding name' do
27
+ subject(:converter) { described_class.new(encoding) }
28
+ let(:encoding) { 'utf-16le' }
29
+
30
+ it 'converts given name to Encoding object and stores' do
31
+ encoding = converter.instance_variable_get(:@encoding)
32
+ expect(encoding).to be(Encoding::UTF_16LE)
33
+ end
31
34
  end
32
- expect(converter.convert('나랑 너랑 봄나들이 배낭 매고 봄나들이 ' \
33
- '버드나무 낭창낭창 남실바람 남실남실 ' \
34
- '개나리 꽃에 나비가 하나 ' \
35
- '배낭 속에 바나나가 하나'))
36
- .to eq('냐랑 너랑 봄냐들이 배냥 매고 봄냐들이 ' \
37
- '버드냐무 냥창냥창 냠실바람 냠실냠실 ' \
38
- '개냐리 꽃에 냐비가 하냐 ' \
39
- '배냥 속에 바냐냐가 하냐')
40
- end
41
-
42
- it 'converts all languages at once' do
43
- expect(converter.convert(%w(な ナ 나).join("\n")))
44
- .to eq(%w(にゃ ニャ 냐).join("\n"))
35
+
36
+ context 'with string encoding alias name' do
37
+ subject(:converter) { described_class.new(encoding) }
38
+ let(:encoding) { 'eucKR' }
39
+
40
+ it 'converts given name to Encoding object and stores' do
41
+ encoding = converter.instance_variable_get(:@encoding)
42
+ expect(encoding).to be(Encoding::EUC_KR)
43
+ end
44
+ end
45
+ end
46
+
47
+ context 'with UTF-8 encoding' do
48
+ subject(:converter) { described_class.new(encoding) }
49
+ let(:encoding) { Encoding::UTF_8 }
50
+ it_behaves_like 'a converter supports japanese', Encoding::UTF_8
51
+ it_behaves_like 'a converter supports hangul', Encoding::UTF_8
52
+
53
+ it 'converts all languages at once' do
54
+ new_source = convert_source(converter, encoding, %w(な ナ 나).join("\n"))
55
+ expect(new_source).to eq(%w(にゃ ニャ 냐).join("\n").encode(encoding))
56
+ end
57
+ end
58
+
59
+ context 'with UTF-16BE encoding' do
60
+ it_behaves_like 'a converter supports japanese', Encoding::UTF_16BE
61
+ it_behaves_like 'a converter supports hangul', Encoding::UTF_16BE
62
+ end
63
+
64
+ context 'with UTF-16LE encoding' do
65
+ it_behaves_like 'a converter supports japanese', Encoding::UTF_16LE
66
+ it_behaves_like 'a converter supports hangul', Encoding::UTF_16LE
67
+ end
68
+
69
+ context 'with EUC-JP encoding' do
70
+ it_behaves_like 'a converter supports japanese', Encoding::EUC_JP
71
+ end
72
+
73
+ context 'with Shift_JIS encoding' do
74
+ it_behaves_like 'a converter supports japanese', Encoding::Shift_JIS
75
+ end
76
+
77
+ context 'with EUC-KR encoding' do
78
+ subject(:converter) { described_class.new(encoding) }
79
+ let(:encoding) { Encoding::EUC_KR }
80
+ it_behaves_like 'a converter supports japanese', Encoding::EUC_KR
81
+
82
+ it 'converts hangul 나, 낙, 난, 날, 남, 낭 to 냐, 냑, 냔, 냘, 냠, 냥' do
83
+ %w(나 낙 난 날 남 낭).zip(%w(냐 냑 냔 냘 냠 냥)).each do |na, nya|
84
+ new_source = convert_source(converter, encoding, na)
85
+ expect(new_source).to eq(nya.encode(encoding))
86
+ end
87
+ new_source = convert_source(converter, encoding,
88
+ '나랑 너랑 봄나들이 배낭 매고 봄나들이 ' \
89
+ '버드나무 낭창낭창 남실바람 남실남실 ' \
90
+ '개나리 꽃에 나비가 하나 ' \
91
+ '배낭 속에 바나나가 하나')
92
+ expect(new_source).to eq('냐랑 너랑 봄냐들이 배냥 매고 봄냐들이 ' \
93
+ '버드냐무 냥창냥창 냠실바람 냠실냠실 ' \
94
+ '개냐리 꽃에 냐비가 하냐 ' \
95
+ '배냥 속에 바냐냐가 하냐'.encode(encoding))
96
+ end
97
+
98
+ it 'preserves other hangul in range 나-낳' do
99
+ %w(낚 낟 낡 낢 납 낫 났 낮 낯 낱 낳).each do |ch|
100
+ new_source = convert_source(converter, encoding, ch)
101
+ expect(new_source).to eq(ch.encode(encoding))
102
+ end
103
+ end
104
+ end
105
+
106
+ context 'with CP949 encoding' do
107
+ it_behaves_like 'a converter supports japanese', Encoding::CP949
108
+ it_behaves_like 'a converter supports hangul', Encoding::CP949
45
109
  end
46
110
  end
@@ -25,6 +25,7 @@ RSpec.describe Sawarineko::Option do
25
25
 
26
26
  expected_help = <<-END
27
27
  Usage: sawarineko [options] [source]
28
+ -e, --encoding ENCODING Specify the encoding of input.
28
29
  -h, --help Print this message.
29
30
  -v, --version Print version.
30
31
  END
@@ -5,7 +5,7 @@ require 'spec_helper'
5
5
  RSpec.describe Sawarineko do
6
6
  subject(:sawarineko) { described_class }
7
7
 
8
- describe '#nya' do
8
+ describe '.nya' do
9
9
  it { is_expected.to respond_to(:nya) }
10
10
 
11
11
  it 'converts string passed as an argument' do
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+
3
+ # Helper methods for testing Converter.
4
+ module ConverterHelper
5
+ # Convert the source with given encoding.
6
+ #
7
+ # converter - The Converter instance to process.
8
+ # encoding - The Encoding used in converter.
9
+ # source - The String source with UTF-8 encoding.
10
+ #
11
+ # Returns the converted source String.
12
+ def convert_source(converter, encoding, source)
13
+ converter.convert(source.encode(encoding))
14
+ end
15
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ shared_examples 'a converter supports hangul' do |encoding|
6
+ include ConverterHelper
7
+
8
+ subject(:converter) { described_class.new(encoding) }
9
+
10
+ describe '#convert' do
11
+ it 'converts hangul 나-낳 to 냐-냫' do
12
+ ('나'..'낳').zip('냐'..'냫').each do |na, nya|
13
+ new_source = convert_source(converter, encoding, na)
14
+ expect(new_source).to eq(nya.encode(encoding))
15
+ end
16
+ new_source = convert_source(converter, encoding,
17
+ '나랑 너랑 봄나들이 배낭 매고 봄나들이 ' \
18
+ '버드나무 낭창낭창 남실바람 남실남실 ' \
19
+ '개나리 꽃에 나비가 하나 ' \
20
+ '배낭 속에 바나나가 하나')
21
+ expect(new_source).to eq('냐랑 너랑 봄냐들이 배냥 매고 봄냐들이 ' \
22
+ '버드냐무 냥창냥창 냠실바람 냠실냠실 ' \
23
+ '개냐리 꽃에 냐비가 하냐 ' \
24
+ '배냥 속에 바냐냐가 하냐'.encode(encoding))
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ shared_examples 'a converter supports japanese' do |encoding|
6
+ include ConverterHelper
7
+
8
+ subject(:converter) { described_class.new(encoding) }
9
+
10
+ describe '#convert' do
11
+ it 'converts hiragana な to にゃ' do
12
+ new_source = convert_source(converter, encoding, 'な')
13
+ expect(new_source).to eq('にゃ'.encode(encoding))
14
+ new_source = convert_source(converter, encoding,
15
+ 'ななめななじゅうななどの' \
16
+ 'ならびでなくなくいななく' \
17
+ 'ななはんななだいなんなく' \
18
+ 'ならべてながながめ')
19
+ expect(new_source).to eq('にゃにゃめにゃにゃじゅうにゃにゃどの' \
20
+ 'にゃらびでにゃくにゃくいにゃにゃく' \
21
+ 'にゃにゃはんにゃにゃだいにゃんにゃく' \
22
+ 'にゃらべてにゃがにゃがめ'.encode(encoding))
23
+ end
24
+
25
+ it 'converts katakana ナ to ニャ' do
26
+ new_source = convert_source(converter, encoding, 'ナ')
27
+ expect(new_source).to eq('ニャ'.encode(encoding))
28
+ new_source = convert_source(converter, encoding,
29
+ 'ナナメナナジュウナナドノ' \
30
+ 'ナラビデナクナクイナナク' \
31
+ 'ナナハンナナダイナンナク' \
32
+ 'ナラベテナガナガメ')
33
+ expect(new_source).to eq('ニャニャメニャニャジュウニャニャドノ' \
34
+ 'ニャラビデニャクニャクイニャニャク' \
35
+ 'ニャニャハンニャニャダイニャンニャク' \
36
+ 'ニャラベテニャガニャガメ'.encode(encoding))
37
+ end
38
+ end
39
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sawarineko
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ChaYoung You
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-28 00:00:00.000000000 Z
11
+ date: 2014-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -111,9 +111,12 @@ files:
111
111
  - spec/sawarineko/version_spec.rb
112
112
  - spec/sawarineko_spec.rb
113
113
  - spec/spec_helper.rb
114
+ - spec/support/converter_helper.rb
114
115
  - spec/support/exit_code_matchers.rb
115
116
  - spec/support/file_helper.rb
116
117
  - spec/support/isolated_environment.rb
118
+ - spec/support/shared_converter_supports_hangul.rb
119
+ - spec/support/shared_converter_supports_japanese.rb
117
120
  homepage: ''
118
121
  licenses:
119
122
  - MIT
@@ -145,6 +148,9 @@ test_files:
145
148
  - spec/sawarineko/version_spec.rb
146
149
  - spec/sawarineko_spec.rb
147
150
  - spec/spec_helper.rb
151
+ - spec/support/converter_helper.rb
148
152
  - spec/support/exit_code_matchers.rb
149
153
  - spec/support/file_helper.rb
150
154
  - spec/support/isolated_environment.rb
155
+ - spec/support/shared_converter_supports_hangul.rb
156
+ - spec/support/shared_converter_supports_japanese.rb