Class: LCBO::CrawlKit::TitleCaseHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/lcbo/crawlkit/titlecase_helper.rb

Constant Summary collapse

SMALL_WORDS =
%w[
  a an and as at but by en for if in of del de on or the to v v. via
  vs.
]
ACRONYMS =
%w[
  i ii iii iv v vi vii viii ix x xiii xi vqa vsop xo nq5 vs xxx igt xoxo
  srl bdb cvbg ocb lcbo gtm hf yo vs ipa
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



16
17
18
# File 'lib/lcbo/crawlkit/titlecase_helper.rb', line 16

def input
  @input
end

Class Method Details

.[](string) ⇒ Object



18
19
20
# File 'lib/lcbo/crawlkit/titlecase_helper.rb', line 18

def self.[](string)
  titlecase(string)
end

.capitalize(string) ⇒ Object



30
31
32
# File 'lib/lcbo/crawlkit/titlecase_helper.rb', line 30

def self.capitalize(string)
  UnicodeUtils.titlecase(string)
end

.downcase(string) ⇒ Object



26
27
28
# File 'lib/lcbo/crawlkit/titlecase_helper.rb', line 26

def self.downcase(string)
  UnicodeUtils.simple_downcase(string)
end

.titlecase(string) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lcbo/crawlkit/titlecase_helper.rb', line 34

def self.titlecase(string)
  preclean = lambda { |s|
    # Strip bracketed stuff and trailing junk: Product (Junk)**
    s.gsub(/\((.+?)\)|\*+|\((.+?)\Z/, '').strip
  }
  count = 0 # Ewwww
  capitalize(preclean.(string)).split.map do |word|
    count += 1
    case word.downcase
    when /[\w]\/[\w]/ # words with slashes
      word.split('/').map { |w| capitalize(w) }.join(' / ')
    when /[\w]\&[\w]/ # words with &, like E&J
      word.split('&').map { |w| capitalize(w) }.join('&')
    when /[\w]\-[\w]/ # words with dashes, like "Super-Cool"
      word.split('-').map { |w| capitalize(w) }.join('-')
    when /[\w]\.[\w]/ # words with dots, like "A.B.C."
      word.split('.').map { |w| upcase(w) }.join('.') + '.'
    when *SMALL_WORDS
      1 == count ? word : word.downcase
    when *ACRONYMS
      word.upcase
    else
      word
    end
  end.
  join(' ').
  gsub(/(['’])S\b/, '\1s'). # Word'S => Word's
  gsub(/(\S{1})'(\S{2,})/u) { "#{$1}'#{capitalize $2}" } # D'aux => D'Aux
end

.upcase(string) ⇒ Object



22
23
24
# File 'lib/lcbo/crawlkit/titlecase_helper.rb', line 22

def self.upcase(string)
  UnicodeUtils.simple_upcase(string)
end