Simple YAML front-matter implementation. Needs tests and to teach Tilt to ignore the YAML

This commit is contained in:
Thomas Reynolds 2011-06-05 18:59:04 -07:00
parent 89ccb10452
commit 5ded17c775
4 changed files with 58 additions and 7 deletions

View file

@ -1,4 +1,5 @@
2.0.0
=====
- Combine views/ and public/ into a single source/ folder.
- Combine views/ and public/ into a single source/ folder.
- Support YAML front-matter

View file

@ -0,0 +1,7 @@
---
layout: false
title: No title
---
<%= data.page.title %>
<%= data.page.inspect %>

View file

@ -25,14 +25,18 @@ module Middleman::Features::Data
def method_missing(path)
response = nil
@@local_sources ||= {}
@@remote_sources ||= {}
if @@remote_sources.has_key?(path.to_s)
response = HTTParty.get(@@remote_sources[path.to_s]).parsed_response
end
file_path = File.join(@app.class.root, "data", "#{path}.yml")
if File.exists? file_path
response = YAML.load_file(file_path)
if @@local_sources.has_key?(path.to_s)
response = @@local_sources[path.to_s]
elsif @@remote_sources.has_key?(path.to_s)
response = HTTParty.get(@@remote_sources[path.to_s]).parsed_response
else
file_path = File.join(@app.class.root, "data", "#{path}.yml")
if File.exists? file_path
response = YAML.load_file(file_path)
end
end
if response
@ -44,6 +48,11 @@ module Middleman::Features::Data
@@remote_sources ||= {}
@@remote_sources[name.to_s] = json_url
end
def self.data_content(name, content)
@@local_sources ||= {}
@@local_sources[name.to_s] = content
end
private
def recursively_enhance(data)
@ -75,5 +84,9 @@ module Middleman::Features::Data
def data_source(name, url)
Middleman::Features::Data::DataObject.add_source(name, url)
end
def data_content(name, content)
Middleman::Features::Data::DataObject.data_content(name, content)
end
end
end

View file

@ -138,6 +138,23 @@ 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={})
@ -153,6 +170,19 @@ module Middleman
return
end
full_file_path = "#{extensionless_path}.#{template_engine}"
data, content = self.class.parse_front_matter(full_file_path)
%w(layout layout_engine).each do |opt|
if data.has_key?(opt)
options[opt.to_sym] = data.delete(opt)
end
end
# Forward remaining data to helpers
self.class.data_content("page", data)
old_layout = settings.current_layout
settings.layout(options[:layout]) if !options[:layout].nil?
layout = settings.fetch_layout_path.to_sym