Skip to content

Commit d3ecf9a

Browse files
committed
DRY out insertion of year/month in news plugin
1 parent 5d08226 commit d3ecf9a

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

_plugins/news.rb

+12-13
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ def news_dir
3333
end
3434
end
3535

36+
def insert_date(string, year, month = 0)
37+
string.gsub(/%Y|%m|%B/, {
38+
'%Y' => year.to_s,
39+
'%m' => "%.2d" % month,
40+
'%B' => @month_names[month]
41+
})
42+
end
43+
3644
end
3745

3846
class MonthlyArchive < ArchivePage
@@ -48,11 +56,7 @@ def initialize(site,base,lang,year,month,posts)
4856

4957
title = @locales['monthly_archive_title']
5058

51-
data['title'] = title.gsub(/%Y|%m|%B/, {
52-
'%Y' => @year.to_s,
53-
'%m' => "%.2d" % @month,
54-
'%B' => @month_names[@month]
55-
})
59+
data['title'] = insert_date(title, @year, @month)
5660
data['year'] = year
5761
end
5862

@@ -71,20 +75,15 @@ def initialize(site,base,lang,year,posts)
7175
title = @locales['yearly_archive_title']
7276
month_link_text = @locales['monthly_archive_link']
7377

74-
data['title'] = title.gsub('%Y', @year.to_s)
78+
data['title'] = insert_date(title, @year)
7579
data['year'] = @year
7680

7781
months = posts.map { |post| post.date.month }.uniq
7882

7983
# hash with url => link_text (including year) elements
8084
data['months'] = Hash[
8185
months.map { |month| "%.2d" % month }.zip(
82-
months.map { |month| month_link_text.gsub(/%Y|%m|%B/, {
83-
'%Y' => @year.to_s,
84-
'%m' => "%.2d" % month,
85-
'%B' => @month_names[month]
86-
})
87-
}
86+
months.map { |month| insert_date(month_link_text, @year, month) }
8887
)
8988
]
9089
end
@@ -111,7 +110,7 @@ def initialize(site,base,lang,posts)
111110
# hash with url => link_text elements
112111
data['years'] = Hash[
113112
years.map { |year| year.to_s }.zip(
114-
years.map { |year| year_link_text.gsub(/%Y/, '%Y' => year.to_s) }
113+
years.map { |year| insert_date(year_link_text, year) }
115114
)
116115
]
117116
end

0 commit comments

Comments
 (0)