JSON frontmatter and allowing frontmatter to be located after a encoding value

This commit is contained in:
Thomas Reynolds 2012-05-09 13:15:39 -07:00
parent f17b19d9de
commit 162622c453
7 changed files with 77 additions and 7 deletions

View file

@ -1,8 +1,11 @@
Feature: YAML Front Matter
In order to specific options and data inline
Scenario: Rendering html
Scenario: Rendering html (yaml)
Given the Server is running at "frontmatter-app"
When I go to "/front-matter-auto.html"
Then I should see "<h1>This is the title</h1>"
Then I should not see "---"
When I go to "/front-matter.html"
Then I should see "<h1>This is the title</h1>"
Then I should not see "---"
@ -10,6 +13,19 @@ Feature: YAML Front Matter
Then I should see "<h1>This is the title</h1>"
Then I should see "<?php"
Then I should not see "---"
Scenario: Rendering html (json)
Given the Server is running at "frontmatter-app"
When I go to "/json-front-matter-auto.html"
Then I should see "<h1>This is the title</h1>"
Then I should not see "{"
When I go to "/json-front-matter.html"
Then I should see "<h1>This is the title</h1>"
Then I should not see "{"
When I go to "/json-front-matter-2.php"
Then I should see "<h1>This is the title</h1>"
Then I should see "<?php"
Then I should not see "{"
Scenario: A template changes frontmatter during preview
Given the Server is running at "frontmatter-app"

View file

@ -0,0 +1,6 @@
---
layout: false
title: This is the title
---
<h1><%= data.page.title %></h1>

View file

@ -1,3 +1,5 @@
<h2> Test</h2>
---
layout: false
title: This is the title

View file

@ -0,0 +1,7 @@
{{{
"layout": false,
"title": "This is the title"
}}}
<h1><%= data.page.title %></h1>
<?php echo "sup"; ?>

View file

@ -0,0 +1,6 @@
{{{
"layout": false,
"title": "This is the title"
}}}
<h1><%= data.page.title %></h1>

View file

@ -0,0 +1,6 @@
{{{
"layout": false,
"title": "This is the title"
}}}
<h1><%= data.page.title %></h1>

View file

@ -11,6 +11,9 @@ module Middleman::CoreExtensions
def registered(app)
# Parsing YAML frontmatter
require "yaml"
# Parsing JSON frontmatter
require "active_support/json"
app.after_configuration do
::Middleman::Sitemap::Resource.send :include, ResourceInstanceMethods
@ -56,13 +59,13 @@ module Middleman::CoreExtensions
@cache.delete(p)
end
# Parse frontmatter out of a string
# Parse YAML frontmatter out of a string
# @param [String] content
# @return [Array<Hash, String>]
def parse_front_matter(content)
def parse_yaml_front_matter(content)
yaml_regex = /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
if content =~ yaml_regex
content = content[($1.size + $2.size)..-1]
content = content.sub(yaml_regex, "")
begin
data = YAML.load($1)
@ -80,6 +83,29 @@ module Middleman::CoreExtensions
[{}, content]
end
def parse_json_front_matter(content)
json_regex = /^(\{\{\{\s*\n.*?\n?)^(\}\}\}\s*$\n?)/m
if content =~ json_regex
content = content.sub(json_regex, "")
begin
json = ($1+$2).sub("{{{", "{").sub("}}}", "}")
data = ActiveSupport::JSON.decode(json)
rescue => e
puts "JSON Exception: #{e.message}"
return false
end
else
return false
end
[data, content]
rescue
[{}, content]
end
# Get the frontmatter and plain content from a file
# @param [String] path
# @return [Array<Thor::CoreExt::HashWithIndifferentAccess, String>]
@ -87,9 +113,10 @@ module Middleman::CoreExtensions
full_path = File.expand_path(path, @app.source_dir)
content = File.read(full_path)
result = parse_front_matter(content)
if result
if result = parse_yaml_front_matter(content)
data, content = result
data = ::Middleman::Util.recursively_enhance(data).freeze
elsif result = parse_json_front_matter(content)
data, content = result
data = ::Middleman::Util.recursively_enhance(data).freeze
else