7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/headingify/core_ext/string.rb', line 7
def headingify
articles = %w[a an the]
conjunctions = %w[and but or nor for yet so]
prepositions = %w[a abaft aboard about above absent across afore after
against along alongside amid amidst among amongst an anenst apropos
apud around as aside astride at athwart atop barring before behind
below beneath beside besides between beyond but by circa concerning
despite down during except excluding failing following for forenenst
from given in including inside into like mid midst minus modulo near
nigh next notwithstanding of off on onto opposite out outside over
pace past per plus pro qua regarding round sans save since than through
thru throughout thruout til till times to toward towards under
underneath unlike until unto up upon versus via vice vis-a-vis
vis-à-vis with within without worth]
blacklist = (articles + conjunctions + prepositions).uniq!
working = self.split
include_open_quote = lambda do |w|
q = %w[' " ’ ‘ “ ”]
return !/^(["'‘“’”]).*/.match(w).nil?
end
working.each_with_index do |s, i|
if s.include? "\%"
working[i] = s.delete "\\%"
if working[i][0] == "\\" || working[i][working.length - 1] == "\\"
working[i].delete! "\\"
end
next
end
next if s =~ /^o'.*$/
if blacklist.include?(s) && i == 0; s.capitalize!; next; end
blacklist.include?(s) ? s.downcase! : s.capitalize!
if include_open_quote.call(s)
open = /^(["'‘“’”])([a-zA-Z]*)(["'‘“’”])?([\.\?!])?$/.match(s)[1]
temp = /^(["'‘“’”])([a-zA-Z]*)(["'‘“’”])?([\.\?!])?$/.match(s)[2]
last = /^(["'‘“’”])([a-zA-Z]*)(["'‘“’”])?([\.\?!])?$/.match(s)[3]
punc = /^(["'‘“’”])([a-zA-Z]*)(["'‘“’”])?([\.\?!])?$/.match(s)[4]
unless temp.nil?
blacklist.include?(temp) ? temp.downcase! : temp.capitalize!
temp = open + temp unless open.nil?
temp = temp + last unless last.nil?
temp = temp + punc unless punc.nil?
working[i] = temp
end
end
end
result = working.join(" ")
result.headingified = true
return result
end
|