From 746ddcc519edb8b0364fc9130c07b2b90da7f15f Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Tue, 7 Jun 2011 21:44:10 -0700 Subject: [PATCH] blog feature --- lib/middleman/features.rb | 3 + lib/middleman/features/blog.rb | 87 ++++++++++++++++++++++++++ lib/middleman/features/front_matter.rb | 49 ++++++++++++--- lib/middleman/server.rb | 4 -- 4 files changed, 130 insertions(+), 13 deletions(-) create mode 100644 lib/middleman/features/blog.rb diff --git a/lib/middleman/features.rb b/lib/middleman/features.rb index dbc7c701..a0f205b5 100644 --- a/lib/middleman/features.rb +++ b/lib/middleman/features.rb @@ -74,6 +74,9 @@ module Middleman::Features # Parse YAML metadata from templates autoload :FrontMatter, "middleman/features/front_matter" + # Treat project as a blog + autoload :Blog, "middleman/features/blog" + # Proxy web services requests in dev mode only # autoload :Proxy, "middleman/features/proxy" diff --git a/lib/middleman/features/blog.rb b/lib/middleman/features/blog.rb new file mode 100644 index 00000000..ec54550c --- /dev/null +++ b/lib/middleman/features/blog.rb @@ -0,0 +1,87 @@ +module Middleman + module Features + module Blog + class << self + def registered(app) + # Depend on FrontMatter + app.activate Middleman::Features::FrontMatter + + # Include helpers + app.helpers Middleman::Features::Blog::Helpers + + app.after_feature_init do + if !app.settings.respond_to? :blog_permalink + app.set :blog_permalink, "/:year/:month/:day/:title.html" + end + + if !app.settings.respond_to? :blog_layout + app.set :blog_layout, "layout" + end + + if !app.settings.respond_to? :blog_summary_separator + app.set :blog_summary_separator, "READMORE" + end + + if !app.settings.respond_to? :blog_layout_engine + app.set :blog_layout_engine, "erb" + end + + if !app.settings.respond_to? :blog_article_template + app.set :blog_article_template, "article_template" + end + + $stderr.puts "== Blog: #{app.settings.blog_permalink}" + app.get(app.settings.blog_permalink) do + options = {} + options[:layout] = settings.blog_layout + options[:layout_engine] = settings.blog_layout_engine + + extensionless_path, template_engine = resolve_template(request.path) + + full_file_path = "#{extensionless_path}.#{template_engine}" + system_path = File.join(settings.views, full_file_path) + data, content = self.class.parse_front_matter(File.read(system_path)) + + # Forward remaining data to helpers + self.class.data_content("page", data) + + output = render(request.path, options) + + # No need for separator on permalink page + output.gsub!(settings.blog_summary_separator, "") + + status 200 + output + 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 blog_title + end + + def current_article_date + DateTime.parse(current_article_metadata.date) + end + + def current_article_title + current_article_metadata.title + end + + def current_article_metadata + data.page + end + end + end + end +end \ No newline at end of file diff --git a/lib/middleman/features/front_matter.rb b/lib/middleman/features/front_matter.rb index 974fc617..0db76884 100644 --- a/lib/middleman/features/front_matter.rb +++ b/lib/middleman/features/front_matter.rb @@ -5,6 +5,13 @@ module Middleman::Features::FrontMatter class << self def registered(app) app.extend ClassMethods + + ::Tilt::register MarukuTemplate, 'markdown', 'mkd', 'md' + ::Tilt::register KramdownTemplate, 'markdown', 'mkd', 'md' + ::Tilt::register BlueClothTemplate, 'markdown', 'mkd', 'md' + ::Tilt::register RedcarpetTemplate, 'markdown', 'mkd', 'md' + ::Tilt::register RDiscountTemplate, 'markdown', 'mkd', 'md' + ::Tilt::register RedClothTemplate, 'textile' end alias :included :registered end @@ -26,13 +33,37 @@ module Middleman::Features::FrontMatter [data, content] end end -end + + module YamlAware + def prepare + options, @data = Middleman::Server.parse_front_matter(@data) + super + end + end -# class FrontMatter < Tilt::RDiscountTemplate -# def prepare -# options, @data = Middleman::Server.parse_front_matter(@data) -# super -# end -# end -# -# Tilt.register 'markdown', FrontMatter \ No newline at end of file + # MARKDOWN + class MarukuTemplate < ::Tilt::MarukuTemplate + include Middleman::Features::FrontMatter::YamlAware + end + + class KramdownTemplate < ::Tilt::KramdownTemplate + include Middleman::Features::FrontMatter::YamlAware + end + + class BlueClothTemplate < ::Tilt::BlueClothTemplate + include Middleman::Features::FrontMatter::YamlAware + end + + class RedcarpetTemplate < ::Tilt::RedcarpetTemplate + include Middleman::Features::FrontMatter::YamlAware + end + + class RDiscountTemplate < ::Tilt::RDiscountTemplate + include Middleman::Features::FrontMatter::YamlAware + end + + # TEXTILE + class RedClothTemplate < ::Tilt::RedClothTemplate + include Middleman::Features::FrontMatter::YamlAware + end +end \ No newline at end of file diff --git a/lib/middleman/server.rb b/lib/middleman/server.rb index 77c4c878..75a66638 100644 --- a/lib/middleman/server.rb +++ b/lib/middleman/server.rb @@ -40,9 +40,6 @@ module Middleman # Activate custom features register Middleman::Features - # Activate Yaml Front Matter - register Middleman::Features::FrontMatter - # Activate built-in helpers register Middleman::Features::DefaultHelpers @@ -156,7 +153,6 @@ module Middleman return end - full_file_path = "#{extensionless_path}.#{template_engine}" system_path = File.join(settings.views, full_file_path) data, content = self.class.parse_front_matter(File.read(system_path))