Module: Octopress::DateFormat

Defined in:
lib/octopress-date-format.rb,
lib/octopress-date-format/version.rb

Defined Under Namespace

Classes: PageHook, PostHook, SiteHook

Constant Summary collapse

DEFAULTS =
{
  'date_format' => 'ordinal',
  'time_format' => '%-I:%M %P'
}
VERSION =
"3.0.2"

Class Method Summary collapse

Class Method Details

.configObject



11
12
13
# File 'lib/octopress-date-format.rb', line 11

def self.config
  @config
end

.config=(config) ⇒ Object



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

def self.config=(config)
  @config = DEFAULTS.merge(config)
end

.date_html(date, time = true) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/octopress-date-format.rb', line 64

def self.date_html(date, time=true)
  tag =  "<time class='entry-date' datetime='#{ date.xmlschema }'>"
  tag += "<span class='date'>#{format_date(date, true)}</span>"
  if time
    tag += " <span class='time'>#{format_time(date)}</span>" if time
  end
  tag += "</time>"
end

.date_updated_html(date, time = true) ⇒ Object



73
74
75
# File 'lib/octopress-date-format.rb', line 73

def self.date_updated_html(date, time=true)
  date_html(date, time).sub('entry-date','updated')
end

.datetime(input) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/octopress-date-format.rb', line 118

def self.datetime(input)
  case input
  when Time
    input
  when Date
    input
  when String
    Time.parse(input) rescue Time.at(input.to_i)
  when Numeric
    Time.at(input)
  else
    raise "Invalid Date:", "'#{input}' is not a valid datetime."
    exit(1)
  end
end

.format_date(date, html = false) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/octopress-date-format.rb', line 77

def self.format_date(date, html=false)
  format = config['date_format']
  if format == 'ordinal'
    html ? ordinalize_html(date) : ordinalize(date)
  else
    date.strftime(format)
  end
end

.format_time(date, html = false) ⇒ Object



86
87
88
89
# File 'lib/octopress-date-format.rb', line 86

def self.format_time(date, html=false)
  format = config['time_format']
  date.strftime(format)
end

.hack_date(page) ⇒ Object



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
# File 'lib/octopress-date-format.rb', line 37

def self.hack_date(page)
  if page.data['date'] || page.respond_to?(:date)
    date = datetime(page.data['date'] || page.date)

    page.data['date_xml']  = date.xmlschema
    page.data['date_text'] = format_date(date)
    page.data['time_text'] = format_time(date)
    page.data['date_html'] = date_html(date, false)
    page.data['date_time_html'] = date_html(date)
  end

  # Legacy support
  if page.data['updated']
    page.data['date_updated'] = page.data['updated']
  end

  if page.data['date_updated']
    updated  = datetime(page.data['date_updated'])
    page.data['date_updated_xml']  = updated.xmlschema
    page.data['date_updated_text'] = format_date(updated)
    page.data['time_updated_text'] = format_time(updated)
    page.data['date_updated_html'] = date_updated_html(updated, false)
    page.data['date_time_updated_html'] = date_updated_html(updated)
  end
  page
end

.ordinal_suffix(date) ⇒ Object

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



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/octopress-date-format.rb', line 104

def self.ordinal_suffix(date)
  number = date.strftime('%e').to_i
  if (11..13).include?(number % 100)
    "th"
  else
    case number % 10
    when 1; "st"
    when 2; "nd"
    when 3; "rd"
    else    "th"
    end
  end
end

.ordinalize(date) ⇒ Object

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



92
93
94
# File 'lib/octopress-date-format.rb', line 92

def self.ordinalize(date)
  "#{date.strftime('%b %-d')}#{ordinal_suffix(date)}, #{date.strftime('%Y')}"
end

.ordinalize_html(date) ⇒ Object



96
97
98
99
100
101
# File 'lib/octopress-date-format.rb', line 96

def self.ordinalize_html(date)
  d = "<span class='date-month'>#{date.strftime('%b')}</span> "
  d += "<span class='date-day'>#{date.strftime('%-d')}</span>"
  d += "<span class='date-suffix'>#{ordinal_suffix(date)}</span>, "
  d += "<span class='date-year'>#{date.strftime('%Y')}</span>"
end