greeklish_iso843 0.3.0 → 0.4.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
  SHA256:
3
- metadata.gz: 3d4c26155ee5e66d06b9a44cc5a452bcac2ed78a9f5ed889c6ec9540a9fc5853
4
- data.tar.gz: 22c733cff1a29127b39a36ca69caa9cd73d96b6e55009ebb017d0fa517484d01
3
+ metadata.gz: e5b8b86245593bbc327610d0bf977b6c148278bdf9f3dbdd35669235ae0eba94
4
+ data.tar.gz: 75207253c6b33972822551a7408b803cc8e5456067366f36ca919cc1b8b170aa
5
5
  SHA512:
6
- metadata.gz: 46c846c9b550c56a8530c232bc4db7d1eb561484d716758e62a23a4dd50956f63a29df91c99410ef7f9b4028f315660c42b21e4607b5200f3a9ea254dd2a2cc2
7
- data.tar.gz: 802ac4eef5af569b492991de5316e7390cf2377acd05d04f3dbcacc8b0adba528d9835be250fd34b64231c9d7ef2917e9f6e614c22cb2289e9fae3b6f886417d
6
+ metadata.gz: 7e440ef3f49da6b2ac6a19c54d7d271a4c6431ea48fb40eb0fa0f7503ab08b6b02cb997f03e86b40e1b30b35c42b99e95b901c22892b0d7303c57fd6b1711681
7
+ data.tar.gz: a8c1fa769ad472322100822b08466355e5b2a921fe36f22f78fff159018f9ddf4a925ff853a857d158933b14a8415055154a434e87c2aa482d8fc64ff600f7c5
data/CHANGELOG.md CHANGED
@@ -7,6 +7,15 @@ The format is based on [Keep a Changelog][] and this project adheres to
7
7
 
8
8
  ## [Unreleased][]
9
9
 
10
+ ## [0.4.0][] - 2021-04-28
11
+
12
+ ### Changed
13
+
14
+ - Return match as-is if it cannot be handled instead of crashing
15
+ - Raise `UnhandledCaseError` in case of an unhandled case when converting pair
16
+ for υ or φ (should never happen)
17
+ - Rewrite a significant part of the code to make it more readable
18
+
10
19
  ## [0.3.0][] - 2021-03-06
11
20
 
12
21
  ### Changed
@@ -34,4 +43,5 @@ Initial release.
34
43
  [Semantic Versioning]: http://semver.org/spec/v2.0.0.html
35
44
  [0.2.0]: https://github.com/agorf/greeklish_iso843/compare/0.1.0...0.2.0
36
45
  [0.3.0]: https://github.com/agorf/greeklish_iso843/compare/0.2.0...0.3.0
37
- [Unreleased]: https://github.com/agorf/greeklish_iso843/compare/0.3.0...HEAD
46
+ [0.4.0]: https://github.com/agorf/greeklish_iso843/compare/0.3.0...0.4.0
47
+ [Unreleased]: https://github.com/agorf/greeklish_iso843/compare/0.4.0...HEAD
data/README.md CHANGED
@@ -66,7 +66,8 @@ verbose output.
66
66
 
67
67
  ## Acknowledgements
68
68
 
69
- This library was based on [an official, buggy implementation in JavaScript][js].
69
+ This library was initially based on [an official, buggy implementation in
70
+ JavaScript][js].
70
71
 
71
72
  [js]: http://www.passport.gov.gr/passports/GrElotConverter/GrElotConverter.html
72
73
 
@@ -5,8 +5,12 @@ class GreeklishIso843::GreekText
5
5
 
6
6
  GREEK_VOWELS = 'αάεέηήιίϊΐοόυύϋΰωώ'.freeze
7
7
 
8
+ PAIRS_FOR_V_OR_F = %w[αυ αύ ευ εύ ηυ ηύ].freeze
9
+
8
10
  GREEK_LETTERS_AFTER_V = "#{GREEK_VOWELS}βγδζλμνρ".freeze
9
11
 
12
+ GREEK_LETTERS_AFTER_F = 'θκξπστφχψ'.freeze
13
+
10
14
  REPLACEMENTS = {
11
15
  'αι' => 'ai',
12
16
  'αί' => 'ai',
@@ -68,7 +72,11 @@ class GreeklishIso843::GreekText
68
72
  'ώ' => 'o'
69
73
  }.freeze
70
74
 
71
- REPLACEMENTS_REGEXP = /#{REPLACEMENTS.keys.join('|')}/i.freeze
75
+ REPLACEMENT_KEYS_REGEXP = /#{REPLACEMENTS.keys.join('|')}/i.freeze
76
+
77
+ class Error < StandardError; end
78
+
79
+ class UnhandledCaseError < Error; end
72
80
 
73
81
  attr_reader :text
74
82
 
@@ -81,66 +89,73 @@ class GreeklishIso843::GreekText
81
89
  end
82
90
 
83
91
  def to_greeklish
84
- text.gsub(REPLACEMENTS_REGEXP) do |match|
85
- match_data = Regexp.last_match
86
- next_char = match_data.post_match[0]&.downcase
87
- greeklish = REPLACEMENTS[match.downcase]
88
- greek = match
89
-
90
- if greeklish
91
- greek = match + next_char.to_s
92
- else
93
- greeklish = convert_to_greeklish(match, match_data, next_char)
94
- end
95
-
96
- fix_case(greeklish, greek)
92
+ text.gsub(REPLACEMENT_KEYS_REGEXP) do |match|
93
+ greeklish = REPLACEMENTS[match.downcase] ||
94
+ convert_pair(match, Regexp.last_match)
95
+
96
+ next match if greeklish.nil? # Unhandled case. Return as-is.
97
+
98
+ fix_case(greeklish, match)
97
99
  end
98
100
  end
99
101
 
100
- private def capitalized?(text)
101
- GREEK_UPPER[text[0]]
102
- end
102
+ private def convert_pair(match, match_data)
103
+ return 'ts' if match.casecmp?('τς')
103
104
 
104
- private def lowercase_char?(char)
105
- GREEK_LOWER[char]
105
+ next_char = match_data.post_match[0]&.downcase
106
+
107
+ if match.casecmp?('μπ')
108
+ prev_char = match_data.pre_match[-1]&.downcase
109
+ return convert_mp_or_b(prev_char, next_char)
110
+ end
111
+
112
+ if PAIRS_FOR_V_OR_F.any? { |pair| match.casecmp?(pair) }
113
+ return convert_pair_for_v_or_f(match, next_char)
114
+ end
115
+
116
+ match # Unhandled case. Return as-is.
106
117
  end
107
118
 
108
- private def fix_case(greeklish, greek)
109
- return greeklish if !capitalized?(greek[0])
119
+ private def fix_case(greeklish, match)
120
+ return greeklish if !uppercase?(match[0])
110
121
 
111
- if greek.length == 1 || capitalized?(greek[1])
122
+ if match.size == 1 || uppercase?(match[1])
112
123
  greeklish.upcase
113
124
  else
114
- greeklish[0].upcase + greeklish[1..-1]
125
+ greeklish[0].upcase + greeklish[1].to_s
115
126
  end
116
127
  end
117
128
 
118
129
  private def convert_mp_or_b(prev_char, next_char)
119
- if prev_char && lowercase_char?(prev_char) && # *μπ
120
- next_char && lowercase_char?(next_char) # and μπ*
130
+ if prev_char && lowercase?(prev_char) && # *μπ
131
+ next_char && lowercase?(next_char) # and μπ*
121
132
  'mp'
122
133
  else # μπ* or *μπ
123
134
  'b'
124
135
  end
125
136
  end
126
137
 
127
- private def convert_v_or_f(next_char)
128
- if next_char && GREEK_LETTERS_AFTER_V[next_char]
129
- 'v'
130
- else
131
- 'f'
138
+ private def convert_pair_for_v_or_f(match, next_char)
139
+ v_or_f = convert_v_or_f(next_char)
140
+
141
+ if v_or_f.nil?
142
+ raise UnhandledCaseError # Should never happen
132
143
  end
144
+
145
+ REPLACEMENTS[match[0].downcase] + v_or_f
133
146
  end
134
147
 
135
- private def convert_to_greeklish(match, match_data, next_char)
136
- return 'ts' if match.casecmp?('τς')
148
+ private def uppercase?(char)
149
+ GREEK_UPPER[char]
150
+ end
137
151
 
138
- if match.casecmp?('μπ')
139
- prev_char = match_data.pre_match[-1]&.downcase
140
- return convert_mp_or_b(prev_char, next_char)
141
- end
152
+ private def lowercase?(char)
153
+ GREEK_LOWER[char]
154
+ end
155
+
156
+ private def convert_v_or_f(next_char)
157
+ return 'f' if next_char.nil? || GREEK_LETTERS_AFTER_F[next_char]
142
158
 
143
- # αυ αύ ευ εύ ηυ ηύ
144
- REPLACEMENTS[match[0].downcase] + convert_v_or_f(next_char)
159
+ 'v' if GREEK_LETTERS_AFTER_V[next_char]
145
160
  end
146
161
  end
@@ -1,3 +1,3 @@
1
1
  module GreeklishIso843
2
- VERSION = '0.3.0'.freeze
2
+ VERSION = '0.4.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: greeklish_iso843
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Angelos Orfanakos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-06 00:00:00.000000000 Z
11
+ date: 2021-04-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: rubygems@angelos.dev