Module: TitleizePT

Included in:
String
Defined in:
lib/titleize_pt.rb,
lib/titleize_pt/version.rb

Overview

Creates properly capitalized titles (original implementation found at git.io/i0UnkA). For example, “a lovely and talented title” becomes “A Lovely and Talented Title”.

Supports unicode characters (using ActiveSupport::Multibyte::Chars). For example, “OLÁ MUNDO” becomes “Olá Mundo” instead of “OlÁ Mundo”.

Constant Summary collapse

WORDS =
{
  # List of words from the "New York Times Manual of Style"
  en: %w{ a an and as at but by en for if in of on or the to v v. via vs vs. },

  # Portuguese words that should not be capitalized. To improve the list, the
  # "Acordo Ortográfico de 1945" document may be a good resource.
  # Wikipedia also has guidelines on this: http://goo.gl/28T0h
  pt: %w{ a as da das de do dos e em na nas no nos o os para por sobre um uns uma umas }
}
VERSION =
'0.1.0'

Instance Method Summary collapse

Instance Method Details

#titleize_locale(locale = I18n.locale) ⇒ Object Also known as: titlecase_locale



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/titleize_pt.rb', line 27

def titleize_locale(locale = I18n.locale)
  locale = :pt unless WORDS.key? locale # Fallback to PT if locale is not supported
  title = mb_chars # This proxies string methods in an encoding safe manner

  # If the title is all-uppercase, assume it needs to be fixed and downcase it entirely
  title.downcase! unless title[/[[:lower:]]/]

  title.split(/(\b)/).each_with_index.map do |word, index|
    if word =~ /^\p{Upper}{2,}$/ # Respect acronyms
      word
    elsif WORDS[locale].include? word.downcase and not index.zero?
      word.downcase!
    else
      word.capitalize!
    end
  end.join($1)
end

#titleize_ptObject Also known as: titlecase_pt



23
24
25
# File 'lib/titleize_pt.rb', line 23

def titleize_pt
  titleize_locale :pt
end