From 6c5d75aca95894e67f8b941af4f8aaecc663b8ae Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Wed, 8 Jun 2011 20:53:46 -0700 Subject: [PATCH] working implementation, dogfooding on my own blog --- lib/middleman/features/blog.rb | 50 ++++++++++++++++++++++++++++------ middleman.gemspec | 1 + 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/lib/middleman/features/blog.rb b/lib/middleman/features/blog.rb index ec54550c..0fde10bf 100644 --- a/lib/middleman/features/blog.rb +++ b/lib/middleman/features/blog.rb @@ -1,3 +1,5 @@ +require "rdiscount" + module Middleman module Features module Blog @@ -19,18 +21,53 @@ module Middleman end if !app.settings.respond_to? :blog_summary_separator - app.set :blog_summary_separator, "READMORE" + app.set :blog_summary_separator, /READMORE/ + end + + if !app.settings.respond_to? :blog_summary_length + app.set :blog_summary_length, 250 end if !app.settings.respond_to? :blog_layout_engine app.set :blog_layout_engine, "erb" end + + if !app.settings.respond_to? :blog_index_template + app.set :blog_index_template, "index_template" + end if !app.settings.respond_to? :blog_article_template app.set :blog_article_template, "article_template" end $stderr.puts "== Blog: #{app.settings.blog_permalink}" + + articles_glob = File.join(app.views, app.settings.blog_permalink.gsub(/(:\w+)/, "*") + ".*") + + articles = Dir[articles_glob].map do |article| + template_content = File.read(article) + data, content = parse_front_matter(template_content) + data["date"] = Date.parse(data["date"]) + + yaml_regex = /^(---\s*\n.*?\n?)^(---\s*$\n?)/m + data["raw"] = template_content.split(yaml_regex).last + data["url"] = article.gsub(app.views, "").split(".html").first + ".html" + + all_content = Tilt.new(article).render + data["body"] = all_content.gsub!(app.settings.blog_summary_separator, "") + + 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 + end + + engine = RDiscount.new(sum) + data["summary"] = engine.to_html + data + end.sort { |a, b| b["date"] <=> a["date"] } + + app.data_content("blog", { :articles => articles }) + app.get(app.settings.blog_permalink) do options = {} options[:layout] = settings.blog_layout @@ -55,18 +92,15 @@ module Middleman end end - # Handle /archives/ - require "middleman/builder" - Middleman::Builder.after_run "blog_archives" do - # source_paths << File.expand_path(File.join(File.dirname(__FILE__), "middleman-slickmap", "templates")) - # tilt_template "slickmap.html.haml", File.join(Middleman::Server.build_dir, sitemap_url), { :force => true } - end - end alias :included :registered end module Helpers + def is_blog_article? + !current_article_title.blank? + end + def blog_title end diff --git a/middleman.gemspec b/middleman.gemspec index 81a35b99..ee473770 100644 --- a/middleman.gemspec +++ b/middleman.gemspec @@ -24,6 +24,7 @@ Gem::Specification.new do |s| s.add_runtime_dependency("shotgun", ["~> 0.8.0"]) s.add_runtime_dependency("thor", ["~> 0.14.0"]) s.add_runtime_dependency("tilt", ["~> 1.3.1"]) + s.add_runtime_dependency("rdiscount", ["~> 1.6.8"]) s.add_runtime_dependency("sinatra", ["~> 1.2.0"]) s.add_runtime_dependency("padrino-core", ["~> 0.9.23"]) s.add_runtime_dependency("padrino-helpers", ["~> 0.9.23"])