titleize_pt 0.0.1
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.
- data/.gitignore +10 -0
- data/Gemfile +2 -0
- data/LICENSE +22 -0
- data/README.md +46 -0
- data/Rakefile +21 -0
- data/lib/titleize_pt/version.rb +3 -0
- data/lib/titleize_pt.rb +75 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/titleize_pt/titleize_spec.rb +70 -0
- data/titleize_pt.gemspec +18 -0
- metadata +88 -0
data/.gitignore
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# See http://help.github.com/ignore-files/ for more about ignoring files.
|
2
|
+
#
|
3
|
+
# If you find yourself ignoring temporary files generated by your text editor
|
4
|
+
# or operating system, you probably want to add a global ignore instead:
|
5
|
+
# git config --global core.excludesfile '~/.gitignore_global'
|
6
|
+
|
7
|
+
# Ruby specifics
|
8
|
+
Gemfile.lock
|
9
|
+
pkg/*
|
10
|
+
.yardoc
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 David Francisco
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
### Properly capitalize titles in Portuguese
|
2
|
+
|
3
|
+
ActiveSupport gives us the `titleize` method to create nice looking titles, but sometimes the result may not be what you excepted:
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
"GUIA DE SOBREVIVÊNCIA".titleize # => "Guia De SobrevivÊncia"
|
7
|
+
"órgão emissor".titleize # => "órgão Emissor"
|
8
|
+
"HTTP request".titleize # => "Http Request"
|
9
|
+
```
|
10
|
+
|
11
|
+
Though this may not be hard to solve:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
"GUIA DE SOBREVIVÊNCIA".mb_chars.titleize # => "Guia De Sobrevivência"
|
15
|
+
"órgão emissor".mb_chars.titleize # => "Órgão Emissor"
|
16
|
+
ActiveSupport::Inflector.inflections { |inflect| inflect.acronym "HTTP" }
|
17
|
+
"HTTP request".titleize # => "HTTP Request"
|
18
|
+
```
|
19
|
+
|
20
|
+
The [`titleize`](https://github.com/granth/titleize) gem does some extra work to properly capitalize titles in English:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
# ActiveSupport titleize method
|
24
|
+
"a lovely and talented title".titleize # => "A Lovely And Talented Title"
|
25
|
+
|
26
|
+
# Using the titleize gem
|
27
|
+
"a lovely and talented title".titleize # => "A Lovely and Talented Title"
|
28
|
+
```
|
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:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
"GUIA DE SOBREVIVÊNCIA".titleize_pt # => "Guia de Sobrevivência"
|
34
|
+
"proposta para uma directiva da UE".titleize_pt # => "Proposta para uma Directiva da UE"
|
35
|
+
"a lovely and talented title".titleize_locale(:en) # => "A Lovely and Talented Title"
|
36
|
+
```
|
37
|
+
|
38
|
+
### Usage
|
39
|
+
|
40
|
+
The gem requires ActiveSupport, so it's more suitable for Ruby on Rails projects. Add to your Gemfile and run `bundle`:
|
41
|
+
|
42
|
+
gem "titleize_pt"
|
43
|
+
|
44
|
+
### Credits
|
45
|
+
|
46
|
+
Some tests and code copied from [granth/titleize](https://github.com/granth/titleize).
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
|
3
|
+
# To turn specs with verbose output:
|
4
|
+
# bundle exec rake test:spec TESTOPTS="--verbose"
|
5
|
+
#
|
6
|
+
# For additional help:
|
7
|
+
# rake --tasks
|
8
|
+
#
|
9
|
+
require 'rake/testtask'
|
10
|
+
require 'bundler/gem_tasks'
|
11
|
+
|
12
|
+
Rake::TestTask.new(:spec) do |t|
|
13
|
+
ENV["RACK_ENV"] = "test"
|
14
|
+
t.libs << "spec"
|
15
|
+
t.pattern = "spec/**/*_spec.rb"
|
16
|
+
end
|
17
|
+
|
18
|
+
desc "Uninstall the current version of titleize_pt"
|
19
|
+
task :uninstall do
|
20
|
+
puts `gem uninstall -x titleize_pt` # -x flag uninstalls executables too without confirmation
|
21
|
+
end
|
data/lib/titleize_pt.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'active_support/multibyte'
|
4
|
+
require 'active_support/core_ext/string'
|
5
|
+
|
6
|
+
# Creates properly capitalized titles (original implementation found at git.io/i0UnkA).
|
7
|
+
# For example, "a lovely and talented title" becomes "A Lovely and Talented Title".
|
8
|
+
#
|
9
|
+
# Supports unicode characters (using ActiveSupport::Multibyte::Chars).
|
10
|
+
# For example, "OLÁ MUNDO" becomes "Olá Mundo" instead of "OlÁ Mundo".
|
11
|
+
#
|
12
|
+
module TitleizePT
|
13
|
+
WORDS = {
|
14
|
+
# List of words from the "New York Times Manual of Style"
|
15
|
+
en: %w{ a an and as at but by en for if in of on or the to v v. via vs vs. },
|
16
|
+
|
17
|
+
# Portuguese words that should not be capitalized. To improve the list, the
|
18
|
+
# "Acordo Ortográfico de 1945" document may be a good resource.
|
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 }
|
21
|
+
}
|
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
|
+
def titleize_pt
|
51
|
+
TitleizePT.titleize_pt(self.mb_chars)
|
52
|
+
end
|
53
|
+
|
54
|
+
def titleize_locale(locale = nil)
|
55
|
+
TitleizePT.titleize_locale(self.mb_chars, locale)
|
56
|
+
end
|
57
|
+
|
58
|
+
alias_method :titlecase_pt, :titleize_pt
|
59
|
+
alias_method :titlecase_locale, :titleize_locale
|
60
|
+
end
|
61
|
+
|
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
|
75
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
# Some of the following tests were taken from the original module (git.io/i0UnkA)
|
6
|
+
describe "TitleizePT module" do
|
7
|
+
it "should return a string if a string is given" do
|
8
|
+
"this".titleize_locale.must_be_instance_of String
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should return an ActiveSupport::Multibyte::Chars object if one is given" do
|
12
|
+
"this".mb_chars.titleize_locale.must_be_instance_of ActiveSupport::Multibyte::Chars
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should properly capitalize titles that contain unicode characters" do
|
16
|
+
"café périferôl".titleize_pt.must_equal "Café Périferôl"
|
17
|
+
"órgão partidário".titleize_pt.must_equal "Órgão Partidário"
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should return the same result for `string.titleize_pt` and `mb_chars.titleize_pt`" do
|
21
|
+
"café périferôl".titleize_pt.must_equal "café périferôl".mb_chars.titleize_pt.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should fix all-caps titles" do
|
25
|
+
"GUIA DE SOBREVIVÊNCIA".titleize_pt.must_equal "Guia de Sobrevivência"
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should capitalize the first letter of regular words" do
|
29
|
+
"cat beats monkey".titleize_locale.must_equal "Cat Beats Monkey"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should not capitalize listed words" do
|
33
|
+
TitleizePT::WORDS[:en].each do |word|
|
34
|
+
"first #{ word } last".titleize_locale.must_equal "First #{ word } Last"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should downcase a listed word if it is capitalized" do
|
39
|
+
TitleizePT::WORDS[:en].each do |word|
|
40
|
+
"first #{ word.capitalize } last".titleize_locale.must_equal "First #{ word } Last"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should not think a quotation mark makes a dot word" do
|
45
|
+
"'quoted.' yes.".titleize_locale.must_equal "'Quoted.' Yes."
|
46
|
+
"ends with 'quotation.'".titleize_locale.must_equal "Ends With 'Quotation.'"
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should not capitalize words that start with a number" do
|
50
|
+
"2lmc".titleize_locale.must_equal "2lmc"
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should handle hyphenated words" do
|
54
|
+
"the la-la land".titleize_locale.must_equal "The La-La Land"
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should handle non-breaking hyphens" do
|
58
|
+
"non‑breaking hyphen".titleize_locale.must_equal "Non‑Breaking Hyphen"
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should capitalize a listed word if it is the first word" do
|
62
|
+
TitleizePT::WORDS[:en].each do |word|
|
63
|
+
"#{ word } is listed".titleize_locale.must_equal "#{ word.capitalize } Is Listed"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should leave all-uppercase words untouched" do
|
68
|
+
"um pedido HTTP".titleize_pt.must_equal "Um Pedido HTTP"
|
69
|
+
end
|
70
|
+
end
|
data/titleize_pt.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.expand_path('../lib/titleize_pt/version', __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.name = 'titleize_pt'
|
5
|
+
gem.version = TitleizePT::VERSION
|
6
|
+
gem.authors = ['David Francisco']
|
7
|
+
gem.email = 'titleize@dmfranc.com'
|
8
|
+
gem.summary = 'Properly capitalize titles in Portuguese'
|
9
|
+
gem.platform = Gem::Platform::RUBY
|
10
|
+
gem.required_ruby_version = '>= 1.9.3'
|
11
|
+
|
12
|
+
gem.files = `git ls-files`.split($\)
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
|
16
|
+
gem.add_development_dependency "rake", "~> 10.0.4"
|
17
|
+
gem.add_runtime_dependency "activesupport", "~> 3.2.13"
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: titleize_pt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- David Francisco
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-20 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 10.0.4
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 10.0.4
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activesupport
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 3.2.13
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 3.2.13
|
46
|
+
description:
|
47
|
+
email: titleize@dmfranc.com
|
48
|
+
executables: []
|
49
|
+
extensions: []
|
50
|
+
extra_rdoc_files: []
|
51
|
+
files:
|
52
|
+
- .gitignore
|
53
|
+
- Gemfile
|
54
|
+
- LICENSE
|
55
|
+
- README.md
|
56
|
+
- Rakefile
|
57
|
+
- lib/titleize_pt.rb
|
58
|
+
- lib/titleize_pt/version.rb
|
59
|
+
- spec/spec_helper.rb
|
60
|
+
- spec/titleize_pt/titleize_spec.rb
|
61
|
+
- titleize_pt.gemspec
|
62
|
+
homepage:
|
63
|
+
licenses: []
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: 1.9.3
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.8.23
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Properly capitalize titles in Portuguese
|
86
|
+
test_files:
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
- spec/titleize_pt/titleize_spec.rb
|