titlecase 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.
data/README ADDED
@@ -0,0 +1,37 @@
1
+ Ruby String Extensions To Add Title Case Support
2
+ ================================================
3
+
4
+ A set of methods added onto the String class to allow easy title casing of strings.
5
+
6
+ This extension is available as a gem via GitHub:
7
+
8
+ $ sudo gem install samsouder-titlecase --source=http://gems.github.com
9
+
10
+ Usage:
11
+
12
+ require 'rubygems'
13
+ require 'titlecase'
14
+
15
+ puts "this is a test".titlecase
16
+
17
+ Derived from the rules set by John Gruber at <http://daringfireball.net/2008/05/title_case>. I also derived my test cases from his set. If you find any edge cases, please feel free to add them to the examples.yaml file.
18
+
19
+ Rules from John Gruber:
20
+ -----------------------
21
+ - capitalize each word
22
+ - downcase each of the small_words
23
+ - words with capitals after the first character are left alone
24
+ - words with periods are left alone
25
+ - first and last word always capitalized
26
+ - small words after colons are capitalized
27
+
28
+ Sam Souder <samsouder@gmail.com>
29
+ Created May 22, 2008
30
+
31
+ Perl version created by:
32
+
33
+ John Gruber
34
+ http://daringfireball.net/
35
+ May 10 2008
36
+
37
+ License: http://www.opensource.org/licenses/mit-license.php
@@ -0,0 +1,36 @@
1
+ class String
2
+ def titlecase
3
+ small_words = %w(a an and as at but by en for if in of on or the to v v. via vs vs.)
4
+
5
+ x = split(" ").map do |word|
6
+ # note: word could contain non-word characters!
7
+ # downcase all small_words, capitalize the rest
8
+ small_words.include?(word.gsub(/\W/, "").downcase) ? word.downcase! : word.smart_capitalize!
9
+ word
10
+ end
11
+ # capitalize first and last words
12
+ x.first.to_s.smart_capitalize!
13
+ x.last.to_s.smart_capitalize!
14
+ # small words after colons are capitalized
15
+ x.join(" ").gsub(/:\s?(\W*#{small_words.join("|")}\W*)\s/) { ": #{$1.smart_capitalize} " }
16
+ end
17
+
18
+ def titlecase!
19
+ replace(titlecase)
20
+ end
21
+
22
+ def smart_capitalize
23
+ # ignore any leading crazy characters and capitalize the first real character
24
+ if self =~ /^['"\(\[']*([a-z])/
25
+ i = index($1)
26
+ x = self[i,self.length]
27
+ # word with capitals and periods mid-word are left alone
28
+ self[i,1] = self[i,1].upcase unless x =~ /[A-Z]/ or x =~ /\.\w+/
29
+ end
30
+ self
31
+ end
32
+
33
+ def smart_capitalize!
34
+ replace(smart_capitalize)
35
+ end
36
+ end
@@ -0,0 +1,45 @@
1
+ should_pass:
2
+ - example: | Q&A with Steve Jobs: 'That's what happens in technology'
3
+ expect: | Q&A With Steve Jobs: 'That's What Happens in Technology'
4
+ - example: | what is AT&T's problem?
5
+ expect: | What Is AT&T's Problem?
6
+ - example: | Apple deal with AT&T falls through
7
+ expect: | Apple Deal With AT&T Falls Through
8
+ - example: | this v that
9
+ expect: | This v That
10
+ - example: | this vs that
11
+ expect: | This vs That
12
+ - example: | this v. that
13
+ expect: | This v. That
14
+ - example: | this vs. that
15
+ expect: | This vs. That
16
+ - example: | The SEC's Apple probe: what you need to know
17
+ expect: | The SEC's Apple Probe: What You Need to Know
18
+ - example: | 'by the way, small word at the start but within quotes.'
19
+ expect: | 'By the Way, Small Word at the Start but Within Quotes.'
20
+ - example: | small word at end is nothing to be afraid of
21
+ expect: | Small Word at End Is Nothing to Be Afraid Of
22
+ - example: | Starting Sub-Phrase with a small word: a trick, perhaps?
23
+ expect: | Starting Sub-Phrase With a Small Word: A Trick, Perhaps?
24
+ - example: | Sub-Phrase With a Small Word in Quotes: 'a trick, perhaps?'
25
+ expect: | Sub-Phrase With a Small Word in Quotes: 'A Trick, Perhaps?'
26
+ - example: | Sub-Phrase With a Small Word in Quotes: "a trick, perhaps?"
27
+ expect: | Sub-Phrase With a Small Word in Quotes: "A Trick, Perhaps?"
28
+ - example: | "nothing to be afraid of?"
29
+ expect: | "Nothing to Be Afraid Of?"
30
+ - example: | a thing
31
+ expect: | A Thing
32
+ - example: | 2lmc Spool: 'Gruber on OmniFocus and Vapo(u)rware'
33
+ expect: | 2lmc Spool: 'Gruber on OmniFocus and Vapo(u)rware'
34
+ - example: | a complex thing
35
+ expect: | A Complex Thing
36
+ - example: | de.licio.us: the new hotness?
37
+ expect: | de.licio.us: The New Hotness?
38
+ - example: | introducing: de.licio.us bookmarks
39
+ expect: | Introducing: de.licio.us Bookmarks
40
+ - example: | i end in a period.
41
+ expect: | I End in a Period.
42
+ - example: | this is a test.com!?
43
+ expect: | This Is a test.com!?
44
+ - example: |
45
+ expect: |
@@ -0,0 +1,20 @@
1
+ require File.join(File.dirname(__FILE__), *%w[../lib/titlecase.rb])
2
+ require "yaml"
3
+
4
+ describe String do
5
+ File.open(File.join(File.dirname(__FILE__), *%w[examples.yaml])) do |file|
6
+ examples = YAML.load(file)
7
+ examples["should_pass"].each do |e|
8
+ it "should be the expected value (#{e["expect"]})" do
9
+ e["example"].titlecase.should == e["expect"]
10
+ end
11
+ end
12
+ end
13
+
14
+ # Ensure the self-modifying version works correctly
15
+ it "should self-modify the original value in place" do
16
+ string = 'a complex thing'
17
+ string.titlecase!
18
+ string.should == 'A Complex Thing'
19
+ end
20
+ end
@@ -0,0 +1,12 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "titlecase"
3
+ s.version = "0.1.0"
4
+ s.date = "2008-05-23"
5
+ s.summary = "String methods to properly title case a headline."
6
+ s.email = "samsouder@gmail.com"
7
+ s.homepage = "http://github.com/samsouder/titlecase"
8
+ s.description = "titlecase is a set of methods on the Ruby String class to add title casing support as seen on Daring Fireball <http://daringfireball.net/2008/05/title_case>."
9
+ s.authors = ["Samuel Souder"]
10
+ s.files = ["README", "titlecase.gemspec", "lib/titlecase.rb", "spec/examples.yaml", "spec/titlecase_spec.rb"]
11
+ s.has_rdoc = false
12
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: titlecase
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Samuel Souder
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-23 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: titlecase is a set of methods on the Ruby String class to add title casing support as seen on Daring Fireball <http://daringfireball.net/2008/05/title_case>.
17
+ email: samsouder@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README
26
+ - titlecase.gemspec
27
+ - lib/titlecase.rb
28
+ - spec/examples.yaml
29
+ - spec/titlecase_spec.rb
30
+ has_rdoc: true
31
+ homepage: http://github.com/samsouder/titlecase
32
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options: []
36
+
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.3.5
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: String methods to properly title case a headline.
58
+ test_files: []
59
+