jekyll-timeago 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -1
- data/.travis.yml +5 -0
- data/README.md +19 -14
- data/Rakefile +5 -0
- data/jekyll-timeago.gemspec +5 -2
- data/lib/jekyll-timeago/filter.rb +39 -27
- data/lib/jekyll-timeago/version.rb +1 -1
- data/spec/jekyll_timeago_spec.rb +81 -0
- data/spec/source/_config.yml +21 -0
- data/spec/source/index.html +8 -0
- data/spec/spec_helper.rb +43 -0
- metadata +70 -7
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Jekyll-Timeago
|
2
2
|
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/jekyll-timeago.svg)](http://badge.fury.io/rb/jekyll-timeago)
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/jekyll-timeago.svg)](http://badge.fury.io/rb/jekyll-timeago) [![Build Status](https://travis-ci.org/markets/jekyll-timeago.svg?branch=master)](https://travis-ci.org/markets/jekyll-timeago)
|
4
4
|
|
5
5
|
Custom and simple implementation of `timeago` date filter. Main features:
|
6
6
|
|
@@ -12,8 +12,8 @@ Custom and simple implementation of `timeago` date filter. Main features:
|
|
12
12
|
|
13
13
|
In fact, `jekyll-timeago` is an extension of [Liquid](https://github.com/Shopify/liquid) Filters and Tags, so you can use it in other Liquid templates (like Octopress).
|
14
14
|
|
15
|
-
|
16
15
|
## Installation
|
16
|
+
|
17
17
|
You have 3 options to install the plugin:
|
18
18
|
|
19
19
|
**Via Jekyll plugin system**
|
@@ -51,25 +51,20 @@ Bundler.require(:default)
|
|
51
51
|
|
52
52
|
Alternatively, you can simply copy [this file](lib/jekyll-timeago/filter.rb) and [this file](lib/jekyll-timeago/tag.rb) directly into your `_plugins/` directory!
|
53
53
|
|
54
|
-
|
55
54
|
## Usage
|
55
|
+
|
56
56
|
By default `timeago` computes distance of dates from passed date to current date (using `Date.today`). But you are able to modify this range passing a second argument in order to compute the distance of these dates in words.
|
57
57
|
|
58
58
|
**Filter example**:
|
59
59
|
|
60
60
|
```html
|
61
|
-
<
|
62
|
-
<h2>{{ page.title }}</h2>
|
63
|
-
|
64
|
-
<div class="post">
|
65
|
-
{{ content }}
|
66
|
-
</div>
|
61
|
+
<p>{{ page.date | timeago }}</p>
|
67
62
|
```
|
68
63
|
|
69
64
|
Passing a parameter:
|
70
65
|
|
71
66
|
```html
|
72
|
-
<
|
67
|
+
<p>{{ page.date | timeago: '2020-1-1' }}</p>
|
73
68
|
```
|
74
69
|
|
75
70
|
**Tag example**:
|
@@ -81,11 +76,11 @@ Passing a parameter:
|
|
81
76
|
Passing a second parameter:
|
82
77
|
|
83
78
|
```html
|
84
|
-
<p>{% timeago 2000-1-1
|
79
|
+
<p>{% timeago 2000-1-1 2010-1-1 %}</p>
|
85
80
|
```
|
86
81
|
|
87
|
-
|
88
82
|
## Localization
|
83
|
+
|
89
84
|
The plugin allows you to localize the strings needed to build the time ago sentences. For do this, you must add some extra keys to your `_config.yml`. You can simply copy them from [this example file](_config.yml.example) and translate it to your site's language. Sample:
|
90
85
|
|
91
86
|
```
|
@@ -109,8 +104,8 @@ jekyll_timeago:
|
|
109
104
|
day: 'day'
|
110
105
|
```
|
111
106
|
|
112
|
-
|
113
107
|
## Level of detail (Depth)
|
108
|
+
|
114
109
|
You are able to change the level of detail (from 1 up to 4, 2 by default) to get higher or lower granularity. This option is setted via the `config` file (see sample in previous section). Examples:
|
115
110
|
|
116
111
|
* Depht => 1 `1 year ago`
|
@@ -118,8 +113,8 @@ You are able to change the level of detail (from 1 up to 4, 2 by default) to get
|
|
118
113
|
* Depht => 3 `1 year, 4 months and 1 week ago`
|
119
114
|
* Depht => 4 `1 year, 4 months, 1 week and 4 days ago`
|
120
115
|
|
121
|
-
|
122
116
|
## Output Examples
|
117
|
+
|
123
118
|
Run `script/console` to start a custom IRB session and play with `timeago` method:
|
124
119
|
|
125
120
|
```ruby
|
@@ -143,5 +138,15 @@ Run `script/console` to start a custom IRB session and play with `timeago` metho
|
|
143
138
|
=> "in 2 years and 8 months"
|
144
139
|
```
|
145
140
|
|
141
|
+
Play with `options`:
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
>> options[:yesterday] = "ayer"
|
145
|
+
=> "ayer"
|
146
|
+
>> timeago(Date.today - 1.day)
|
147
|
+
=> "ayer"
|
148
|
+
```
|
149
|
+
|
146
150
|
## License
|
151
|
+
|
147
152
|
Copyright (c) 2013-2014 Marc Anguera. Jekyll-Timeago is released under the [MIT](LICENSE) License.
|
data/Rakefile
CHANGED
data/jekyll-timeago.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = Jekyll::Timeago::VERSION
|
9
9
|
spec.authors = ["markets"]
|
10
10
|
spec.email = ["srmarc.ai@gmail.com"]
|
11
|
-
spec.description =
|
12
|
-
spec.summary =
|
11
|
+
spec.description = "Custom timeago filter for Jekyll (Liquid Filter and Tag). Localization and futures supported."
|
12
|
+
spec.summary = "Custom timeago filter for Jekyll (Liquid Filter and Tag). Localization and futures supported."
|
13
13
|
spec.homepage = "https://github.com/markets/jekyll-timeago"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
@@ -18,6 +18,9 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
+
spec.add_development_dependency "jekyll", ">= 1.5"
|
21
22
|
spec.add_development_dependency "bundler", "~> 1.3"
|
22
23
|
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
spec.add_development_dependency "activesupport"
|
23
26
|
end
|
@@ -3,6 +3,7 @@ require 'date'
|
|
3
3
|
module Jekyll
|
4
4
|
module Timeago
|
5
5
|
module Filter
|
6
|
+
extend self
|
6
7
|
|
7
8
|
DAYS_PER = {
|
8
9
|
:days => 1,
|
@@ -28,6 +29,15 @@ module Jekyll
|
|
28
29
|
time_ago_to_now(from, to, depth)
|
29
30
|
end
|
30
31
|
|
32
|
+
def options
|
33
|
+
@options ||= setup
|
34
|
+
end
|
35
|
+
|
36
|
+
# Restore default configuration
|
37
|
+
def reset!
|
38
|
+
setup
|
39
|
+
end
|
40
|
+
|
31
41
|
private
|
32
42
|
|
33
43
|
def validate_date!(date)
|
@@ -39,30 +49,31 @@ module Jekyll
|
|
39
49
|
depth
|
40
50
|
end
|
41
51
|
|
42
|
-
#
|
43
|
-
def
|
44
|
-
@
|
52
|
+
# Load settings from Jekyll configuration
|
53
|
+
def jekyll_config
|
54
|
+
@jekyll_config ||= Jekyll.configuration({}).fetch('jekyll_timeago', {}) rescue {}
|
45
55
|
end
|
46
56
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
:
|
51
|
-
:
|
52
|
-
:
|
53
|
-
:
|
54
|
-
:
|
55
|
-
:
|
56
|
-
:
|
57
|
-
:
|
58
|
-
:
|
59
|
-
:
|
60
|
-
:
|
61
|
-
:
|
62
|
-
:
|
63
|
-
:
|
64
|
-
:
|
65
|
-
:
|
57
|
+
# Configure plugin options with defaults
|
58
|
+
def setup
|
59
|
+
@options = {
|
60
|
+
:depth => jekyll_config['depth'] || DEFAULT_DEPTH_LEVEL,
|
61
|
+
:today => jekyll_config['day'] || 'today',
|
62
|
+
:yesterday => jekyll_config['yesterday'] || 'yesterday',
|
63
|
+
:tomorrow => jekyll_config['tomorrow'] || 'tomorrow',
|
64
|
+
:and => jekyll_config['and'] ||'and',
|
65
|
+
:suffix => jekyll_config['suffix'] || 'ago',
|
66
|
+
:prefix => jekyll_config['prefix'] || '',
|
67
|
+
:suffix_future => jekyll_config['suffix_future'] || '',
|
68
|
+
:prefix_future => jekyll_config['prefix_future'] || 'in',
|
69
|
+
:years => jekyll_config['years'] || 'years',
|
70
|
+
:year => jekyll_config['year'] || 'year',
|
71
|
+
:months => jekyll_config['months'] || 'months',
|
72
|
+
:month => jekyll_config['month'] || 'month',
|
73
|
+
:weeks => jekyll_config['weeks'] || 'weeks',
|
74
|
+
:week => jekyll_config['week'] || 'week',
|
75
|
+
:days => jekyll_config['days'] || 'days',
|
76
|
+
:day => jekyll_config['day'] || 'day'
|
66
77
|
}
|
67
78
|
end
|
68
79
|
|
@@ -99,7 +110,8 @@ module Jekyll
|
|
99
110
|
|
100
111
|
time_range = days_to_time_range(days_passed)
|
101
112
|
days = DAYS_PER[time_range]
|
102
|
-
num_elems = days_passed / days
|
113
|
+
num_elems = (days_passed / days).to_i
|
114
|
+
|
103
115
|
range_type = if num_elems == 1
|
104
116
|
t(time_range[0...-1]) # singularize key
|
105
117
|
else
|
@@ -107,18 +119,18 @@ module Jekyll
|
|
107
119
|
end
|
108
120
|
|
109
121
|
current_slots << "#{num_elems} #{range_type}"
|
110
|
-
pending_days = days_passed - (num_elems*days)
|
122
|
+
pending_days = days_passed - (num_elems * days)
|
111
123
|
build_time_ago_slots(pending_days, depth - 1, current_slots)
|
112
124
|
end
|
113
125
|
|
114
126
|
# Number of days to minimum period time which can be grouped
|
115
127
|
def days_to_time_range(days_passed)
|
116
128
|
case days_passed.abs
|
117
|
-
when
|
129
|
+
when 1..6
|
118
130
|
:days
|
119
|
-
when 7
|
131
|
+
when 7..30
|
120
132
|
:weeks
|
121
|
-
when 31
|
133
|
+
when 31..365
|
122
134
|
:months
|
123
135
|
else
|
124
136
|
:years
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Jekyll::Timeago do
|
4
|
+
context 'Jekyll integration' do
|
5
|
+
let(:site) do
|
6
|
+
Jekyll::Site.new(site_configuration)
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'setup from Jekyll configuration' do
|
10
|
+
expect(site.config['jekyll_timeago']).to eql(configuration_file['jekyll_timeago'])
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'process successfully the site using filters and tags' do
|
14
|
+
expect { site.process }.to_not raise_error
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'Timeago calculations' do
|
19
|
+
let (:sample_date) { Date.new(2014, 7, 30) }
|
20
|
+
|
21
|
+
before(:each) do
|
22
|
+
Jekyll::Timeago::Filter.reset!
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'does not accept invalid depth' do
|
26
|
+
options[:depth] = 5
|
27
|
+
|
28
|
+
expect { timeago(today) }.to raise_error
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'yesterday, today and tomorrow' do
|
32
|
+
today = Date.today
|
33
|
+
|
34
|
+
expect(timeago(today - 1.day)).to eql(options[:yesterday])
|
35
|
+
expect(timeago(today)).to eql(options[:today])
|
36
|
+
expect(timeago(today + 1.day)).to eql(options[:tomorrow])
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'past time' do
|
40
|
+
it 'should process distances' do
|
41
|
+
expect(timeago(sample_date - 10.days, sample_date)).to eql('1 week and 3 days ago')
|
42
|
+
expect(timeago(sample_date - 100.days, sample_date)).to eql('3 months and 1 week ago')
|
43
|
+
expect(timeago(sample_date - 500.days, sample_date)).to eql('1 year and 4 months ago')
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'future time' do
|
48
|
+
it 'should process distances' do
|
49
|
+
expect(timeago(sample_date + 7.days, sample_date)).to eql('in 1 week')
|
50
|
+
expect(timeago(sample_date + 1000.days, sample_date)).to eql('in 2 years and 9 months')
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'allow different date inputs' do
|
55
|
+
expect(timeago('2010-1-1', '2012-1-1')).to eql('2 years ago')
|
56
|
+
expect(timeago('2010/1/1', '2012/1/1')).to eql('2 years ago')
|
57
|
+
expect(timeago('Jan 2010, 1', 'Jan 2012, 1')).to eql('2 years ago')
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'allow to change level of detail' do
|
61
|
+
options[:depth] = 1
|
62
|
+
expect(timeago(sample_date - 500.days, sample_date)).to eql('1 year ago')
|
63
|
+
|
64
|
+
options[:depth] = 3
|
65
|
+
expect(timeago(sample_date - 500.days, sample_date)).to eql('1 year, 4 months and 2 weeks ago')
|
66
|
+
|
67
|
+
options[:depth] = 4
|
68
|
+
expect(timeago(sample_date - 500.days, sample_date)).to eql('1 year, 4 months, 2 weeks and 1 day ago')
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'allow localization' do
|
72
|
+
options[:prefix] = 'hace'
|
73
|
+
options[:months] = 'meses'
|
74
|
+
options[:and] = 'y'
|
75
|
+
options[:week] = 'semana'
|
76
|
+
options[:suffix] = nil
|
77
|
+
|
78
|
+
expect(timeago(sample_date - 100.days, sample_date)).to eql('hace 3 meses y 1 semana')
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
gems:
|
2
|
+
- jekyll-timeago
|
3
|
+
|
4
|
+
jekyll_timeago:
|
5
|
+
depth: 3
|
6
|
+
today: 'today'
|
7
|
+
yesterday: 'yesterday'
|
8
|
+
tomorrow: 'tomorrow'
|
9
|
+
and: 'and'
|
10
|
+
suffix: 'ago'
|
11
|
+
prefix: ''
|
12
|
+
suffix_future: 'in'
|
13
|
+
prefix_future: ''
|
14
|
+
years: 'years'
|
15
|
+
year: 'year'
|
16
|
+
months: 'months'
|
17
|
+
month: 'month'
|
18
|
+
weeks: 'weeks'
|
19
|
+
week: 'week'
|
20
|
+
days: 'days'
|
21
|
+
day: 'day'
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'active_support'
|
3
|
+
require 'active_support/core_ext'
|
4
|
+
require 'jekyll'
|
5
|
+
require File.expand_path('lib/jekyll-timeago/filter')
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.run_all_when_everything_filtered = true
|
9
|
+
config.filter_run :focus
|
10
|
+
config.order = 'random'
|
11
|
+
|
12
|
+
SOURCE_DIR = File.expand_path('../source', __FILE__)
|
13
|
+
DEST_DIR = File.expand_path('../_site', __FILE__)
|
14
|
+
FileUtils.rm_rf(DEST_DIR)
|
15
|
+
FileUtils.mkdir_p(DEST_DIR)
|
16
|
+
|
17
|
+
def source_dir(*files)
|
18
|
+
File.join(SOURCE_DIR, *files)
|
19
|
+
end
|
20
|
+
|
21
|
+
def dest_dir(*files)
|
22
|
+
File.join(DEST_DIR, *files)
|
23
|
+
end
|
24
|
+
|
25
|
+
def configuration_file
|
26
|
+
YAML.load_file(File.join(SOURCE_DIR, '_config.yml'))
|
27
|
+
end
|
28
|
+
|
29
|
+
def site_configuration(overrides = {})
|
30
|
+
Jekyll.configuration(overrides.merge({
|
31
|
+
'source' => source_dir,
|
32
|
+
'destination' => dest_dir
|
33
|
+
}))
|
34
|
+
end
|
35
|
+
|
36
|
+
def timeago(from, to = Date.today)
|
37
|
+
Jekyll::Timeago::Filter.timeago(from, to)
|
38
|
+
end
|
39
|
+
|
40
|
+
def options
|
41
|
+
@options ||= Jekyll::Timeago::Filter.options
|
42
|
+
end
|
43
|
+
end
|
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.8.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,24 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-07-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: jekyll
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.5'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.5'
|
14
30
|
- !ruby/object:Gem::Dependency
|
15
31
|
name: bundler
|
16
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -43,8 +59,40 @@ dependencies:
|
|
43
59
|
- - ! '>='
|
44
60
|
- !ruby/object:Gem::Version
|
45
61
|
version: '0'
|
46
|
-
|
47
|
-
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: activesupport
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: Custom timeago filter for Jekyll (Liquid Filter and Tag). Localization
|
95
|
+
and futures supported.
|
48
96
|
email:
|
49
97
|
- srmarc.ai@gmail.com
|
50
98
|
executables: []
|
@@ -52,6 +100,7 @@ extensions: []
|
|
52
100
|
extra_rdoc_files: []
|
53
101
|
files:
|
54
102
|
- .gitignore
|
103
|
+
- .travis.yml
|
55
104
|
- Gemfile
|
56
105
|
- LICENSE
|
57
106
|
- README.md
|
@@ -63,6 +112,10 @@ files:
|
|
63
112
|
- lib/jekyll-timeago/tag.rb
|
64
113
|
- lib/jekyll-timeago/version.rb
|
65
114
|
- script/console
|
115
|
+
- spec/jekyll_timeago_spec.rb
|
116
|
+
- spec/source/_config.yml
|
117
|
+
- spec/source/index.html
|
118
|
+
- spec/spec_helper.rb
|
66
119
|
homepage: https://github.com/markets/jekyll-timeago
|
67
120
|
licenses:
|
68
121
|
- MIT
|
@@ -76,17 +129,27 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
76
129
|
- - ! '>='
|
77
130
|
- !ruby/object:Gem::Version
|
78
131
|
version: '0'
|
132
|
+
segments:
|
133
|
+
- 0
|
134
|
+
hash: 3767954602793850700
|
79
135
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
136
|
none: false
|
81
137
|
requirements:
|
82
138
|
- - ! '>='
|
83
139
|
- !ruby/object:Gem::Version
|
84
140
|
version: '0'
|
141
|
+
segments:
|
142
|
+
- 0
|
143
|
+
hash: 3767954602793850700
|
85
144
|
requirements: []
|
86
145
|
rubyforge_project:
|
87
146
|
rubygems_version: 1.8.23
|
88
147
|
signing_key:
|
89
148
|
specification_version: 3
|
90
|
-
summary: Custom timeago filter for Jekyll (Liquid
|
91
|
-
supported.
|
92
|
-
test_files:
|
149
|
+
summary: Custom timeago filter for Jekyll (Liquid Filter and Tag). Localization and
|
150
|
+
futures supported.
|
151
|
+
test_files:
|
152
|
+
- spec/jekyll_timeago_spec.rb
|
153
|
+
- spec/source/_config.yml
|
154
|
+
- spec/source/index.html
|
155
|
+
- spec/spec_helper.rb
|