mirror of
https://github.com/middleman/middleman.git
synced 2022-11-09 12:20:27 -05:00
Simple YAML front-matter implementation. Needs tests and to teach Tilt to ignore the YAML
This commit is contained in:
parent
89ccb10452
commit
5ded17c775
4 changed files with 58 additions and 7 deletions
|
@ -2,3 +2,4 @@
|
|||
=====
|
||||
|
||||
- Combine views/ and public/ into a single source/ folder.
|
||||
- Support YAML front-matter
|
7
fixtures/test-app/source/front-matter.html.erb
Normal file
7
fixtures/test-app/source/front-matter.html.erb
Normal file
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
layout: false
|
||||
title: No title
|
||||
---
|
||||
|
||||
<%= data.page.title %>
|
||||
<%= data.page.inspect %>
|
|
@ -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
|
||||
|
@ -45,6 +49,11 @@ module Middleman::Features::Data
|
|||
@@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)
|
||||
if data.is_a? Hash
|
||||
|
@ -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
|
|
@ -139,6 +139,23 @@ module Middleman
|
|||
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={})
|
||||
# Normalize the path and add index if we're looking at a directory
|
||||
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue