Merge pull request #263 from bhollis/master

Get rid of build_reroute
This commit is contained in:
Thomas Reynolds 2012-02-10 14:37:37 -08:00
commit 6799606324
6 changed files with 43 additions and 108 deletions

View file

@ -197,10 +197,11 @@ module Middleman::Cli
# Sort order, images, fonts, js/css and finally everything else. # Sort order, images, fonts, js/css and finally everything else.
sort_order = %w(.png .jpeg .jpg .gif .bmp .svg .svgz .ico .woff .otf .ttf .eot .js .css) sort_order = %w(.png .jpeg .jpg .gif .bmp .svg .svgz .ico .woff .otf .ttf .eot .js .css)
@app.sitemap.all_paths.select do |p| # Pre-request CSS to give Compass a chance to build sprites
File.extname(p) == ".css" @app.sitemap.pages.select do |p|
p.ext == ".css"
end.each do |p| end.each do |p|
Middleman::Cli::Build.shared_rack.get("/" + p.gsub(/\s/, "%20")) Middleman::Cli::Build.shared_rack.get(p.request_path.gsub(/\s/, "%20"))
end end
# Double-check for compass sprites # Double-check for compass sprites
@ -210,23 +211,17 @@ module Middleman::Cli
# find files in the build folder when it needs to generate sprites for the # find files in the build folder when it needs to generate sprites for the
# css files # css files
# TODO: deal with pages, not paths pages = @app.sitemap.pages.sort do |a, b|
paths = @app.sitemap.all_paths.sort do |a, b| a_idx = sort_order.index(a.ext) || 100
a_ext = File.extname(a) b_idx = sort_order.index(b.ext) || 100
b_ext = File.extname(b)
a_idx = sort_order.index(a_ext) || 100
b_idx = sort_order.index(b_ext) || 100
a_idx <=> b_idx a_idx <=> b_idx
end end
# Loop over all the paths and build them. # Loop over all the paths and build them.
paths.each do |path| pages.each do |page|
page = @app.sitemap.page(path)
next if page.ignored? next if page.ignored?
next if @config[:glob] && !File.fnmatch(@config[:glob], path) next if @config[:glob] && !File.fnmatch(@config[:glob], page.path)
base.tilt_template(page) base.tilt_template(page)

View file

@ -6,45 +6,7 @@ module Middleman::CoreExtensions::Builder
# @private # @private
def registered(app) def registered(app)
app.define_hook :after_build app.define_hook :after_build
app.extend ClassMethods
app.send :include, InstanceMethods
app.delegate :build_reroute, :to => :"self.class"
end end
alias :included :registered alias :included :registered
end end
# 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<Proc>]
def build_reroute(&block)
@build_rerouters ||= []
@build_rerouters << block if block_given?
@build_rerouters
end
end
# Build Instance Methods
module InstanceMethods
# Run through callbacks and get 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 = block.call(destination, request_path)
if output
result = output
break
end
end
result
end
end
end end

View file

@ -72,7 +72,7 @@ module Middleman::Sitemap
@_template ||= ::Middleman::Sitemap::Template.new(self) @_template ||= ::Middleman::Sitemap::Template.new(self)
end end
# Extension of the path # Extension of the path (i.e. '.js')
# @return [String] # @return [String]
def ext def ext
File.extname(path) File.extname(path)
@ -192,8 +192,9 @@ module Middleman::Sitemap
# This path can be affected by proxy callbacks. # This path can be affected by proxy callbacks.
# @return [String] # @return [String]
def destination_path def destination_path
# TODO: memoize this value # memoizing this means that reroute callbacks should be in place before the sitemap
store.reroute_callbacks.inject(self.path) do |destination, callback| # gets built
@destination_path ||= store.reroute_callbacks.inject(self.path) do |destination, callback|
callback.call(destination, self) callback.call(destination, self)
end end
end end
@ -235,29 +236,27 @@ module Middleman::Sitemap
if eponymous_directory? if eponymous_directory?
base_path = eponymous_directory_path base_path = eponymous_directory_path
prefix = /^#{base_path.sub("/", "\\/")}/ prefix = %r|^#{base_path.sub("/", "\\/")}|
else else
base_path = path.sub("#{app.index_file}", "") base_path = path.sub("#{app.index_file}", "")
prefix = /^#{base_path.sub("/", "\\/")}/ prefix = %r|^#{base_path.sub("/", "\\/")}|
end end
store.all_paths.select do |sub_path| store.pages.select do |sub_page|
sub_path =~ prefix if sub_page == self || sub_page.path !~ prefix || sub_page.ignored?
end.select do |sub_path| false
path != sub_path else
end.select do |sub_path| inner_path = sub_page.path.sub(prefix, "")
inner_path = sub_path.sub(prefix, "") parts = inner_path.split("/")
parts = inner_path.split("/") if parts.length == 1
if parts.length == 1 true
true elsif parts.length == 2
elsif parts.length == 2 parts.last == app.index_file
parts.last == app.index_file else
else false
false end
end end
end.map do |p| end
store.page(p)
end.reject { |p| p.ignored? }
end end
# This page's sibling pages # This page's sibling pages

View file

@ -24,6 +24,12 @@ module Middleman::Sitemap
@reroute_callbacks = [] @reroute_callbacks = []
end end
# A list of all pages
# @return [Array<Middleman::Sitemap::Page>]
def pages
@pages.values
end
# Check to see if we know about a specific path # Check to see if we know about a specific path
# @param [String] path # @param [String] path
# @return [Boolean] # @return [Boolean]
@ -82,22 +88,7 @@ module Middleman::Sitemap
def page_by_destination(destination_path) def page_by_destination(destination_path)
# TODO: memoize this # TODO: memoize this
destination_path = normalize_path(destination_path) destination_path = normalize_path(destination_path)
@pages.values.find {|p| p.destination_path == destination_path } pages.find {|p| p.destination_path == destination_path }
end
# Loop over known pages
# @yield [path, page]
# @return [void]
def each
@pages.each do |k, v|
yield k, v
end
end
# Get all known paths
# @return [Array<String>]
def all_paths
@pages.keys
end end
# Whether a path is ignored # Whether a path is ignored
@ -119,7 +110,7 @@ module Middleman::Sitemap
# Get a list of ignored paths # Get a list of ignored paths
# @return [Array<String>] # @return [Array<String>]
def ignored_paths def ignored_paths
@pages.values.select(&:ignored?).map(&:path) pages.select(&:ignored?).map(&:path)
end end
# Whether the given path is generic # Whether the given path is generic
@ -133,7 +124,7 @@ module Middleman::Sitemap
# @return [Array<String>] # @return [Array<String>]
def generic_paths def generic_paths
app.cache.fetch :generic_paths do app.cache.fetch :generic_paths do
@pages.values.select(&:generic?).map(&:path) pages.select(&:generic?).map(&:path)
end end
end end
@ -148,7 +139,7 @@ module Middleman::Sitemap
# @return [Array<String>] # @return [Array<String>]
def proxied_paths def proxied_paths
app.cache.fetch :proxied_paths do app.cache.fetch :proxied_paths do
@pages.values.select(&:proxy?).map(&:path) pages.select(&:proxy?).map(&:path)
end end
end end

View file

@ -4,10 +4,10 @@ require "rubygems"
module Middleman module Middleman
# Current Version # Current Version
# @return [String] # @return [String]
VERSION = '3.0.0.beta.1' unless const_defined?(:VERSION) VERSION = '3.0.0.beta.2' unless const_defined?(:VERSION)
# Parsed version for RubyGems # Parsed version for RubyGems
# @private # @private
# @return [String] # @return [String]
GEM_VERSION = ::Gem::Version.create(VERSION) unless const_defined?(:GEM_VERSION) GEM_VERSION = ::Gem::Version.create(VERSION) unless const_defined?(:GEM_VERSION)
end end

View file

@ -13,18 +13,6 @@ module Middleman::CoreExtensions::Sprockets
app.set :js_compressor, false app.set :js_compressor, false
app.set :css_compressor, false app.set :css_compressor, false
# Cut off every extension after .js (which sprockets eats up)
app.build_reroute do |destination, request_path|
if !request_path.match(/\.js\./i)
false
else
[
destination.gsub(/\.js(\..*)$/, ".js"),
request_path.gsub(/\.js(\..*)$/, ".js")
]
end
end
# Once Middleman is setup # Once Middleman is setup
app.ready do app.ready do
# Create sprockets env for JS # Create sprockets env for JS
@ -172,4 +160,4 @@ module Middleman::CoreExtensions::Sprockets
super(exception) super(exception)
end end
end end
end end