harukizaemon-titleizer 1.0.2 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +8 -0
- data/MIT-LICENSE +20 -0
- data/lib/haruki_zaemon/titleizer/string.rb +62 -0
- data/lib/titleizer.rb +1 -0
- data/titleizer.gemspec +4 -4
- metadata +4 -4
- data/Rakefile +0 -7
- data/spec/haruki_zaemon/titleizer/string_spec.rb +0 -44
data/CHANGELOG.rdoc
CHANGED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Simon Harris
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# This filter changes all words to Title Caps, and attempts to be clever
|
2
|
+
# about *un*capitalizing small words like a/an/the in the input.
|
3
|
+
#
|
4
|
+
# The list of "small words" which are not capped comes from
|
5
|
+
# the New York Times Manual of Style, plus 'vs' and 'v'.
|
6
|
+
#
|
7
|
+
# Original Perl version by:
|
8
|
+
# John Gruber
|
9
|
+
# http://daringfireball.net/
|
10
|
+
# 10 May 2008
|
11
|
+
#
|
12
|
+
# Adapted to Ruby by:
|
13
|
+
# Marshall Elfstrand
|
14
|
+
# http://vengefulcow.com/
|
15
|
+
# 21 May 2008
|
16
|
+
#
|
17
|
+
# License: http://www.opensource.org/licenses/mit-license.php
|
18
|
+
#
|
19
|
+
module HarukiZaemon
|
20
|
+
module Titleizer
|
21
|
+
module String
|
22
|
+
SMALL_WORDS = %w( is a an and as at but by en for if in of on or the to v[.]? via vs[.]? )
|
23
|
+
SMALL_RE = SMALL_WORDS.join('|')
|
24
|
+
|
25
|
+
def titleize
|
26
|
+
result = ""
|
27
|
+
self.gsub(/[_-]/, ' ').split(/( [:.;?!][ ] | (?:[ ]|^)["“] )/x).each do |s|
|
28
|
+
s.gsub!(/ \b( [[:alpha:]] [[:lower:].'’]* )\b /x) do |w|
|
29
|
+
# Skip words with inresult dots, e.g. "del.icio.us" or "example.com"
|
30
|
+
(w =~ / [[:alpha:]] [.] [[:alpha:]] /x) ? w : w.capitalize
|
31
|
+
end #gsub!
|
32
|
+
|
33
|
+
# Lowercase our list of small words:
|
34
|
+
s.gsub!(/\b(#{SMALL_RE})\b/io) { |w| w.downcase }
|
35
|
+
|
36
|
+
# If the first word in the title is a small word, then capitalize it:
|
37
|
+
s.gsub!(/\A([[:punct:]]*)(#{SMALL_RE})\b/io) { |w| $1 + $2.capitalize }
|
38
|
+
|
39
|
+
# If the last word in the title is a small word, then capitalize it:
|
40
|
+
s.gsub!(/\b(#{SMALL_RE})([[:punct:]]*)\Z/io) { |w| $1.capitalize + $2 }
|
41
|
+
|
42
|
+
# Append current substring to output
|
43
|
+
result += s
|
44
|
+
end #each
|
45
|
+
|
46
|
+
# Special Cases:
|
47
|
+
result.gsub!(/ V(s?)\. /, ' v\1. ') # "v." and "vs."
|
48
|
+
result.gsub!(/(['’])S\b/, '\1s') # 'S (otherwise you get "the SEC'S decision")
|
49
|
+
result.gsub!(/\b(AT&T|Q&A)\b/i) { |w| w.upcase } # "AT&T" and "Q&A", which get tripped up by
|
50
|
+
# self-contained small words "at" and "a"
|
51
|
+
|
52
|
+
result
|
53
|
+
end
|
54
|
+
alias_method :titlecase, :titleize
|
55
|
+
|
56
|
+
def titleize!
|
57
|
+
self.replace(self.titleize)
|
58
|
+
end
|
59
|
+
alias_method :titlecase!, :titleize!
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/lib/titleizer.rb
CHANGED
data/titleizer.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "titleizer"
|
3
|
-
s.version = "1.0.
|
4
|
-
s.date = "2009-
|
3
|
+
s.version = "1.0.4"
|
4
|
+
s.date = "2009-03-13"
|
5
5
|
s.summary = "Smart capitalisation of titles"
|
6
6
|
s.email = "haruki.zaemon@gmail.com"
|
7
7
|
s.homepage = "http://github.com/harukizaemon/titleizer"
|
@@ -9,11 +9,11 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.has_rdoc = true
|
10
10
|
s.authors = ["Simon Harris"]
|
11
11
|
s.files = ["CHANGELOG.rdoc",
|
12
|
-
"
|
12
|
+
"MIT-LICENSE",
|
13
13
|
"README.rdoc",
|
14
14
|
"titleizer.gemspec",
|
15
15
|
"lib/titleizer.rb",
|
16
|
-
"
|
16
|
+
"lib/haruki_zaemon/titleizer/string.rb"]
|
17
17
|
s.rdoc_options = ["--main", "README.rdoc"]
|
18
18
|
s.extra_rdoc_files = ["CHANGELOG.rdoc", "README.rdoc"]
|
19
19
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: harukizaemon-titleizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Simon Harris
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-03-13 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -24,11 +24,11 @@ extra_rdoc_files:
|
|
24
24
|
- README.rdoc
|
25
25
|
files:
|
26
26
|
- CHANGELOG.rdoc
|
27
|
-
-
|
27
|
+
- MIT-LICENSE
|
28
28
|
- README.rdoc
|
29
29
|
- titleizer.gemspec
|
30
30
|
- lib/titleizer.rb
|
31
|
-
-
|
31
|
+
- lib/haruki_zaemon/titleizer/string.rb
|
32
32
|
has_rdoc: true
|
33
33
|
homepage: http://github.com/harukizaemon/titleizer
|
34
34
|
post_install_message:
|
data/Rakefile
DELETED
@@ -1,44 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../../../lib/haruki_zaemon/titleizer/string.rb'
|
2
|
-
require File.dirname(__FILE__) + '/../../../lib/titleizer'
|
3
|
-
|
4
|
-
describe String do
|
5
|
-
describe "#titleize" do
|
6
|
-
it "should return a new string" do
|
7
|
-
original = "have a nice day"
|
8
|
-
titleized = original.titleize
|
9
|
-
original.should_not === titleized
|
10
|
-
end
|
11
|
-
|
12
|
-
it "should always capitalise the first word" do
|
13
|
-
"a b".titleize.should =~ /^A/
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should always capitalise the last word" do
|
17
|
-
"a b".titleize.should =~ /B$/
|
18
|
-
end
|
19
|
-
|
20
|
-
%w(is a an and as at but by en for if in of on or the to via vs).each do |word|
|
21
|
-
it "should not capitalise '#{word}'" do
|
22
|
-
"X #{word} Y".titleize.should == "X #{word} Y"
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
%w{- _}.each do |punc|
|
27
|
-
it "should convert '#{punc}' to spaces" do
|
28
|
-
"a#{punc}title".titleize.should == "A Title"
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
it "should capitalise according to title case rules" do
|
33
|
-
"once upon a time in a far off land".titleize.should == "Once Upon a Time in a Far Off Land"
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
describe "#titleize!" do
|
38
|
-
it "should replace the contents of the string" do
|
39
|
-
original = "have a nice day"
|
40
|
-
titleized = original.titleize!
|
41
|
-
original.should === titleized
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|