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.
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"

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