diff --git a/middleman-core/lib/middleman-core/cli/build.rb b/middleman-core/lib/middleman-core/cli/build.rb index 1f28ae5c..45d61c35 100644 --- a/middleman-core/lib/middleman-core/cli/build.rb +++ b/middleman-core/lib/middleman-core/cli/build.rb @@ -95,25 +95,17 @@ module Middleman::Cli # Ignore following method desc "", "", :hide => true - # Render a template to a file. + # Render a page to a file. # - # @param [String] source - # @param [String] destination - # @param [Hash] config - # @return [String] the actual destination file path that was created - def tilt_template(source, destination, config={}) + # @param [Middleman::Sitemap::Page] page + # @return [void] + def tilt_template(page) build_dir = self.class.shared_instance.build_dir - request_path = destination.sub(/^#{build_dir}/, "") - config[:force] = true begin - destination, request_path = self.class.shared_instance.reroute_builder(destination, request_path) - - response = self.class.shared_rack.get(request_path.gsub(/\s/, "%20")) - - create_file(destination, response.body, config) - - destination + 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) + create_file(output_file, response.body, { :force => true }) rescue say_status :error, destination, :red abort @@ -221,7 +213,10 @@ module Middleman::Cli # Loop over all the paths and build them. paths.each do |path| + puts "SOURCE: #{path}" + file_source = path + # TODO: OMG use pathnames? file_destination = File.join(given_destination, file_source.gsub(source, '.')) file_destination.gsub!('/./', '/') @@ -231,11 +226,13 @@ module Middleman::Cli next end + page = @app.sitemap.page(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 diff --git a/middleman-core/lib/middleman-core/core_extensions/builder.rb b/middleman-core/lib/middleman-core/core_extensions/builder.rb index 5ba2b160..7f67222b 100644 --- a/middleman-core/lib/middleman-core/core_extensions/builder.rb +++ b/middleman-core/lib/middleman-core/core_extensions/builder.rb @@ -16,7 +16,8 @@ module Middleman::CoreExtensions::Builder # Build Class Methods module ClassMethods # 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] def build_reroute(&block) @build_rerouters ||= [] @@ -29,14 +30,14 @@ module Middleman::CoreExtensions::Builder module InstanceMethods # Run through callbacks and get the new values # - # @param [String] destination The current destination of the built file - # @param [String] request_path The current request path of the file - # @return [Array] The new values + # @param [String] destination The current destination path of the built file + # @param [String] request_path The request path of the file + # @return [String] The new destination path def reroute_builder(destination, request_path) result = [destination, request_path] build_reroute.each do |block| - output = instance_exec(destination, request_path, &block) + output = block.call(destination, request_path) if output result = output break diff --git a/middleman-core/lib/middleman-core/extensions/directory_indexes.rb b/middleman-core/lib/middleman-core/extensions/directory_indexes.rb index 1c8ea2df..52264097 100644 --- a/middleman-core/lib/middleman-core/extensions/directory_indexes.rb +++ b/middleman-core/lib/middleman-core/extensions/directory_indexes.rb @@ -74,10 +74,7 @@ module Middleman::Extensions elsif frontmatter_ignore false else - [ - destination.sub(/#{index_ext.gsub(".", "\\.")}$/, new_index_path), - request_path - ] + destination.sub(/#{index_ext.gsub(".", "\\.")}$/, new_index_path), end end end @@ -110,4 +107,4 @@ module Middleman::Extensions # Register the extension register :directory_indexes, DirectoryIndexes -end \ No newline at end of file +end diff --git a/middleman-core/lib/middleman-core/sitemap/page.rb b/middleman-core/lib/middleman-core/sitemap/page.rb index 33af034e..2fc1c9b2 100644 --- a/middleman-core/lib/middleman-core/sitemap/page.rb +++ b/middleman-core/lib/middleman-core/sitemap/page.rb @@ -187,6 +187,15 @@ module Middleman::Sitemap def relative_path self.source_file ? self.source_file.sub(app.source_dir, '') : nil 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 # @return [Hash, nil] diff --git a/middleman-core/lib/middleman-core/sitemap/store.rb b/middleman-core/lib/middleman-core/sitemap/store.rb index f7f96abf..f7471916 100644 --- a/middleman-core/lib/middleman-core/sitemap/store.rb +++ b/middleman-core/lib/middleman-core/sitemap/store.rb @@ -21,6 +21,7 @@ module Middleman::Sitemap @ignored_globs = [] @ignored_regexes = [] @ignored_callbacks = [] + @reroute_callbacks = [] end # Check to see if we know about a specific path @@ -47,6 +48,19 @@ module Middleman::Sitemap 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] + def reroute_callbacks + @reroute_callbacks + end + # Setup a proxy from a path to a target # @param [String] path # @param [String] target