plugins/date.rb
c2a68cc2
 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}<span>th</span>"
       else
         case number.to_i % 10
         when 1; "#{number}<span>st</span>"
         when 2; "#{number}<span>nd</span>"
         when 3; "#{number}<span>rd</span>"
         else    "#{number}<span>th</span>"
         end
       end
     end
 
987dccee
     def format_date(date, format)
       date = datetime(date)
       if format.nil? || format.empty? || format == "ordinal"
         date_formatted = ordinalize(date)
       else
         date_formatted = date.strftime(format)
       end
       date_formatted
     end
 
c2a68cc2
   end
 end
 
 
 module Jekyll
 
   class Post
     include Octopress::Date
 
     # Convert this post into a Hash for use in Liquid templates.
     #
     # Returns <Hash>
     def to_liquid
987dccee
       date_format = self.site.config['date_format']
c2a68cc2
       self.data.deep_merge({
987dccee
         "title"             => self.data['title'] || self.slug.split('-').select {|w| w.capitalize! || w }.join(' '),
         "url"               => self.url,
         "date"              => self.date,
c2a68cc2
         # Monkey patch
987dccee
         "date_formatted"    => format_date(self.date, date_format),
         "updated_formatted" => self.data.has_key?('updated') ? format_date(self.data['updated'], date_format) : nil,
         "id"                => self.id,
         "categories"        => self.categories,
         "next"              => self.next,
         "previous"          => self.previous,
         "tags"              => self.tags,
         "content"           => self.content })
c2a68cc2
     end
987dccee
   end
 
   class Page
     include Octopress::Date
 
     # Initialize a new Page.
     #
     # site - The Site object.
     # base - The String path to the source.
     # dir  - The String path between the source and the file.
     # name - The String filename of the file.
     def initialize(site, base, dir, name)
       @site = site
       @base = base
       @dir  = dir
       @name = name
c2a68cc2
 
987dccee
       self.process(name)
       self.read_yaml(File.join(base, dir), name)
       # Monkey patch
       date_format = self.site.config['date_format']
       self.data['date_formatted']    = format_date(self.data['date'], date_format) if self.data.has_key?('date')
       self.data['updated_formatted'] = format_date(self.data['updated'], date_format) if self.data.has_key?('updated')
     end
c2a68cc2
   end
 end