title_case 0.1.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.
- checksums.yaml +7 -0
- data/README.md +43 -0
- data/bin/titlecase +16 -0
- data/lib/title_case.rb +27 -0
- data/spec/title_case_spec.rb +58 -0
- data/title_case.gemspec +15 -0
- metadata +51 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6520a30961251756fa27f58ef65ae05e3f741700
|
4
|
+
data.tar.gz: b9b0b74909e8906d8bd5823933262367c01167dc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8d27296244fe8273f23e446a788b31036fc86a6b64247d44c8878e9e04f5ff384c45c5a3c1c9e928c2fff219249896c2dc34d9f53a06d86bab66e5eb8783e19b
|
7
|
+
data.tar.gz: cf788e6391772dd0c2b19a3a825bc2347cf12c9c489fbaa1cbf329ce97720e6a16fb6e13f00d656ed840b278fc239afcc0898986654617cc43d69576fcd13625
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
title_case
|
2
|
+
---------
|
3
|
+
|
4
|
+
A ruby gem consisting of String methods to title case strings,
|
5
|
+
including a command-line demonstration utility.
|
6
|
+
|
7
|
+
Method Usage:
|
8
|
+
|
9
|
+
require 'rubygems'
|
10
|
+
require 'title_case'
|
11
|
+
|
12
|
+
FIXME this is not done yet.
|
13
|
+
|
14
|
+
Command Line Usage:
|
15
|
+
|
16
|
+
$ echo capitalize a phrase like a newspaper editor would | titlecase
|
17
|
+
|
18
|
+
Capitalize a Phrase Like a Newspaper Editor Would
|
19
|
+
|
20
|
+
Inspiration and History
|
21
|
+
---------
|
22
|
+
|
23
|
+
This is partially inspired and informed by John Gruber's titlecase:
|
24
|
+
<http://daringfireball.net/2008/05/title_case>
|
25
|
+
and Sam Souder's ruby implementation:
|
26
|
+
FIXME
|
27
|
+
|
28
|
+
I didn't like a few of the rules in Gruber's version, though.
|
29
|
+
Major differences are:
|
30
|
+
- don't ever dowcase anything
|
31
|
+
- the last word doesn't get upcased if it is on the exclude list
|
32
|
+
- the exclude list is slightly different
|
33
|
+
- colons get no special treatment
|
34
|
+
|
35
|
+
|
36
|
+
License
|
37
|
+
---------
|
38
|
+
© 2014 Noah Birnel
|
39
|
+
Do whatever you want to with this code.
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
|
data/bin/titlecase
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'title_case'
|
4
|
+
|
5
|
+
# Boilerplate for adding flags.
|
6
|
+
loop { case ARGV[0]
|
7
|
+
when /--/ then ARGV.shift; break
|
8
|
+
when /^-/ then usage("Unknown option: #{ARGV[0].inspect}")
|
9
|
+
else break
|
10
|
+
end; }
|
11
|
+
|
12
|
+
while gets() do
|
13
|
+
line = $_.chomp
|
14
|
+
tced = line.title_case_line
|
15
|
+
puts tced
|
16
|
+
end
|
data/lib/title_case.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
class String
|
2
|
+
|
3
|
+
def title_case_word
|
4
|
+
# the non-alpha beginning lets us capitalize "'the'" to "'The'",
|
5
|
+
self.sub(/^[^[:alpha:]]*[[:alpha:]]/){|first| first.upcase}
|
6
|
+
end
|
7
|
+
|
8
|
+
def title_case_line
|
9
|
+
art = "(a)|(an)|(the)"
|
10
|
+
prep = "(on)|(in)|(to)|(by)|(for)|(at)|(of)|(as)|(off)|(as)|(out)|(up)"
|
11
|
+
part = "(if)"
|
12
|
+
conj = "(and)|(but)|(nor)|(or)|(yet)|(so)"
|
13
|
+
abbr = "(re)|(w)|(etc)|(v)|(vs)"
|
14
|
+
ignore_raw = '(' + [art, prep, part, conj, abbr].join('|') + ')'
|
15
|
+
punct_wrap = "[^[:alpha:]]*"
|
16
|
+
ignore = Regexp.new('^' + punct_wrap + ignore_raw + punct_wrap + '$')
|
17
|
+
|
18
|
+
title_case_words = self.split.map.with_index do |word, index|
|
19
|
+
(word =~ /[[:upper:][:digit:]]/) || ( index > 0 && ignore.match(word)) \
|
20
|
+
? word \
|
21
|
+
: word.title_case_word
|
22
|
+
end
|
23
|
+
|
24
|
+
title_case_words.join(' ')
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
load "#{File.dirname(__FILE__)}/../lib/title_case.rb"
|
2
|
+
|
3
|
+
#FIXME can't seem to do the fancy ruby native relative path thing
|
4
|
+
# without naming our program title_case.rb, which I don't want
|
5
|
+
|
6
|
+
describe "title_case_word" do
|
7
|
+
it "title_cases a single word" do
|
8
|
+
"foo".title_case_word.should eq "Foo"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "Ignores external apostrophes" do
|
12
|
+
"'thing'".title_case_word.should eq "'Thing'"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "sees internal apostrophes" do
|
16
|
+
"don't".title_case_word.should eq "Don't"
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "title_case_line" do
|
22
|
+
it "basically works" do
|
23
|
+
"do it".title_case_line.should eq "Do It"
|
24
|
+
end
|
25
|
+
|
26
|
+
it "ignores short preposition, conjunction, and articles in middle " do
|
27
|
+
"go to".title_case_line.should eq "Go to"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "ignores 'words' with mixed numbers" do
|
31
|
+
"buy me a x0xb0x".title_case_line.should eq "Buy Me a x0xb0x"
|
32
|
+
end
|
33
|
+
|
34
|
+
it "capitalizes the first word even it it's on the ignore list" do
|
35
|
+
"to France".title_case_line.should eq "To France"
|
36
|
+
end
|
37
|
+
|
38
|
+
it "but doesn't capitalize the first word if it's camelcased" do
|
39
|
+
"eTrade is still around".title_case_line.should eq "eTrade Is Still Around"
|
40
|
+
end
|
41
|
+
|
42
|
+
it "Ignores external apostrophes in exceptions" do
|
43
|
+
"don't go 'to' Chicago".title_case_line.should eq "Don't Go 'to' Chicago"
|
44
|
+
end
|
45
|
+
|
46
|
+
it "retains punctuation" do
|
47
|
+
"shazam! what? don't go, young man...".title_case_line.should eq "Shazam! What? Don't Go, Young Man..."
|
48
|
+
end
|
49
|
+
|
50
|
+
it "ignores mixed case even as first word" do
|
51
|
+
"iPhone".title_case_line.should eq "iPhone"
|
52
|
+
end
|
53
|
+
|
54
|
+
it "doesn't falsely capitalize Dutch abbreviations" do
|
55
|
+
pending
|
56
|
+
"Mr 't Horne went to 's Grave".title_case_line.should eq "Mr 't Horne went to 's Grave"
|
57
|
+
end
|
58
|
+
end
|
data/title_case.gemspec
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'title_case'
|
3
|
+
s.version = '0.1.1'
|
4
|
+
s.date = '2014-02-14'
|
5
|
+
s.summary = 'String methods and CLI to properly title case a headline.'
|
6
|
+
s.description = 'title_case is a set of Ruby String methods for title casing,
|
7
|
+
and a command-line utility using those methods.'
|
8
|
+
s.authors = ['Noah Birnel']
|
9
|
+
s.email = 'nbirnel@gmail.com'
|
10
|
+
s.homepage = 'http://github.com/nbirnel/titlecase'
|
11
|
+
s.files = ['README.md', 'title_case.gemspec', 'lib/title_case.rb', 'spec/title_case_spec.rb', 'bin/titlecase']
|
12
|
+
s.has_rdoc = false
|
13
|
+
s.executables = ['titlecase']
|
14
|
+
s.license = 'MIT'
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: title_case
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Noah Birnel
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-14 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |-
|
14
|
+
title_case is a set of Ruby String methods for title casing,
|
15
|
+
and a command-line utility using those methods.
|
16
|
+
email: nbirnel@gmail.com
|
17
|
+
executables:
|
18
|
+
- titlecase
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- README.md
|
23
|
+
- bin/titlecase
|
24
|
+
- lib/title_case.rb
|
25
|
+
- spec/title_case_spec.rb
|
26
|
+
- title_case.gemspec
|
27
|
+
homepage: http://github.com/nbirnel/titlecase
|
28
|
+
licenses:
|
29
|
+
- MIT
|
30
|
+
metadata: {}
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements: []
|
46
|
+
rubyforge_project:
|
47
|
+
rubygems_version: 2.2.2
|
48
|
+
signing_key:
|
49
|
+
specification_version: 4
|
50
|
+
summary: String methods and CLI to properly title case a headline.
|
51
|
+
test_files: []
|