forked from middleman/middleman-blog
/
extension.rb
137 lines (118 loc) · 4.27 KB
/
extension.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require 'middleman-blog/blog_data'
require 'middleman-blog/blog_article'
require 'middleman-blog/calendar_pages'
require 'middleman-blog/tag_pages'
module Middleman
module Blog
class << self
def registered(app)
app.set :blog_permalink, "/:year/:month/:day/:title.html"
app.set :blog_sources, ":year-:month-:day-:title.html"
app.set :blog_taglink, "tags/:tag.html"
app.set :blog_layout, "layout"
app.set :blog_summary_separator, /(READMORE)/
app.set :blog_summary_length, 250
app.set :blog_year_link, "/:year.html"
app.set :blog_month_link, "/:year/:month.html"
app.set :blog_day_link, "/:year/:month/:day.html"
app.set :blog_default_extension, ".markdown"
app.send :include, Helpers
app.after_configuration do
# optional: :blog_tag_template
# optional: :blog_year_template
# optional: :blog_month_template
# optional: :blog_day_template
# Allow one setting to set all the calendar templates
if respond_to? :blog_calendar_template
set :blog_year_template, blog_calendar_template
set :blog_month_template, blog_calendar_template
set :blog_day_template, blog_calendar_template
end
matcher = Regexp.escape(blog_sources).
sub(/^\//, "").
sub(":year", "(\\d{4})").
sub(":month", "(\\d{2})").
sub(":day", "(\\d{2})").
sub(":title", "(.*)")
path_matcher = /^#{matcher}/
file_matcher = /^#{source_dir}\/#{matcher}/
app.ready do
sitemap.register_resource_list_manipulator(
:blog_articles,
blog,
false
)
if defined? blog_tag_template
ignore blog_tag_template
sitemap.register_resource_list_manipulator(
:blog_tags,
TagPages.new(self),
false
)
end
if defined? blog_year_template ||
defined? blog_month_template ||
defined? blog_day_template
sitemap.register_resource_list_manipulator(
:blog_calendar,
CalendarPages.new(self),
false
)
end
sitemap.rebuild_resource_list!(:registered_new)
end
end
end
alias :included :registered
end
# Helpers for use within templates and layouts.
module Helpers
# Get the {BlogData} for this site.
# @return [BlogData]
def blog
@_blog ||= BlogData.new(self)
end
# Determine whether the currently rendering template is a blog article.
# This can be useful in layouts.
# @return [Boolean]
def is_blog_article?
!current_article.nil?
end
# Get a {Resource} with mixed in {BlogArticle} methods representing the current article.
# @return [Middleman::Sitemap::Resource]
def current_article
blog.article(current_page.path)
end
# Get a path to the given tag, based on the :blog_taglink setting.
# @param [String] tag
# @return [String]
def tag_path(tag)
blog_taglink.sub(':tag', tag.parameterize)
end
# Get a path to the given year-based calendar page, based on the :blog_year_link setting.
# @param [Number] year
# @return [String]
def blog_year_path(year)
blog_year_link.sub(':year', year.to_s)
end
# Get a path to the given month-based calendar page, based on the :blog_month_link setting.
# @param [Number] year
# @param [Number] month
# @return [String]
def blog_month_path(year, month)
blog_month_link.sub(':year', year.to_s).
sub(':month', month.to_s.rjust(2,'0'))
end
# Get a path to the given day-based calendar page, based on the :blog_day_link setting.
# @param [Number] year
# @param [Number] month
# @param [Number] day
# @return [String]
def blog_day_path(year, month, day)
blog_day_link.sub(':year', year.to_s).
sub(':month', month.to_s.rjust(2,'0')).
sub(':day', day.to_s.rjust(2,'0'))
end
end
end
end