From 5ded17c775cbf6f06a381deb4af7d5f3267d18d1 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Sun, 5 Jun 2011 18:59:04 -0700 Subject: [PATCH] Simple YAML front-matter implementation. Needs tests and to teach Tilt to ignore the YAML --- CHANGELOG | 3 +- .../test-app/source/front-matter.html.erb | 7 +++++ lib/middleman/features/data.rb | 25 ++++++++++++---- lib/middleman/server.rb | 30 +++++++++++++++++++ 4 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 fixtures/test-app/source/front-matter.html.erb diff --git a/CHANGELOG b/CHANGELOG index baea23df..a87c6452 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,5 @@ 2.0.0 ===== -- Combine views/ and public/ into a single source/ folder. \ No newline at end of file +- Combine views/ and public/ into a single source/ folder. +- Support YAML front-matter \ No newline at end of file diff --git a/fixtures/test-app/source/front-matter.html.erb b/fixtures/test-app/source/front-matter.html.erb new file mode 100644 index 00000000..15136dad --- /dev/null +++ b/fixtures/test-app/source/front-matter.html.erb @@ -0,0 +1,7 @@ +--- +layout: false +title: No title +--- + +<%= data.page.title %> +<%= data.page.inspect %> \ No newline at end of file diff --git a/lib/middleman/features/data.rb b/lib/middleman/features/data.rb index d0ba03df..f6160ae0 100755 --- a/lib/middleman/features/data.rb +++ b/lib/middleman/features/data.rb @@ -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 \ No newline at end of file diff --git a/lib/middleman/server.rb b/lib/middleman/server.rb index 78861320..57af3d0a 100644 --- a/lib/middleman/server.rb +++ b/lib/middleman/server.rb @@ -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