use private before filters, leave sinatra alone. fixes #76

This commit is contained in:
Thomas Reynolds 2011-07-14 12:32:39 -07:00
parent be506c60cc
commit 292ec97bd8
6 changed files with 84 additions and 55 deletions

6
features/sinatra.feature Normal file
View file

@ -0,0 +1,6 @@
Feature: Sinatra Routes
Scenario: Rendering html
Given the Server is running
When I go to "/sinatra_test"
Then I should see "Ratpack"

View file

@ -28,3 +28,7 @@ with_layout false do
page path
end
end
get "/sinatra_test" do
"Ratpack"
end

View file

@ -78,7 +78,7 @@ module Middleman::Base
end
# See if Tilt cannot handle this file
app.before do
app.before_processing do
if !settings.views.include?(settings.root)
settings.set :views, File.join(settings.root, settings.views)
end
@ -92,12 +92,14 @@ module Middleman::Base
content_type mime_type(File.extname(request.path_info)), :charset => 'utf-8'
status 200
send_file File.join(settings.views, request.path_info)
request["already_sent"] = true
false
else
true
end
else
$stderr.puts "File not found: #{request.path_info}"
status 404
request["already_sent"] = true
false
end
end
end
@ -115,6 +117,19 @@ module Middleman::Base
super(option, value, &nil)
end
def before_processing(&block)
@before_processes ||= []
@before_processes << block
end
def execute_before_processing!(inst)
@before_processes ||= []
@before_processes.all? do |block|
inst.instance_eval(&block)
end
end
# Convenience method to check if we're in build mode
def build?; environment == :build; end
end
@ -122,7 +137,7 @@ module Middleman::Base
module InstanceMethods
# Internal method to look for templates and evaluate them if found
def process_request(options={})
return if request["already_sent"]
return unless settings.execute_before_processing!(self)
options.merge!(request['custom_options'] || {})

View file

@ -23,7 +23,7 @@ module Middleman::CoreExtensions::FrontMatter
::Tilt.prefer(HamlTemplate)
app.after_feature_init do
app.before do
app.before_processing do
result = resolve_template(request.path_info, :raise_exceptions => false)
if result && Tilt.mappings.has_key?(result[1].to_s)
@ -42,6 +42,8 @@ module Middleman::CoreExtensions::FrontMatter
# Forward remaining data to helpers
app.data_content("page", data)
end
true
end
end
end

View file

@ -4,8 +4,9 @@ module Middleman::CoreExtensions::Routing
app.extend ClassMethods
# Normalize the path and add index if we're looking at a directory
app.before do
app.before_processing do
request.path_info = self.class.path_to_index(request.path)
true
end
end
alias :included :registered
@ -43,7 +44,7 @@ module Middleman::CoreExtensions::Routing
paths = [url]
paths << "#{url}/" if url.length > 1 && url.split("/").last.split('.').length <= 1
paths << "#{path_to_index(url)}"
paths << "/#{path_to_index(url)}"
options[:layout] = settings.layout if options[:layout].nil?

View file

@ -42,59 +42,60 @@ module Middleman
end
$stderr.puts "== Blog: #{app.settings.blog_permalink}"
end
app.before do
articles_glob = File.join(app.views, app.settings.blog_permalink.gsub(/(:\w+)/, "*") + ".*")
app.before_processing 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 = app.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"])
data["raw"] = content
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
engine = app.settings.markdown_engine.new { sum }
data["summary"] = engine.render
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"]
end
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
app.data_content("blog", { :articles => articles, :tags => tags })
engine = app.settings.markdown_engine.new { sum }
data["summary"] = engine.render
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"]
end
end
end
app.get(app.settings.blog_permalink) do
process_request({
:layout => settings.blog_layout,
:layout_engine => settings.blog_layout_engine
})
app.data_content("blog", { :articles => articles, :tags => tags })
true
end
# No need for separator on permalink page
body body.gsub!(settings.blog_summary_separator, "")
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
alias :included :registered