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
=====
- 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.
- Support YAML front-matter
- 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,19 +14,20 @@ module Middleman::CoreExtensions::FrontMatter
app.before do
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
full_file_path = "#{extensionless_path}.#{template_engine}"
system_path = File.join(settings.views, full_file_path)
data, content = app.parse_front_matter(File.read(system_path))
request['custom_options'] = {}
%w(layout layout_engine).each do |opt|
if data.has_key?(opt)
request['custom_options'][opt.to_sym] = data.delete(opt)
end
end
# Forward remaining data to helpers
app.data_content("page", data)
end

View file

@ -10,11 +10,11 @@ module Middleman
app.after_feature_init do
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
if !app.settings.respond_to? :blog_taglink
app.set :blog_taglink, "/tags/:tag.html"
app.set :blog_taglink, "tags/:tag.html"
end
if !app.settings.respond_to? :blog_layout
@ -43,71 +43,58 @@ module Middleman
$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|
template_content = File.read(article)
data, content = parse_front_matter(template_content)
data["date"] = Date.parse(data["date"])
articles = Dir[articles_glob].map do |article|
template_content = File.read(article)
data, content = app.parse_front_matter(template_content)
data["date"] = Date.parse(data["date"])
yaml_regex = /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
data["raw"] = template_content.split(yaml_regex).last
data["url"] = article.gsub(app.views, "").split(".html").first + ".html"
data["raw"] = content
data["url"] = article.gsub(app.views, "").split(".html").first + ".html"
all_content = Tilt.new(article).render
data["body"] = all_content.gsub!(app.settings.blog_summary_separator, "")
all_content = Tilt.new(article).render
data["body"] = all_content.gsub!(app.settings.blog_summary_separator, "")
sum = if data["raw"] =~ app.settings.blog_summary_separator
data["raw"].split(app.settings.blog_summary_separator).first
else
data["raw"].match(/(.{1,#{app.settings.blog_summary_length}}.*?)(\n|\Z)/m).to_s
end
sum = if data["raw"] =~ app.settings.blog_summary_separator
data["raw"].split(app.settings.blog_summary_separator).first
else
data["raw"].match(/(.{1,#{app.settings.blog_summary_length}}.*?)(\n|\Z)/m).to_s
end
engine = RDiscount.new(sum)
data["summary"] = engine.to_html
data
end.sort { |a, b| b["date"] <=> a["date"] }
engine = RDiscount.new(sum)
data["summary"] = engine.to_html
data
end.sort { |a, b| b["date"] <=> a["date"] }
tags = {}
articles.each do |article|
article["tags"] ||= ""
if !article["tags"].empty?
tags_array = article["tags"].split(',').map{|t| t.strip}
tags_array.each do |tag_title|
tag_key = tag_title.parameterize
tag_path = blog_taglink.gsub(/(:\w+)/, tag_key)
(tags[tag_path] ||= {})["title"] = tag_title
tags[tag_path]["ident"] = tag_key
(tags[tag_path]["pages"] ||= {})[article["title"]] = article["url"]
tags = {}
articles.each do |article|
article["tags"] ||= ""
if !article["tags"].empty?
tags_array = article["tags"].split(',').map{|t| t.strip}
tags_array.each do |tag_title|
tag_key = tag_title.parameterize
tag_path = blog_taglink.gsub(/(:\w+)/, tag_key)
(tags[tag_path] ||= {})["title"] = tag_title
tags[tag_path]["ident"] = tag_key
(tags[tag_path]["pages"] ||= {})[article["title"]] = article["url"]
end
end
end
app.data_content("blog", { :articles => articles, :tags => tags })
end
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)
process_request({
:layout => settings.blog_layout,
:layout_engine => settings.blog_layout_engine
})
# No need for separator on permalink page
output.gsub!(settings.blog_summary_separator, "")
status 200
output
body body.gsub!(settings.blog_summary_separator, "")
end
end
end
alias :included :registered

View file

@ -82,16 +82,20 @@ module Middleman
content_type mime_type(File.extname(request.path_info)), :charset => 'utf-8'
status 200
send_file File.join(Middleman::Server.views, request.path_info)
request["already_sent"] = true
end
else
$stderr.puts "File not found: #{request.path_info}"
status 404
request["already_sent"] = true
end
end
private
# Internal method to look for templates and evaluate them if found
def process_request(options={})
return if request["already_sent"]
options.merge!(request['custom_options'] || {})
old_layout = settings.current_layout
@ -107,10 +111,10 @@ module Middleman
if result
content_type mime_type(File.extname(request.path_info)), :charset => 'utf-8'
status 200
return result
body result
else
status 404
end
status 404
end
end
end