Module: Octopress::PageDate

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

Defined Under Namespace

Modules: Configuration Classes: PageHook, PostHook, SiteHook

Class Method Summary collapse

Class Method Details

.configObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/octopress-date-format/configuration.rb', line 32

def self.config
  @config ||= begin
    default = PageDate::Configuration::DEFAULTS
    jekyll = PageDate::Configuration::jekyll_date_config
    octopress = Octopress.config

    config = Jekyll::Utils.deep_merge_hashes(default, jekyll)
    Jekyll::Utils.deep_merge_hashes(config, octopress)
  end
end

.date_html(date, time = true) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/octopress-date-format.rb', line 52

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



61
62
63
# File 'lib/octopress-date-format.rb', line 61

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

.datetime(input) ⇒ Object



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

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



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

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



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

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

.hack_date(page) ⇒ Object



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

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.



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/octopress-date-format.rb', line 92

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



80
81
82
# File 'lib/octopress-date-format.rb', line 80

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

.ordinalize_html(date) ⇒ Object



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

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