Class: TwitterCldr::Shared::Hyphenator
- Inherits:
-
Object
- Object
- TwitterCldr::Shared::Hyphenator
- Defined in:
- lib/twitter_cldr/shared/hyphenator.rb
Defined Under Namespace
Classes: UnsupportedLocaleError
Constant Summary collapse
- BASE_RESOURCE_PATH =
%w(shared hyphenation).freeze
- DEFAULT_LEFT_HYPHEN_MIN =
2
- DEFAULT_RIGHT_HYPHEN_MIN =
2
- DEFAULT_NO_HYPHEN =
"-'’".freeze
Instance Attribute Summary collapse
-
#locale ⇒ Object
readonly
Returns the value of attribute locale.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
-
#trie ⇒ Object
readonly
Returns the value of attribute trie.
Class Method Summary collapse
Instance Method Summary collapse
- #each_chunk(text) ⇒ Object
- #each_position(text) ⇒ Object
-
#hyphenate(text, hyphen = "\u00AD") ⇒ Object
0x00AD is a soft hyphen.
-
#initialize(rules, locale, options) ⇒ Hyphenator
constructor
A new instance of Hyphenator.
Constructor Details
#initialize(rules, locale, options) ⇒ Hyphenator
Returns a new instance of Hyphenator.
71 72 73 74 75 76 |
# File 'lib/twitter_cldr/shared/hyphenator.rb', line 71 def initialize(rules, locale, ) @rules = rules @locale = locale @options = @trie = build_trie_from(rules) end |
Instance Attribute Details
#locale ⇒ Object (readonly)
Returns the value of attribute locale.
69 70 71 |
# File 'lib/twitter_cldr/shared/hyphenator.rb', line 69 def locale @locale end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
69 70 71 |
# File 'lib/twitter_cldr/shared/hyphenator.rb', line 69 def @options end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
69 70 71 |
# File 'lib/twitter_cldr/shared/hyphenator.rb', line 69 def rules @rules end |
#trie ⇒ Object (readonly)
Returns the value of attribute trie.
69 70 71 |
# File 'lib/twitter_cldr/shared/hyphenator.rb', line 69 def trie @trie end |
Class Method Details
.get(locale) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/twitter_cldr/shared/hyphenator.rb', line 19 def get(locale) locale = find_supported_locale(locale) unless locale raise UnsupportedLocaleError, "'#{locale}' is not a supported hyphenation locale" end cache[locale] ||= begin resource = resource_for(locale) new(resource[:rules], locale, resource[:options]) end end |
.supported_locale?(locale) ⇒ Boolean
33 34 35 |
# File 'lib/twitter_cldr/shared/hyphenator.rb', line 33 def supported_locale?(locale) !!find_supported_locale(locale) end |
.supported_locales ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/twitter_cldr/shared/hyphenator.rb', line 37 def supported_locales @supported_locales ||= begin absolute_resource_path = TwitterCldr.absolute_resource_path( File.join(BASE_RESOURCE_PATH) ) files = Dir.glob(File.join(absolute_resource_path, '*.yml')) files.map { |f| File.basename(f).chomp('.yml') } end end |
Instance Method Details
#each_chunk(text) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/twitter_cldr/shared/hyphenator.rb', line 83 def each_chunk(text) if block_given? last_pos = 0 each_position(text) do |pos| yield text[last_pos...pos].tap { last_pos = pos } end if last_pos < text.size yield text[last_pos..text.size] end else to_enum(__method__, text) end end |
#each_position(text) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/twitter_cldr/shared/hyphenator.rb', line 99 def each_position(text) if block_given? text = ".#{text}." break_weights = break_weights_for(text) left = left_hyphen_min right = text.size - right_hyphen_min - 2 (left...right).each do |idx| yield idx if break_weights[idx].odd? end else to_enum(__method__, text) end end |
#hyphenate(text, hyphen = "\u00AD") ⇒ Object
0x00AD is a soft hyphen
79 80 81 |
# File 'lib/twitter_cldr/shared/hyphenator.rb', line 79 def hyphenate(text, hyphen = "\u00AD") each_chunk(text).to_a.join(hyphen) end |