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,71 +43,58 @@ module Middleman
$stderr.puts "== Blog: #{app.settings.blog_permalink}" $stderr.puts "== Blog: #{app.settings.blog_permalink}"
articles_glob = File.join(app.views, app.settings.blog_permalink.gsub(/(:\w+)/, "*") + ".*") app.before do
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
data["body"] = all_content.gsub!(app.settings.blog_summary_separator, "") data["body"] = all_content.gsub!(app.settings.blog_summary_separator, "")
sum = if data["raw"] =~ app.settings.blog_summary_separator sum = if data["raw"] =~ app.settings.blog_summary_separator
data["raw"].split(app.settings.blog_summary_separator).first data["raw"].split(app.settings.blog_summary_separator).first
else else
data["raw"].match(/(.{1,#{app.settings.blog_summary_length}}.*?)(\n|\Z)/m).to_s data["raw"].match(/(.{1,#{app.settings.blog_summary_length}}.*?)(\n|\Z)/m).to_s
end end
engine = RDiscount.new(sum) engine = RDiscount.new(sum)
data["summary"] = engine.to_html data["summary"] = engine.to_html
data data
end.sort { |a, b| b["date"] <=> a["date"] } end.sort { |a, b| b["date"] <=> a["date"] }
tags = {} tags = {}
articles.each do |article| articles.each do |article|
article["tags"] ||= "" article["tags"] ||= ""
if !article["tags"].empty? if !article["tags"].empty?
tags_array = article["tags"].split(',').map{|t| t.strip} tags_array = article["tags"].split(',').map{|t| t.strip}
tags_array.each do |tag_title| tags_array.each do |tag_title|
tag_key = tag_title.parameterize tag_key = tag_title.parameterize
tag_path = blog_taglink.gsub(/(:\w+)/, tag_key) tag_path = blog_taglink.gsub(/(:\w+)/, tag_key)
(tags[tag_path] ||= {})["title"] = tag_title (tags[tag_path] ||= {})["title"] = tag_title
tags[tag_path]["ident"] = tag_key tags[tag_path]["ident"] = tag_key
(tags[tag_path]["pages"] ||= {})[article["title"]] = article["url"] (tags[tag_path]["pages"] ||= {})[article["title"]] = article["url"]
end
end end
end end
end
app.data_content("blog", { :articles => articles, :tags => tags }) app.data_content("blog", { :articles => articles, :tags => tags })
end
app.get(app.settings.blog_permalink) do app.get(app.settings.blog_permalink) do
options = {} process_request({
options[:layout] = settings.blog_layout :layout => settings.blog_layout,
options[:layout_engine] = settings.blog_layout_engine :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 # No need for separator on permalink page
output.gsub!(settings.blog_summary_separator, "") body body.gsub!(settings.blog_summary_separator, "")
status 200
output
end 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
else
status 404
end end
status 404
end end
end end
end end