Sha256: 4ccd24da625096b254595836e50be2450534be6f6e261c84fb90dfb900a9594f

Contents?: true

Size: 1.37 KB

Versions: 53

Compression:

Stored size: 1.37 KB

Contents

module Octopress
  module Utils

    # Smart capitalization for titles
    #
    def self.titlecase(input)
      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 = input.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! : smart_capitalize!(word)
        word
      end
      # capitalize first and last words
      smart_capitalize!(x.first)
      smart_capitalize!(x.last)
      # small words are capitalized after colon, period, exclamation mark, question mark
      x.join(" ").gsub(/(:|\.|!|\?)\s?(\W*#{small_words.join("|")}\W*)\s/) { "#{$1} #{smart_capitalize($2)} " }
    end

    def self.titlecase!(input)
      input.replace(titlecase(input))
    end

    def self.smart_capitalize(input)
      target = input.dup
      # ignore any leading crazy characters and capitalize the first real character
      if target =~ /^['"\(\[']*([a-z])/
        i = input.index($1)
        x = target[i,target.length]
        # word with capitals and periods mid-word are left alone
        target[i,1] = target[i,1].upcase unless x =~ /[A-Z]/ or x =~ /\.\w+/
      end
      target
    end
    
    def self.smart_capitalize!(input)
      input.replace(smart_capitalize(input))
    end
  end
end

Version data entries

53 entries across 53 versions & 2 rubygems

Version Path
octopress-3.0.0.rc.32 lib/octopress/utils.rb
octopress-3.0.0.rc.31 lib/octopress/utils.rb
octopress-3.0.0.rc.30 lib/octopress/utils.rb
octopress-3.0.0.rc.29 lib/octopress/utils.rb
octopress-3.0.0.rc.28 lib/octopress/utils.rb
octopress-3.0.0.rc.27 lib/octopress/utils.rb
octopress-3.0.0.rc.26 lib/octopress/utils.rb
octopress-3.0.0.rc.25 lib/octopress/utils.rb
octopress-3.0.0.rc.24 lib/octopress/utils.rb
octopress-3.0.0.rc.23 lib/octopress/utils.rb
octopress-3.0.0.rc.22 lib/octopress/utils.rb
octopress-3.0.0.rc.21 lib/octopress/utils.rb
octopress-3.0.0.rc.20 lib/octopress/utils.rb
octopress-3.0.0.rc.19 lib/octopress/utils.rb
octopress-3.0.0.rc.18 lib/octopress/utils.rb
octopress-3.0.0.rc.17 lib/octopress/utils.rb
octopress-3.0.0.rc.16 lib/octopress/utils.rb
octopress-3.0.0.rc.15 lib/octopress/utils.rb
octopress-3.0.0.rc.14 lib/octopress/utils.rb
octopress-3.0.0.rc.13 lib/octopress/utils.rb