jekyll-timeago 0.9.2 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 83655f4748686c8e8eacc7a5ca7334f5a8bc0ada
4
- data.tar.gz: 634b32d997525a8f06d2dd1f7fc73476b74b7608
3
+ metadata.gz: 19ad06f15553f22b78e77f182ba857181dbc2eb8
4
+ data.tar.gz: 2aa9841812c6d4d0f731bbb8ef15de6904b45454
5
5
  SHA512:
6
- metadata.gz: 662a10474c372620681c94591b10f04fa6e0d7b80a600a627c81ebca387b8d8665e300979fafe172ec3b61ab7e97929e579444cf279ac71db768cc2fd9486ae4
7
- data.tar.gz: 33790183dd1a4c81b13d97a2041044cb72d5d8d22fc7e86b678c353c3c20256d867c16f3e1e74c349b9164af132e4b746843dafdfc90178aa3ea2def1a114e56
6
+ metadata.gz: 9a07686e7c7e5085071122186ec7f88b0b687de47593edfe5655a84c73b5b86ec4a02f7e82bae56f9ee4d8bd61b9d215bdd49d2fd60e6dfc2a65b6f13ee03b65
7
+ data.tar.gz: 97125bca25ce2e56f0de941aa9daa19a236eb8d93b90cc24ba0afda6dee1b23e4d54848c09ce782d4cb882be8ae2fd9fb80fe3dc775dcf0143bcd58845c03814
data/.gitignore CHANGED
@@ -3,4 +3,5 @@ pkg
3
3
  Gemfile.lock
4
4
  spec/_site
5
5
  .rpsec
6
- *.gemfile.lock
6
+ *.gemfile.lock
7
+ .byebug_history
data/.travis.yml CHANGED
@@ -5,22 +5,29 @@ cache: bundler
5
5
  sudo: false
6
6
 
7
7
  rvm:
8
- - 2.4.0
9
- - 2.3.3
10
- - 2.2
11
- - 2.1
12
- - 2.0
8
+ - ruby-head
9
+ - 2.5.1
10
+ - 2.4.4
11
+ - 2.3.7
13
12
 
14
13
  gemfile:
15
- - gemfiles/jekyll_v1.gemfile
16
- - gemfiles/jekyll_v2.gemfile
17
14
  - gemfiles/jekyll_v3.gemfile
15
+ - gemfiles/jekyll_v2.gemfile
16
+ - gemfiles/jekyll_v1.gemfile
18
17
 
19
18
  matrix:
20
19
  exclude:
21
- - rvm: 1.9.3
22
- gemfile: gemfiles/jekyll_v3.gemfile
23
- - rvm: 2.4.0
20
+ - rvm: ruby-head
21
+ gemfile: gemfiles/jekyll_v2.gemfile
22
+ - rvm: ruby-head
23
+ gemfile: gemfiles/jekyll_v1.gemfile
24
+ - rvm: 2.5.1
25
+ gemfile: gemfiles/jekyll_v2.gemfile
26
+ - rvm: 2.5.1
24
27
  gemfile: gemfiles/jekyll_v1.gemfile
25
- - rvm: 2.4.0
28
+ - rvm: 2.4.4
26
29
  gemfile: gemfiles/jekyll_v2.gemfile
30
+ - rvm: 2.4.4
31
+ gemfile: gemfiles/jekyll_v1.gemfile
32
+ allow_failures:
33
+ - rvm: ruby-head
data/Appraisals CHANGED
@@ -4,10 +4,8 @@ end
4
4
 
5
5
  appraise 'jekyll-v2' do
6
6
  gem 'jekyll', '~> 2.5'
7
- gem 'listen', '< 3.1.0' if RUBY_VERSION <= '2.2'
8
7
  end
9
8
 
10
9
  appraise 'jekyll-v3' do
11
- gem 'jekyll', '~> 3.1'
12
- gem 'listen', '< 3.1.0' if RUBY_VERSION <= '2.2'
10
+ gem 'jekyll', '~> 3.8'
13
11
  end
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2013-2017 Marc Anguera Insa
1
+ Copyright 2013-2018 Marc Anguera Insa
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,186 +1,206 @@
1
1
  # Jekyll-Timeago
2
2
 
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)
3
+ [![Gem Version](https://badge.fury.io/rb/jekyll-timeago.svg)](http://badge.fury.io/rb/jekyll-timeago)
4
+ [![Build Status](https://travis-ci.org/markets/jekyll-timeago.svg?branch=master)](https://travis-ci.org/markets/jekyll-timeago)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/a8be458ba0532c2d057d/maintainability)](https://codeclimate.com/github/markets/jekyll-timeago/maintainability)
4
6
 
5
- > A Ruby library to compute distance of dates in words. Originally built for Jekyll, as a Liquid extension.
7
+ > A Ruby library to compute distance of dates in words, with localization support. Originally built for Jekyll.
6
8
 
7
9
  Main features:
8
10
 
9
- * Compute distance of dates in words, ie: `1 week and 2 days ago`, `5 months ago`, `in 1 year`
10
- * Future times.
11
- * Out of the box support for `Jekyll` (v1, v2 and v3) projects, available as a Liquid Filter and as a Liquid Tag.
12
- * Localization (i18n).
13
- * Level of detail.
14
- * Available via the command line.
11
+ * Compute distance of dates, in words, ie: `1 week and 2 days ago`, `5 months ago`, `in 1 year`
12
+ * Future times
13
+ * Out of the box support for `Jekyll` (`v1`, `v2` and `v3`) projects, available as a Liquid Filter and as a Liquid Tag
14
+ * Localization (i18n)
15
+ * Level of detail
16
+ * Command line utility
15
17
 
16
- In fact, `jekyll-timeago` started just as an extension for [Liquid](https://github.com/Shopify/liquid) template engine, to be used in Jekyll and Octopress backed sites. But actually, you can use it easily in any Ruby project. Read more about usage outside Jekyll [in this section](#usage-outside-jekyll).
18
+ In fact, `jekyll-timeago` started as an extension for [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.
19
+
20
+ Read more about the Jekyll integration [in this section](#jekyll-integration).
17
21
 
18
22
  ## Installation
19
23
 
20
- You have different options to install and plugging it into Jekyll projects:
24
+ Add this line to your application's Gemfile:
21
25
 
22
- **Via Jekyll plugin system (recommended)**
26
+ ```ruby
27
+ gem 'jekyll-timeago'
28
+ ```
23
29
 
24
- Install the `gem` to your system:
30
+ And then execute:
25
31
 
26
- ```
27
- gem install jekyll-timeago
28
- ```
32
+ > bundle install
29
33
 
30
- In your `_config.yml` file, add a new array with the key gems and the values of the gem names of the plugins you’d like to use. In this case:
34
+ Or install it yourself as:
31
35
 
32
- ```
33
- gems:
34
- - jekyll-timeago
35
- ```
36
+ > gem install jekyll-timeago
36
37
 
37
- **Via Bundler**
38
+ ## Usage
38
39
 
39
- Add this gem to your `Gemfile` and run `bundle install`:
40
+ The gem provides the `#timeago` method:
40
41
 
41
42
  ```ruby
42
- group :jekyll_plugins do
43
- gem 'jekyll-timeago'
44
- end
43
+ Jekyll::Timeago.timeago(from, to = Date.today, options = {})
45
44
  ```
46
45
 
47
- **Manually (less recommended)**
48
-
49
- Alternatively, you can simply copy the files under [lib/jekyll-timeago](lib/jekyll-timeago/) directly into your `_plugins/` directory. All those files will be loaded by Jekyll.
46
+ Examples:
50
47
 
51
- ## Usage
48
+ ```ruby
49
+ >> Jekyll::Timeago.timeago(Date.today)
50
+ => "today"
51
+ >> Jekyll::Timeago.timeago(Date.today.prev_day)
52
+ => "yesterday"
53
+ >> Jekyll::Timeago.timeago(Date.today.prev_day(10))
54
+ => "1 week and 3 days ago"
55
+ >> Jekyll::Timeago.timeago(Date.today.prev_day(100))
56
+ => "3 months and 1 week ago"
57
+ >> Jekyll::Timeago.timeago(Date.today.prev_day(500))
58
+ => "1 year and 4 months ago"
59
+ >> Jekyll::Timeago.timeago('2010-1-1', '2012-1-1')
60
+ => "2 years ago"
61
+ >> Jekyll::Timeago.timeago(Date.today.next_day)
62
+ => "tomorrow"
63
+ >> Jekyll::Timeago.timeago(Date.today.next_day(7))
64
+ => "in 1 week"
65
+ >> Jekyll::Timeago.timeago(Date.today.next_day(1000))
66
+ => "in 2 years and 8 months"
67
+ ```
52
68
 
53
- By default, the `timeago` helper computes distance of dates from passed date to current date (using `Date.today`). But you are able to modify this range by passing a second argument. Examples:
69
+ **NOTE** If you have the gem installed in your system, and you're not using Bundler (probably because you're are writing a basic script), don't forget to require the library first:
54
70
 
55
- **Filter example**:
71
+ ```ruby
72
+ require 'jekyll-timeago'
56
73
 
57
- ```html
58
- <p>{{ page.date | timeago }}</p>
74
+ puts Jekyll::Timeago.timeago('2030-1-1')
59
75
  ```
60
76
 
61
- Passing a parameter:
77
+ ### Options
62
78
 
63
- ```html
64
- <p>{{ page.date | timeago: '2020-1-1' }}</p>
65
- ```
79
+ * `locale`
66
80
 
67
- **Tag example**:
81
+ To use a different language:
68
82
 
69
- ```html
70
- <p>{% timeago 2000-1-1 %}</p>
83
+ ```ruby
84
+ >> Jekyll::Timeago.timeago(Date.today.prev_day(200), locale: :es)
85
+ => "hace 6 meses y 2 semanas"
86
+ >> Jekyll::Timeago.timeago(Date.today.prev_day(200), locale: :fr)
87
+ => "il y a environ 6 mois et 2 semaines"
71
88
  ```
72
89
 
73
- Passing a second parameter:
90
+ Read more about the localization options [here](#i18n).
74
91
 
75
- ```html
76
- <p>{% timeago 2000-1-1 2010-1-1 %}</p>
92
+ * `depth`
93
+
94
+ You are able to change the level of detail (from 1 up to 4, 2 by default) to get higher or lower granularity:
95
+
96
+ ```ruby
97
+ >> Jekyll::Timeago.timeago(Date.today.prev_day(2000), depth: 3)
98
+ => "5 years, 5 months and 3 weeks ago"
99
+ >> Jekyll::Timeago.timeago(Date.today.prev_day(2000), depth: 4)
100
+ => "5 years, 5 months, 3 weeks and 4 days ago"
77
101
  ```
78
102
 
79
- ## Localization
103
+ ## I18n
80
104
 
81
- This plugin allows you to localize the strings needed to build the sentences. To do this, you just need to add some extra keys in your `_config.yml`. You can simply copy them from one of the [provided examples](lib/jekyll-timeago/config/). Or even, translate it to your site's language just overriding it.
105
+ 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.
82
106
 
83
- English example (default):
107
+ This project uses the [mini_i18n](https://github.com/markets/mini_i18n) gem under the hood to deal with translations. You can read further about all options in [its docs](https://github.com/markets/mini_i18n#usage). Example:
84
108
 
109
+ ```ruby
110
+ MiniI18n.configure do |config|
111
+ config.load_translations('/path_to_your_translations_files/*.yml')
112
+ config.default_locale = :en
113
+ end
85
114
  ```
86
- jekyll_timeago:
87
- depth: 2 # Level of detail
88
- today: 'today'
89
- yesterday: 'yesterday'
90
- tomorrow: 'tomorrow'
91
- and: 'and'
92
- suffix: 'ago'
93
- prefix: ''
94
- suffix_future: ''
95
- prefix_future: 'in'
96
- years: 'years'
97
- year: 'year'
98
- months: 'months'
99
- month: 'month'
100
- weeks: 'weeks'
101
- week: 'week'
102
- days: 'days'
103
- day: 'day'
115
+
116
+ If you want to contribute and support more languages by default, please feel free to send a pull request.
117
+
118
+ ## CLI
119
+
120
+ ```
121
+ > jekyll-timeago 2016-1-1
122
+ 2 years and 6 months ago
123
+ > jekyll-timeago 2016-1-1 --locale fr
124
+ il y a environ 2 années et 6 mois
104
125
  ```
105
126
 
106
- **NOTE** You also can play with suffixes and prefixes to modify the sentences. For example, set `suffix: nil` and you'll get only the distance of dates: `1 year, 4 months and 1 week`.
127
+ ### Console
107
128
 
108
- ## Level of detail (Depth)
129
+ Starts a custom IRB session with the `#timeago` method included:
109
130
 
110
- 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:
131
+ ```
132
+ > jekyll-timeago --console
133
+ >> timeago(Date.today)
134
+ => "today"
135
+ ```
136
+
137
+ ## Jekyll integration
111
138
 
112
- * Depth => 1 `1 year ago`
113
- * Depth => 2 `1 year and 4 months ago` (default)
114
- * Depth => 3 `1 year, 4 months and 1 week ago`
115
- * Depth => 4 `1 year, 4 months, 1 week and 4 days ago`
139
+ You have different options to install and use `jekyll-timeago` into your Jekyll project:
116
140
 
117
- ## Usage outside Jekyll
141
+ **Via Jekyll plugin system (recommended)**
118
142
 
119
- You just need to install the gem to your application (add `gem 'jekyll-timeago'` to your Gemfile). From now on, you can use the provided method by calling:
143
+ Install the `gem` to your system:
120
144
 
121
- ```ruby
122
- Jekyll::Timeago::Core.timeago(from, to, options)
145
+ ```
146
+ > gem install jekyll-timeago
123
147
  ```
124
148
 
125
- Note, that you can use the `options` parameter (must be a `Hash` and be passed as the last parameter) to override the default localization or the level of detail, e.g.:
149
+ In your `_config.yml` file, add a new array with the key gems and the values of the gem names of the plugins you’d like to use. In this case:
126
150
 
127
- ```ruby
128
- options = {
129
- "depth" => 3,
130
- "prefix" => "hace",
131
- "months" => "meses",
132
- "and" => "y",
133
- "week" => "semana",
134
- "suffix" => nil
135
- }
151
+ ```yaml
152
+ gems:
153
+ - jekyll-timeago
136
154
  ```
137
155
 
138
- Or if you have the gem installed in your system and you're not using Bundler:
156
+ **Via Bundler**
157
+
158
+ Add this gem to your `Gemfile` and run `bundle install`:
139
159
 
140
160
  ```ruby
141
- require 'jekyll-timeago'
142
- puts Jekyll::Timeago::Core.timeago(from, to, options)
161
+ group :jekyll_plugins do
162
+ gem 'jekyll-timeago'
163
+ end
143
164
  ```
144
165
 
145
- ## CLI
166
+ ### Usage
167
+
168
+ **Liquid Filter**:
146
169
 
170
+ ```html
171
+ <p>{{ page.date | timeago }}</p>
172
+ <p>{{ page.date | timeago: '2020-1-1' }}</p>
147
173
  ```
148
- $ jekyll-timeago 2016-1-1
149
- 2 months and 6 days ago
174
+
175
+ **Liquid Tag**:
176
+
177
+ ```html
178
+ <p>{% timeago 2000-1-1 %}</p>
179
+ <p>{% timeago 2000-1-1 2010-1-1 %}</p>
150
180
  ```
151
181
 
152
- ### Console
182
+ ### Configuration
153
183
 
154
- Run `$ jekyll-timeago --console` to start a custom IRB session and play with the `timeago` method:
184
+ In your `_config.yml` file:
155
185
 
156
- ```ruby
157
- >> timeago(Date.today)
158
- => "today"
159
- >> timeago(Date.today.prev_day)
160
- => "yesterday"
161
- >> timeago(Date.today.prev_day(10))
162
- => "1 week and 3 days ago"
163
- >> timeago(Date.today.prev_day(100))
164
- => "3 months and 1 week ago"
165
- >> timeago(Date.today.prev_day(500))
166
- => "1 year and 4 months ago"
167
- >> timeago('2010-1-1', '2012-1-1')
168
- => "2 years ago"
169
- >> timeago(Date.today.next_day)
170
- => "tomorrow"
171
- >> timeago(Date.today.next_day(7))
172
- => "in 1 week"
173
- >> timeago(Date.today.next_day(1000))
174
- => "in 2 years and 8 months"
186
+ ```
187
+ jekyll_timeago:
188
+ depth: 2
189
+ translations_path: '/path_to_your_translations/*.yaml'
190
+ default_locale: 'en'
191
+ fallbacks: true
192
+ available_locales:
193
+ - 'en'
194
+ - 'es'
195
+ - 'fr'
175
196
  ```
176
197
 
177
- You can modify globally, via the `configure` method, all the available options:
198
+ Also, you can set a different language per page using the [Front Matter](https://jekyllrb.com/docs/frontmatter/) functionality:
178
199
 
179
- ```ruby
180
- >> configure({ "yesterday" => "ayer" })
181
- => "ayer"
182
- >> timeago(Date.today.prev_day)
183
- => "ayer"
200
+ ```yaml
201
+ ---
202
+ locale: 'es'
203
+ ---
184
204
  ```
185
205
 
186
206
  ## Development
@@ -190,10 +210,10 @@ Any kind of feedback, bug report, idea or enhancement are really appreciated.
190
210
  To contribute, just fork the repo, hack on it and send a pull request. Don't forget to add specs for behaviour changes and run the test suite:
191
211
 
192
212
  ```
193
- bundle exec appraisal rake
213
+ > bundle exec appraisal rake
194
214
  ```
195
215
 
196
- `Appraisal` library is used to ensure compatibility with different Jekyll versions. Check out current supported versions [here](Appraisals).
216
+ We use the `Appraisal` gem to ensure compatibility with different Jekyll versions. Check out current supported versions [here](Appraisals).
197
217
 
198
218
  ## License
199
219
 
data/bin/jekyll-timeago CHANGED
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require_relative "../lib/jekyll-timeago/core"
4
- include Jekyll::Timeago::Core
3
+ require "bundler/setup"
4
+ require_relative "../lib/jekyll-timeago"
5
+ include Jekyll::Timeago
5
6
 
6
7
  help_message = <<END
7
8
  Usage:
@@ -12,16 +13,36 @@ Notes:
12
13
  [to_date] Optional, defaults to current date
13
14
 
14
15
  Options:
15
- --help, -h Print this message
16
- --console Start an interactive session with jekyll-timeago included
16
+ --help, -h Prints this message
17
+ --version, -v Prints the current version
18
+ --console, -c Starts an interactive IRB session with jekyll-timeago included
19
+ --locale, -l Uses the provided locale
17
20
  END
18
21
 
19
22
  if ARGV.empty? || ARGV.include?("--help") || ARGV.include?("-h")
20
23
  puts help_message
21
- elsif ARGV.include? "--console"
24
+ elsif ARGV.include?("--version") || ARGV.include?("-v")
25
+ puts "v#{Jekyll::Timeago::VERSION}"
26
+ elsif ARGV.include?("--console") || ARGV.include?("-c")
22
27
  require "irb"
23
28
  ARGV.clear
24
29
  IRB.start
25
30
  else
26
- puts timeago *ARGV
31
+ custom_locale = "--locale" if ARGV.include?("--locale")
32
+ custom_locale = "-l" if ARGV.include?("-l")
33
+
34
+ if custom_locale
35
+ index = ARGV.index(custom_locale)
36
+ locale = ARGV.delete_at(index + 1)
37
+ ARGV.delete_at(index)
38
+
39
+ options = { locale: locale }
40
+ ARGV << options
41
+ end
42
+
43
+ begin
44
+ puts timeago *ARGV
45
+ rescue ArgumentError => e
46
+ puts "ERROR! #{e}"
47
+ end
27
48
  end
@@ -3,6 +3,5 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "jekyll", "~> 2.5"
6
- gem "listen", "< 3.1.0"
7
6
 
8
7
  gemspec :path => "../"
@@ -2,7 +2,6 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "jekyll", "~> 3.1"
6
- gem "listen", "< 3.1.0"
5
+ gem "jekyll", "~> 3.8"
7
6
 
8
7
  gemspec :path => "../"
@@ -15,9 +15,12 @@ Gem::Specification.new do |spec|
15
15
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
16
  spec.require_paths = ["lib"]
17
17
 
18
+ spec.add_dependency "mini_i18n", '~> 0.3.1'
19
+
18
20
  spec.add_development_dependency "jekyll", ">= 1.5"
19
21
  spec.add_development_dependency "bundler", "~> 1.3"
20
22
  spec.add_development_dependency "rake"
21
23
  spec.add_development_dependency "rspec"
22
24
  spec.add_development_dependency "appraisal"
25
+ spec.add_development_dependency "byebug"
23
26
  end
@@ -1,10 +1,17 @@
1
- require 'jekyll-timeago/core'
2
- require 'jekyll-timeago/version'
1
+ require 'mini_i18n'
2
+ require_relative 'jekyll-timeago/version'
3
+ require_relative 'jekyll-timeago/core'
3
4
 
4
- if defined?(Liquid)
5
- require 'jekyll-timeago/filter'
6
- require 'jekyll-timeago/tag'
5
+ if defined?(Jekyll) && defined?(Liquid)
6
+ require_relative 'jekyll-timeago/jekyll_integration'
7
+ end
7
8
 
8
- Liquid::Template.register_filter(Jekyll::Timeago::Filter)
9
- Liquid::Template.register_tag('timeago', Jekyll::Timeago::Tag)
9
+ module Jekyll
10
+ module Timeago
11
+ include Jekyll::Timeago::Core
12
+ end
13
+ end
14
+
15
+ MiniI18n.configure do |config|
16
+ config.load_translations(__dir__ + '/locales/*.yml')
10
17
  end
@@ -27,57 +27,28 @@ module Jekyll
27
27
  to = Date.today
28
28
  end
29
29
 
30
- @defaults = defaults unless defined?(@defaults)
31
- @options = @defaults.merge(options)
30
+ @options = options
32
31
 
33
- from = validate_date!(from)
34
- to = validate_date!(to)
35
- depth = validate_depth!(@options["depth"])
32
+ from = validate_date(from)
33
+ to = validate_date(to)
34
+ depth = validate_depth(@options[:depth] || @options["depth"])
36
35
 
37
36
  time_ago_to_now(from, to, depth)
38
37
  end
39
38
 
40
- def configure(options = {})
41
- @defaults = defaults.merge(options)
42
- end
43
-
44
39
  private
45
40
 
46
- def validate_date!(date)
41
+ def validate_date(date)
47
42
  Date.parse(date.to_s)
48
43
  end
49
44
 
50
- def validate_depth!(depth)
51
- (1..MAX_DEPTH_LEVEL).include?(depth) or raise(ArgumentError, "Invalid depth level: #{depth.inspect}")
52
- depth
53
- end
54
-
55
- def defaults
56
- {
57
- "depth" => DEFAULT_DEPTH_LEVEL,
58
- "today" => 'today',
59
- "yesterday" => 'yesterday',
60
- "tomorrow" => 'tomorrow',
61
- "and" => 'and',
62
- "suffix" => 'ago',
63
- "prefix" => '',
64
- "suffix_future" => '',
65
- "prefix_future" => 'in',
66
- "years" => 'years',
67
- "year" => 'year',
68
- "months" => 'months',
69
- "month" => 'month',
70
- "weeks" => 'weeks',
71
- "week" => 'week',
72
- "days" => 'days',
73
- "day" => 'day'
74
- }
45
+ def validate_depth(depth)
46
+ (1..MAX_DEPTH_LEVEL).include?(depth) ? depth : DEFAULT_DEPTH_LEVEL
75
47
  end
76
48
 
77
- def translate(key)
78
- @options[key.to_s]
49
+ def t(key)
50
+ MiniI18n.translate(key, @options)
79
51
  end
80
- alias_method :t, :translate
81
52
 
82
53
  # Days passed to time ago sentence
83
54
  def time_ago_to_now(from, to, depth)
@@ -0,0 +1,59 @@
1
+ module Jekyll
2
+ module Timeago
3
+ @@jekyll_initialized = nil
4
+
5
+ def self.jekyll_config(context)
6
+ @@jekyll_config ||= context.registers[:site].config['jekyll_timeago'] || {}
7
+ end
8
+
9
+ def self.jekyll_page_data(context)
10
+ options = jekyll_config(context)
11
+ locale = context['page']['locale']
12
+
13
+ options[:locale] = locale if locale
14
+
15
+ if !@@jekyll_initialized
16
+ MiniI18n.configure do |config|
17
+ if options['translations_path']
18
+ path = context.registers[:site].source + options['translations_path']
19
+ config.load_translations(path)
20
+ end
21
+
22
+ config.available_locales = options['available_locales']
23
+ config.default_locale = options['default_locale']
24
+ config.fallbacks = options['fallbacks']
25
+ end
26
+
27
+ @@jekyll_initialized = true
28
+ end
29
+
30
+ options
31
+ end
32
+
33
+ module Filter
34
+ def timeago(from, to = Date.today)
35
+ options = Jekyll::Timeago.jekyll_page_data(@context)
36
+
37
+ Core.timeago(from, to, options)
38
+ end
39
+ end
40
+
41
+ class Tag < Liquid::Tag
42
+ def initialize(tag_name, dates, tokens)
43
+ super
44
+ @dates = dates.strip.split(' ')
45
+ end
46
+
47
+ def render(context)
48
+ options = Jekyll::Timeago.jekyll_page_data(context)
49
+
50
+ from, to = @dates[0], @dates[1]
51
+ to = options if to.nil?
52
+ Core.timeago(from, to, options)
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ Liquid::Template.register_filter(Jekyll::Timeago::Filter)
59
+ Liquid::Template.register_tag('timeago', Jekyll::Timeago::Tag)
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
2
  module Timeago
3
- VERSION = "0.9.2"
3
+ VERSION = "0.10.0"
4
4
  end
5
5
  end
@@ -1,5 +1,4 @@
1
- jekyll_timeago:
2
- depth: 2 # Level of detail
1
+ en:
3
2
  today: 'today'
4
3
  yesterday: 'yesterday'
5
4
  tomorrow: 'tomorrow'
@@ -1,5 +1,4 @@
1
- jekyll_timeago:
2
- depth: 2 # Nivel de detalle
1
+ es:
3
2
  today: 'hoy'
4
3
  yesterday: 'ayer'
5
4
  tomorrow: 'mañana'
@@ -1,5 +1,4 @@
1
- jekyll_timeago:
2
- depth: 2 # niveau de détail
1
+ fr:
3
2
  today: "aujourd'hui"
4
3
  yesterday: "hier"
5
4
  tomorrow: "demain"
@@ -16,4 +15,3 @@ jekyll_timeago:
16
15
  week: "semaine"
17
16
  days: "jours"
18
17
  day: "jour"
19
-
@@ -0,0 +1,17 @@
1
+ pt:
2
+ today: 'hoje'
3
+ yesterday: 'ontem'
4
+ tomorrow: 'amanhã'
5
+ and: 'e'
6
+ suffix: 'atrás'
7
+ prefix: ''
8
+ suffix_future: ''
9
+ prefix_future: 'em'
10
+ years: 'anos'
11
+ year: 'ano'
12
+ months: 'meses'
13
+ month: 'mês'
14
+ weeks: 'semanas'
15
+ week: 'semanas'
16
+ days: 'dias'
17
+ day: 'dia'
@@ -14,20 +14,20 @@ describe Jekyll::Timeago do
14
14
  let(:contents) { File.read(dest_dir("index.html")) }
15
15
 
16
16
  it 'setup from Jekyll configuration' do
17
- expect(site.config['jekyll_timeago']).to eql(configuration_file['jekyll_timeago'])
17
+ expect(site.config['jekyll_timeago']).to eq(configuration_file['jekyll_timeago'])
18
18
  end
19
19
 
20
20
  it 'process successfully the site using filters and tags' do
21
21
  allow(Date).to receive(:today) { Date.new(2016, 1, 1) }
22
22
  expect { site.process }.to_not raise_error
23
23
 
24
- lines = [
25
- "<p>2 years yeah</p>",
26
- "<p>12 months yeah</p>",
27
- "<p>12 months yeah</p>",
28
- "<p>2 years yeah</p>"
29
- ]
30
- expect(contents).to eq(lines.join("\n"))
24
+ expected =
25
+ "<p>2 años</p>\n"\
26
+ "<p>12 meses</p>\n"\
27
+ "<p>12 meses</p>\n"\
28
+ "<p>2 años</p>"
29
+
30
+ expect(contents).to eq(expected)
31
31
  end
32
32
  end
33
33
 
@@ -35,64 +35,42 @@ describe Jekyll::Timeago do
35
35
  let (:sample_date) { Date.new(2014, 7, 30) }
36
36
  let (:today) { Date.today }
37
37
 
38
- before do
39
- Jekyll::Timeago::Core.configure
40
- end
41
-
42
- it 'does not accept invalid depth' do
43
- expect { timeago(today, sample_date, "depth" => 5) }.to raise_error(ArgumentError)
44
- end
45
-
46
- it 'accepts a hash (options) as a second parameter (implicit "to")' do
47
- expect(timeago(today, "today" => "during the day")).to eq("during the day")
48
- end
49
-
50
38
  it 'yesterday, today and tomorrow' do
51
- expect(timeago(today.prev_day)).to eql("yesterday")
52
- expect(timeago(today)).to eql("today")
53
- expect(timeago(today.next_day)).to eql("tomorrow")
39
+ expect(timeago(today.prev_day)).to eq("yesterday")
40
+ expect(timeago(today)).to eq("today")
41
+ expect(timeago(today.next_day)).to eq("tomorrow")
54
42
  end
55
43
 
56
44
  it 'past time' do
57
- expect(timeago(sample_date.prev_day(10), sample_date)).to eql('1 week and 3 days ago')
58
- expect(timeago(sample_date.prev_day(100), sample_date)).to eql('3 months and 1 week ago')
59
- expect(timeago(sample_date.prev_day(500), sample_date)).to eql('1 year and 4 months ago')
45
+ expect(timeago(sample_date.prev_day(10), sample_date)).to eq('1 week and 3 days ago')
46
+ expect(timeago(sample_date.prev_day(100), sample_date)).to eq('3 months and 1 week ago')
47
+ expect(timeago(sample_date.prev_day(500), sample_date)).to eq('1 year and 4 months ago')
60
48
  end
61
49
 
62
50
  it 'future time' do
63
- expect(timeago(sample_date.next_day(7), sample_date)).to eql('in 1 week')
64
- expect(timeago(sample_date.next_day(1000), sample_date)).to eql('in 2 years and 9 months')
51
+ expect(timeago(sample_date.next_day(7), sample_date)).to eq('in 1 week')
52
+ expect(timeago(sample_date.next_day(1000), sample_date)).to eq('in 2 years and 9 months')
65
53
  end
66
54
 
67
55
  it 'allow different date formats' do
68
- expect(timeago('2010-1-1', '2012-1-1')).to eql('2 years ago')
69
- expect(timeago('2010/1/1', '2012/1/1')).to eql('2 years ago')
70
- expect(timeago('Jan 2010, 1', 'Jan 2012, 1')).to eql('2 years ago')
71
- expect(timeago('20100101', '20120101')).to eql('2 years ago')
72
- expect(timeago('2014-10-06 20:00:00', '2014-10-07 20:00:00')).to eql('yesterday')
73
- end
74
-
75
- it 'allow to change defaults at global level' do
76
- Jekyll::Timeago::Core.configure("year" => nil, "depth" => 1, "suffix" => nil)
77
- expect(timeago(sample_date.prev_day(500), sample_date)).to eql('1')
56
+ expect(timeago('2010-1-1', '2012-1-1')).to eq('2 years ago')
57
+ expect(timeago('2010/1/1', '2012/1/1')).to eq('2 years ago')
58
+ expect(timeago('Jan 2010, 1', 'Jan 2012, 1')).to eq('2 years ago')
59
+ expect(timeago('20100101', '20120101')).to eq('2 years ago')
60
+ expect(timeago('2014-10-06 20:00:00', '2014-10-07 20:00:00')).to eq('yesterday')
78
61
  end
79
62
 
80
63
  it 'allow to change level of detail' do
81
- expect(timeago(sample_date.prev_day(500), sample_date, "depth" => 1)).to eql('1 year ago')
82
- expect(timeago(sample_date.prev_day(500), sample_date, "depth" => 3)).to eql('1 year, 4 months and 2 weeks ago')
83
- expect(timeago(sample_date.prev_day(500), sample_date, "depth" => 4)).to eql('1 year, 4 months, 2 weeks and 1 day ago')
64
+ expect(timeago(sample_date.prev_day(500), sample_date, depth: 1)).to eq('1 year ago')
65
+ expect(timeago(sample_date.prev_day(500), sample_date, "depth" => 1)).to eq('1 year ago')
66
+ expect(timeago(sample_date.prev_day(500), sample_date, depth: 2)).to eq('1 year and 4 months ago')
67
+ expect(timeago(sample_date.prev_day(500), sample_date, depth: 3)).to eq('1 year, 4 months and 2 weeks ago')
68
+ expect(timeago(sample_date.prev_day(500), sample_date, depth: 4)).to eq('1 year, 4 months, 2 weeks and 1 day ago')
69
+ expect(timeago(sample_date.prev_day(500), sample_date, depth: 5)).to eq('1 year and 4 months ago')
84
70
  end
85
71
 
86
72
  it 'allow localization' do
87
- new_options = {
88
- "prefix" => 'hace',
89
- "months" => 'meses',
90
- "and" => 'y',
91
- "week" => 'semana',
92
- "suffix" => nil
93
- }
94
-
95
- expect(timeago(sample_date.prev_day(100), sample_date, new_options)).to eql('hace 3 meses y 1 semana')
73
+ expect(timeago(sample_date.prev_day(100), sample_date, locale: :fr)).to eq('il y a environ 3 mois et 1 semaine')
96
74
  end
97
75
  end
98
76
 
@@ -102,8 +80,22 @@ describe Jekyll::Timeago do
102
80
  expect(`jekyll-timeago --help`).to match("Usage")
103
81
  end
104
82
 
83
+ it 'prints current version' do
84
+ expect(`jekyll-timeago -v`).to match("v#{Jekyll::Timeago::VERSION}")
85
+ expect(`jekyll-timeago --version`).to match("v#{Jekyll::Timeago::VERSION}")
86
+ end
87
+
105
88
  it 'computes distance of dates' do
106
89
  expect(`jekyll-timeago 2016-1-1 2016-1-5`).to match("4 days ago")
107
90
  end
91
+
92
+ it 'prints error with invalid date' do
93
+ expect(`jekyll-timeago 1`).to match("ERROR!")
94
+ end
95
+
96
+ it 'with custom locale' do
97
+ expect(`jekyll-timeago 2016-1-1 2016-1-5 -l fr`).to match("il y a environ 4 jours")
98
+ expect(`jekyll-timeago 2016-1-1 2016-1-5 --locale fr`).to match("il y a environ 4 jours")
99
+ end
108
100
  end
109
101
  end
@@ -3,19 +3,9 @@ gems:
3
3
 
4
4
  jekyll_timeago:
5
5
  depth: 1
6
- today: 'today'
7
- yesterday: 'yesterday'
8
- tomorrow: 'tomorrow'
9
- and: 'and'
10
- suffix: 'yeah'
11
- prefix: ''
12
- suffix_future: ''
13
- prefix_future: 'in'
14
- years: 'years'
15
- year: 'year'
16
- months: 'months'
17
- month: 'month'
18
- weeks: 'weeks'
19
- week: 'week'
20
- days: 'days'
21
- day: 'day'
6
+ translations_path: "/_locales/*.yaml"
7
+ default_locale: 'en'
8
+ available_locales:
9
+ - 'en'
10
+ - 'es'
11
+ - 'fr'
@@ -0,0 +1,2 @@
1
+ es:
2
+ prefix: ''
@@ -1,4 +1,5 @@
1
1
  ---
2
+ locale: 'es'
2
3
  ---
3
4
 
4
5
  <p>{% timeago 2014-1-1 %}</p>
data/spec/spec_helper.rb CHANGED
@@ -1,11 +1,9 @@
1
1
  require 'fileutils'
2
2
  require 'jekyll'
3
- require File.expand_path('lib/jekyll-timeago')
3
+ require 'jekyll-timeago'
4
4
 
5
5
  RSpec.configure do |config|
6
- config.run_all_when_everything_filtered = true
7
- config.filter_run :focus
8
- config.order = 'random'
6
+ config.order = :random
9
7
 
10
8
  SOURCE_DIR = File.expand_path('../source', __FILE__)
11
9
  DEST_DIR = File.expand_path('../_site', __FILE__)
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-timeago
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - markets
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-04 00:00:00.000000000 Z
11
+ date: 2018-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mini_i18n
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.3.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.3.1
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: jekyll
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +94,20 @@ dependencies:
80
94
  - - ">="
81
95
  - !ruby/object:Gem::Version
82
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
83
111
  description: A Ruby library to compute distance of dates in words. Originally built
84
112
  for Jekyll, as a Liquid extension. It also supports localization and futures.
85
113
  email:
@@ -102,15 +130,16 @@ files:
102
130
  - gemfiles/jekyll_v3.gemfile
103
131
  - jekyll-timeago.gemspec
104
132
  - lib/jekyll-timeago.rb
105
- - lib/jekyll-timeago/config/_config.yml.example.en
106
- - lib/jekyll-timeago/config/_config.yml.example.es
107
- - lib/jekyll-timeago/config/_config.yml.example.fr
108
133
  - lib/jekyll-timeago/core.rb
109
- - lib/jekyll-timeago/filter.rb
110
- - lib/jekyll-timeago/tag.rb
134
+ - lib/jekyll-timeago/jekyll_integration.rb
111
135
  - lib/jekyll-timeago/version.rb
136
+ - lib/locales/en.yml
137
+ - lib/locales/es.yml
138
+ - lib/locales/fr.yml
139
+ - lib/locales/pt.yml
112
140
  - spec/jekyll-timeago_spec.rb
113
141
  - spec/source/_config.yml
142
+ - spec/source/_locales/overrides.yaml
114
143
  - spec/source/index.html
115
144
  - spec/spec_helper.rb
116
145
  homepage: https://github.com/markets/jekyll-timeago
@@ -133,12 +162,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
162
  version: '0'
134
163
  requirements: []
135
164
  rubyforge_project:
136
- rubygems_version: 2.5.2
165
+ rubygems_version: 2.6.13
137
166
  signing_key:
138
167
  specification_version: 4
139
168
  summary: A date helper to compute distance of dates in words.
140
169
  test_files:
141
170
  - spec/jekyll-timeago_spec.rb
142
171
  - spec/source/_config.yml
172
+ - spec/source/_locales/overrides.yaml
143
173
  - spec/source/index.html
144
174
  - spec/spec_helper.rb
@@ -1,11 +0,0 @@
1
- module Jekyll
2
- module Timeago
3
- module Filter
4
- def timeago(from, to = Date.today)
5
- config = @context.registers[:site].config.fetch('jekyll_timeago', {})
6
-
7
- Jekyll::Timeago::Core.timeago(from, to, config)
8
- end
9
- end
10
- end
11
- end
@@ -1,21 +0,0 @@
1
- module Jekyll
2
- module Timeago
3
- class Tag < Liquid::Tag
4
- def initialize(tag_name, dates, tokens)
5
- super
6
- @dates = dates.strip.split(' ')
7
- end
8
-
9
- def render(context)
10
- from, to = @dates[0], @dates[1]
11
- config = context.registers[:site].config.fetch('jekyll_timeago', {})
12
-
13
- if to
14
- Jekyll::Timeago::Core.timeago(from, to, config)
15
- else
16
- Jekyll::Timeago::Core.timeago(from, config)
17
- end
18
- end
19
- end
20
- end
21
- end