jekyll-timeago 1.0.0 → 1.1.0
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 +4 -4
- data/README.md +24 -7
- data/bin/timeago +1 -1
- data/lib/jekyll-timeago/core.rb +38 -10
- data/lib/jekyll-timeago/version.rb +1 -1
- data/lib/locales/{ja.yaml → ja.yml} +1 -1
- data/spec/jekyll-timeago_spec.rb +32 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f8ae690f6686791715f0397fd0c38f6284c5857e78d9cc7669d437d402ce838
|
4
|
+
data.tar.gz: '083e7d05d403c01c22dac5f5f2bc5b832279dd5a7fb65a049e497939078ed0e9'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c1184731bf638031b1ab559e9fc26c0e98a641d33a4e71b5ae7fb88c5a6c6982975de6f6d0c311daf88f76de4c1fdb6931acc54241cb8b860b4bb4b8621e45ed
|
7
|
+
data.tar.gz: 3c03eebcd1100a75059fc96f11987204c7fe08acf6879da4ea35d4a451f9902952b3932915e3a79d8ee532b2bd21dd1a6f7fa64db984d91af2ca57fa8261b649
|
data/README.md
CHANGED
@@ -8,14 +8,14 @@
|
|
8
8
|
|
9
9
|
Main features:
|
10
10
|
|
11
|
-
- Compute distance of dates, in words
|
12
|
-
- Future times
|
13
|
-
- Alternative formats: short (`2y and 1mo ago`)
|
11
|
+
- Compute distance of dates, in words: `1 week and 2 days ago`, `5 months ago`
|
12
|
+
- Future times: `in 1 year`
|
13
|
+
- Alternative formats: short (`2y and 1mo ago`), array (`['2 years', '1 month']`), and hash (`{years: 2, months: 1}`)
|
14
14
|
- Out of the box support for `Jekyll` projects, available as a Liquid Filter and as a Liquid Tag
|
15
|
-
- Localization
|
15
|
+
- Localization: `hace 3 semanas`, `3週間前`, `il y a environ 3 semaines`
|
16
16
|
- Level of detail customization
|
17
|
-
-
|
18
|
-
- Approximate distance, with customizable threshold
|
17
|
+
- CLI
|
18
|
+
- Approximate distance, with customizable threshold: `366 days` becomes `1 year ago` instead of `1 year and 1 day ago`
|
19
19
|
|
20
20
|
In fact, `jekyll-timeago` started as an extension for the [Liquid](https://github.com/Shopify/liquid) template engine, to be used in Jekyll backed sites. But actually, you can use it easily on any Ruby project and even as a tool from the [terminal](#cli)!
|
21
21
|
|
@@ -74,7 +74,7 @@ Examples:
|
|
74
74
|
=> "in 2 years and 8 months"
|
75
75
|
```
|
76
76
|
|
77
|
-
**NOTE** If you have the gem installed in your system globally, and you're not using Bundler (probably because you're are writing a
|
77
|
+
**NOTE** If you have the gem installed in your system globally, and you're not using Bundler (probably because you're are writing a small script), don't forget to require the library first:
|
78
78
|
|
79
79
|
```ruby
|
80
80
|
require 'jekyll-timeago'
|
@@ -138,6 +138,19 @@ Use `:array` style for structured data:
|
|
138
138
|
=> ["1 year"]
|
139
139
|
>> timeago(Date.today.prev_day(160), style: :array)
|
140
140
|
=> ["5 months", "1 week"]
|
141
|
+
>> timeago(Date.today.prev_day(160), style: :array, locale: :es)
|
142
|
+
=> ["5 meses", "1 semana"]
|
143
|
+
```
|
144
|
+
|
145
|
+
Use `:hash` style for structured hash data:
|
146
|
+
|
147
|
+
```ruby
|
148
|
+
>> timeago(Date.today.prev_day(365), style: :hash)
|
149
|
+
=> {:years=>1}
|
150
|
+
>> timeago(Date.today.prev_day(160), style: :hash)
|
151
|
+
=> {:months=>5, :weeks=>1}
|
152
|
+
>> timeago(Date.today.prev_day(160), style: :hash, locale: :es)
|
153
|
+
=> {:meses=>5, :semanas=>1}
|
141
154
|
```
|
142
155
|
|
143
156
|
#### `only`
|
@@ -157,6 +170,8 @@ Use the `only` option to accumulate all time into a single unit. Supported value
|
|
157
170
|
=> "52w ago"
|
158
171
|
>> timeago(Date.today.prev_day(365), only: :months, locale: :es)
|
159
172
|
=> "hace 12 meses"
|
173
|
+
>> timeago(Date.today.prev_day(365), only: :months, style: :hash)
|
174
|
+
=> {:months=>12}
|
160
175
|
```
|
161
176
|
|
162
177
|
## Localization
|
@@ -188,6 +203,8 @@ il y a environ 2 années et 6 mois
|
|
188
203
|
2y ago
|
189
204
|
> timeago 2016-1-1 2018-1-1 -l es -s short
|
190
205
|
hace 2a y 1d
|
206
|
+
> timeago 2016-1-1 2018-1-1 --style hash
|
207
|
+
{:years=>2, :days=>1}
|
191
208
|
> timeago 2016-1-1 2018-1-1 --only weeks
|
192
209
|
104 weeks ago
|
193
210
|
> timeago 2016-1-1 2018-1-1 --only months -s short
|
data/bin/timeago
CHANGED
@@ -36,7 +36,7 @@ parser = OptionParser.new do |opts|
|
|
36
36
|
options[:locale] = locale
|
37
37
|
end
|
38
38
|
|
39
|
-
opts.on("-s", "--style STYLE", "Uses the provided style (short, array)") do |style|
|
39
|
+
opts.on("-s", "--style STYLE", "Uses the provided style (short, array, hash)") do |style|
|
40
40
|
options[:style] = style
|
41
41
|
end
|
42
42
|
|
data/lib/jekyll-timeago/core.rb
CHANGED
@@ -15,7 +15,7 @@ module Jekyll
|
|
15
15
|
DEFAULT_THRESHOLD = 0
|
16
16
|
|
17
17
|
# Available styles
|
18
|
-
STYLES = %w(
|
18
|
+
STYLES = %w(short array hash)
|
19
19
|
|
20
20
|
# Available "only" options
|
21
21
|
ONLY_OPTIONS = %w(years months weeks days)
|
@@ -58,7 +58,6 @@ module Jekyll
|
|
58
58
|
end
|
59
59
|
|
60
60
|
def validate_only(only)
|
61
|
-
return nil if only.nil?
|
62
61
|
only = only.to_s
|
63
62
|
ONLY_OPTIONS.include?(only) ? only : nil
|
64
63
|
end
|
@@ -66,14 +65,24 @@ module Jekyll
|
|
66
65
|
def time_ago_to_now
|
67
66
|
days_passed = (@to - @from).to_i
|
68
67
|
|
69
|
-
|
70
|
-
|
71
|
-
|
68
|
+
if @style == "hash"
|
69
|
+
return { localized_unit_name(:days) => 0 } if days_passed == 0
|
70
|
+
return { localized_unit_name(:days) => 1 } if days_passed == 1
|
71
|
+
return { localized_unit_name(:days) => -1 } if days_passed == -1
|
72
|
+
elsif @style == "array"
|
73
|
+
return [t(:today)] if days_passed == 0
|
74
|
+
return [t(:yesterday)] if days_passed == 1
|
75
|
+
return [t(:tomorrow)] if days_passed == -1
|
76
|
+
else
|
77
|
+
return t(:today) if days_passed == 0
|
78
|
+
return t(:yesterday) if days_passed == 1
|
79
|
+
return t(:tomorrow) if days_passed == -1
|
80
|
+
end
|
72
81
|
|
73
82
|
past_or_future = @from < @to ? :past : :future
|
74
83
|
slots = build_time_ago_slots(days_passed.abs)
|
75
84
|
|
76
|
-
if @style == "array"
|
85
|
+
if @style == "array" || @style == "hash"
|
77
86
|
slots
|
78
87
|
else
|
79
88
|
t(past_or_future, date_range: to_sentence(slots))
|
@@ -93,7 +102,15 @@ module Jekyll
|
|
93
102
|
end
|
94
103
|
end
|
95
104
|
|
96
|
-
#
|
105
|
+
# Get localized unit name for hash keys (always plural form)
|
106
|
+
def localized_unit_name(unit)
|
107
|
+
# Extract the unit name from the plural form translation
|
108
|
+
translated = t(unit, count: 2)
|
109
|
+
# Remove any count prefix (e.g. "2 años" -> "años")
|
110
|
+
translated.gsub(/^\d+\s+/, '').to_sym
|
111
|
+
end
|
112
|
+
|
113
|
+
# Builds time ranges with natural unit conversions: ['1 month', '5 days'] or {:months => 1, :days => 5}
|
97
114
|
def build_time_ago_slots(days_passed)
|
98
115
|
# If "only" option is specified, calculate total time in that unit
|
99
116
|
return build_only_slots(days_passed) if @only
|
@@ -104,15 +121,26 @@ module Jekyll
|
|
104
121
|
# Select components based on depth and threshold
|
105
122
|
selected = select_components(components, days_passed)
|
106
123
|
|
107
|
-
#
|
108
|
-
|
124
|
+
# Format output based on current style
|
125
|
+
if @style == "hash"
|
126
|
+
result = {}
|
127
|
+
selected.each { |unit, count| result[localized_unit_name(unit)] = count }
|
128
|
+
result
|
129
|
+
else
|
130
|
+
selected.map { |unit, count| translate_unit(unit, count) }
|
131
|
+
end
|
109
132
|
end
|
110
133
|
|
111
134
|
# Build time slots when "only" option is specified
|
112
135
|
def build_only_slots(days_passed)
|
113
136
|
unit = @only.to_sym
|
114
137
|
count = calculate_total_in_unit(days_passed, unit)
|
115
|
-
|
138
|
+
|
139
|
+
if @style == "hash"
|
140
|
+
{ localized_unit_name(unit) => count }
|
141
|
+
else
|
142
|
+
[translate_unit(unit, count)]
|
143
|
+
end
|
116
144
|
end
|
117
145
|
|
118
146
|
# Calculate total time in specified unit
|
data/spec/jekyll-timeago_spec.rb
CHANGED
@@ -97,6 +97,8 @@ describe Jekyll::Timeago do
|
|
97
97
|
it 'allows localization' do
|
98
98
|
expect(timeago(sample_date.prev_day(100), sample_date, locale: :fr)).to eq('il y a environ 3 mois et 1 semaine')
|
99
99
|
expect(timeago(sample_date.prev_day(100), sample_date, locale: :ru)).to eq('3 месяца и неделю назад')
|
100
|
+
expect(timeago(sample_date.prev_day(100), sample_date, locale: :it)).to eq('3 mesi e 1 settimana fa')
|
101
|
+
expect(timeago(sample_date.prev_day(100), sample_date, locale: :pt)).to eq('3 meses e 1 semana atrás')
|
100
102
|
end
|
101
103
|
|
102
104
|
it 'allows short style formatting' do
|
@@ -116,6 +118,8 @@ describe Jekyll::Timeago do
|
|
116
118
|
expect(timeago(sample_date.prev_day(365), sample_date, locale: :ru, style: :short)).to eq('1г назад')
|
117
119
|
expect(timeago(sample_date.prev_day(365), sample_date, locale: :es, style: :short)).to eq('hace 1a')
|
118
120
|
expect(timeago(sample_date.prev_day(30), sample_date, locale: :de, style: :short)).to eq('vor 1mo')
|
121
|
+
expect(timeago(sample_date.prev_day(120), sample_date, locale: :ca, style: :short)).to eq('fa 4m')
|
122
|
+
expect(timeago(sample_date.prev_day(120), sample_date, locale: :ja, style: :short)).to eq('4月前')
|
119
123
|
end
|
120
124
|
|
121
125
|
it 'allows complex combinations with short style' do
|
@@ -125,12 +129,26 @@ describe Jekyll::Timeago do
|
|
125
129
|
end
|
126
130
|
|
127
131
|
it 'allows array style formatting' do
|
132
|
+
expect(timeago(sample_date, sample_date, style: :array)).to eq(['today'])
|
128
133
|
expect(timeago(sample_date.prev_day(365), sample_date, style: :array)).to eq(['1 year'])
|
129
134
|
expect(timeago(sample_date.prev_day(365), sample_date, "style" => "array")).to eq(['1 year'])
|
130
135
|
expect(timeago(sample_date.prev_day(160), sample_date, style: :array)).to eq(['5 months', '1 week'])
|
131
136
|
expect(timeago(sample_date.prev_day(160), sample_date, style: :array, locale: :es)).to eq(['5 meses', '1 semana'])
|
132
137
|
end
|
133
138
|
|
139
|
+
it 'allows hash style formatting' do
|
140
|
+
expect(timeago(sample_date, sample_date, style: :hash)).to eq({days: 0})
|
141
|
+
expect(timeago(sample_date, sample_date, style: :hash, locale: :es)).to eq({días: 0})
|
142
|
+
expect(timeago(sample_date.prev_day(365), sample_date, style: :hash)).to eq({years: 1})
|
143
|
+
expect(timeago(sample_date.prev_day(365), sample_date, "style" => "hash")).to eq({years: 1})
|
144
|
+
expect(timeago(sample_date.prev_day(160), sample_date, style: :hash)).to eq({months: 5, weeks: 1})
|
145
|
+
expect(timeago(sample_date.prev_day(500), sample_date, style: :hash)).to eq({years: 1, months: 4})
|
146
|
+
expect(timeago(sample_date.prev_day(10), sample_date, style: :hash)).to eq({weeks: 1, days: 3})
|
147
|
+
expect(timeago(sample_date.prev_day(160), sample_date, style: :hash, locale: :es)).to eq({meses: 5, semanas: 1})
|
148
|
+
expect(timeago(sample_date.prev_day(500), sample_date, style: :hash, locale: :fr)).to eq({années: 1, mois: 4})
|
149
|
+
expect(timeago(sample_date.prev_day(500), sample_date, style: :hash, depth: 4)).to eq({years: 1, months: 4, weeks: 2, days: 1})
|
150
|
+
end
|
151
|
+
|
134
152
|
it 'allows "only" option to accumulate time into single unit' do
|
135
153
|
# Test "only: :days"
|
136
154
|
expect(timeago(sample_date.prev_day(7), sample_date, only: :days)).to eq('7 days ago')
|
@@ -162,6 +180,13 @@ describe Jekyll::Timeago do
|
|
162
180
|
# Test with array style
|
163
181
|
expect(timeago(sample_date.prev_day(365), sample_date, only: :weeks, style: :array)).to eq(['52 weeks'])
|
164
182
|
expect(timeago(sample_date.prev_day(30), sample_date, only: :months, style: :array)).to eq(['1 month'])
|
183
|
+
|
184
|
+
# Test with hash style
|
185
|
+
expect(timeago(sample_date.prev_day(365), sample_date, only: :weeks, style: :hash)).to eq({weeks: 52})
|
186
|
+
expect(timeago(sample_date.prev_day(30), sample_date, only: :months, style: :hash)).to eq({months: 1})
|
187
|
+
expect(timeago(sample_date.prev_day(365), sample_date, only: :days, style: :hash)).to eq({days: 365})
|
188
|
+
expect(timeago(sample_date.prev_day(365), sample_date, only: :years, style: :hash)).to eq({years: 1})
|
189
|
+
expect(timeago(sample_date.prev_day(365), sample_date, only: :weeks, style: :hash, locale: :es)).to eq({semanas: 52})
|
165
190
|
end
|
166
191
|
|
167
192
|
it 'allows "only" option with different locales' do
|
@@ -203,12 +228,19 @@ describe Jekyll::Timeago do
|
|
203
228
|
expect(`bin/timeago 2016-1-1 2018-1-1 --locale ru --style short`).to match("2г и 1д назад")
|
204
229
|
end
|
205
230
|
|
231
|
+
it 'with hash style' do
|
232
|
+
expect(`bin/timeago 2016-1-1 2018-1-1 --style hash`).to match("{:years=>2, :days=>1}")
|
233
|
+
expect(`bin/timeago 2016-1-1 2018-1-1 -s hash`).to match("{:years=>2, :days=>1}")
|
234
|
+
expect(`bin/timeago 2016-1-1 2016-2-1 --style hash`).to match("{:months=>1, :days=>1}")
|
235
|
+
end
|
236
|
+
|
206
237
|
it 'with only option' do
|
207
238
|
expect(`bin/timeago 2016-1-1 2018-1-1 --only weeks`).to match("104 weeks ago")
|
208
239
|
expect(`bin/timeago 2016-1-1 2018-1-1 -o months`).to match("24 months ago")
|
209
240
|
expect(`bin/timeago 2016-1-1 2016-2-1 --only days`).to match("31 days ago")
|
210
241
|
expect(`bin/timeago 2016-1-1 2018-1-1 -l fr --only months`).to match("il y a environ 24 mois")
|
211
242
|
expect(`bin/timeago 2016-1-1 2018-1-1 --only weeks -s short`).to match("104w ago")
|
243
|
+
expect(`bin/timeago 2016-1-1 2018-1-1 --only weeks --style hash`).to match("{:weeks=>104}")
|
212
244
|
end
|
213
245
|
end
|
214
246
|
end
|
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: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- markets
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-08-
|
11
|
+
date: 2025-08-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mini_i18n
|
@@ -110,7 +110,7 @@ files:
|
|
110
110
|
- lib/locales/es.yml
|
111
111
|
- lib/locales/fr.yml
|
112
112
|
- lib/locales/it.yml
|
113
|
-
- lib/locales/ja.
|
113
|
+
- lib/locales/ja.yml
|
114
114
|
- lib/locales/pt.yml
|
115
115
|
- lib/locales/ru.yml
|
116
116
|
- lib/locales/tr.yml
|