Browse code

Add custom date format to pages, add 'updated' field again

Reverts changes of c2a68cc where I accidentally removed support for 'updated' field, see comments of issue #164 for details.

Frederic Hemberger authored on 03/11/2011 at 19:32:38
Showing 4 changed files
... ...
@@ -2,6 +2,14 @@
2 2
 {% capture date_formatted %}{{ page.date_formatted }}{{ post.date_formatted }}{% endcapture %}
3 3
 {% capture has_date %}{{ date | size }}{% endcapture %}
4 4
 
5
+{% capture updated %}{{ page.updated }}{{ post.updated }}{% endcapture %}
6
+{% capture updated_formatted %}{{ page.updated_formatted }}{{ post.updated_formatted }}{% endcapture %}
7
+{% capture was_updated %}{{ updated | size }}{% endcapture %}
8
+
5 9
 {% if has_date != '0' %}
6
-  {% capture time %}<time datetime="{{ date | datetime | date_to_xmlschema }}" pubdate>{{ date_formatted }}</time>{% endcapture %}
10
+  {% capture time %}<time datetime="{{ date | datetime | date_to_xmlschema }}" pubdate{% if updated %} data-updated="true"{% endif %}>{{ date_formatted }}</time>{% endcapture %}
7 11
 {% endif %}
12
+
13
+{% if was_updated != '0' %}
14
+  {% capture updated %}<time datetime="{{ updated | datetime | date_to_xmlschema }}" class="updated">Updated {{ updated_formatted }}</time>{% endcapture %}
15
+{% else %}{% assign updated = false %}{% endif %}
8 16
\ No newline at end of file
... ...
@@ -15,7 +15,7 @@ layout: default
15 15
     <footer>
16 16
       {% if page.date or page.author %}<p class="meta">
17 17
         {% if page.author %}{% include post/author.html %}{% endif %}
18
-        {% include post/date.html %}{{ time }}
18
+        {% include post/date.html %}{% if updated %}{{ updated }}{% else %}{{ time }}{% endif %}
19 19
         {% if page.categories %}{% include post/categories.html %}{% endif %}
20 20
       </p>{% endif %}
21 21
       {% unless page.sharing == false %}
... ...
@@ -9,7 +9,7 @@ single: true
9 9
   <footer>
10 10
     <p class="meta">
11 11
       {% include post/author.html %}
12
-      {% include post/date.html %}{{ time }}
12
+      {% include post/date.html %}{% if updated %}{{ updated }}{% else %}{{ time }}{% endif %}
13 13
       {% include post/categories.html %}
14 14
     </p>
15 15
     {% unless page.sharing == false %}
... ...
@@ -29,6 +29,16 @@ module Octopress
29 29
       end
30 30
     end
31 31
 
32
+    def format_date(date, format)
33
+      date = datetime(date)
34
+      if format.nil? || format.empty? || format == "ordinal"
35
+        date_formatted = ordinalize(date)
36
+      else
37
+        date_formatted = date.strftime(format)
38
+      end
39
+      date_formatted
40
+    end
41
+
32 42
   end
33 43
 end
34 44
 
... ...
@@ -38,32 +48,48 @@ module Jekyll
38 38
   class Post
39 39
     include Octopress::Date
40 40
 
41
-    attr_accessor :date_formatted
42
-
43 41
     # Convert this post into a Hash for use in Liquid templates.
44 42
     #
45 43
     # Returns <Hash>
46 44
     def to_liquid
47
-      format = self.site.config['date_format']
48
-      if format.nil? || format.empty? || format == "ordinal"
49
-        date_formatted = ordinalize(self.date)
50
-      else
51
-        date_formatted = self.date.strftime(format)
52
-      end
53
-
45
+      date_format = self.site.config['date_format']
54 46
       self.data.deep_merge({
55
-        "title"          => self.data["title"] || self.slug.split('-').select {|w| w.capitalize! || w }.join(' '),
56
-        "url"            => self.url,
57
-        "date"           => self.date,
47
+        "title"             => self.data['title'] || self.slug.split('-').select {|w| w.capitalize! || w }.join(' '),
48
+        "url"               => self.url,
49
+        "date"              => self.date,
58 50
         # Monkey patch
59
-        "date_formatted" => date_formatted,
60
-        "id"             => self.id,
61
-        "categories"     => self.categories,
62
-        "next"           => self.next,
63
-        "previous"       => self.previous,
64
-        "tags"           => self.tags,
65
-        "content"        => self.content })
51
+        "date_formatted"    => format_date(self.date, date_format),
52
+        "updated_formatted" => self.data.has_key?('updated') ? format_date(self.data['updated'], date_format) : nil,
53
+        "id"                => self.id,
54
+        "categories"        => self.categories,
55
+        "next"              => self.next,
56
+        "previous"          => self.previous,
57
+        "tags"              => self.tags,
58
+        "content"           => self.content })
66 59
     end
60
+  end
61
+
62
+  class Page
63
+    include Octopress::Date
64
+
65
+    # Initialize a new Page.
66
+    #
67
+    # site - The Site object.
68
+    # base - The String path to the source.
69
+    # dir  - The String path between the source and the file.
70
+    # name - The String filename of the file.
71
+    def initialize(site, base, dir, name)
72
+      @site = site
73
+      @base = base
74
+      @dir  = dir
75
+      @name = name
67 76
 
77
+      self.process(name)
78
+      self.read_yaml(File.join(base, dir), name)
79
+      # Monkey patch
80
+      date_format = self.site.config['date_format']
81
+      self.data['date_formatted']    = format_date(self.data['date'], date_format) if self.data.has_key?('date')
82
+      self.data['updated_formatted'] = format_date(self.data['updated'], date_format) if self.data.has_key?('updated')
83
+    end
68 84
   end
69 85
 end
70 86
\ No newline at end of file