jekyll-timeago 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +38 -13
- data/jekyll-timeago.gemspec +0 -1
- data/lib/jekyll/timeago.rb +57 -29
- data/lib/jekyll/timeago/version.rb +1 -1
- metadata +4 -20
data/README.md
CHANGED
@@ -1,22 +1,28 @@
|
|
1
1
|
Jekyll-Timeago
|
2
2
|
==============
|
3
|
-
Custom and simple implementation of `timeago` date filter. Futures supported.
|
3
|
+
Custom and simple implementation of `timeago` date filter. Futures and personalization are also supported.
|
4
4
|
|
5
5
|
In fact, `jekyll-timeago` is an extension of [Liquid](https://github.com/Shopify/liquid) filters, so you can use it in all your Liquid templates.
|
6
6
|
|
7
|
+
|
7
8
|
## Installation
|
8
|
-
Add this gem to your Gemfile and run bundle
|
9
|
+
Add this gem to your `Gemfile` and run `bundle`:
|
10
|
+
|
9
11
|
```
|
10
12
|
gem 'jekyll-timeago'
|
11
13
|
```
|
12
|
-
|
13
|
-
|
14
|
+
|
15
|
+
To use this filter, just add the following to the top of another plugin (found under `_plugins/`):
|
16
|
+
|
17
|
+
```ruby
|
14
18
|
require 'jekyll/timeago'
|
15
19
|
```
|
16
|
-
|
20
|
+
|
21
|
+
Alternatively, you can simply copy [this file](https://github.com/markets/jekyll-timeago/blob/master/lib/jekyll/timeago.rb) directly into your `_plugins/` directory! :)
|
22
|
+
|
17
23
|
|
18
24
|
## Usage
|
19
|
-
```
|
25
|
+
```html
|
20
26
|
<span>{{ page.date | timeago }}</span>
|
21
27
|
<h2>{{ page.title }}</h2>
|
22
28
|
|
@@ -25,23 +31,42 @@ You can copy this [file](https://github.com/markets/jekyll-timeago/blob/master/l
|
|
25
31
|
</div>
|
26
32
|
```
|
27
33
|
|
28
|
-
|
34
|
+
### Customization
|
35
|
+
You can personalize the level of detail (from 1 up to 4, 2 by default) passing a parameter:
|
36
|
+
```html
|
37
|
+
<span>{{ page.date | timeago: 4 }}</span>
|
29
38
|
```
|
39
|
+
|
40
|
+
## Output Examples
|
41
|
+
Default behavior:
|
42
|
+
```ruby
|
30
43
|
> timeago(Date.today)
|
31
44
|
=> "today"
|
32
45
|
> timeago(Date.today - 1.day)
|
33
46
|
=> "yesterday"
|
34
47
|
> timeago(Date.today - 10.days)
|
35
|
-
=> "1 week ago"
|
48
|
+
=> "1 week and 3 days ago"
|
36
49
|
> timeago(Date.today - 100.days)
|
37
|
-
=> "3 months ago"
|
38
|
-
> timeago(Date.today -
|
39
|
-
=> "1 year ago"
|
50
|
+
=> "3 months and 1 week ago"
|
51
|
+
> timeago(Date.today - 500.days)
|
52
|
+
=> "1 year ago and 4 months ago"
|
40
53
|
> timeago(Date.today + 1.days)
|
41
54
|
=> "tomorrow"
|
42
|
-
> timeago(Date.today +
|
55
|
+
> timeago(Date.today + 7.days)
|
43
56
|
=> "in 1 week"
|
44
57
|
```
|
45
58
|
|
59
|
+
Change level of detail to get higher or lower granularity:
|
60
|
+
```ruby
|
61
|
+
> timeago(Date.today - 500.days) # default
|
62
|
+
=> "1 year ago and 4 months ago"
|
63
|
+
> timeago(Date.today - 500.days, 3)
|
64
|
+
=> "1 year and 4 months and 1 week ago"
|
65
|
+
> timeago(Date.today - 500.days, 4)
|
66
|
+
=> "1 year and 4 months and 1 week and 4 days ago"
|
67
|
+
> timeago(Date.today - 500.days, 1)
|
68
|
+
=> "1 year ago"
|
69
|
+
```
|
70
|
+
|
46
71
|
## License
|
47
|
-
Copyright (c) 2013 Marc Anguera. Unscoped Associations is released under the [MIT](http://opensource.org/licenses/MIT) License.
|
72
|
+
Copyright (c) 2013 Marc Anguera. Unscoped Associations is released under the [MIT](http://opensource.org/licenses/MIT) License.
|
data/jekyll-timeago.gemspec
CHANGED
data/lib/jekyll/timeago.rb
CHANGED
@@ -1,60 +1,88 @@
|
|
1
1
|
require "jekyll/timeago/version"
|
2
|
-
require "active_support/inflector"
|
3
2
|
|
4
3
|
module Jekyll
|
5
4
|
module Timeago
|
6
5
|
|
7
|
-
|
6
|
+
DAYS_PER = {
|
8
7
|
:days => 1,
|
9
8
|
:weeks => 7,
|
10
9
|
:months => 31,
|
11
10
|
:years => 365,
|
12
11
|
}
|
13
12
|
|
14
|
-
|
15
|
-
|
13
|
+
# Max level of detail
|
14
|
+
# years > months > weeks > days
|
15
|
+
# 1 year and 7 months and 2 weeks and 6 days
|
16
|
+
MAX_DEPTH_LEVEL = 4
|
17
|
+
|
18
|
+
# Default level of detail
|
19
|
+
# 1 month and 5 days, 3 weeks and 2 days, 2 years and 6 months
|
20
|
+
DEFAULT_DEPTH_LEVEL = 2
|
21
|
+
|
22
|
+
def timeago(input, depth = DEFAULT_DEPTH_LEVEL)
|
23
|
+
unless depth_allowed?(depth)
|
24
|
+
raise "Invalid depth level: #{depth.inspect}"
|
25
|
+
end
|
26
|
+
|
27
|
+
unless input.is_a?(Date) || input.is_a?(Time)
|
28
|
+
raise "Invalid input type: #{input.inspect}"
|
29
|
+
end
|
30
|
+
|
31
|
+
days_passed = (Date.today - Date.parse(input.to_s)).to_i
|
32
|
+
time_ago_to_now(days_passed, depth)
|
16
33
|
end
|
17
34
|
|
18
35
|
private
|
19
36
|
|
20
|
-
def time_ago_to_now(
|
21
|
-
|
22
|
-
|
23
|
-
|
37
|
+
def time_ago_to_now(days_passed, depth)
|
38
|
+
return "today" if days_passed == 0
|
39
|
+
return "yesterday" if days_passed == 1
|
40
|
+
return "tomorrow" if days_passed == -1
|
24
41
|
|
25
|
-
|
42
|
+
future = days_passed < 0
|
43
|
+
slots = build_time_ago_slots(days_passed, depth)
|
26
44
|
|
27
|
-
|
28
|
-
|
29
|
-
time_ago_to_s(days_passed, :days)
|
30
|
-
when 8 .. 31
|
31
|
-
time_ago_to_s(days_passed, :weeks)
|
32
|
-
when 32 .. 365
|
33
|
-
time_ago_to_s(days_passed, :months)
|
45
|
+
if future
|
46
|
+
"in #{slots.join(' and ')}"
|
34
47
|
else
|
35
|
-
|
48
|
+
"#{slots.join(' and ')} ago"
|
36
49
|
end
|
37
50
|
end
|
38
51
|
|
39
|
-
|
40
|
-
|
41
|
-
return
|
42
|
-
return "tomorrow" if days_passed == -1
|
52
|
+
# Builds time ranges: ['1 month', '5 days']
|
53
|
+
def build_time_ago_slots(days_passed, depth, current_slots = [])
|
54
|
+
return current_slots if depth == 0 || days_passed == 0
|
43
55
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
56
|
+
time_range = days_to_time_range(days_passed)
|
57
|
+
days = DAYS_PER[time_range]
|
58
|
+
num_elems = days_passed.abs / days
|
59
|
+
range_type = if num_elems == 1
|
60
|
+
time_range.to_s[0...-1] # singularize
|
48
61
|
else
|
49
|
-
|
62
|
+
time_range.to_s
|
50
63
|
end
|
51
64
|
|
52
|
-
|
53
|
-
|
65
|
+
current_slots << "#{num_elems} #{range_type}"
|
66
|
+
pending_days = days_passed - (num_elems*days)
|
67
|
+
build_time_ago_slots(pending_days, depth - 1, current_slots)
|
68
|
+
end
|
69
|
+
|
70
|
+
def days_to_time_range(days_passed)
|
71
|
+
case days_passed.abs
|
72
|
+
when 0...7
|
73
|
+
:days
|
74
|
+
when 7...31
|
75
|
+
:weeks
|
76
|
+
when 31...365
|
77
|
+
:months
|
54
78
|
else
|
55
|
-
|
79
|
+
:years
|
56
80
|
end
|
57
81
|
end
|
82
|
+
|
83
|
+
def depth_allowed?(depth)
|
84
|
+
(1..MAX_DEPTH_LEVEL).include?(depth)
|
85
|
+
end
|
58
86
|
end
|
59
87
|
end
|
60
88
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jekyll-timeago
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-09-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -43,22 +43,6 @@ dependencies:
|
|
43
43
|
- - ! '>='
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '0'
|
46
|
-
- !ruby/object:Gem::Dependency
|
47
|
-
name: activesupport
|
48
|
-
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
|
-
requirements:
|
51
|
-
- - ! '>='
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: '0'
|
54
|
-
type: :runtime
|
55
|
-
prerelease: false
|
56
|
-
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
|
-
requirements:
|
59
|
-
- - ! '>='
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
46
|
description: Custom timeago filter for Jekyll (Liquid filter)
|
63
47
|
email:
|
64
48
|
- srmarc.ai@gmail.com
|
@@ -88,7 +72,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
88
72
|
version: '0'
|
89
73
|
segments:
|
90
74
|
- 0
|
91
|
-
hash:
|
75
|
+
hash: 284869011
|
92
76
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
77
|
none: false
|
94
78
|
requirements:
|
@@ -97,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
81
|
version: '0'
|
98
82
|
segments:
|
99
83
|
- 0
|
100
|
-
hash:
|
84
|
+
hash: 284869011
|
101
85
|
requirements: []
|
102
86
|
rubyforge_project:
|
103
87
|
rubygems_version: 1.8.24
|