Skip to content

Commit 64ba603

Browse files
committed
Merge pull request imathis#1397 from IQAndreas/cleanup-date
Call overridden version of `to_liquid` and remove duplicate code in `Octopress::Date`
2 parents 1230e01 + 408cade commit 64ba603

File tree

1 file changed

+18
-35
lines changed

1 file changed

+18
-35
lines changed

plugins/date.rb

+18-35
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ def format_date(date, format)
4141
end
4242
date_formatted
4343
end
44+
45+
# Returns the date-specific liquid attributes
46+
def liquid_date_attributes
47+
date_format = self.site.config['date_format']
48+
date_attributes = {}
49+
date_attributes['date_formatted'] = format_date(self.data['date'], date_format) if self.data.has_key?('date')
50+
date_attributes['updated_formatted'] = format_date(self.data['updated'], date_format) if self.data.has_key?('updated')
51+
date_attributes
52+
end
4453

4554
end
4655
end
@@ -51,48 +60,22 @@ module Jekyll
5160
class Post
5261
include Octopress::Date
5362

54-
# Convert this post into a Hash for use in Liquid templates.
55-
#
56-
# Returns <Hash>
63+
# Convert this Convertible's data to a Hash suitable for use by Liquid.
64+
# Overrides the default return data and adds any date-specific liquid attributes
65+
alias :super_to_liquid :to_liquid
5766
def to_liquid
58-
date_format = self.site.config['date_format']
59-
self.data.deep_merge({
60-
"title" => self.data['title'] || self.slug.split('-').select {|w| w.capitalize! || w }.join(' '),
61-
"url" => self.url,
62-
"date" => self.date,
63-
# Monkey patch
64-
"date_formatted" => format_date(self.date, date_format),
65-
"updated_formatted" => self.data.has_key?('updated') ? format_date(self.data['updated'], date_format) : nil,
66-
"id" => self.id,
67-
"categories" => self.categories,
68-
"next" => self.next,
69-
"previous" => self.previous,
70-
"tags" => self.tags,
71-
"content" => self.content })
67+
super_to_liquid.deep_merge(liquid_date_attributes)
7268
end
7369
end
7470

7571
class Page
7672
include Octopress::Date
7773

78-
# Initialize a new Page.
79-
#
80-
# site - The Site object.
81-
# base - The String path to the source.
82-
# dir - The String path between the source and the file.
83-
# name - The String filename of the file.
84-
def initialize(site, base, dir, name)
85-
@site = site
86-
@base = base
87-
@dir = dir
88-
@name = name
89-
90-
self.process(name)
91-
self.read_yaml(File.join(base, dir), name)
92-
# Monkey patch
93-
date_format = self.site.config['date_format']
94-
self.data['date_formatted'] = format_date(self.data['date'], date_format) if self.data.has_key?('date')
95-
self.data['updated_formatted'] = format_date(self.data['updated'], date_format) if self.data.has_key?('updated')
74+
# Convert this Convertible's data to a Hash suitable for use by Liquid.
75+
# Overrides the default return data and adds any date-specific liquid attributes
76+
alias :super_to_liquid :to_liquid
77+
def to_liquid
78+
super_to_liquid.deep_merge(liquid_date_attributes)
9679
end
9780
end
9881
end

0 commit comments

Comments
 (0)