The beginnings of refactoring page rerouting. Added methods to Sitemap::Store and Sitemap::Page to collect and execute reroute callbacks, and rework builder to use page methods (simplifying it greatly in the process). All tests that don't involve directory indexes pass - next step is to make the directory index extension register its callbacks with Sitemap::Store#reroute.
This commit is contained in:
parent
a41bf57f2d
commit
4ce6913baf
5 changed files with 45 additions and 27 deletions
|
@ -95,25 +95,17 @@ module Middleman::Cli
|
||||||
# Ignore following method
|
# Ignore following method
|
||||||
desc "", "", :hide => true
|
desc "", "", :hide => true
|
||||||
|
|
||||||
# Render a template to a file.
|
# Render a page to a file.
|
||||||
#
|
#
|
||||||
# @param [String] source
|
# @param [Middleman::Sitemap::Page] page
|
||||||
# @param [String] destination
|
# @return [void]
|
||||||
# @param [Hash] config
|
def tilt_template(page)
|
||||||
# @return [String] the actual destination file path that was created
|
|
||||||
def tilt_template(source, destination, config={})
|
|
||||||
build_dir = self.class.shared_instance.build_dir
|
build_dir = self.class.shared_instance.build_dir
|
||||||
request_path = destination.sub(/^#{build_dir}/, "")
|
|
||||||
config[:force] = true
|
|
||||||
|
|
||||||
begin
|
begin
|
||||||
destination, request_path = self.class.shared_instance.reroute_builder(destination, request_path)
|
response = self.class.shared_rack.get(page.request_path.gsub(/\s/, "%20"))
|
||||||
|
output_file = File.join(self.class.shared_instance.build_dir, page.destination_path)
|
||||||
response = self.class.shared_rack.get(request_path.gsub(/\s/, "%20"))
|
create_file(output_file, response.body, { :force => true })
|
||||||
|
|
||||||
create_file(destination, response.body, config)
|
|
||||||
|
|
||||||
destination
|
|
||||||
rescue
|
rescue
|
||||||
say_status :error, destination, :red
|
say_status :error, destination, :red
|
||||||
abort
|
abort
|
||||||
|
@ -221,7 +213,10 @@ module Middleman::Cli
|
||||||
|
|
||||||
# Loop over all the paths and build them.
|
# Loop over all the paths and build them.
|
||||||
paths.each do |path|
|
paths.each do |path|
|
||||||
|
puts "SOURCE: #{path}"
|
||||||
|
|
||||||
file_source = path
|
file_source = path
|
||||||
|
# TODO: OMG use pathnames?
|
||||||
file_destination = File.join(given_destination, file_source.gsub(source, '.'))
|
file_destination = File.join(given_destination, file_source.gsub(source, '.'))
|
||||||
file_destination.gsub!('/./', '/')
|
file_destination.gsub!('/./', '/')
|
||||||
|
|
||||||
|
@ -231,11 +226,13 @@ module Middleman::Cli
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
|
|
||||||
|
page = @app.sitemap.page(file_source)
|
||||||
|
|
||||||
next if @config[:glob] && !File.fnmatch(@config[:glob], file_source)
|
next if @config[:glob] && !File.fnmatch(@config[:glob], file_source)
|
||||||
|
|
||||||
file_destination = base.tilt_template(file_source, file_destination)
|
base.tilt_template(page)
|
||||||
|
|
||||||
@cleaning_queue.delete(Pathname.new(file_destination).realpath) if cleaning?
|
@cleaning_queue.delete(Pathname.new(page.destination_path).realpath) if cleaning?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -16,7 +16,8 @@ module Middleman::CoreExtensions::Builder
|
||||||
# Build Class Methods
|
# Build Class Methods
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
# Get a list of callbacks which can modify a files build path
|
# Get a list of callbacks which can modify a files build path
|
||||||
#
|
# Each callback takes a destination path and a request path and
|
||||||
|
# returns a new destination path, or false if it doesn't want to reroute.
|
||||||
# @return [Array<Proc>]
|
# @return [Array<Proc>]
|
||||||
def build_reroute(&block)
|
def build_reroute(&block)
|
||||||
@build_rerouters ||= []
|
@build_rerouters ||= []
|
||||||
|
@ -29,14 +30,14 @@ module Middleman::CoreExtensions::Builder
|
||||||
module InstanceMethods
|
module InstanceMethods
|
||||||
# Run through callbacks and get the new values
|
# Run through callbacks and get the new values
|
||||||
#
|
#
|
||||||
# @param [String] destination The current destination of the built file
|
# @param [String] destination The current destination path of the built file
|
||||||
# @param [String] request_path The current request path of the file
|
# @param [String] request_path The request path of the file
|
||||||
# @return [Array<String>] The new values
|
# @return [String] The new destination path
|
||||||
def reroute_builder(destination, request_path)
|
def reroute_builder(destination, request_path)
|
||||||
result = [destination, request_path]
|
result = [destination, request_path]
|
||||||
|
|
||||||
build_reroute.each do |block|
|
build_reroute.each do |block|
|
||||||
output = instance_exec(destination, request_path, &block)
|
output = block.call(destination, request_path)
|
||||||
if output
|
if output
|
||||||
result = output
|
result = output
|
||||||
break
|
break
|
||||||
|
|
|
@ -74,10 +74,7 @@ module Middleman::Extensions
|
||||||
elsif frontmatter_ignore
|
elsif frontmatter_ignore
|
||||||
false
|
false
|
||||||
else
|
else
|
||||||
[
|
|
||||||
destination.sub(/#{index_ext.gsub(".", "\\.")}$/, new_index_path),
|
destination.sub(/#{index_ext.gsub(".", "\\.")}$/, new_index_path),
|
||||||
request_path
|
|
||||||
]
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -188,6 +188,15 @@ module Middleman::Sitemap
|
||||||
self.source_file ? self.source_file.sub(app.source_dir, '') : nil
|
self.source_file ? self.source_file.sub(app.source_dir, '') : nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Get the destination path, relative to the build directory.
|
||||||
|
# This path can be affected by proxy callbacks.
|
||||||
|
# @return [String]
|
||||||
|
def destination_path
|
||||||
|
store.reroute_callbacks.inject(self.path) do |destination, callback|
|
||||||
|
callback.call(destination)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# This page's frontmatter
|
# This page's frontmatter
|
||||||
# @return [Hash, nil]
|
# @return [Hash, nil]
|
||||||
def data
|
def data
|
||||||
|
|
|
@ -21,6 +21,7 @@ module Middleman::Sitemap
|
||||||
@ignored_globs = []
|
@ignored_globs = []
|
||||||
@ignored_regexes = []
|
@ignored_regexes = []
|
||||||
@ignored_callbacks = []
|
@ignored_callbacks = []
|
||||||
|
@reroute_callbacks = []
|
||||||
end
|
end
|
||||||
|
|
||||||
# Check to see if we know about a specific path
|
# Check to see if we know about a specific path
|
||||||
|
@ -47,6 +48,19 @@ module Middleman::Sitemap
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Add a callback that will be run with each page's destination path
|
||||||
|
# and can produce a new destination path or pass through the old one.
|
||||||
|
# @return [void]
|
||||||
|
def reroute(&block)
|
||||||
|
@reroute_callbacks << block if block_given?
|
||||||
|
end
|
||||||
|
|
||||||
|
# The list of reroute callbacks
|
||||||
|
# @return [Array<Proc>]
|
||||||
|
def reroute_callbacks
|
||||||
|
@reroute_callbacks
|
||||||
|
end
|
||||||
|
|
||||||
# Setup a proxy from a path to a target
|
# Setup a proxy from a path to a target
|
||||||
# @param [String] path
|
# @param [String] path
|
||||||
# @param [String] target
|
# @param [String] target
|
||||||
|
|
Loading…
Add table
Reference in a new issue