jekyll-timeago 0.12.3 → 0.13.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -18
- data/Gemfile +3 -1
- data/LICENSE +1 -1
- data/README.md +10 -0
- data/jekyll-timeago.gemspec +2 -3
- data/lib/jekyll-timeago/core.rb +22 -8
- data/lib/jekyll-timeago/version.rb +1 -1
- data/spec/jekyll-timeago_spec.rb +6 -1
- data/spec/source/_config.yml +1 -0
- data/spec/source/index.html +2 -1
- metadata +6 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e0d0a282e371c82c7a78ec06daf099ab43248b2164f4eeb2f3ccb99936e266dd
|
4
|
+
data.tar.gz: c0b57570ebd40598b83b62e9cb46cae40b7922923382bc03bc11359680ff7964
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dcfd9bc02249b9ae0309130506b4cd13b05b13a1cf3777fa4059c3610d6121c4fa62043a4efd2dbabbe51f2c3796973006846853a2a65fc6aa56e78d0eecba95
|
7
|
+
data.tar.gz: 9e7ab93694539ab08bd0df48289c784ae08d41726589af4a5e9d9f72df7a325a950eb07aed2c228af613a8f45b05c369fa765e2fade737c8d55ba8c8dd0ffba0
|
data/.travis.yml
CHANGED
@@ -1,33 +1,20 @@
|
|
1
1
|
language: ruby
|
2
|
-
|
3
2
|
cache: bundler
|
4
|
-
|
5
|
-
sudo: false
|
6
|
-
|
7
3
|
rvm:
|
8
4
|
- ruby-head
|
9
|
-
- 2.
|
5
|
+
- 2.6.3
|
6
|
+
- 2.5.5
|
10
7
|
- 2.4.5
|
11
8
|
- 2.3.8
|
12
9
|
|
13
10
|
gemfile:
|
14
11
|
- gemfiles/jekyll_v3.gemfile
|
15
|
-
- gemfiles/jekyll_v2.gemfile
|
16
|
-
- gemfiles/jekyll_v1.gemfile
|
17
12
|
|
18
13
|
matrix:
|
19
|
-
|
20
|
-
- rvm:
|
21
|
-
gemfile: gemfiles/jekyll_v2.gemfile
|
22
|
-
- rvm: ruby-head
|
23
|
-
gemfile: gemfiles/jekyll_v1.gemfile
|
24
|
-
- rvm: 2.5.3
|
25
|
-
gemfile: gemfiles/jekyll_v2.gemfile
|
26
|
-
- rvm: 2.5.3
|
27
|
-
gemfile: gemfiles/jekyll_v1.gemfile
|
28
|
-
- rvm: 2.4.5
|
14
|
+
include:
|
15
|
+
- rvm: 2.3.8
|
29
16
|
gemfile: gemfiles/jekyll_v2.gemfile
|
30
|
-
- rvm: 2.
|
17
|
+
- rvm: 2.3.8
|
31
18
|
gemfile: gemfiles/jekyll_v1.gemfile
|
32
19
|
allow_failures:
|
33
20
|
- rvm: ruby-head
|
data/Gemfile
CHANGED
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -14,6 +14,7 @@ Main features:
|
|
14
14
|
- Localization
|
15
15
|
- Level of detail customization
|
16
16
|
- Command line utility
|
17
|
+
- Approximate distance, with customizable threshold, ie: `366 days` becomes `1 year ago` instead of `1 year and 1 day ago`
|
17
18
|
|
18
19
|
In fact, `jekyll-timeago` started as an extension for the [Liquid](https://github.com/Shopify/liquid) template engine, to be used in Jekyll and Octopress backed sites. But actually, you can use it easily on any Ruby project and even as a tool from the [terminal](#cli)!
|
19
20
|
|
@@ -107,6 +108,15 @@ You are able to change the level of detail (from 1 up to 4, 2 by default) to get
|
|
107
108
|
=> "5 years, 5 months, 3 weeks and 4 days ago"
|
108
109
|
```
|
109
110
|
|
111
|
+
- `threshold`
|
112
|
+
|
113
|
+
The next component in the time must at least match this threshold to be picked. Set to 0 by default, so you don't get any approximations. Can be used to drop "straggling" values which are too low to be of any use (`in 7 months and 2 days` is as good as saying `in 7 months`).
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
>> timeago(Date.today.prev_day(366), depth: 2, threshold: 0.05)
|
117
|
+
=> "1 year ago"
|
118
|
+
```
|
119
|
+
|
110
120
|
## Localization
|
111
121
|
|
112
122
|
By default, `jekyll-timego` already provides translations for some languages. You can check the list [here](lib/locales/). However, you are able to provide your own translations, or even override the originals, easily.
|
data/jekyll-timeago.gemspec
CHANGED
@@ -5,8 +5,8 @@ Gem::Specification.new do |spec|
|
|
5
5
|
spec.version = Jekyll::Timeago::VERSION
|
6
6
|
spec.authors = ["markets"]
|
7
7
|
spec.email = ["srmarc.ai@gmail.com"]
|
8
|
-
spec.description = "A Ruby library to compute distance of dates in words. Originally built for Jekyll, as a Liquid extension. It also supports localization and futures."
|
9
8
|
spec.summary = "A date helper to compute distance of dates in words."
|
9
|
+
spec.description = "A Ruby library to compute distance of dates in words. Originally built for Jekyll, as a Liquid extension. It also supports localization and futures."
|
10
10
|
spec.homepage = "https://github.com/markets/jekyll-timeago"
|
11
11
|
spec.license = "MIT"
|
12
12
|
|
@@ -18,9 +18,8 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.add_dependency "mini_i18n", '>= 0.8.0'
|
19
19
|
|
20
20
|
spec.add_development_dependency "jekyll", ">= 1.5"
|
21
|
-
spec.add_development_dependency "bundler"
|
21
|
+
spec.add_development_dependency "bundler"
|
22
22
|
spec.add_development_dependency "rake"
|
23
23
|
spec.add_development_dependency "rspec"
|
24
24
|
spec.add_development_dependency "appraisal"
|
25
|
-
spec.add_development_dependency "byebug"
|
26
25
|
end
|
data/lib/jekyll-timeago/core.rb
CHANGED
@@ -11,6 +11,9 @@ module Jekyll
|
|
11
11
|
# Default level of detail
|
12
12
|
DEFAULT_DEPTH_LEVEL = 2
|
13
13
|
|
14
|
+
# Default threshold
|
15
|
+
DEFAULT_THRESHOLD = 0
|
16
|
+
|
14
17
|
def timeago(from, to = Date.today, options = {})
|
15
18
|
if to.is_a?(Hash)
|
16
19
|
options = to
|
@@ -19,11 +22,12 @@ module Jekyll
|
|
19
22
|
|
20
23
|
@options = options
|
21
24
|
|
22
|
-
from
|
23
|
-
to
|
24
|
-
depth
|
25
|
+
from = validate_date(from)
|
26
|
+
to = validate_date(to)
|
27
|
+
depth = validate_depth(@options[:depth] || @options["depth"])
|
28
|
+
threshold = validate_threshold(@options[:threshold])
|
25
29
|
|
26
|
-
time_ago_to_now(from, to, depth)
|
30
|
+
time_ago_to_now(from, to, depth, threshold)
|
27
31
|
end
|
28
32
|
|
29
33
|
private
|
@@ -32,11 +36,15 @@ module Jekyll
|
|
32
36
|
Date.parse(date.to_s)
|
33
37
|
end
|
34
38
|
|
39
|
+
def validate_threshold(threshold)
|
40
|
+
(0.0..1.0).include?(threshold) ? threshold : DEFAULT_THRESHOLD
|
41
|
+
end
|
42
|
+
|
35
43
|
def validate_depth(depth)
|
36
44
|
(1..MAX_DEPTH_LEVEL).include?(depth) ? depth : DEFAULT_DEPTH_LEVEL
|
37
45
|
end
|
38
46
|
|
39
|
-
def time_ago_to_now(from, to, depth)
|
47
|
+
def time_ago_to_now(from, to, depth, threshold)
|
40
48
|
days_passed = (to - from).to_i
|
41
49
|
|
42
50
|
return t(:today) if days_passed == 0
|
@@ -44,7 +52,7 @@ module Jekyll
|
|
44
52
|
return t(:tomorrow) if days_passed == -1
|
45
53
|
|
46
54
|
past_or_future = from < to ? :past : :future
|
47
|
-
slots = build_time_ago_slots(days_passed.abs, depth)
|
55
|
+
slots = build_time_ago_slots(days_passed.abs, depth, threshold)
|
48
56
|
|
49
57
|
t(past_or_future, date_range: to_sentence(slots))
|
50
58
|
end
|
@@ -56,8 +64,9 @@ module Jekyll
|
|
56
64
|
# Builds time ranges: ['1 month', '5 days']
|
57
65
|
# - days_passed: integer in absolute
|
58
66
|
# - depth: level of detail
|
67
|
+
# - threshold: minimum fractional difference to keep for next slot
|
59
68
|
# - current_slots: built time slots
|
60
|
-
def build_time_ago_slots(days_passed, depth, current_slots = [])
|
69
|
+
def build_time_ago_slots(days_passed, depth, threshold, current_slots = [])
|
61
70
|
return current_slots if depth == 0 || days_passed == 0
|
62
71
|
|
63
72
|
range = days_to_range(days_passed)
|
@@ -67,7 +76,12 @@ module Jekyll
|
|
67
76
|
current_slots << t(range, count: num_elems)
|
68
77
|
|
69
78
|
pending_days = days_passed - (num_elems * days)
|
70
|
-
|
79
|
+
|
80
|
+
if pending_days >= (days_passed * threshold).floor
|
81
|
+
build_time_ago_slots(pending_days, depth - 1, threshold, current_slots)
|
82
|
+
else
|
83
|
+
current_slots
|
84
|
+
end
|
71
85
|
end
|
72
86
|
|
73
87
|
def days_to_range(days)
|
data/spec/jekyll-timeago_spec.rb
CHANGED
@@ -25,7 +25,8 @@ describe Jekyll::Timeago do
|
|
25
25
|
"<p>2 años</p>\n"\
|
26
26
|
"<p>12 meses</p>\n"\
|
27
27
|
"<p>12 meses</p>\n"\
|
28
|
-
"<p>2 años</p
|
28
|
+
"<p>2 años</p>\n"\
|
29
|
+
"<p>en 1 año</p>\n"
|
29
30
|
|
30
31
|
expect(contents).to eq(expected)
|
31
32
|
end
|
@@ -71,6 +72,10 @@ describe Jekyll::Timeago do
|
|
71
72
|
expect(timeago(sample_date.prev_day(500), sample_date, depth: 5)).to eq('1 year and 4 months ago')
|
72
73
|
end
|
73
74
|
|
75
|
+
it 'allow threshold configuration' do
|
76
|
+
expect(timeago(sample_date.prev_day(366), sample_date, threshold: 0.05)).to eq('1 year ago')
|
77
|
+
end
|
78
|
+
|
74
79
|
it 'allow localization' do
|
75
80
|
expect(timeago(sample_date.prev_day(100), sample_date, locale: :fr)).to eq('il y a environ 3 mois et 1 semaine')
|
76
81
|
expect(timeago(sample_date.prev_day(100), sample_date, locale: :ru)).to eq('3 месяца и неделю назад')
|
data/spec/source/_config.yml
CHANGED
data/spec/source/index.html
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jekyll-timeago
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.13.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- markets
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-05-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mini_i18n
|
@@ -40,20 +40,6 @@ dependencies:
|
|
40
40
|
version: '1.5'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: bundler
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '1.3'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '1.3'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: rake
|
57
43
|
requirement: !ruby/object:Gem::Requirement
|
58
44
|
requirements:
|
59
45
|
- - ">="
|
@@ -67,7 +53,7 @@ dependencies:
|
|
67
53
|
- !ruby/object:Gem::Version
|
68
54
|
version: '0'
|
69
55
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
56
|
+
name: rake
|
71
57
|
requirement: !ruby/object:Gem::Requirement
|
72
58
|
requirements:
|
73
59
|
- - ">="
|
@@ -81,7 +67,7 @@ dependencies:
|
|
81
67
|
- !ruby/object:Gem::Version
|
82
68
|
version: '0'
|
83
69
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
70
|
+
name: rspec
|
85
71
|
requirement: !ruby/object:Gem::Requirement
|
86
72
|
requirements:
|
87
73
|
- - ">="
|
@@ -95,7 +81,7 @@ dependencies:
|
|
95
81
|
- !ruby/object:Gem::Version
|
96
82
|
version: '0'
|
97
83
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
84
|
+
name: appraisal
|
99
85
|
requirement: !ruby/object:Gem::Requirement
|
100
86
|
requirements:
|
101
87
|
- - ">="
|
@@ -165,8 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
165
151
|
- !ruby/object:Gem::Version
|
166
152
|
version: '0'
|
167
153
|
requirements: []
|
168
|
-
|
169
|
-
rubygems_version: 2.7.6
|
154
|
+
rubygems_version: 3.0.3
|
170
155
|
signing_key:
|
171
156
|
specification_version: 4
|
172
157
|
summary: A date helper to compute distance of dates in words.
|