Browse code

Make post's date output configurable via _config.yml, closes #164

A new config variable 'date_format' is introduced in _config.yml.
It can either be set to "ordinal" to use the current format or it
can be given a string complying to strftime() format identifiers.

Frederic Hemberger authored on 18/10/2011 at 15:25:31
Showing 4 changed files
... ...
@@ -1,10 +1,11 @@
1 1
 {% capture date %}{{ page.date }}{{ post.date }}{% endcapture %}
2
+{% capture date_formatted %}{{ page.date_formatted }}{{ post.date_formatted }}{% endcapture %}
2 3
 {% capture has_date %}{{ date | size }}{% endcapture %}
3 4
 {% capture updated %}{{ page.updated }}{{ post.updated }}{% endcapture %}
4 5
 {% capture was_updated %}{{ updated | size }}{% endcapture %}
5 6
 
6 7
 {% if has_date != '0' %}
7
-  {% capture time %}<time datetime="{{ date | datetime | date_to_xmlschema }}" pubdate {% if updated %} data-updated="true" {% endif %}>{{ date | ordinalize }}</time>{% endcapture %}
8
+  {% capture time %}<time datetime="{{ date | datetime | date_to_xmlschema }}" pubdate {% if updated %} data-updated="true" {% endif %}>{{ date_formatted }}</time>{% endcapture %}
8 9
 {% endif %}
9 10
 
10 11
 {% if was_updated != '0' %}
... ...
@@ -8,6 +8,11 @@ subtitle: A blogging framework for hackers.
8 8
 author: Your Name
9 9
 simple_search: http://google.com/search
10 10
 
11
+# Default date format is "ordinal" (resulting in "July 22nd 2007")
12
+# You can customize the format as defined in
13
+# http://www.ruby-doc.org/core-1.9.2/Time.html#method-i-strftime
14
+date_format: "ordinal"
15
+
11 16
 # RSS / Email (optional) subscription links (change if using something like Feedburner)
12 17
 subscribe_rss: /atom.xml
13 18
 subscribe_email:
14 19
new file mode 100644
... ...
@@ -0,0 +1,69 @@
0
+module Octopress
1
+  module Date
2
+
3
+    # Returns a datetime if the input is a string
4
+    def datetime(date)
5
+      if date.class == String
6
+        date = Time.parse(date)
7
+      end
8
+      date
9
+    end
10
+
11
+    # Returns an ordidinal date eg July 22 2007 -> July 22nd 2007
12
+    def ordinalize(date)
13
+      date = datetime(date)
14
+      "#{date.strftime('%b')} #{ordinal(date.strftime('%e').to_i)}, #{date.strftime('%Y')}"
15
+    end
16
+
17
+    # Returns an ordinal number. 13 -> 13th, 21 -> 21st etc.
18
+    def ordinal(number)
19
+      if (11..13).include?(number.to_i % 100)
20
+        "#{number}<span>th</span>"
21
+      else
22
+        case number.to_i % 10
23
+        when 1; "#{number}<span>st</span>"
24
+        when 2; "#{number}<span>nd</span>"
25
+        when 3; "#{number}<span>rd</span>"
26
+        else    "#{number}<span>th</span>"
27
+        end
28
+      end
29
+    end
30
+
31
+  end
32
+end
33
+
34
+
35
+module Jekyll
36
+
37
+  class Post
38
+    include Octopress::Date
39
+
40
+    attr_accessor :date_formatted
41
+
42
+    # Convert this post into a Hash for use in Liquid templates.
43
+    #
44
+    # Returns <Hash>
45
+    def to_liquid
46
+      format = self.site.config['date_format']
47
+      if format.nil? || format.empty? || format == "ordinal"
48
+        date_formatted = ordinalize(self.date)
49
+      else
50
+        date_formatted = self.date.strftime(format)
51
+      end
52
+
53
+      self.data.deep_merge({
54
+        "title"          => self.data["title"] || self.slug.split('-').select {|w| w.capitalize! || w }.join(' '),
55
+        "url"            => self.url,
56
+        "date"           => self.date,
57
+        # Monkey patch
58
+        "date_formatted" => date_formatted,
59
+        "id"             => self.id,
60
+        "categories"     => self.categories,
61
+        "next"           => self.next,
62
+        "previous"       => self.previous,
63
+        "tags"           => self.tags,
64
+        "content"        => self.content })
65
+    end
66
+
67
+  end
68
+end
0 69
\ No newline at end of file
... ...
@@ -2,6 +2,7 @@
2 2
 require './plugins/backtick_code_block'
3 3
 require './plugins/post_filters'
4 4
 require './plugins/raw'
5
+require './plugins/date'
5 6
 require 'rubypants'
6 7
 
7 8
 module OctopressFilters
... ...
@@ -33,6 +34,8 @@ end
33 33
 
34 34
 
35 35
 module OctopressLiquidFilters
36
+  include Octopress::Date
37
+
36 38
   # Used on the blog index to split posts on the <!--more--> marker
37 39
   def excerpt(input)
38 40
     if input.index(/<!--\s*more\s*-->/i)
... ...
@@ -96,33 +99,6 @@ module OctopressLiquidFilters
96 96
     input.titlecase
97 97
   end
98 98
 
99
-  # Returns a datetime if the input is a string
100
-  def datetime(date)
101
-    if date.class == String
102
-      date = Time.parse(date)
103
-    end
104
-    date
105
-  end
106
-
107
-  # Returns an ordidinal date eg July 22 2007 -> July 22nd 2007
108
-  def ordinalize(date)
109
-    date = datetime(date)
110
-    "#{date.strftime('%b')} #{ordinal(date.strftime('%e').to_i)}, #{date.strftime('%Y')}"
111
-  end
112
-
113
-  # Returns an ordinal number. 13 -> 13th, 21 -> 21st etc.
114
-  def ordinal(number)
115
-    if (11..13).include?(number.to_i % 100)
116
-      "#{number}<span>th</span>"
117
-    else
118
-      case number.to_i % 10
119
-      when 1; "#{number}<span>st</span>"
120
-      when 2; "#{number}<span>nd</span>"
121
-      when 3; "#{number}<span>rd</span>"
122
-      else    "#{number}<span>th</span>"
123
-      end
124
-    end
125
-  end
126 99
 end
127 100
 Liquid::Template.register_filter OctopressLiquidFilters
128 101