From c2a68cc2a980506b12b9054d5be8e4806fe79c1c Mon Sep 17 00:00:00 2001 From: Frederic Hemberger Date: Tue, 18 Oct 2011 17:25:31 +0200 Subject: Make post's date output configurable via _config.yml, closes #164 A new config variable 'date_format' is introduced in _config.yml. It can either be set to "ordinal" to use the current format or it can be given a string complying to strftime() format identifiers. --- plugins/date.rb | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 plugins/date.rb (limited to 'plugins/date.rb') diff --git a/plugins/date.rb b/plugins/date.rb new file mode 100644 index 00000000..b3e9e35e --- /dev/null +++ b/plugins/date.rb @@ -0,0 +1,69 @@ +module Octopress + module Date + + # Returns a datetime if the input is a string + def datetime(date) + if date.class == String + date = Time.parse(date) + end + date + end + + # Returns an ordidinal date eg July 22 2007 -> July 22nd 2007 + def ordinalize(date) + date = datetime(date) + "#{date.strftime('%b')} #{ordinal(date.strftime('%e').to_i)}, #{date.strftime('%Y')}" + end + + # Returns an ordinal number. 13 -> 13th, 21 -> 21st etc. + def ordinal(number) + if (11..13).include?(number.to_i % 100) + "#{number}th" + else + case number.to_i % 10 + when 1; "#{number}st" + when 2; "#{number}nd" + when 3; "#{number}rd" + else "#{number}th" + end + end + end + + end +end + + +module Jekyll + + class Post + include Octopress::Date + + attr_accessor :date_formatted + + # Convert this post into a Hash for use in Liquid templates. + # + # Returns + def to_liquid + format = self.site.config['date_format'] + if format.nil? || format.empty? || format == "ordinal" + date_formatted = ordinalize(self.date) + else + date_formatted = self.date.strftime(format) + end + + self.data.deep_merge({ + "title" => self.data["title"] || self.slug.split('-').select {|w| w.capitalize! || w }.join(' '), + "url" => self.url, + "date" => self.date, + # Monkey patch + "date_formatted" => date_formatted, + "id" => self.id, + "categories" => self.categories, + "next" => self.next, + "previous" => self.previous, + "tags" => self.tags, + "content" => self.content }) + end + + end +end \ No newline at end of file -- cgit v1.2.1