Skip to content

Commit

Permalink
update datetime plugin for jekyll 3
Browse files Browse the repository at this point in the history
use post prerender hook to populate the "datetime" field and monkey
patch merge_data! to preserve the original date value
  • Loading branch information
willnorris committed Nov 3, 2015
1 parent 875a788 commit dcb7ef0
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions src/_plugins/datetime.rb
Expand Up @@ -10,28 +10,29 @@
# Otherwise, the yaml module will attempt to parse "naked" values as a Time,
# which loses the specified timezone value.

require "liquid"
require "date"

module Jekyll
class Post
def datetime
if !data.has_key?("datetime")
unless data["date"].kind_of? String
Jekyll.logger.error "ERROR:", "datetime plugin requires that all dates be specified as strings"
Jekyll.logger.error "", "#{@name} contains date of type '#{data["date"].class}'"
exit(1)
end
data["datetime"] = DateTime.parse(data["date"].to_s)
end
data["datetime"]
end
# Hook to populate the "datetime" data field for posts.
Jekyll::Hooks.register :posts, :pre_render do |post, data|
page = data["page"]
unless page["original_date"].kind_of? String
Jekyll.logger.error "ERROR:", "datetime plugin requires that all dates be specified as strings"
Jekyll.logger.error "", "#{@name} contains date of type '#{page["original_date"].class}'"
exit(1)
end
page["datetime"] = DateTime.parse(page["original_date"].to_s)
end

def to_liquid_with_datetime(attrs = ATTRIBUTES_FOR_LIQUID)
to_liquid_without_datetime(attrs + %w[datetime])
module Jekyll
class Document
# monkey-patch merge_data in order to preserve the originally specified date value
def merge_data_preserve_date!(other)
data = merge_data_overwrite_datetime!(other)
data["original_date"] = other["date"] if !other["date"].nil?
data
end
alias_method :to_liquid_without_datetime, :to_liquid
alias_method :to_liquid, :to_liquid_with_datetime
alias_method :merge_data_overwrite_datetime!, :merge_data!
alias_method :merge_data!, :merge_data_preserve_date!
end

module DateFilters
Expand Down

0 comments on commit dcb7ef0

Please sign in to comment.