titleize_pt 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 737c0ea857ef938f57b0faa675b27fe442661509
4
+ data.tar.gz: 8e946795da834170d191b96c493dcc02fe631485
5
+ SHA512:
6
+ metadata.gz: 6167c902302ff51dbb51e4a0a92373f4c94c89b3cedc05e2353064ec185360ae79ef3c53e04893b5829a27b9fc742efa3a0375a81c35c704652af9700401588a
7
+ data.tar.gz: 7c1a2c2ee2d5a18c7fb43c86406edd839d02be0c2aa7fc26c7b6df3058d71ad1fe4f6e00f2d537153651581dbdca5c014ac64dcb841375144d3cf90887867e22
data/README.md CHANGED
@@ -27,7 +27,7 @@ The [`titleize`](https://github.com/granth/titleize) gem does some extra work to
27
27
  "a lovely and talented title".titleize # => "A Lovely and Talented Title"
28
28
  ```
29
29
 
30
- This gem is not nearly as advanced as the `titleize` gem but knows how to deal with characters and handles titles in Portuguese:
30
+ This gem is not nearly as advanced as the `titleize` gem but knows how to deal with unicode characters and titles in Portuguese:
31
31
 
32
32
  ```ruby
33
33
  "GUIA DE SOBREVIVÊNCIA".titleize_pt # => "Guia de Sobrevivência"
@@ -17,59 +17,35 @@ module TitleizePT
17
17
  # Portuguese words that should not be capitalized. To improve the list, the
18
18
  # "Acordo Ortográfico de 1945" document may be a good resource.
19
19
  # Wikipedia also has guidelines on this: http://goo.gl/28T0h
20
- pt: %w{ a as da das de do dos e o os para por sobre um uns uma umas }
20
+ pt: %w{ a as da das de do dos e em na nas no nos o os para por sobre um uns uma umas }
21
21
  }
22
22
 
23
- def self.titleize_pt(title)
24
- titleize_locale(title, :pt)
25
- end
26
-
27
- def self.titleize_locale(title, locale = nil)
28
- locale = (WORDS.key?(I18n.locale) ? I18n.locale : :pt) if locale.blank?
29
-
30
- if title[/[[:lower:]]/]
31
- # Similar to the existent `titleize` method from ActiveSupport but respect acronyms
32
- title = title.split.map { |w| /^\p{Upper}*$/.match(w) ? w : w.downcase }.join(' ')
33
- else
34
- title.downcase! # If the entire title is all-uppercase, assume it needs to be fixed
35
- end
36
-
37
- title = title.to_s.gsub(/\b('?[\S])/u) { ActiveSupport::Multibyte::Unicode.apply_mapping $1, :uppercase_mapping }
38
- words = title.split
39
- first = words.shift
40
-
41
- if words.empty?
42
- first # The first word should always be capitalized
43
- else
44
- first +" "+ words.map { |w| WORDS[locale].include?(w.downcase) ? w.downcase : w }.join(' ')
45
- end
46
- end
47
- end
48
-
49
- class String
50
23
  def titleize_pt
51
- TitleizePT.titleize_pt(self.mb_chars)
24
+ titleize_locale :pt
52
25
  end
53
26
 
54
- def titleize_locale(locale = nil)
55
- TitleizePT.titleize_locale(self.mb_chars, locale)
27
+ def titleize_locale(locale = I18n.locale)
28
+ locale = :pt unless WORDS.key? locale # Fallback to PT if locale is not supported
29
+ title = mb_chars # This proxies string methods in an encoding safe manner
30
+
31
+ # If the title is all-uppercase, assume it needs to be fixed and downcase it entirely
32
+ title.downcase! unless title[/[[:lower:]]/]
33
+
34
+ title.split(/(\b)/).each_with_index.map do |word, index|
35
+ if word =~ /^\p{Upper}{2,}$/ # Respect acronyms
36
+ word
37
+ elsif WORDS[locale].include? word.downcase and not index.zero?
38
+ word.downcase!
39
+ else
40
+ word.capitalize!
41
+ end
42
+ end.join($1)
56
43
  end
57
44
 
58
45
  alias_method :titlecase_pt, :titleize_pt
59
46
  alias_method :titlecase_locale, :titleize_locale
60
47
  end
61
48
 
62
- module ActiveSupport::Multibyte
63
- class Chars
64
- def titleize_pt
65
- chars TitleizePT.titleize_pt(self)
66
- end
67
-
68
- def titleize_locale(locale = nil)
69
- chars TitleizePT.titleize_locale(self, locale)
70
- end
71
-
72
- alias_method :titlecase_pt, :titleize_pt
73
- alias_method :titlecase_locale, :titleize_locale
74
- end
49
+ class String
50
+ include TitleizePT
75
51
  end
@@ -1,3 +1,3 @@
1
1
  module TitleizePT
2
- VERSION = '0.0.1'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -67,4 +67,14 @@ describe "TitleizePT module" do
67
67
  it "should leave all-uppercase words untouched" do
68
68
  "um pedido HTTP".titleize_pt.must_equal "Um Pedido HTTP"
69
69
  end
70
+
71
+ it "should not modify its receiver" do
72
+ title = "olá, mundo"
73
+ title.titleize_pt.must_equal "Olá, Mundo"
74
+ title.must_equal "olá, mundo"
75
+
76
+ title = "OLÁ, MUNDO"
77
+ title.titleize_pt.must_equal "Olá, Mundo"
78
+ title.must_equal "OLÁ, MUNDO"
79
+ end
70
80
  end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: titleize_pt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
4
+ version: 0.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - David Francisco
@@ -14,7 +13,6 @@ dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rake
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: activesupport
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ~>
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ~>
44
39
  - !ruby/object:Gem::Version
@@ -61,27 +56,26 @@ files:
61
56
  - titleize_pt.gemspec
62
57
  homepage:
63
58
  licenses: []
59
+ metadata: {}
64
60
  post_install_message:
65
61
  rdoc_options: []
66
62
  require_paths:
67
63
  - lib
68
64
  required_ruby_version: !ruby/object:Gem::Requirement
69
- none: false
70
65
  requirements:
71
- - - ! '>='
66
+ - - '>='
72
67
  - !ruby/object:Gem::Version
73
68
  version: 1.9.3
74
69
  required_rubygems_version: !ruby/object:Gem::Requirement
75
- none: false
76
70
  requirements:
77
- - - ! '>='
71
+ - - '>='
78
72
  - !ruby/object:Gem::Version
79
73
  version: '0'
80
74
  requirements: []
81
75
  rubyforge_project:
82
- rubygems_version: 1.8.23
76
+ rubygems_version: 2.0.0
83
77
  signing_key:
84
- specification_version: 3
78
+ specification_version: 4
85
79
  summary: Properly capitalize titles in Portuguese
86
80
  test_files:
87
81
  - spec/spec_helper.rb