jekyll-timeago 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,11 +1,29 @@
1
1
  Jekyll-Timeago
2
2
  ==============
3
- Custom and simple implementation of `timeago` date filter. Futures and personalization (level of detail) supported.
4
3
 
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.
4
+ Custom and simple implementation of `timeago` date filter. Main features:
5
+
6
+ * Localization
7
+ * Future time
8
+ * Level of detail
9
+
10
+ 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 (Octopress as well).
6
11
 
7
12
 
8
13
  ## Installation
14
+
15
+ You have 3 options for installing the plugin:
16
+
17
+ **Via Jekyll plugin system**
18
+
19
+ 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:
20
+
21
+ ```
22
+ gems: [jekyll-timeago]
23
+ ```
24
+
25
+ **Via Bundler**
26
+
9
27
  Add this gem to your `Gemfile` and run `bundle`:
10
28
 
11
29
  ```
@@ -18,10 +36,13 @@ To use this filter, just add the following to the top of another plugin (found u
18
36
  require 'jekyll/timeago'
19
37
  ```
20
38
 
39
+ **Manually**
40
+
21
41
  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
42
 
23
43
 
24
44
  ## Usage
45
+
25
46
  ```html
26
47
  <span>{{ page.date | timeago }}</span>
27
48
  <h2>{{ page.title }}</h2>
@@ -32,13 +53,41 @@ Alternatively, you can simply copy [this file](https://github.com/markets/jekyll
32
53
  ```
33
54
 
34
55
  ### Customization
56
+
35
57
  You can personalize the level of detail (from 1 up to 4, 2 by default) passing a parameter:
58
+
36
59
  ```html
37
60
  <span>{{ page.date | timeago: 4 }}</span>
38
61
  ```
39
62
 
63
+ ## Localization
64
+
65
+ This 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](https://github.com/markets/jekyll-timeago/blob/master/_config.yml.example) and translate it to your site's language. Sample:
66
+
67
+ ```
68
+ jekyll_timeago:
69
+ today: 'today'
70
+ yesterday: 'yesterday'
71
+ tomorrow: 'tomorrow'
72
+ and: 'and'
73
+ suffix: 'ago'
74
+ prefix: ''
75
+ suffix_future: 'in'
76
+ prefix_future: ''
77
+ years: 'years'
78
+ year: 'year'
79
+ months: 'months'
80
+ month: 'month'
81
+ weeks: 'weeks'
82
+ week: 'week'
83
+ days: 'days'
84
+ day: 'day'
85
+ ```
86
+
40
87
  ## Output Examples
88
+
41
89
  Default behavior:
90
+
42
91
  ```ruby
43
92
  > timeago(Date.today)
44
93
  => "today"
@@ -59,6 +108,7 @@ Default behavior:
59
108
  ```
60
109
 
61
110
  Change level of detail to get higher or lower granularity:
111
+
62
112
  ```ruby
63
113
  > timeago(Date.today - 500.days) # default
64
114
  => "1 year ago and 4 months ago"
@@ -71,4 +121,5 @@ Change level of detail to get higher or lower granularity:
71
121
  ```
72
122
 
73
123
  ## License
124
+
74
125
  Copyright (c) 2013 Marc Anguera. Unscoped Associations is released under the [MIT](http://opensource.org/licenses/MIT) License.
@@ -0,0 +1,17 @@
1
+ jekyll_timeago:
2
+ today: 'today'
3
+ yesterday: 'yesterday'
4
+ tomorrow: 'tomorrow'
5
+ and: 'and'
6
+ suffix: 'ago'
7
+ prefix: ''
8
+ suffix_future: 'in'
9
+ prefix_future: ''
10
+ years: 'years'
11
+ year: 'year'
12
+ months: 'months'
13
+ month: 'month'
14
+ weeks: 'weeks'
15
+ week: 'week'
16
+ days: 'days'
17
+ day: 'day'
@@ -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 = %q{Custom timeago filter for Jekyll (Liquid filter)}
12
- spec.summary = %q{Custom timeago filter for Jekyll (Liquid filter)}
11
+ spec.description = %q{Custom timeago filter for Jekyll (Liquid filter). Localization and futures supported.}
12
+ spec.summary = %q{Custom timeago filter for Jekyll (Liquid filter). Localization and futures supported.}
13
13
  spec.homepage = "https://github.com/markets/jekyll-timeago"
14
14
  spec.license = "MIT"
15
15
 
@@ -6,8 +6,8 @@ module Jekyll
6
6
  DAYS_PER = {
7
7
  :days => 1,
8
8
  :weeks => 7,
9
- :months => 31,
10
- :years => 365,
9
+ :months => 30,
10
+ :years => 365
11
11
  }
12
12
 
13
13
  # Max level of detail
@@ -20,6 +20,15 @@ module Jekyll
20
20
  DEFAULT_DEPTH_LEVEL = 2
21
21
 
22
22
  def timeago(input, depth = DEFAULT_DEPTH_LEVEL)
23
+ validate!(input, depth)
24
+
25
+ time_ago_to_now(input, depth)
26
+ end
27
+
28
+ private
29
+
30
+ # Validates inputs
31
+ def validate!(input, depth)
23
32
  unless depth_allowed?(depth)
24
33
  raise "Invalid depth level: #{depth.inspect}"
25
34
  end
@@ -27,27 +36,55 @@ module Jekyll
27
36
  unless input.is_a?(Date) || input.is_a?(Time)
28
37
  raise "Invalid input type: #{input.inspect}"
29
38
  end
39
+ end
30
40
 
31
- days_passed = (Date.today - Date.parse(input.to_s)).to_i
32
- time_ago_to_now(days_passed, depth)
41
+ # Get plugin configuration from site. Returns an empty hash if not provided.
42
+ def config
43
+ @config ||= Jekyll.configuration({}).fetch('jekyll_timeago', {})
33
44
  end
34
45
 
35
- private
46
+ def strings
47
+ {
48
+ :today => config['day'] || 'today',
49
+ :yesterday => config['yesterday'] || 'yesterday',
50
+ :tomorrow => config['tomorrow'] || 'tomorrow',
51
+ :and => config['and'] ||'and',
52
+ :suffix => config['suffix'] || 'ago',
53
+ :prefix => config['prefix'] || '',
54
+ :suffix_future => config['suffix_future'] || '',
55
+ :prefix_future => config['prefix_future'] || 'in',
56
+ :years => config['years'] || 'years',
57
+ :year => config['year'] || 'year',
58
+ :months => config['months'] || 'months',
59
+ :month => config['month'] || 'month',
60
+ :weeks => config['weeks'] || 'weeks',
61
+ :week => config['week'] || 'week',
62
+ :days => config['days'] || 'days',
63
+ :day => config['day'] || 'day'
64
+ }
65
+ end
66
+
67
+ def translate(key)
68
+ strings[key.to_sym]
69
+ end
70
+ alias_method :t, :translate
36
71
 
37
72
  # Days passed to time ago sentence
38
- def time_ago_to_now(days_passed, depth)
39
- return "today" if days_passed == 0
40
- return "yesterday" if days_passed == 1
41
- return "tomorrow" if days_passed == -1
73
+ def time_ago_to_now(input_date, depth)
74
+ days_passed = (Date.today - Date.parse(input_date.to_s)).to_i
75
+
76
+ return t(:today) if days_passed == 0
77
+ return t(:yesterday) if days_passed == 1
78
+ return t(:tomorrow) if days_passed == -1
42
79
 
43
80
  future = days_passed < 0
44
81
  slots = build_time_ago_slots(days_passed.abs, depth)
45
82
  sentence = to_sentence(slots)
46
83
 
47
84
  if future
48
- "in #{sentence}"
85
+ "#{t(:prefix_future)} #{sentence} #{t(:suffix_future)}".strip
49
86
  else
50
- "#{sentence} ago"
87
+ "#{t(:prefix)} #{sentence} #{t(:suffix)}".strip
51
88
  end
52
89
  end
53
90
 
@@ -62,9 +99,9 @@ module Jekyll
62
99
  days = DAYS_PER[time_range]
63
100
  num_elems = days_passed / days
64
101
  range_type = if num_elems == 1
65
- time_range.to_s[0...-1] # singularize
102
+ t(time_range[0...-1]) # singularize key
66
103
  else
67
- time_range.to_s
104
+ t(time_range)
68
105
  end
69
106
 
70
107
  current_slots << "#{num_elems} #{range_type}"
@@ -96,10 +133,10 @@ module Jekyll
96
133
  if slots.length == 1
97
134
  slots[0]
98
135
  else
99
- "#{slots[0...-1].join(', ')} and #{slots[-1]}"
136
+ "#{slots[0...-1].join(', ')} #{t(:and)} #{slots[-1]}"
100
137
  end
101
138
  end
102
139
  end
103
140
  end
104
141
 
105
- Liquid::Template.register_filter(Jekyll::Timeago)
142
+ Liquid::Template.register_filter(Jekyll::Timeago) if defined?(Liquid)
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
2
  module Timeago
3
- VERSION = "0.4.2"
3
+ VERSION = "0.5.0"
4
4
  end
5
5
  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.2
4
+ version: 0.5.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-09-11 00:00:00.000000000 Z
12
+ date: 2014-01-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -43,7 +43,8 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
- description: Custom timeago filter for Jekyll (Liquid filter)
46
+ description: Custom timeago filter for Jekyll (Liquid filter). Localization and futures
47
+ supported.
47
48
  email:
48
49
  - srmarc.ai@gmail.com
49
50
  executables: []
@@ -54,6 +55,7 @@ files:
54
55
  - Gemfile
55
56
  - README.md
56
57
  - Rakefile
58
+ - _config.yml.example
57
59
  - jekyll-timeago.gemspec
58
60
  - lib/jekyll/timeago.rb
59
61
  - lib/jekyll/timeago/version.rb
@@ -70,22 +72,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
70
72
  - - ! '>='
71
73
  - !ruby/object:Gem::Version
72
74
  version: '0'
73
- segments:
74
- - 0
75
- hash: 1046143283
76
75
  required_rubygems_version: !ruby/object:Gem::Requirement
77
76
  none: false
78
77
  requirements:
79
78
  - - ! '>='
80
79
  - !ruby/object:Gem::Version
81
80
  version: '0'
82
- segments:
83
- - 0
84
- hash: 1046143283
85
81
  requirements: []
86
82
  rubyforge_project:
87
- rubygems_version: 1.8.24
83
+ rubygems_version: 1.8.23
88
84
  signing_key:
89
85
  specification_version: 3
90
- summary: Custom timeago filter for Jekyll (Liquid filter)
86
+ summary: Custom timeago filter for Jekyll (Liquid filter). Localization and futures
87
+ supported.
91
88
  test_files: []