Merge pull request #67 from matt-hh/master

Tags for blog feature
This commit is contained in:
Matthias Döring 2011-06-27 00:09:36 -07:00
commit 44894f9b74

View file

@ -4,23 +4,27 @@ module Middleman
module Features module Features
module Blog module Blog
class << self class << self
def registered(app) def registered(app)
# Include helpers # Include helpers
app.helpers Middleman::Features::Blog::Helpers app.helpers Middleman::Features::Blog::Helpers
app.after_feature_init do app.after_feature_init do
if !app.settings.respond_to? :blog_permalink if !app.settings.respond_to? :blog_permalink
app.set :blog_permalink, "/:year/:month/:day/:title.html" app.set :blog_permalink, "/:year/:month/:day/:title.html"
end end
if !app.settings.respond_to? :blog_taglink
app.set :blog_taglink, "/tags/:tag.html"
end
if !app.settings.respond_to? :blog_layout if !app.settings.respond_to? :blog_layout
app.set :blog_layout, "layout" app.set :blog_layout, "layout"
end end
if !app.settings.respond_to? :blog_summary_separator if !app.settings.respond_to? :blog_summary_separator
app.set :blog_summary_separator, /READMORE/ app.set :blog_summary_separator, /READMORE/
end end
if !app.settings.respond_to? :blog_summary_length if !app.settings.respond_to? :blog_summary_length
app.set :blog_summary_length, 250 app.set :blog_summary_length, 250
end end
@ -32,44 +36,60 @@ module Middleman
if !app.settings.respond_to? :blog_index_template if !app.settings.respond_to? :blog_index_template
app.set :blog_index_template, "index_template" app.set :blog_index_template, "index_template"
end end
if !app.settings.respond_to? :blog_article_template if !app.settings.respond_to? :blog_article_template
app.set :blog_article_template, "article_template" app.set :blog_article_template, "article_template"
end end
$stderr.puts "== Blog: #{app.settings.blog_permalink}" $stderr.puts "== Blog: #{app.settings.blog_permalink}"
articles_glob = File.join(app.views, app.settings.blog_permalink.gsub(/(:\w+)/, "*") + ".*") articles_glob = File.join(app.views, app.settings.blog_permalink.gsub(/(:\w+)/, "*") + ".*")
articles = Dir[articles_glob].map do |article| articles = Dir[articles_glob].map do |article|
template_content = File.read(article) template_content = File.read(article)
data, content = parse_front_matter(template_content) data, content = parse_front_matter(template_content)
data["date"] = Date.parse(data["date"]) data["date"] = Date.parse(data["date"])
yaml_regex = /^(---\s*\n.*?\n?)^(---\s*$\n?)/m yaml_regex = /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
data["raw"] = template_content.split(yaml_regex).last data["raw"] = template_content.split(yaml_regex).last
data["url"] = article.gsub(app.views, "").split(".html").first + ".html" data["url"] = article.gsub(app.views, "").split(".html").first + ".html"
all_content = Tilt.new(article).render all_content = Tilt.new(article).render
data["body"] = all_content.gsub!(app.settings.blog_summary_separator, "") data["body"] = all_content.gsub!(app.settings.blog_summary_separator, "")
sum = if data["raw"] =~ app.settings.blog_summary_separator sum = if data["raw"] =~ app.settings.blog_summary_separator
data["raw"].split(app.settings.blog_summary_separator).first 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 end
engine = RDiscount.new(sum) engine = RDiscount.new(sum)
data["summary"] = engine.to_html data["summary"] = engine.to_html
data data
end.sort { |a, b| b["date"] <=> a["date"] } 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 app.get(app.settings.blog_permalink) do
options = {} options = {}
options[:layout] = settings.blog_layout options[:layout] = settings.blog_layout
options[:layout_engine] = settings.blog_layout_engine options[:layout_engine] = settings.blog_layout_engine
extensionless_path, template_engine = resolve_template(request.path) extensionless_path, template_engine = resolve_template(request.path)
full_file_path = "#{extensionless_path}.#{template_engine}" full_file_path = "#{extensionless_path}.#{template_engine}"
@ -78,40 +98,70 @@ module Middleman
# Forward remaining data to helpers # Forward remaining data to helpers
self.class.data_content("page", data) self.class.data_content("page", data)
output = render(request.path, options) output = render(request.path, options)
# No need for separator on permalink page # No need for separator on permalink page
output.gsub!(settings.blog_summary_separator, "") output.gsub!(settings.blog_summary_separator, "")
status 200 status 200
output output
end end
end end
end end
alias :included :registered alias :included :registered
end end
module Helpers module Helpers
def is_blog_article? def is_blog_article?
!current_article_title.blank? !current_article_title.blank?
end end
def blog_title def blog_title
end end
def current_article_date def current_article_date
DateTime.parse(current_article_metadata.date) DateTime.parse(current_article_metadata.date)
end end
def current_article_title def current_article_title
current_article_metadata.title current_article_metadata.title
end end
def current_article_metadata def current_article_metadata
data.page data.page
end 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 end
end end