aboutsummaryrefslogtreecommitdiff
path: root/plugins/titlecase.rb
diff options
context:
space:
mode:
authorBrandon Mathis <brandon@imathis.com>2011-07-19 09:06:54 -0400
committerBrandon Mathis <brandon@imathis.com>2011-07-19 09:06:54 -0400
commit17c59fb1d1bf3e0c05137af4b4bd09ae271a2d31 (patch)
treea4b3b5d43173f9b02ec4b6401cb6e14f6e716a35 /plugins/titlecase.rb
parent873a604e144c53cfc5465a790e43db5b7ebb429e (diff)
downloadmy_new_personal_website-17c59fb1d1bf3e0c05137af4b4bd09ae271a2d31.tar.xz
my_new_personal_website-17c59fb1d1bf3e0c05137af4b4bd09ae271a2d31.zip
Moved plugins to root directory. I'm ditching the idea of shipping plugins with themes until it's more obviously necessary. This way it's easier to merge and update plugins.
Diffstat (limited to 'plugins/titlecase.rb')
-rw-r--r--plugins/titlecase.rb36
1 files changed, 36 insertions, 0 deletions
diff --git a/plugins/titlecase.rb b/plugins/titlecase.rb
new file mode 100644
index 00000000..103bf702
--- /dev/null
+++ b/plugins/titlecase.rb
@@ -0,0 +1,36 @@
+class String
+ def titlecase
+ small_words = %w(a an and as at but by en for if in of on or the to v v. via vs vs.)
+
+ x = split(" ").map do |word|
+ # note: word could contain non-word characters!
+ # downcase all small_words, capitalize the rest
+ small_words.include?(word.gsub(/\W/, "").downcase) ? word.downcase! : word.smart_capitalize!
+ word
+ end
+ # capitalize first and last words
+ x.first.to_s.smart_capitalize!
+ x.last.to_s.smart_capitalize!
+ # small words after colons are capitalized
+ x.join(" ").gsub(/:\s?(\W*#{small_words.join("|")}\W*)\s/) { ": #{$1.smart_capitalize} " }
+ end
+
+ def titlecase!
+ replace(titlecase)
+ end
+
+ def smart_capitalize
+ # ignore any leading crazy characters and capitalize the first real character
+ if self =~ /^['"\(\[']*([a-z])/
+ i = index($1)
+ x = self[i,self.length]
+ # word with capitals and periods mid-word are left alone
+ self[i,1] = self[i,1].upcase unless x =~ /[A-Z]/ or x =~ /\.\w+/
+ end
+ self
+ end
+
+ def smart_capitalize!
+ replace(smart_capitalize)
+ end
+end