frontmatter test

This commit is contained in:
Thomas Reynolds 2011-07-07 16:30:30 -07:00
parent edb60cd83a
commit ef51434146
5 changed files with 62 additions and 61 deletions

View file

@ -1,6 +1,8 @@
2.0.0 2.0.0
===== =====
- Use sinatra before/after hooks for Blog and FrontMatter
- Converted a lot of Middleman::Server in CoreExtensions
- Combine views/ and public/ into a single source/ folder. - Combine views/ and public/ into a single source/ folder.
- Support YAML front-matter - Support YAML front-matter
- Blog-aware - Blog-aware

View file

@ -0,0 +1,7 @@
Feature: YAML Front Matter
In order to specific options and data inline
Scenario: Rendering html
Given the Server is running
When I go to "/front-matter.html"
Then I should see "<h1>This is the title</h1>"

View file

@ -14,7 +14,8 @@ module Middleman::CoreExtensions::FrontMatter
app.before do app.before do
result = resolve_template(request.path_info, :raise_exceptions => false) result = resolve_template(request.path_info, :raise_exceptions => false)
if result
if result && Tilt.mappings.has_key?(result[1].to_s)
extensionless_path, template_engine = result extensionless_path, template_engine = result
full_file_path = "#{extensionless_path}.#{template_engine}" full_file_path = "#{extensionless_path}.#{template_engine}"
system_path = File.join(settings.views, full_file_path) system_path = File.join(settings.views, full_file_path)

View file

@ -10,11 +10,11 @@ module Middleman
app.after_feature_init do app.after_feature_init do
if !app.settings.respond_to? :blog_permalink if !app.settings.respond_to? :blog_permalink
app.set :blog_permalink, "/:year/:month/:day/:title.html" app.set :blog_permalink, ":year/:month/:day/:title.html"
end end
if !app.settings.respond_to? :blog_taglink if !app.settings.respond_to? :blog_taglink
app.set :blog_taglink, "/tags/:tag.html" app.set :blog_taglink, "tags/:tag.html"
end end
if !app.settings.respond_to? :blog_layout if !app.settings.respond_to? :blog_layout
@ -43,15 +43,15 @@ module Middleman
$stderr.puts "== Blog: #{app.settings.blog_permalink}" $stderr.puts "== Blog: #{app.settings.blog_permalink}"
app.before do
articles_glob = File.join(app.views, app.settings.blog_permalink.gsub(/(:\w+)/, "*") + ".*") articles_glob = File.join(app.views, app.settings.blog_permalink.gsub(/(:\w+)/, "*") + ".*")
articles = Dir[articles_glob].map do |article| articles = Dir[articles_glob].map do |article|
template_content = File.read(article) template_content = File.read(article)
data, content = parse_front_matter(template_content) data, content = app.parse_front_matter(template_content)
data["date"] = Date.parse(data["date"]) data["date"] = Date.parse(data["date"])
yaml_regex = /^(---\s*\n.*?\n?)^(---\s*$\n?)/m data["raw"] = content
data["raw"] = template_content.split(yaml_regex).last
data["url"] = article.gsub(app.views, "").split(".html").first + ".html" data["url"] = article.gsub(app.views, "").split(".html").first + ".html"
all_content = Tilt.new(article).render all_content = Tilt.new(article).render
@ -84,30 +84,17 @@ module Middleman
end end
app.data_content("blog", { :articles => articles, :tags => tags }) app.data_content("blog", { :articles => articles, :tags => tags })
app.get(app.settings.blog_permalink) do
options = {}
options[:layout] = settings.blog_layout
options[:layout_engine] = settings.blog_layout_engine
extensionless_path, template_engine = resolve_template(request.path)
full_file_path = "#{extensionless_path}.#{template_engine}"
system_path = File.join(settings.views, full_file_path)
data, content = self.class.parse_front_matter(File.read(system_path))
# Forward remaining data to helpers
self.class.data_content("page", data)
output = render(request.path, options)
# No need for separator on permalink page
output.gsub!(settings.blog_summary_separator, "")
status 200
output
end end
app.get(app.settings.blog_permalink) do
process_request({
:layout => settings.blog_layout,
:layout_engine => settings.blog_layout_engine
})
# No need for separator on permalink page
body body.gsub!(settings.blog_summary_separator, "")
end
end end
end end
alias :included :registered alias :included :registered

View file

@ -82,16 +82,20 @@ module Middleman
content_type mime_type(File.extname(request.path_info)), :charset => 'utf-8' content_type mime_type(File.extname(request.path_info)), :charset => 'utf-8'
status 200 status 200
send_file File.join(Middleman::Server.views, request.path_info) send_file File.join(Middleman::Server.views, request.path_info)
request["already_sent"] = true
end end
else else
$stderr.puts "File not found: #{request.path_info}" $stderr.puts "File not found: #{request.path_info}"
status 404 status 404
request["already_sent"] = true
end end
end end
private private
# 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={})
return if request["already_sent"]
options.merge!(request['custom_options'] || {}) options.merge!(request['custom_options'] || {})
old_layout = settings.current_layout old_layout = settings.current_layout
@ -107,10 +111,10 @@ module Middleman
if result if result
content_type mime_type(File.extname(request.path_info)), :charset => 'utf-8' content_type mime_type(File.extname(request.path_info)), :charset => 'utf-8'
status 200 status 200
return result body result
end else
status 404 status 404
end end
end end
end
end end