Module: UnicodeTitlecase::CoreExt::String
- Defined in:
- lib/unicode_titlecase/core_ext/string.rb
Constant Summary collapse
- SMALL_WORDS =
List of exceptions: SMALL_WORDS are words that should always be in lowercase; BIG_WORDS are words that should always be in uppercase
%w(a an and as at but by be for if in is of on or the to v v. via vs vs.)
- BIG_WORDS =
%w(AB A.B. A/B AS A.S. A/S S.A. KG LLC LLP DNA RNA HBV HIV I II III IV V VI VII VIII IX X AC DC Q&A AT&T)
Instance Method Summary collapse
- #all_caps? ⇒ Boolean
- #capitalize_small_words_after_colons(str) ⇒ Object
- #has_caps? ⇒ Boolean
- #is_big_word? ⇒ Boolean
- #is_small_word? ⇒ Boolean
- #smart_capitalize ⇒ Object
- #smart_capitalize! ⇒ Object
- #smart_capitalize_ends!(ary) ⇒ Object
- #strip_non_word_chars ⇒ Object
- #strip_spaces(ary) ⇒ Object
- #unicode_downcase ⇒ Object
- #unicode_downcase! ⇒ Object
- #unicode_titlecase ⇒ Object
- #unicode_titlecase! ⇒ Object
- #unicode_upcase ⇒ Object
- #unicode_upcase! ⇒ Object
Instance Method Details
#all_caps? ⇒ Boolean
61 62 63 |
# File 'lib/unicode_titlecase/core_ext/string.rb', line 61 def all_caps? return UnicodeUtils.upcase(self) == self end |
#capitalize_small_words_after_colons(str) ⇒ Object
96 97 98 99 |
# File 'lib/unicode_titlecase/core_ext/string.rb', line 96 def capitalize_small_words_after_colons(str) # small words after colons are capitalized str.gsub(/:\s?(\W*#{SMALL_WORDS.join("|")}\W*)\s/) { ": #{$1.smart_capitalize} " } end |
#has_caps? ⇒ Boolean
57 58 59 |
# File 'lib/unicode_titlecase/core_ext/string.rb', line 57 def has_caps? return !(UnicodeUtils.downcase(self) == self) end |
#is_big_word? ⇒ Boolean
65 66 67 |
# File 'lib/unicode_titlecase/core_ext/string.rb', line 65 def is_big_word? BIG_WORDS.include?(self) end |
#is_small_word? ⇒ Boolean
69 70 71 |
# File 'lib/unicode_titlecase/core_ext/string.rb', line 69 def is_small_word? SMALL_WORDS.include?(self) end |
#smart_capitalize ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/unicode_titlecase/core_ext/string.rb', line 42 def smart_capitalize # ignore any leading crazy characters and capitalize the first real character if self =~ /^['"\(\[']*(\S)/ start_of_word = index($1) word = self[start_of_word, self.length] # word with capitals and periods mid-word are left alone self[start_of_word, 1] = UnicodeUtils.upcase(self[start_of_word, 1]) unless self.has_caps? or word =~ /\.\w+/ end self end |
#smart_capitalize! ⇒ Object
53 54 55 |
# File 'lib/unicode_titlecase/core_ext/string.rb', line 53 def smart_capitalize! replace(smart_capitalize) end |
#smart_capitalize_ends!(ary) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/unicode_titlecase/core_ext/string.rb', line 89 def smart_capitalize_ends!(ary) # capitalize first and last words ary.first.to_s.smart_capitalize! # Uncomment the next line if you want the last word to be always initial caps ary.last.to_s.smart_capitalize! end |
#strip_non_word_chars ⇒ Object
105 106 107 |
# File 'lib/unicode_titlecase/core_ext/string.rb', line 105 def strip_non_word_chars self.gsub(/\W/, "") end |
#strip_spaces(ary) ⇒ Object
101 102 103 |
# File 'lib/unicode_titlecase/core_ext/string.rb', line 101 def strip_spaces(ary) ary - [' '] end |
#unicode_downcase ⇒ Object
73 74 75 |
# File 'lib/unicode_titlecase/core_ext/string.rb', line 73 def unicode_downcase UnicodeUtils.downcase(self) end |
#unicode_downcase! ⇒ Object
77 78 79 |
# File 'lib/unicode_titlecase/core_ext/string.rb', line 77 def unicode_downcase! replace(unicode_downcase) end |
#unicode_titlecase ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/unicode_titlecase/core_ext/string.rb', line 13 def unicode_titlecase component_words = split(" ").map do |word| # note: word could contain non-word characters! # downcase all small_words, upcase all big words, smart capitalize the rest word.unicode_downcase! if word.all_caps? and not word.is_big_word? if word.strip_non_word_chars.unicode_downcase.is_small_word? word.unicode_downcase! else if word.strip_non_word_chars.unicode_upcase.is_big_word? word.unicode_upcase! else word.smart_capitalize! end end end component_words = strip_spaces(component_words) smart_capitalize_ends!(component_words) result = component_words.join(" ") capitalize_small_words_after_colons(result) end |
#unicode_titlecase! ⇒ Object
38 39 40 |
# File 'lib/unicode_titlecase/core_ext/string.rb', line 38 def unicode_titlecase! replace(unicode_titlecase) end |
#unicode_upcase ⇒ Object
81 82 83 |
# File 'lib/unicode_titlecase/core_ext/string.rb', line 81 def unicode_upcase UnicodeUtils.upcase(self) end |
#unicode_upcase! ⇒ Object
85 86 87 |
# File 'lib/unicode_titlecase/core_ext/string.rb', line 85 def unicode_upcase! replace(unicode_upcase) end |