Class: Jekyll::DateFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-date-format.rb,
lib/jekyll-date-format/version.rb

Constant Summary collapse

VERSION =
"1.0.1"

Class Method Summary collapse

Class Method Details

.datetime(date) ⇒ Object

Returns a datetime if the input is a string



6
7
8
9
10
11
# File 'lib/jekyll-date-format.rb', line 6

def self.datetime(date)
  if date.class == String
    date = Time.parse(date)
  end
  date
end

.format_date(date, format) ⇒ Object

Formats date either as ordinal or by given date format Adds %o as ordinal representation of the day



42
43
44
45
46
47
48
49
50
51
# File 'lib/jekyll-date-format.rb', line 42

def self.format_date(date, format)
  date = datetime(date)
  if format.nil? || format.empty? || format == "ordinal"
    date_formatted = ordinalize(date)
  else
    date_formatted = date.strftime(format)
    date_formatted.gsub!(/%o/, ordinal(date.strftime('%e').to_i))
  end
  date_formatted
end

.ordinal(number) ⇒ Object

Returns an ordinal number. 13 -> 13th, 21 -> 21st etc.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/jekyll-date-format.rb', line 27

def self.ordinal(number)
  if (11..13).include?(number.to_i % 100)
    "#{number}<sup>th</sup>"
  else
    case number.to_i % 10
    when 1; "#{number}<sup>st</sup>"
    when 2; "#{number}<sup>nd</sup>"
    when 3; "#{number}<sup>rd</sup>"
    else    "#{number}<sup>th</sup>"
    end
  end
end

.ordinalize(date) ⇒ Object

Returns an ordidinal date eg July 22 2007 -> July 22nd 2007



14
15
16
17
# File 'lib/jekyll-date-format.rb', line 14

def self.ordinalize(date)
  date = datetime(date)
  "#{date.strftime('%b')} #{ordinal(date.strftime('%e').to_i)}, #{date.strftime('%Y')}"
end

.time_tag(date, formatted, type) ⇒ Object



19
20
21
22
23
# File 'lib/jekyll-date-format.rb', line 19

def self.time_tag(date, formatted, type)
  t = "<time class='date-#{type}' datetime='#{date}'"
  t += " pubdate" unless type == 'updated'
  t += ">#{formatted}</time>"
end