titlify 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -6,3 +6,6 @@ doc/*
6
6
 
7
7
  # OS X Crap
8
8
  .DS_Store
9
+
10
+ # Build Gems
11
+ pkg/*
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- titlify (0.1.0)
4
+ titlify (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -1,11 +1,11 @@
1
1
  Titlify
2
2
  =======
3
3
 
4
- The best damn titlecase function. (Or at least on its way.)
4
+ The best damn titlecase function. (Or at least on its way.) Titlify will be moving closer to adhere to the [AP Stylebook](http://www.apstylebook.com/). It is the most commonly used style guide in the United States in journalism. Because I expect this gem will be used in situations more similar to journalism, I think the AP Stylebook makes sense. However, if there's enough support for Chicago Manual of Style and Elements of Typographic Style, I'll reconsider. It's still early on.
5
5
 
6
6
  Credits
7
7
  -------
8
- The most thorough titlecasing function I found was Patrick Hogan's in his [sterile gem](https://github.com/pbhogan/sterile). I started with his titlecasing function and expanded to include some more logic to bring it closer to the AP guidelines and handle more fringe cases.
8
+ The most thorough titlecasing function I found was Patrick Hogan's in his [sterile gem](https://github.com/pbhogan/sterile). I started with his titlecasing function which was an expansion on John Gruber's Perl titlecasing function.
9
9
 
10
10
  Usage
11
11
  -----
@@ -35,7 +35,6 @@ Format text appropriately for titles. This method is much smarter than ActiveSup
35
35
 
36
36
  Installation
37
37
  ------------
38
- __Not Yet Published__
39
38
 
40
39
  Install with RubyGems:
41
40
 
@@ -44,5 +43,34 @@ Install with RubyGems:
44
43
  License
45
44
  -------
46
45
 
47
- Copyright (c) 2012 Josh Hepworth, released under the MIT License.
46
+ __Sterile__
47
+
48
+ Copyright (c) 2011 Patrick Hogan, released under the MIT License
48
49
  http://www.opensource.org/licenses/mit-license
50
+
51
+ __Titlify__
52
+
53
+ This is free and unencumbered software released into the public domain.
54
+
55
+ Anyone is free to copy, modify, publish, use, compile, sell, or
56
+ distribute this software, either in source code form or as a compiled
57
+ binary, for any purpose, commercial or non-commercial, and by any
58
+ means.
59
+
60
+ In jurisdictions that recognize copyright laws, the author or authors
61
+ of this software dedicate any and all copyright interest in the
62
+ software to the public domain. We make this dedication for the benefit
63
+ of the public at large and to the detriment of our heirs and
64
+ successors. We intend this dedication to be an overt act of
65
+ relinquishment in perpetuity of all present and future rights to this
66
+ software under copyright law.
67
+
68
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
69
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
70
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
71
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
72
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
73
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
74
+ OTHER DEALINGS IN THE SOFTWARE.
75
+
76
+ For more information, please refer to <http://unlicense.org/>
@@ -0,0 +1 @@
1
+ Bundler::GemHelper.install_tasks
@@ -1,7 +1,6 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module Titlify
4
- # @private
5
4
  class Data
6
5
  def self.smart_format_rules
7
6
  [
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+
3
+ module Titlify
4
+ class Data
5
+ def self.articles
6
+ %w(a an the some)
7
+ end
8
+
9
+ def self.coordinating_conjunctions
10
+ %w(and but or nor for yet so)
11
+ end
12
+
13
+ def self.prepositions
14
+ %w(a abaft aboard about above absent across afore after against along alongside amid amidst among amongst an apropos around as aside astride athwart atop barring before behind below beneath beside besides between betwixt beyond but by circa concerning despite down during except excluding failing following for from given in including inside into lest like mid midst minus modulo near next notwithstanding of off on onto opposite out outside over pace past per plus pro qua regarding round sans save since than through throughout till to toward towards under underneath unlike until up upon versus via vice with within without worth)
15
+
16
+ # Not included because same spelling is more commonly a different part of speech
17
+ # times
18
+
19
+ # Included in special cases
20
+ # at
21
+ end
22
+
23
+ def self.special_cases
24
+ # I bet this is going to get out of control...
25
+ %w(at(?!&t) v[.]? vs[.]?)
26
+ end
27
+
28
+ def self.word_list
29
+ Data.articles | Data.coordinating_conjunctions | Data.prepositions | Data.special_cases
30
+ end
31
+ end
32
+ end
@@ -19,7 +19,7 @@ module Titlify
19
19
  string.gsub!(/\s+/, " ")
20
20
  string.downcase! unless string =~ /[[:lower:]]/
21
21
 
22
- small_words = %w{ a an and as at(?!&t) but by en for if in nor of on or the to v[.]? via vs[.]? }.join("|")
22
+ small_words = word_list.join("|")
23
23
  apos = / (?: ['#{rsquo}] [[:lower:]]* )? /xu
24
24
 
25
25
  string.gsub!(
@@ -39,23 +39,52 @@ module Titlify
39
39
  string.gsub!(
40
40
  /
41
41
  \b
42
- ([_\*]*)
42
+ ([_\*]*) # [1]
43
43
  (?:
44
- ( [-\+\w]+ [@.\:\/] [-\w@.\:\/]+ #{apos} ) # URL, domain, or email
44
+ ( # [2] entire matched URL
45
+ (?:[A-Za-z0-9]+[\.]{1})+(?:ac|ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|asia|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cat|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cu|cv|cw|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|info|int|io|iq|ir|is|it|je|jm|jo|jobs|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mobi|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sx|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|travel|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|xn|xxx|ye|yt|za|zm|zw)
46
+ |
47
+ (?:
48
+ (?:
49
+ [a-z][\w-]+:
50
+ (?:
51
+ \/{1,3}
52
+ |
53
+ [a-z0-9%]
54
+ )
55
+ |
56
+ www\d{0,3}[.]
57
+ |
58
+ [a-z0-9.\-]+[.][a-z]{2,4}\/
59
+ )
60
+ (?:
61
+ [^\s()<>]+
62
+ |
63
+ \([^\s()<>]+|\([^\s()<>]+\)*\)
64
+ )+
65
+ (?:
66
+ \([^\s()<>]+|\([^\s()<>]+\)*\)
67
+ |
68
+ [^\s`!()\[\]{};:'".,<>?«»“”‘’]
69
+ )
70
+ )
71
+ )
72
+ |
73
+ ((?:[\w-\.]+)@(?:[\w]+\.)+(?:[a-zA-Z]){2,4}) # [3] email
45
74
  |
46
- ( (?i: #{small_words} ) #{apos} ) # or small word, case-insensitive
75
+ ( (?i: #{small_words} ) #{apos} ) # [4] or small word, case-insensitive
47
76
  |
48
- ( [[:alpha:]] [[:lower:]'#{rsquo}()\[\]{}]* #{apos} ) # or word without internal caps
77
+ ( [[:alpha:]] [[:lower:]'#{rsquo}()\[\]{}]* #{apos} ) # [5] or word without internal caps
49
78
  |
50
- ( [[:alpha:]] [[:alpha:]'#{rsquo}()\[\]{}]* #{apos} ) # or some other word
79
+ ( [[:alpha:]] [[:alpha:]'#{rsquo}()\[\]{}]* #{apos} ) # [6] or some other word
51
80
  )
52
- ([_\*]*)
81
+ ([_\*]*) # [7]
53
82
  \b
54
83
  /xu
55
84
  ) do
56
85
  ($1 ? $1 : "") +
57
- ($2 ? $2 : ($3 ? $3.downcase : ($4 ? $4.downcase.capitalize : $5))) +
58
- ($6 ? $6 : "")
86
+ ($2 ? $2 : ($3 ? $3.downcase : ($4 ? $4.downcase : ($5 ? $5.downcase.capitalize : $6)))) +
87
+ ($7 ? $7 : "")
59
88
  end
60
89
 
61
90
  if RUBY_VERSION < "1.9.0"
@@ -125,9 +154,16 @@ module Titlify
125
154
  # Lazy load smart formatting rules
126
155
  def smart_format_rules
127
156
  @smart_format_rules ||= begin
128
- require "titlify/data/smart_format_rules"
129
- Data.smart_format_rules
130
- end
157
+ require "titlify/data/smart_format_rules"
158
+ Data.smart_format_rules
159
+ end
160
+ end
161
+
162
+ def word_list
163
+ @word_list ||= begin
164
+ require "titlify/data/word_lists"
165
+ Data.word_list
166
+ end
131
167
  end
132
168
 
133
169
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module Titlify
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -26,11 +26,54 @@ describe Titlify do
26
26
 
27
27
  it "capitalizes the first letter after a hyphen" do
28
28
  title = "that shit-faced guy fell over the curb".titlify
29
- title.should eq("That Shit-Faced Guy Fell Over the Curb")
29
+ title.should eq("That Shit-Faced Guy Fell over the Curb")
30
30
  end
31
31
 
32
32
  it "capitalizes the first word following a colon" do
33
33
  title = "the big guns: a little man's tale".titlify
34
34
  title.should eq("The Big Guns: A Little Man's Tale")
35
35
  end
36
+
37
+ it "capitalizes words that begin with small words" do
38
+ # Without punctuation
39
+ title = "another ardvark used a thesaurus in overgrowth".titlify
40
+ title.should eq("Another Ardvark Used a Thesaurus in Overgrowth")
41
+ end
42
+
43
+ it "capitalizes the first letter when '/' is used" do
44
+ title = "this is a cat/dog thing".titlify
45
+ title.should eq("This Is a Cat/Dog Thing")
46
+ end
47
+
48
+ it "capitalizes the New York Times correctly" do
49
+ title = "the new york times prints a newspaper".titlify
50
+ title.should eq("The New York Times Prints a Newspaper")
51
+ end
52
+
53
+ it "capitalizes words in quotes correctly" do
54
+ title = 'the "best" are in the cards'.titlify
55
+ title.should eq('The "Best" Are in the Cards')
56
+ title = 'sometimes "more than one thing ends up in quotes" and stuff'.titlify
57
+ title.should eq('Sometimes "More than One Thing Ends up in Quotes" and Stuff')
58
+ end
59
+
60
+ it "keeps domain names in lowercase n stuff" do
61
+ # With HTTP
62
+ title = "look at http://www.wikipedia.org sometime".titlify
63
+ title.should eq("Look at http://www.wikipedia.org Sometime")
64
+
65
+ # With www, without HTTP
66
+ title = "look at www.wikipedia.org sometime".titlify
67
+ title.should eq("Look at www.wikipedia.org Sometime")
68
+
69
+ # Without www and HTTP
70
+ title = "look at en.wikipedia.org sometime".titlify
71
+ title.should eq("Look at en.wikipedia.org Sometime")
72
+
73
+ title = "look at wikipedia.org sometime".titlify
74
+ title.should eq("Look at wikipedia.org Sometime")
75
+
76
+ title = "look at wikipedia.co.uk sometime".titlify
77
+ title.should eq("Look at wikipedia.co.uk Sometime")
78
+ end
36
79
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: titlify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-14 00:00:00.000000000 Z
12
+ date: 2012-02-21 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Gonna be the best damn titlecasing function around.
15
15
  email:
@@ -24,8 +24,10 @@ files:
24
24
  - Gemfile.lock
25
25
  - Guardfile
26
26
  - README.markdown
27
+ - Rakefile
27
28
  - lib/titlify.rb
28
29
  - lib/titlify/data/smart_format_rules.rb
30
+ - lib/titlify/data/word_lists.rb
29
31
  - lib/titlify/string_extensions.rb
30
32
  - lib/titlify/titlecase.rb
31
33
  - lib/titlify/version.rb
@@ -44,12 +46,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
44
46
  - - ! '>='
45
47
  - !ruby/object:Gem::Version
46
48
  version: '0'
49
+ segments:
50
+ - 0
51
+ hash: 327711217174080911
47
52
  required_rubygems_version: !ruby/object:Gem::Requirement
48
53
  none: false
49
54
  requirements:
50
55
  - - ! '>='
51
56
  - !ruby/object:Gem::Version
52
57
  version: '0'
58
+ segments:
59
+ - 0
60
+ hash: 327711217174080911
53
61
  requirements: []
54
62
  rubyforge_project: titlify
55
63
  rubygems_version: 1.8.11
@@ -59,4 +67,3 @@ summary: Gonna be the best damn titlecasing function around.
59
67
  test_files:
60
68
  - spec/spec_helper.rb
61
69
  - spec/titlify_spec.rb
62
- has_rdoc: