refactor sitemap to add context cache
This commit is contained in:
parent
7dbc7d680a
commit
74bfcbe1aa
|
@ -96,36 +96,6 @@ module Middleman::Base
|
||||||
content_type 'text/html'
|
content_type 'text/html'
|
||||||
"<html><body><h1>File Not Found</h1><p>#{request.path_info}</p></body>"
|
"<html><body><h1>File Not Found</h1><p>#{request.path_info}</p></body>"
|
||||||
end
|
end
|
||||||
|
|
||||||
# See if Tilt cannot handle this file
|
|
||||||
app.before_processing(:base) do |result|
|
|
||||||
request_path = request.path_info.gsub("%20", " ")
|
|
||||||
|
|
||||||
should_be_ignored = !(request["is_proxy"]) && settings.sitemap.ignored_path?("/#{request_path}")
|
|
||||||
|
|
||||||
if result && !should_be_ignored
|
|
||||||
extensionless_path, template_engine = result
|
|
||||||
|
|
||||||
# Return static files
|
|
||||||
if !::Tilt.mappings.has_key?(template_engine.to_s)
|
|
||||||
matched_mime = mime_type(File.extname(request_path))
|
|
||||||
matched_mime = "application/octet-stream" if matched_mime.nil?
|
|
||||||
content_type matched_mime
|
|
||||||
status 200
|
|
||||||
send_file File.join(settings.views, request_path)
|
|
||||||
false
|
|
||||||
else
|
|
||||||
true
|
|
||||||
end
|
|
||||||
else
|
|
||||||
if !%w(favicon.ico).include?(request_path)
|
|
||||||
$stderr.puts "File not found: #{request_path}"
|
|
||||||
end
|
|
||||||
|
|
||||||
status 404
|
|
||||||
false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
alias :included :registered
|
alias :included :registered
|
||||||
end
|
end
|
||||||
|
@ -141,6 +111,14 @@ module Middleman::Base
|
||||||
super(option, value, ignore_setter, &nil)
|
super(option, value, ignore_setter, &nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def full_path(path)
|
||||||
|
parts = path ? path.split('/') : []
|
||||||
|
if parts.last.nil? || parts.last.split('.').length == 1
|
||||||
|
path = File.join(path, index_file)
|
||||||
|
end
|
||||||
|
"/" + path.sub(%r{^/}, '')
|
||||||
|
end
|
||||||
|
|
||||||
def before_processing(name=:unnamed, idx=-1, &block)
|
def before_processing(name=:unnamed, idx=-1, &block)
|
||||||
@before_processes ||= []
|
@before_processes ||= []
|
||||||
@before_processes.insert(idx, [name, block])
|
@before_processes.insert(idx, [name, block])
|
||||||
|
@ -179,25 +157,48 @@ module Middleman::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
# 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
|
||||||
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
|
||||||
|
|
||||||
# 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
|
||||||
request.path_info = self.class.path_to_index(request.path)
|
request_path = settings.full_path(request.path.gsub("%20", " "))
|
||||||
request_path = request.path_info.gsub("%20", " ")
|
original_path = request_path.dup
|
||||||
|
|
||||||
|
return status(404) if settings.sitemap.ignored_path?(request_path)
|
||||||
|
|
||||||
|
if settings.sitemap.path_is_proxy?(request_path)
|
||||||
|
request["is_proxy"] = true
|
||||||
|
request_path = "/" + settings.sitemap.path_target(request_path)
|
||||||
|
end
|
||||||
|
|
||||||
found_template = resolve_template(request_path, :raise_exceptions => false)
|
found_template = resolve_template(request_path, :raise_exceptions => false)
|
||||||
return status(404) unless found_template
|
return status(404) unless found_template
|
||||||
|
|
||||||
|
path, engine = found_template
|
||||||
|
|
||||||
|
if !::Tilt.mappings.has_key?(engine.to_s)
|
||||||
|
matched_mime = mime_type(File.extname(request_path))
|
||||||
|
matched_mime = "application/octet-stream" if matched_mime.nil?
|
||||||
|
content_type matched_mime
|
||||||
|
status 200
|
||||||
|
send_file File.join(settings.views, request_path)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
return unless settings.execute_before_processing!(self, found_template)
|
return unless settings.execute_before_processing!(self, found_template)
|
||||||
|
|
||||||
|
context = settings.sitemap.get_context(original_path) || {}
|
||||||
|
|
||||||
|
options = context.has_key?(:options) ? context[:options] : {}
|
||||||
options.merge!(request['custom_options'] || {})
|
options.merge!(request['custom_options'] || {})
|
||||||
|
|
||||||
old_layout = settings.layout
|
old_layout = settings.layout
|
||||||
settings.set :layout, options[:layout] if !options[:layout].nil?
|
settings.set :layout, options[:layout] if !options[:layout].nil?
|
||||||
|
|
||||||
local_layout = if settings.layout
|
local_layout = if settings.layout
|
||||||
if options[:layout] == false || request.path_info =~ /\.(css|js)$/
|
if options[:layout] == false || request_path =~ /\.(css|js)$/
|
||||||
false
|
false
|
||||||
else
|
else
|
||||||
settings.fetch_layout_path(settings.layout).to_sym
|
settings.fetch_layout_path(settings.layout).to_sym
|
||||||
|
@ -207,11 +208,14 @@ module Middleman::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
render_options = { :layout => local_layout }
|
render_options = { :layout => local_layout }
|
||||||
render_options[:layout_engine] = options[:layout_engine] if options.has_key? :layout_engine
|
render_options[:layout_engine] = options[:layout_engine] if options.has_key?(:layout_engine)
|
||||||
|
|
||||||
path, engine = found_template
|
|
||||||
locals = request['custom_locals'] || {}
|
locals = request['custom_locals'] || {}
|
||||||
|
|
||||||
|
if context.has_key?(:block) && context[:block]
|
||||||
|
instance_eval(&context[:block])
|
||||||
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
result = render(engine, path, render_options, locals)
|
result = render(engine, path, render_options, locals)
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ module Middleman::CoreExtensions::FrontMatter
|
||||||
end
|
end
|
||||||
|
|
||||||
app.after_configuration do
|
app.after_configuration do
|
||||||
app.before_processing(:front_matter, 0) do |result|
|
app.before_processing(:front_matter) do |result|
|
||||||
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}"
|
||||||
|
|
|
@ -7,14 +7,6 @@ module Middleman::CoreExtensions::Routing
|
||||||
end
|
end
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
def path_to_index(path)
|
|
||||||
parts = path ? path.split('/') : []
|
|
||||||
if parts.last.nil? || parts.last.split('.').length == 1
|
|
||||||
path = File.join(path, index_file)
|
|
||||||
end
|
|
||||||
path.gsub(%r{^/}, '')
|
|
||||||
end
|
|
||||||
|
|
||||||
# Takes a block which allows many pages to have the same layout
|
# Takes a block which allows many pages to have the same layout
|
||||||
# with_layout :admin do
|
# with_layout :admin do
|
||||||
# page "/admin/"
|
# page "/admin/"
|
||||||
|
@ -29,47 +21,33 @@ module Middleman::CoreExtensions::Routing
|
||||||
set :layout, old_layout
|
set :layout, old_layout
|
||||||
end
|
end
|
||||||
|
|
||||||
def paths_for_url(url)
|
|
||||||
url = url.gsub(%r{\/#{index_file}$}, "")
|
|
||||||
url = url.gsub(%r{(\/)$}, "") if url.length > 1
|
|
||||||
|
|
||||||
paths = [url]
|
|
||||||
paths << "#{url}/" if url.length > 1 && url.split("/").last.split('.').length <= 1
|
|
||||||
paths << "/#{path_to_index(url)}"
|
|
||||||
paths
|
|
||||||
end
|
|
||||||
|
|
||||||
# The page method allows the layout to be set on a specific path
|
# The page method allows the layout to be set on a specific path
|
||||||
# page "/about.html", :layout => false
|
# page "/about.html", :layout => false
|
||||||
# page "/", :layout => :homepage_layout
|
# page "/", :layout => :homepage_layout
|
||||||
def page(url, options={}, &block)
|
def page(url, options={}, &block)
|
||||||
has_block = block_given?
|
|
||||||
options[:layout] = layout if options[:layout].nil?
|
options[:layout] = layout if options[:layout].nil?
|
||||||
|
|
||||||
|
url = full_path(url)
|
||||||
|
|
||||||
if options.has_key?(:proxy)
|
if options.has_key?(:proxy)
|
||||||
reroute(url, options[:proxy])
|
reroute(url, options[:proxy])
|
||||||
|
|
||||||
if options.has_key?(:ignore) && options[:ignore]
|
if options.has_key?(:ignore) && options[:ignore]
|
||||||
ignore(options[:proxy])
|
ignore(options[:proxy])
|
||||||
|
options.delete(:ignore)
|
||||||
end
|
end
|
||||||
|
|
||||||
options.delete(:proxy)
|
options.delete(:proxy)
|
||||||
else
|
else
|
||||||
if options.has_key?(:ignore) && options[:ignore]
|
if options.has_key?(:ignore) && options[:ignore]
|
||||||
ignore(url)
|
ignore(url)
|
||||||
|
options.delete(:ignore)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
paths_for_url(url).each do |p|
|
a_block = block_given? ? block : nil
|
||||||
get(p) do
|
if a_block || !options.empty?
|
||||||
if settings.sitemap.path_is_proxy?(url)
|
sitemap.set_context(url, options, a_block)
|
||||||
request["is_proxy"] = true
|
|
||||||
request.path_info = settings.sitemap.path_target(url)
|
|
||||||
end
|
|
||||||
|
|
||||||
instance_eval(&block) if has_block
|
|
||||||
process_request(options)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -43,6 +43,7 @@ module Middleman::CoreExtensions::Sitemap
|
||||||
@app = app
|
@app = app
|
||||||
@source = File.expand_path(@app.views, @app.root)
|
@source = File.expand_path(@app.views, @app.root)
|
||||||
@map = {}
|
@map = {}
|
||||||
|
@context_map = {}
|
||||||
@source_map = {}
|
@source_map = {}
|
||||||
@ignored_paths = false
|
@ignored_paths = false
|
||||||
@generic_paths = false
|
@generic_paths = false
|
||||||
|
@ -68,6 +69,11 @@ module Middleman::CoreExtensions::Sitemap
|
||||||
@map[path]
|
@map[path]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def path_context(path)
|
||||||
|
path = path.sub(/^\//, "")
|
||||||
|
@context_map[path]
|
||||||
|
end
|
||||||
|
|
||||||
def set_path(path, target=true)
|
def set_path(path, target=true)
|
||||||
path = path.sub(/^\//, "")
|
path = path.sub(/^\//, "")
|
||||||
target = target.sub(/^\//, "") if target.is_a?(String)
|
target = target.sub(/^\//, "") if target.is_a?(String)
|
||||||
|
@ -79,8 +85,22 @@ module Middleman::CoreExtensions::Sitemap
|
||||||
@proxied_paths = false if target.is_a?(String)
|
@proxied_paths = false if target.is_a?(String)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def set_context(path, options={}, block=nil)
|
||||||
|
path = path.sub(/^\//, "")
|
||||||
|
@context_map[path] = {
|
||||||
|
:options => options,
|
||||||
|
:block => block
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_context(path)
|
||||||
|
path = path.sub(/^\//, "")
|
||||||
|
@context_map[path]
|
||||||
|
end
|
||||||
|
|
||||||
def ignore_path(path)
|
def ignore_path(path)
|
||||||
set_path(path, false)
|
set_path(path, false)
|
||||||
|
@ignored_paths = false
|
||||||
end
|
end
|
||||||
|
|
||||||
def each(&block)
|
def each(&block)
|
||||||
|
@ -154,6 +174,7 @@ module Middleman::CoreExtensions::Sitemap
|
||||||
def remove_path(path)
|
def remove_path(path)
|
||||||
path = path.sub(/^\//, "")
|
path = path.sub(/^\//, "")
|
||||||
@map.delete(path) if path_exists?(path)
|
@map.delete(path) if path_exists?(path)
|
||||||
|
@context_map.delete(path) if @context_map.has_key?(path)
|
||||||
end
|
end
|
||||||
|
|
||||||
def source_map
|
def source_map
|
||||||
|
|
Loading…
Reference in a new issue