more robust frontmatter impl, still need to tie into Tilt

This commit is contained in:
Thomas Reynolds 2011-06-06 11:39:57 -07:00
parent 80e120060b
commit 9849e2aa2f
3 changed files with 46 additions and 18 deletions

View file

@ -71,6 +71,9 @@ module Middleman::Features
# to dynamic requests. # to dynamic requests.
autoload :Data, "middleman/features/data" autoload :Data, "middleman/features/data"
# Parse YAML metadata from templates
autoload :FrontMatter, "middleman/features/front_matter"
# Proxy web services requests in dev mode only # Proxy web services requests in dev mode only
# autoload :Proxy, "middleman/features/proxy" # autoload :Proxy, "middleman/features/proxy"

View file

@ -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

View file

@ -40,6 +40,9 @@ module Middleman
# Activate custom features # Activate custom features
register Middleman::Features register Middleman::Features
# Activate Yaml Front Matter
register Middleman::Features::FrontMatter
# Activate built-in helpers # Activate built-in helpers
register Middleman::Features::DefaultHelpers register Middleman::Features::DefaultHelpers
@ -139,23 +142,6 @@ module Middleman
path.gsub(%r{^/}, '') path.gsub(%r{^/}, '')
end 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 # Internal method to look for templates and evaluate them if found
def process_request(options={}) def process_request(options={})
# Normalize the path and add index if we're looking at a directory # Normalize the path and add index if we're looking at a directory
@ -172,7 +158,8 @@ module Middleman
full_file_path = "#{extensionless_path}.#{template_engine}" 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| %w(layout layout_engine).each do |opt|
if data.has_key?(opt) if data.has_key?(opt)