Module: Octopress::Utils

Defined in:
lib/octopress/utils.rb

Class Method Summary collapse

Class Method Details

.smart_capitalize(input) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/octopress/utils.rb', line 26

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

.smart_capitalize!(input) ⇒ Object



38
39
40
# File 'lib/octopress/utils.rb', line 38

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

.titlecase(input) ⇒ Object

Smart capitalization for titles



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/octopress/utils.rb', line 6

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

.titlecase!(input) ⇒ Object



22
23
24
# File 'lib/octopress/utils.rb', line 22

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