use private before filters, leave sinatra alone. fixes #76
This commit is contained in:
parent
be506c60cc
commit
292ec97bd8
6 changed files with 84 additions and 55 deletions
6
features/sinatra.feature
Normal file
6
features/sinatra.feature
Normal 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"
|
|
@ -27,4 +27,8 @@ with_layout false do
|
||||||
}.each do |path|
|
}.each do |path|
|
||||||
page path
|
page path
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
get "/sinatra_test" do
|
||||||
|
"Ratpack"
|
||||||
end
|
end
|
|
@ -78,7 +78,7 @@ module Middleman::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
# See if Tilt cannot handle this file
|
# See if Tilt cannot handle this file
|
||||||
app.before do
|
app.before_processing do
|
||||||
if !settings.views.include?(settings.root)
|
if !settings.views.include?(settings.root)
|
||||||
settings.set :views, File.join(settings.root, settings.views)
|
settings.set :views, File.join(settings.root, settings.views)
|
||||||
end
|
end
|
||||||
|
@ -92,12 +92,14 @@ module Middleman::Base
|
||||||
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(settings.views, request.path_info)
|
send_file File.join(settings.views, request.path_info)
|
||||||
request["already_sent"] = true
|
false
|
||||||
|
else
|
||||||
|
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
|
false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -115,6 +117,19 @@ module Middleman::Base
|
||||||
super(option, value, &nil)
|
super(option, value, &nil)
|
||||||
end
|
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
|
# Convenience method to check if we're in build mode
|
||||||
def build?; environment == :build; end
|
def build?; environment == :build; end
|
||||||
end
|
end
|
||||||
|
@ -122,7 +137,7 @@ module Middleman::Base
|
||||||
module InstanceMethods
|
module InstanceMethods
|
||||||
# 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"]
|
return unless settings.execute_before_processing!(self)
|
||||||
|
|
||||||
options.merge!(request['custom_options'] || {})
|
options.merge!(request['custom_options'] || {})
|
||||||
|
|
||||||
|
|
|
@ -23,25 +23,27 @@ module Middleman::CoreExtensions::FrontMatter
|
||||||
::Tilt.prefer(HamlTemplate)
|
::Tilt.prefer(HamlTemplate)
|
||||||
|
|
||||||
app.after_feature_init do
|
app.after_feature_init do
|
||||||
app.before do
|
app.before_processing do
|
||||||
result = resolve_template(request.path_info, :raise_exceptions => false)
|
result = resolve_template(request.path_info, :raise_exceptions => false)
|
||||||
|
|
||||||
if result && Tilt.mappings.has_key?(result[1].to_s)
|
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)
|
||||||
data, content = app.parse_front_matter(File.read(system_path))
|
data, content = app.parse_front_matter(File.read(system_path))
|
||||||
|
|
||||||
request['custom_options'] = {}
|
request['custom_options'] = {}
|
||||||
%w(layout layout_engine).each do |opt|
|
%w(layout layout_engine).each do |opt|
|
||||||
if data.has_key?(opt)
|
if data.has_key?(opt)
|
||||||
request['custom_options'][opt.to_sym] = data.delete(opt)
|
request['custom_options'][opt.to_sym] = data.delete(opt)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Forward remaining data to helpers
|
# Forward remaining data to helpers
|
||||||
app.data_content("page", data)
|
app.data_content("page", data)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,8 +4,9 @@ module Middleman::CoreExtensions::Routing
|
||||||
app.extend ClassMethods
|
app.extend ClassMethods
|
||||||
|
|
||||||
# Normalize the path and add index if we're looking at a directory
|
# 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)
|
request.path_info = self.class.path_to_index(request.path)
|
||||||
|
true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
alias :included :registered
|
alias :included :registered
|
||||||
|
@ -43,7 +44,7 @@ module Middleman::CoreExtensions::Routing
|
||||||
|
|
||||||
paths = [url]
|
paths = [url]
|
||||||
paths << "#{url}/" if url.length > 1 && url.split("/").last.split('.').length <= 1
|
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?
|
options[:layout] = settings.layout if options[:layout].nil?
|
||||||
|
|
||||||
|
|
|
@ -42,59 +42,60 @@ module Middleman
|
||||||
end
|
end
|
||||||
|
|
||||||
$stderr.puts "== Blog: #{app.settings.blog_permalink}"
|
$stderr.puts "== Blog: #{app.settings.blog_permalink}"
|
||||||
|
end
|
||||||
|
|
||||||
|
app.before_processing do
|
||||||
|
articles_glob = File.join(app.views, app.settings.blog_permalink.gsub(/(:\w+)/, "*") + ".*")
|
||||||
|
|
||||||
app.before do
|
articles = Dir[articles_glob].map do |article|
|
||||||
articles_glob = File.join(app.views, app.settings.blog_permalink.gsub(/(:\w+)/, "*") + ".*")
|
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|
|
data["raw"] = content
|
||||||
template_content = File.read(article)
|
data["url"] = article.gsub(app.views, "").split(".html").first + ".html"
|
||||||
data, content = app.parse_front_matter(template_content)
|
|
||||||
data["date"] = Date.parse(data["date"])
|
|
||||||
|
|
||||||
data["raw"] = content
|
all_content = Tilt.new(article).render
|
||||||
data["url"] = article.gsub(app.views, "").split(".html").first + ".html"
|
data["body"] = all_content.gsub!(app.settings.blog_summary_separator, "")
|
||||||
|
|
||||||
all_content = Tilt.new(article).render
|
sum = if data["raw"] =~ app.settings.blog_summary_separator
|
||||||
data["body"] = all_content.gsub!(app.settings.blog_summary_separator, "")
|
data["raw"].split(app.settings.blog_summary_separator).first
|
||||||
|
else
|
||||||
sum = if data["raw"] =~ app.settings.blog_summary_separator
|
data["raw"].match(/(.{1,#{app.settings.blog_summary_length}}.*?)(\n|\Z)/m).to_s
|
||||||
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
|
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
|
app.data_content("blog", { :articles => articles, :tags => tags })
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
app.get(app.settings.blog_permalink) do
|
||||||
|
process_request({
|
||||||
|
:layout => settings.blog_layout,
|
||||||
|
:layout_engine => settings.blog_layout_engine
|
||||||
|
})
|
||||||
|
|
||||||
app.get(app.settings.blog_permalink) do
|
# No need for separator on permalink page
|
||||||
process_request({
|
body body.gsub!(settings.blog_summary_separator, "")
|
||||||
: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
|
||||||
|
|
Loading…
Add table
Reference in a new issue