diff options
author | B Mathis <brandon@imathis.com> | 2009-12-07 10:55:50 -0600 |
---|---|---|
committer | B Mathis <brandon@imathis.com> | 2009-12-07 10:55:50 -0600 |
commit | 6a1a9a8bb8966cc83c3664830ce45a1907b4212a (patch) | |
tree | 7f5da49a614a00df52b026d5836ab1d1aae10d7e /source | |
parent | a6262dcbff3901135f0941e1f5f03be0db196874 (diff) | |
download | my_new_personal_website-6a1a9a8bb8966cc83c3664830ce45a1907b4212a.tar.xz my_new_personal_website-6a1a9a8bb8966cc83c3664830ce45a1907b4212a.zip |
added titlize so posts created with rake tasks will have nice titles
Diffstat (limited to 'source')
-rw-r--r-- | source/_helpers.rb | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/source/_helpers.rb b/source/_helpers.rb index 43377dcd..48e40b0d 100644 --- a/source/_helpers.rb +++ b/source/_helpers.rb @@ -161,3 +161,39 @@ module Helpers end end +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
\ No newline at end of file |