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.
|
- 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,15 +25,19 @@ module Middleman::Features::Data
|
||||||
def method_missing(path)
|
def method_missing(path)
|
||||||
response = nil
|
response = nil
|
||||||
|
|
||||||
|
@@local_sources ||= {}
|
||||||
@@remote_sources ||= {}
|
@@remote_sources ||= {}
|
||||||
if @@remote_sources.has_key?(path.to_s)
|
|
||||||
response = HTTParty.get(@@remote_sources[path.to_s]).parsed_response
|
|
||||||
end
|
|
||||||
|
|
||||||
|
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")
|
file_path = File.join(@app.class.root, "data", "#{path}.yml")
|
||||||
if File.exists? file_path
|
if File.exists? file_path
|
||||||
response = YAML.load_file(file_path)
|
response = YAML.load_file(file_path)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if response
|
if response
|
||||||
recursively_enhance(response)
|
recursively_enhance(response)
|
||||||
|
@ -45,6 +49,11 @@ module Middleman::Features::Data
|
||||||
@@remote_sources[name.to_s] = json_url
|
@@remote_sources[name.to_s] = json_url
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.data_content(name, content)
|
||||||
|
@@local_sources ||= {}
|
||||||
|
@@local_sources[name.to_s] = content
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def recursively_enhance(data)
|
def recursively_enhance(data)
|
||||||
if data.is_a? Hash
|
if data.is_a? Hash
|
||||||
|
@ -75,5 +84,9 @@ module Middleman::Features::Data
|
||||||
def data_source(name, url)
|
def data_source(name, url)
|
||||||
Middleman::Features::Data::DataObject.add_source(name, url)
|
Middleman::Features::Data::DataObject.add_source(name, url)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def data_content(name, content)
|
||||||
|
Middleman::Features::Data::DataObject.data_content(name, content)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -139,6 +139,23 @@ 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
|
||||||
|
@ -153,6 +170,19 @@ module Middleman
|
||||||
return
|
return
|
||||||
end
|
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
|
old_layout = settings.current_layout
|
||||||
settings.layout(options[:layout]) if !options[:layout].nil?
|
settings.layout(options[:layout]) if !options[:layout].nil?
|
||||||
layout = settings.fetch_layout_path.to_sym
|
layout = settings.fetch_layout_path.to_sym
|
||||||
|
|
Loading…
Reference in a new issue