aboutsummaryrefslogtreecommitdiff
path: root/plugins/date.rb
blob: b3e9e35e50ba06134a38c10b0b4d0b5cf690154a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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}<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

  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 <Hash>
    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