tags for blog entries

This commit is contained in:
Matthias Döring 2011-06-24 19:03:10 +02:00
parent 3dbfe05409
commit 352f8ec816

View file

@ -13,6 +13,10 @@ module Middleman
app.set :blog_permalink, "/:year/:month/:day/:title.html"
end
if !app.settings.respond_to? :blog_taglink
app.set :blog_taglink, "/tags/:tag.html"
end
if !app.settings.respond_to? :blog_layout
app.set :blog_layout, "layout"
end
@ -55,7 +59,8 @@ module Middleman
sum = if data["raw"] =~ app.settings.blog_summary_separator
data["raw"].split(app.settings.blog_summary_separator).first
else data["raw"].match(/(.{1,#{app.settings.blog_summary_length}}.*?)(\n|\Z)/m).to_s
else
data["raw"].match(/(.{1,#{app.settings.blog_summary_length}}.*?)(\n|\Z)/m).to_s
end
engine = RDiscount.new(sum)
@ -63,7 +68,22 @@ module Middleman
data
end.sort { |a, b| b["date"] <=> a["date"] }
app.data_content("blog", { :articles => articles })
tags = {}
articles.each do |article|
article["tags"] ||= ""
if !article["tags"].empty?
tags_array = article["tags"].split(',').map{|t| t.strip}
tags_array.each do |tag_title|
tag_key = tag_title.parameterize
tag_path = blog_taglink.gsub(/(:\w+)/, tag_key)
(tags[tag_path] ||= {})["title"] = tag_title
tags[tag_path]["ident"] = tag_key
(tags[tag_path]["pages"] ||= {})[article["title"]] = article["url"]
end
end
end
app.data_content("blog", { :articles => articles, :tags => tags })
app.get(app.settings.blog_permalink) do
options = {}
@ -87,9 +107,9 @@ module Middleman
status 200
output
end
end
end
end
alias :included :registered
end
@ -112,6 +132,36 @@ module Middleman
def current_article_metadata
data.page
end
def current_article_tags
article_tags_hash = {}
if is_blog_article? && current_article_metadata.tags
article_tags = current_article_metadata.tags.split(',').map{|t| t.strip}
article_tags.each do |tag_title|
article_tags_hash[tag_title] = self.class.blog_taglink.gsub(/(:\w+)/, tag_title.parameterize)
end
end
article_tags_hash
end
def blog_tags
data.blog.tags
end
def current_tag_data
data.blog.tags[request.path]
end
def current_tag_articles
data.blog.articles.map do |article|
article if current_tag_data.pages.has_value?(article.url)
end.compact
end
def current_tag_title
current_tag_data[:title]
end
end
end
end