harukizaemon-titleizer 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +3 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +33 -0
- data/lib/haruki_zaemon/titleizer/string.rb +62 -0
- data/lib/titleizer.rb +1 -0
- data/titleizer.gemspec +19 -0
- metadata +60 -0
data/CHANGELOG.rdoc
ADDED
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.
|
data/README.rdoc
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
= Titleizer
|
2
|
+
|
3
|
+
Titleizer is a gem that adds String#titleize (aliased as String#titlecase) to perform smart capitalisation of titles:
|
4
|
+
|
5
|
+
"terms of reference".titleize # => "Terms of Reference"
|
6
|
+
"my name is barron von sampy pants and you shall refer to me as such".titleize # => "My Name is Barron Von Sampy Pants and You Shall Refer to Me as Such"
|
7
|
+
|
8
|
+
Titleizer also converts underscores and dashes to spaces:
|
9
|
+
|
10
|
+
"a-slug-from-a-url".titleize # => "A Slug From a Url"
|
11
|
+
|
12
|
+
Titleizer also preserves the capitalisation of any word it would capitalise itself:
|
13
|
+
|
14
|
+
"THIS IS A TITLE".titleize # => THIS is a TITLE"
|
15
|
+
|
16
|
+
And lastly, String#titleize is aliased as String#titlecase.
|
17
|
+
|
18
|
+
Oh, one last, last thing, there are self-updating versions as well: String#titleize!, and String#titlecase!
|
19
|
+
|
20
|
+
== Installation
|
21
|
+
|
22
|
+
For plain Ruby, simply install the gem and you're good to go.
|
23
|
+
|
24
|
+
sudo gem sources -a http://gems.github.com
|
25
|
+
sudo gem install harukizaemon-titleizer
|
26
|
+
|
27
|
+
For Rails, update environment.rb to include the gem:
|
28
|
+
|
29
|
+
config.gem "harukizaemon-titleizer", :lib => "titleizer", :source => "http://gems.github.com"
|
30
|
+
|
31
|
+
=== License
|
32
|
+
|
33
|
+
This plugin is copyright 2009 Simon Harris and is released under the MIT license.
|
@@ -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
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
String.send(:include, HarukiZaemon::Titleizer::String)
|
data/titleizer.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "titleizer"
|
3
|
+
s.version = "1.0.1"
|
4
|
+
s.date = "2009-02-01"
|
5
|
+
s.summary = "Smart capitalisation of titles"
|
6
|
+
s.email = "haruki.zaemon@gmail.com"
|
7
|
+
s.homepage = "http://github.com/harukizaemon/titleizer"
|
8
|
+
s.description = "Adds String#titleize and String#titlecase! to perform smart capitalisation of titles"
|
9
|
+
s.has_rdoc = true
|
10
|
+
s.authors = ["Simon Harris"]
|
11
|
+
s.files = ["CHANGELOG.rdoc",
|
12
|
+
"MIT-LICENSE",
|
13
|
+
"README.rdoc",
|
14
|
+
"titleizer.gemspec",
|
15
|
+
"lib/titleizer.rb",
|
16
|
+
"lib/haruki_zaemon/titleizer/string.rb"]
|
17
|
+
s.rdoc_options = ["--main", "README.rdoc"]
|
18
|
+
s.extra_rdoc_files = ["CHANGELOG.rdoc", "README.rdoc"]
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: harukizaemon-titleizer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Simon Harris
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-01 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Adds String#titleize and String#titlecase! to perform smart capitalisation of titles
|
17
|
+
email: haruki.zaemon@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- CHANGELOG.rdoc
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- CHANGELOG.rdoc
|
27
|
+
- MIT-LICENSE
|
28
|
+
- README.rdoc
|
29
|
+
- titleizer.gemspec
|
30
|
+
- lib/titleizer.rb
|
31
|
+
- lib/haruki_zaemon/titleizer/string.rb
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://github.com/harukizaemon/titleizer
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options:
|
36
|
+
- --main
|
37
|
+
- README.rdoc
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.2.0
|
56
|
+
signing_key:
|
57
|
+
specification_version: 2
|
58
|
+
summary: Smart capitalisation of titles
|
59
|
+
test_files: []
|
60
|
+
|