msgtrail 0.9.3 → 0.9.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 28e7072fe3ebdebc29a89edccec28b74522208f82be5aeef20ad627564b37d07
4
- data.tar.gz: 2e1eab46f795631e71f00f9125afb82bff67f67e2cdc58cf684b61d862b1a375
3
+ metadata.gz: 37b3e65f039cda9ff376bbea5476a4e2ed3f867cf65cf7454366280d433b587e
4
+ data.tar.gz: 3cec58c5e959057b87d17ad66453e96beb4b6c87efae0b78472992e4f626b6a6
5
5
  SHA512:
6
- metadata.gz: 4bc9b1f3bd053300bab75a40ea78ba82451f604c431b38a07dd8978dfcc889dd4fcbde208d96b822171c1d8292f411429a760fd92a637c8ad4f2d26445f4bf05
7
- data.tar.gz: 58a263140a89b1b189ecbc56c90190ddf1e96552095591f56cc0d5426862529b2f53749934ae57a62a6acd28a0c9b3ddd755a390d1c1491537569f9649ec02ed
6
+ metadata.gz: 10141e2815729de2799466e5e8b02b7b5ee9960ba87f327bf76f83aae02ae14cc715ba940f9e67d6d2444d8f06b85187600f2b126f215e86a485b6804ad8593b
7
+ data.tar.gz: 72503d1dec88d0794977667838591c2b923d0aaefca49765e9af750e79f5dccc3b879f1be68b5d139ff07c108c01cdf6bb286ee014cdba509bf27b77b3237f2a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## Version 0.9.4
4
+
5
+ - Add `titlecase` method based on [John Gruber's title casing](https://daringfireball.net/2008/08/title_case_update) rules.
6
+
3
7
  ## Version 0.9.3
4
8
 
5
9
  - Make published and updated date/time datestamps explicit
@@ -11,6 +11,7 @@ require 'msgtrail/markdown_file'
11
11
  require 'msgtrail/renderers'
12
12
  require 'msgtrail/site'
13
13
  require 'msgtrail/slug'
14
+ require 'msgtrail/titlecase'
14
15
  require 'msgtrail/twitter'
15
16
  require 'multi_json'
16
17
  require 'ostruct'
@@ -1,9 +1,5 @@
1
1
  module RenderHelper
2
2
 
3
- def now_as_rfc3339
4
- DateTime.now.rfc3339
5
- end
6
-
7
3
  def as_rfc3339(published, updated = nil)
8
4
  updated.nil? ? datestamp = published : datestamp = updated
9
5
  ymd = datestamp[:date].split(/\D/).map(&:to_i)
@@ -11,6 +7,14 @@ module RenderHelper
11
7
  DateTime.new(ymd[0], ymd[1], ymd[2], hm[0], hm[1], 0, datestamp[:utc_offset]).rfc3339
12
8
  end
13
9
 
10
+ def now_as_rfc3339
11
+ DateTime.now.rfc3339
12
+ end
13
+
14
+ def titlecase(word)
15
+ Msgtrail::Titlecase.titlecase(word)
16
+ end
17
+
14
18
  def xml_safe(str)
15
19
  str.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&gt;").gsub("\"", "&quot;").gsub("'", "&apos;")
16
20
  end
@@ -0,0 +1,82 @@
1
+ module Msgtrail
2
+ class Titlecase
3
+
4
+ SMALL_WORDS = [
5
+ /\Aa(nd|n|s|t)?\z/,
6
+ /\Ab(ut|y)\z/,
7
+ /\Aen\z/,
8
+ /\Afor\z/,
9
+ /\Ai(f|n)\z/,
10
+ /\Ao(f|n|r)\z/,
11
+ /\At(he|o)\z/,
12
+ /\Avs?\.?\z/
13
+ ].freeze
14
+
15
+ RE = 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
16
+
17
+ def self.is_small_word?(word)
18
+ !RE.match(word).nil?
19
+ end
20
+
21
+ # "__foo" => "__Foo"
22
+ def self.upcase_first_real_letter(word)
23
+ word.sub(/[a-zA-Z0-9]/, &:upcase)
24
+ end
25
+
26
+ # step-by-step => Step-by-Step
27
+ def self.upcase_word_with_dashes(word)
28
+ word.split('-').map { |part| is_small_word?(part) ? part : part.capitalize }.join("-")
29
+ end
30
+
31
+ # before/after => Before/After
32
+ def self.upcase_word_with_slashes(word)
33
+ word.split('/').map { |part| part.capitalize }.join('/')
34
+ end
35
+
36
+ def self.titlecase(str)
37
+ return if (str || '').strip.empty?
38
+
39
+ # Downcase an all-upcase sentence
40
+ str.downcase! if str.scan(/[A-Z]|\s|\W/).length == str.length
41
+
42
+ # Replace tabs by single space
43
+ # Replace weird spaces by regular space
44
+ # Split sentence at space boundaries
45
+ word_arr = str.gsub(/\t/, ' ').gsub("\u{2011}", ' ').split(' ')
46
+
47
+ # Initialize operand array
48
+ operand_arr = Array.new(word_arr.size, :nop)
49
+
50
+ word_arr.each_with_index do |word, idx|
51
+ # Don't capitalize small words...
52
+ # ... unless it's first word
53
+ # ... unless it's last word
54
+ # ... unless word is preceded by word ending with colon
55
+ operand_arr[idx] = :do_not_upcase if idx != 0 && idx != word_arr.size - 1 && is_small_word?(word) && word_arr[idx - 1][-1] != ':'
56
+ # Don't simply capitalize first letter if word starts with (, _, ', or "
57
+ operand_arr[idx] = :upcase_later if word[0].match(/\(|_|'|"/)
58
+ # Capitalize first letter and letters preceded by -
59
+ operand_arr[idx] = :upcase_dashed if word.count('-') > 0
60
+ # Capitalize letters preceded by / inside word
61
+ operand_arr[idx] = :upcase_slashed if word[1..].count('/') > 0
62
+ # Don't capitalize word if it starts with /
63
+ operand_arr[idx] = :do_not_upcase if word[0] == '/'
64
+ # Don't capitalize URLs
65
+ operand_arr[idx] = :do_not_upcase if word.match(/https?:\/\//i)
66
+ # Don't capitalize words containing capitals besides first letter
67
+ # Don't capitalize words containing dots inside word
68
+ operand_arr[idx] = :do_not_upcase if word[1..].match(/[A-Z]/) || word[1..-3].match(/\.|&/)
69
+ end
70
+
71
+ word_arr.each_with_index do |word, idx|
72
+ word_arr[idx] = upcase_first_real_letter(word) if operand_arr[idx] == :upcase_later
73
+ word_arr[idx] = upcase_word_with_dashes(word) if operand_arr[idx] == :upcase_dashed
74
+ word_arr[idx] = upcase_word_with_slashes(word) if operand_arr[idx] == :upcase_slashed
75
+ word_arr[idx] = word.capitalize if operand_arr[idx] == :nop
76
+ end
77
+
78
+ return word_arr.join(' ')
79
+ end
80
+
81
+ end
82
+ end
@@ -1,3 +1,3 @@
1
1
  module Msgtrail
2
- VERSION = "0.9.3"
2
+ VERSION = "0.9.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: msgtrail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.3
4
+ version: 0.9.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik van Eykelen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-22 00:00:00.000000000 Z
11
+ date: 2019-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -148,6 +148,7 @@ files:
148
148
  - lib/msgtrail/renderers.rb
149
149
  - lib/msgtrail/site.rb
150
150
  - lib/msgtrail/slug.rb
151
+ - lib/msgtrail/titlecase.rb
151
152
  - lib/msgtrail/twitter.rb
152
153
  - lib/msgtrail/version.rb
153
154
  - msgtrail.gemspec