Support sideloading from .frontmatter files. Closes #855

This commit is contained in:
Thomas Reynolds 2013-04-12 16:14:16 -07:00
parent a0445e405a
commit 0574cc3d28
34 changed files with 245 additions and 2 deletions

View file

@ -1,6 +1,7 @@
master master
=== ===
* Allow frontmatter to be side-loaded from a neighboring file with a .frontmatter extension: #855
* Allow frontmatter "renderer_options" key to overwrite renderer options on a per-file basis. #859 * Allow frontmatter "renderer_options" key to overwrite renderer options on a per-file basis. #859
* Added a "t" helper that delegates to I18n.t, just like Rails. #853. * Added a "t" helper that delegates to I18n.t, just like Rails. #853.
* I18n will fall back to the default locale if a translation in the current locale is not found. You can disable this behavior by passing `:no_fallbacks => true` when activating `:i18n`. More settings documented at https://github.com/svenfuchs/i18n/wiki/Fallbacks . #853 * I18n will fall back to the default locale if a translation in the current locale is not found. You can disable this behavior by passing `:no_fallbacks => true` when activating `:i18n`. More settings documented at https://github.com/svenfuchs/i18n/wiki/Fallbacks . #853

View file

@ -21,6 +21,9 @@ gem "liquid", :require => false
gem "less", :require => false gem "less", :require => false
gem "stylus", :require => false gem "stylus", :require => false
gem "pry"
gem "pry-debugger"
platforms :ruby do platforms :ruby do
gem "therubyracer" gem "therubyracer"
gem "redcarpet" gem "redcarpet"

View file

@ -0,0 +1,151 @@
Feature: Neighboring YAML Front Matter
Scenario: Rendering html (yaml)
Given the Server is running at "frontmatter-neighbor-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-auto.erb.frontmatter"
Then I should see "File Not Found"
When I go to "/front-matter-2.php"
Then I should see "<h1>This is the title</h1>"
Then I should see "<?php"
Then I should not see "---"
When I go to "/front-matter-2.php.erb.frontmatter"
Then I should see "File Not Found"
Scenario: Rendering raw (template-less) (yaml)
Given the Server is running at "frontmatter-neighbor-app"
When I go to "/raw-front-matter.html"
Then I should see "<h1><%= data.page.title %></h1>"
Then I should not see "---"
When I go to "/raw-front-matter.html.frontmatter"
Then I should see "File Not Found"
When I go to "/raw-front-matter.php"
Then I should see '<?php echo "sup"; ?>'
Then I should see "<?php"
Then I should not see "---"
When I go to "/raw-front-matter.php.frontmatter"
Then I should see "File Not Found"
Scenario: YAML not on first line, with encoding
Given the Server is running at "frontmatter-neighbor-app"
When I go to "/front-matter-encoding.html"
Then I should see "<h1>This is the title</h1>"
Then I should not see "---"
When I go to "/front-matter-encoding.html.erb.frontmatter"
Then I should see "File Not Found"
Scenario: Rendering html (json)
Given the Server is running at "frontmatter-neighbor-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-auto.erb.frontmatter"
Then I should see "File Not Found"
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.html.erb.frontmatter"
Then I should see "File Not Found"
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 ";;;"
When I go to "/json-front-matter-2.php.erb.frontmatter"
Then I should see "File Not Found"
Scenario: A template changes frontmatter during preview
Given the Server is running at "frontmatter-neighbor-app"
And the file "source/front-matter-change.html.erb" has the contents
"""
<%= data.page.title %>
"""
And the file "source/front-matter-change.html.erb.frontmatter" has the contents
"""
---
title: Hello World
layout: false
---
"""
When I go to "/front-matter-change.html"
Then I should see "Hello World"
And the file "source/front-matter-change.html.erb.frontmatter" has the contents
"""
---
title: Hola Mundo
layout: false
---
"""
When I go to "/front-matter-change.html"
Then I should see "Hola Mundo"
When I go to "/front-matter-change.html.erb.frontmatter"
Then I should see "File Not Found"
Scenario: A template should handle an empty YAML feed
Given the Server is running at "frontmatter-neighbor-app"
And the file "source/front-matter-change.html.erb.frontmatter" has the contents
"""
---
---
"""
When I go to "/front-matter-change.html"
Then I should not see "Hello World"
Then I should not see "Hola Mundo"
When I go to "/front-matter-change.html.erb.frontmatter"
Then I should see "File Not Found"
Scenario: Setting layout, ignoring, and disabling directory indexes through frontmatter (build)
Given a successfully built app at "frontmatter-settings-neighbor-app"
Then the following files should exist:
| build/proxied.html |
And the file "build/alternate_layout.html" should contain "Alternate layout"
And the following files should not exist:
| build/ignored.html |
| build/alternate_layout.html.erb.frontmatter |
| build/ignored.html.erb.frontmatter |
| build/override_layout.html.erb.frontmatter |
| build/page_mentioned.html.erb.frontmatter |
Scenario: Setting layout, ignoring, and disabling directory indexes through frontmatter (preview)
Given the Server is running at "frontmatter-settings-neighbor-app"
When I go to "/alternate_layout.html"
Then I should not see "File Not Found"
And I should see "Alternate layout"
When I go to "/alternate_layout.html.erb.frontmatter"
Then I should see "File Not Found"
When I go to "/ignored.html"
Then I should see "File Not Found"
When I go to "/ignored.html.erb.frontmatter"
Then I should see "File Not Found"
When I go to "/ignored/index.html"
Then I should see "File Not Found"
Scenario: Changing frontmatter in preview server
Given the Server is running at "frontmatter-settings-neighbor-app"
When I go to "/ignored.html"
Then I should see "File Not Found"
And the file "source/ignored.html.erb.frontmatter" has the contents
"""
---
ignored: false
---
"""
When I go to "/ignored.html"
Then I should see "This file ignores itself! But it can still be proxied."
When I go to "/ignored.html.erb.frontmatter"
Then I should see "File Not Found"
Scenario: Overriding layout through frontmatter
Given the Server is running at "frontmatter-settings-neighbor-app"
When I go to "/override_layout.html"
Then I should see "Layout in use: Override"
When I go to "/override_layout.html.erb.frontmatter"
Then I should see "File Not Found"
Scenario: Setting layout through frontmatter even if page is mentioned in config
Given the Server is running at "frontmatter-settings-neighbor-app"
When I go to "/page_mentioned.html"
Then I should see "Layout in use: Override"
When I go to "/page_mentioned.html.erb.frontmatter"
Then I should see "File Not Found"

View file

@ -0,0 +1,2 @@
<h1><%= data.page.title %></h1>
<?php echo "sup"; ?>

View file

@ -0,0 +1,4 @@
---
layout: false
title: This is the title
---

View file

@ -0,0 +1 @@
<h1><%= data.page.title %></h1>

View file

@ -0,0 +1,4 @@
---
layout: false
title: This is the title
---

View file

@ -0,0 +1 @@
<%= data.page.title %>

View file

@ -0,0 +1,4 @@
---
title: Hola Mundo
layout: false
---

View file

@ -0,0 +1 @@
<h1><%= data.page.title %></h1>

View file

@ -0,0 +1,5 @@
# encoding: UTF-8
---
layout: false
title: This is the title
---

View file

@ -0,0 +1,2 @@
<h1><%= data.page.title %></h1>
<?php echo "sup"; ?>

View file

@ -0,0 +1,4 @@
;;;
"layout": false,
"title": "This is the title"
;;;

View file

@ -0,0 +1 @@
<h1><%= data.page.title %></h1>

View file

@ -0,0 +1,4 @@
;;;
"layout": false,
"title": "This is the title"
;;;

View file

@ -0,0 +1 @@
<h1><%= data.page.title %></h1>

View file

@ -0,0 +1,4 @@
;;;
"layout": false,
"title": "This is the title"
;;;

View file

@ -0,0 +1 @@
<h1><%= data.page.title %></h1>

View file

@ -0,0 +1,4 @@
---
layout: false
title: This is the title
---

View file

@ -0,0 +1 @@
<?php echo "sup"; ?>

View file

@ -0,0 +1,4 @@
---
layout: false
title: This is the title
---

View file

@ -0,0 +1,4 @@
# Proxy ignored.html, which should ignore itself through a frontmatter
page 'proxied.html', :proxy => 'ignored.html'
page 'override_layout.html', :layout => :alternate
page 'page_mentioned.html'

View file

@ -0,0 +1 @@
This uses an alternate layout

View file

@ -0,0 +1,3 @@
---
layout: alternate
---

View file

@ -0,0 +1 @@
This file ignores itself! But it can still be proxied.

View file

@ -0,0 +1,3 @@
---
ignored: true
---

View file

@ -0,0 +1,3 @@
Alternate layout!
<%= yield %>

View file

@ -0,0 +1,2 @@
<%= yield %>
Override.

View file

@ -0,0 +1,3 @@
---
layout: override
---

View file

@ -28,6 +28,8 @@ module Middleman::CoreExtensions
app.after_configuration do app.after_configuration do
::Middleman::Sitemap::Resource.send :include, ResourceInstanceMethods ::Middleman::Sitemap::Resource.send :include, ResourceInstanceMethods
ignore %r{\.frontmatter$}
sitemap.provides_metadata do |path| sitemap.provides_metadata do |path|
fmdata = frontmatter_manager.data(path).first || {} fmdata = frontmatter_manager.data(path).first || {}
@ -54,7 +56,18 @@ module Middleman::CoreExtensions
def data(path) def data(path)
p = normalize_path(path) p = normalize_path(path)
@cache[p] ||= frontmatter_and_content(p) @cache[p] ||= begin
in_file = frontmatter_and_content(p)
external_file = frontmatter_and_content("#{p}.frontmatter")
return in_file if external_file.nil?
[
external_file[0].deep_merge(in_file[0]),
in_file[1]
]
end
end end
def clear_data(file) def clear_data(file)
@ -63,7 +76,7 @@ module Middleman::CoreExtensions
file = File.join(@app.root, file) file = File.join(@app.root, file)
prefix = @app.source_dir.sub(/\/$/, "") + "/" prefix = @app.source_dir.sub(/\/$/, "") + "/"
return unless file.include?(prefix) return unless file.include?(prefix)
path = file.sub(prefix, "") path = file.sub(prefix, "").sub(/\.frontmatter$/, "")
@cache.delete(path) @cache.delete(path)
end end
@ -131,6 +144,8 @@ module Middleman::CoreExtensions
path path
end end
return nil unless File.exists?(full_path)
data = {} data = {}
content = nil content = nil