diff --git a/lib/middleman/features.rb b/lib/middleman/features.rb index b448c6f0..dbc7c701 100644 --- a/lib/middleman/features.rb +++ b/lib/middleman/features.rb @@ -71,6 +71,9 @@ module Middleman::Features # to dynamic requests. autoload :Data, "middleman/features/data" + # Parse YAML metadata from templates + autoload :FrontMatter, "middleman/features/front_matter" + # Proxy web services requests in dev mode only # autoload :Proxy, "middleman/features/proxy" diff --git a/lib/middleman/features/front_matter.rb b/lib/middleman/features/front_matter.rb new file mode 100644 index 00000000..974fc617 --- /dev/null +++ b/lib/middleman/features/front_matter.rb @@ -0,0 +1,38 @@ +require "yaml" +require "tilt" + +module Middleman::Features::FrontMatter + class << self + def registered(app) + app.extend ClassMethods + end + alias :included :registered + end + + module ClassMethods + def parse_front_matter(content) + yaml_regex = /^(---\s*\n.*?\n?)^(---\s*$\n?)/m + if content =~ yaml_regex + begin + data = YAML.load($1) + rescue => e + puts "YAML Exception: #{e.message}" + end + + content = content.split(yaml_regex).last + end + + data ||= {} + [data, content] + end + 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 diff --git a/lib/middleman/server.rb b/lib/middleman/server.rb index 57af3d0a..77c4c878 100644 --- a/lib/middleman/server.rb +++ b/lib/middleman/server.rb @@ -40,6 +40,9 @@ module Middleman # Activate custom features register Middleman::Features + # Activate Yaml Front Matter + register Middleman::Features::FrontMatter + # Activate built-in helpers register Middleman::Features::DefaultHelpers @@ -138,23 +141,6 @@ module Middleman end path.gsub(%r{^/}, '') end - - def self.parse_front_matter(path) - content = File.read(File.join(settings.views, path)) - - if content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m - content = $POSTMATCH - - begin - data = YAML.load($1) - rescue => e - puts "YAML Exception: #{e.message}" - end - end - - data ||= {} - [data, content] - end # Internal method to look for templates and evaluate them if found def process_request(options={}) @@ -172,7 +158,8 @@ module Middleman full_file_path = "#{extensionless_path}.#{template_engine}" - data, content = self.class.parse_front_matter(full_file_path) + system_path = File.join(settings.views, full_file_path) + data, content = self.class.parse_front_matter(File.read(system_path)) %w(layout layout_engine).each do |opt| if data.has_key?(opt)