From c42eb7fc3a9030b23f90db77437cd41f2b4c7701 Mon Sep 17 00:00:00 2001 From: Pat Hawks Date: Thu, 22 Dec 2016 14:39:08 -0600 Subject: [PATCH 1/5] Take advantage of Liquid::Utils.to_date --- lib/jekyll/filters.rb | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index f537e847..ca47e06a 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -345,16 +345,9 @@ module Jekyll private def time(input) - case input - when Time - input.clone - when Date - input.to_time - when String - Time.parse(input) rescue Time.at(input.to_i) - when Numeric - Time.at(input) - else + begin + Liquid::Utils.to_date(input).to_time + rescue NoMethodError raise Errors::InvalidDateError, "Invalid Date: '#{input.inspect}' is not a valid datetime." end.localtime From 607ab66707744c3ec09fe1a03d89bd30337ebf57 Mon Sep 17 00:00:00 2001 From: Pat Hawks Date: Thu, 22 Dec 2016 15:13:27 -0600 Subject: [PATCH 2/5] Use Liquid's own `to_integer` --- lib/jekyll/filters.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index ca47e06a..1d21b99e 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -280,10 +280,11 @@ module Jekyll end end - def pop(array, input = 1) + def pop(array, num = 1) return array unless array.is_a?(Array) + num = Liquid::Utils.to_integer(num) new_ary = array.dup - new_ary.pop(input.to_i || 1) + new_ary.pop(num) new_ary end @@ -294,10 +295,11 @@ module Jekyll new_ary end - def shift(array, input = 1) + def shift(array, num = 1) return array unless array.is_a?(Array) + num = Liquid::Utils.to_integer(num) new_ary = array.dup - new_ary.shift(input.to_i || 1) + new_ary.shift(num) new_ary end @@ -310,11 +312,11 @@ module Jekyll def sample(input, num = 1) return input unless input.respond_to?(:sample) - n = num.to_i rescue 1 - if n == 1 + num = Liquid::Utils.to_integer(num) + if num == 1 input.sample else - input.sample(n) + input.sample(num) end end From 2eb84699362e0f34f28f2dd5bd48cacbe0e1a26e Mon Sep 17 00:00:00 2001 From: Pat Hawks Date: Thu, 22 Dec 2016 15:48:03 -0600 Subject: [PATCH 3/5] Never throw on `sample` --- lib/jekyll/filters.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index 1d21b99e..06a09345 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -312,7 +312,7 @@ module Jekyll def sample(input, num = 1) return input unless input.respond_to?(:sample) - num = Liquid::Utils.to_integer(num) + num = Liquid::Utils.to_integer(num) rescue 1 if num == 1 input.sample else From f7edcceb70281fca0ef2a9a95a5b64ffa7d620db Mon Sep 17 00:00:00 2001 From: Pat Hawks Date: Thu, 22 Dec 2016 16:15:09 -0600 Subject: [PATCH 4/5] Check return value of `to_date` --- lib/jekyll/filters.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index 06a09345..5ca2e10a 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -347,12 +347,12 @@ module Jekyll private def time(input) - begin - Liquid::Utils.to_date(input).to_time - rescue NoMethodError + date = Liquid::Utils.to_date(input) + unless date.respond_to?(:strftime) raise Errors::InvalidDateError, "Invalid Date: '#{input.inspect}' is not a valid datetime." - end.localtime + end + date.to_time.localtime end private From 72a56d9eb747edff8b79b7fe4f109a15c9b2e937 Mon Sep 17 00:00:00 2001 From: Pat Hawks Date: Thu, 22 Dec 2016 19:18:43 -0600 Subject: [PATCH 5/5] Check for `to_time` --- lib/jekyll/filters.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index 5ca2e10a..b7103668 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -348,7 +348,7 @@ module Jekyll private def time(input) date = Liquid::Utils.to_date(input) - unless date.respond_to?(:strftime) + unless date.respond_to?(:to_time) raise Errors::InvalidDateError, "Invalid Date: '#{input.inspect}' is not a valid datetime." end