nicetitle 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/nicetitle.rb +6 -0
- data/lib/nicetitle/titlecase.rb +70 -0
- data/lib/nicetitle/version.rb +1 -1
- metadata +2 -2
- data/.travis.yml +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: acdf70e5f6210b7dc71ec29abbe687d930fbf6545ac94b5497685efe0f9f7cfc
|
4
|
+
data.tar.gz: ef8ef1482ba1048733fbbf240c2265ecbd6541bb5d79874e76716616318494bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 95dbfaee5c62af9e4c91ead24e1b5c780747856c69f46452d61fd8bbcaae69ed21e405d74013173f1a1e5c7d65f4317dcc5d215eb4e146af73402cdd091e555a
|
7
|
+
data.tar.gz: '0591667493b91dc4947164dfbfe7750f379edfef3a2e4acc8365eb28257955f7316b06242eb5c532282aed09864a9c6016319469fd2554bc2e8518e497eb676f'
|
data/README.md
CHANGED
@@ -17,7 +17,7 @@ The rule set is:
|
|
17
17
|
- Words containing dots are not capitalized.
|
18
18
|
- All caps sentences are down-cased before applying above mentioned rules.
|
19
19
|
|
20
|
-
Check out the [test cases](https://github.com/evaneykelen/nicetitle/test/nicetitle_test.rb) for a detailed overview.
|
20
|
+
Check out the [test cases](https://github.com/evaneykelen/nicetitle/blob/master/test/nicetitle_test.rb) for a detailed overview.
|
21
21
|
|
22
22
|
## Installation
|
23
23
|
|
@@ -39,7 +39,7 @@ Or install it yourself as:
|
|
39
39
|
|
40
40
|
Calling
|
41
41
|
|
42
|
-
`
|
42
|
+
`Nice.title('What am I reading/listing/applying these days?')`
|
43
43
|
|
44
44
|
outputs
|
45
45
|
|
data/lib/nicetitle.rb
CHANGED
@@ -0,0 +1,70 @@
|
|
1
|
+
module Nicetitle
|
2
|
+
class Titlecase
|
3
|
+
SMALL_WORDS = Regexp.new('\b(a(nd|n|s|t)?|b(ut|y)|en|for|i(f|n)|o(f|n|r)|t(he|o)|vs?\.?)\b').freeze
|
4
|
+
|
5
|
+
def self.is_small_word?(word)
|
6
|
+
SMALL_WORDS.match(word)
|
7
|
+
end
|
8
|
+
|
9
|
+
# "__foo" => "__Foo"
|
10
|
+
def self.upcase_first_real_letter(word)
|
11
|
+
word.sub(/[a-zA-Z0-9]/, &:upcase)
|
12
|
+
end
|
13
|
+
|
14
|
+
# step-by-step => Step-by-Step
|
15
|
+
def self.upcase_word_with_dashes(word)
|
16
|
+
word.split('-').map { |part| is_small_word?(part) ? part : part.capitalize }.join("-")
|
17
|
+
end
|
18
|
+
|
19
|
+
# before/after => Before/After
|
20
|
+
def self.upcase_word_with_slashes(word)
|
21
|
+
word.split('/').map(&:capitalize).join('/')
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.titlecase(str)
|
25
|
+
# Replace tabs by single space
|
26
|
+
# Replace weird spaces by regular space
|
27
|
+
str = (str || '').gsub(/\t/, ' ').gsub("\u{2011}", ' ').strip
|
28
|
+
return '' if str.empty?
|
29
|
+
|
30
|
+
# Downcase an all-upcase sentence
|
31
|
+
str.downcase! if str.scan(/[A-Z]|\s|\W/).length == str.length
|
32
|
+
|
33
|
+
# Split sentence at space boundaries
|
34
|
+
word_arr = str.split(' ')
|
35
|
+
|
36
|
+
# Initialize operand array
|
37
|
+
operand_arr = Array.new(word_arr.size, :capitalize)
|
38
|
+
|
39
|
+
word_arr.each_with_index do |word, idx|
|
40
|
+
# Don't capitalize small words...
|
41
|
+
# ... unless it's first word
|
42
|
+
# ... unless it's last word
|
43
|
+
# ... unless word is preceded by word ending with colon
|
44
|
+
operand_arr[idx] = :do_not_upcase if idx != 0 && idx != word_arr.size - 1 && is_small_word?(word) && word_arr[idx - 1][-1] != ':'
|
45
|
+
# Don't simply capitalize first letter if word starts with (, _, ', or "
|
46
|
+
operand_arr[idx] = :upcase_later if word[0].match(/\(|_|'|"/)
|
47
|
+
# Capitalize first letter and letters preceded by -
|
48
|
+
operand_arr[idx] = :upcase_dashed if word.count('-') > 0
|
49
|
+
# Capitalize letters preceded by / inside word
|
50
|
+
operand_arr[idx] = :upcase_slashed if word[1..].count('/') > 0
|
51
|
+
# Don't capitalize word if it starts with /
|
52
|
+
operand_arr[idx] = :do_not_upcase if word[0] == '/'
|
53
|
+
# Don't capitalize URLs
|
54
|
+
operand_arr[idx] = :do_not_upcase if word.match(/https?:\/\//i)
|
55
|
+
# Don't capitalize words containing capitals besides first letter
|
56
|
+
# Don't capitalize words containing dots inside word
|
57
|
+
operand_arr[idx] = :do_not_upcase if word[1..].match(/[A-Z]/) || word[1..-3].match(/\.|&/)
|
58
|
+
end
|
59
|
+
|
60
|
+
word_arr.each_with_index do |word, idx|
|
61
|
+
word_arr[idx] = upcase_first_real_letter(word) if operand_arr[idx] == :upcase_later
|
62
|
+
word_arr[idx] = upcase_word_with_dashes(word) if operand_arr[idx] == :upcase_dashed
|
63
|
+
word_arr[idx] = upcase_word_with_slashes(word) if operand_arr[idx] == :upcase_slashed
|
64
|
+
word_arr[idx] = word.capitalize if operand_arr[idx] == :capitalize
|
65
|
+
end
|
66
|
+
|
67
|
+
word_arr.join(' ')
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/lib/nicetitle/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nicetitle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Erik van Eykelen
|
@@ -60,7 +60,6 @@ extensions: []
|
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
62
|
- ".gitignore"
|
63
|
-
- ".travis.yml"
|
64
63
|
- Gemfile
|
65
64
|
- LICENSE.txt
|
66
65
|
- README.md
|
@@ -68,6 +67,7 @@ files:
|
|
68
67
|
- bin/console
|
69
68
|
- bin/setup
|
70
69
|
- lib/nicetitle.rb
|
70
|
+
- lib/nicetitle/titlecase.rb
|
71
71
|
- lib/nicetitle/version.rb
|
72
72
|
- nicetitle.gemspec
|
73
73
|
homepage: https://www.msgtrail.com/articles/20190525-1530-title-casing-is-harder-than-i-thought/
|