Module: CineworldUk::Internal::Titleize Private
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Modified from titleize gem github.com/granth/titleize
Constant Summary collapse
- WORDS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
List of words not to capitalize unless they lead a phrase
%w(a an and as at but by en for if in of on or the to via vs vs.)
Instance Method Summary collapse
-
#phrases(title) ⇒ Array<String>
private
Splits a title into an array based on punctuation.
-
#titleize(title) ⇒ String
private
Capitalizes most words to create a nicer looking title string.
Instance Method Details
#phrases(title) ⇒ Array<String>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Splits a title into an array based on punctuation.
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/cineworld_uk/internal/titleize.rb', line 72 def phrases(title) phrases = title.scan(/.+?(?:[-:.;?!] |$)/).map { |phrase| phrase.strip } # rejoin phrases that were split on the '.' from a small word if phrases.size > 1 phrases[0..-2].each_with_index do |phrase, index| next unless WORDS.include?(phrase.split.last.downcase) phrases[index] << ' ' + phrases.slice!(index + 1) end end phrases end |
#titleize(title) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Capitalizes most words to create a nicer looking title string.
The list of “small words” which are not capped comes from the New York Times Manual of Style, plus ‘vs’.
Also capitalises roman numerals
"notes on a scandal" # => "Notes on a Scandal"
"ghostbusters ii" # => "Ghostbusters II"
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/cineworld_uk/internal/titleize.rb', line 26 def titleize(title) title = title.dup title.downcase! unless title[/[[:lower:]]/] # assume all-caps fixing phrases(title).map do |phrase| words = phrase.split words.map do |word| def word.capitalize # like String#capitalize, but it starts with the first letter sub(/[[:alpha:]].*/) { |subword| subword.capitalize } end case word when /[[:alpha:]]\.[[:alpha:]]/ # words with dots in word when /[-‑]/ # hyphenated word (regular and non-breaking) word.split(/([-‑])/).map do |part| WORDS.include?(part) ? part : part.capitalize end.join when /^[[:alpha:]].*[[:upper:]]/ # non-first letter capitalized word when /^[[:digit:]]/ # first character is a number word when /^(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/i word.upcase when words.first, words.last word.capitalize when *(WORDS + WORDS.map { |small| small.capitalize }) word.downcase else word.capitalize end end.join(' ') end.join(' ') end |