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.12.pre.1 lib/octopress/utils.rb
octopress-3.0.11 lib/octopress/utils.rb
octopress-3.0.10 lib/octopress/utils.rb
octopress-3.0.9 lib/octopress/utils.rb
octopress-3.0.8 lib/octopress/utils.rb
octopress-3.0.7 lib/octopress/utils.rb
octopress-3.0.6 lib/octopress/utils.rb
octopress-3.0.5 lib/octopress/utils.rb
octopress-3.0.4 lib/octopress/utils.rb
octopress-3.0.3.alpha.2 lib/octopress/utils.rb
octopress-3.0.3.alpha.1 lib/octopress/utils.rb
octopress-3.0.3.alpha.0 lib/octopress/utils.rb
octopress-3.0.2 lib/octopress/utils.rb
octopress-3.0.1 lib/octopress/utils.rb
octopress-3.0.0 lib/octopress/utils.rb
octopress-3.0.0.rc.37 lib/octopress/utils.rb
octopress-3.0.0.rc.36 lib/octopress/utils.rb
octopress-3.0.0.rc.35 lib/octopress/utils.rb
octopress-3.0.0.rc.34 lib/octopress/utils.rb
octopress-3.0.0.rc.33 lib/octopress/utils.rb