From 57061a17c0c00563de65c9afbcbac55b4a3e4718 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Wed, 8 Feb 2012 22:11:20 -0800 Subject: [PATCH 1/4] Update version so path-based Gemfile dependencies work --- middleman-core/lib/middleman-core/version.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/middleman-core/lib/middleman-core/version.rb b/middleman-core/lib/middleman-core/version.rb index 3bc6ccb7..16ba6679 100644 --- a/middleman-core/lib/middleman-core/version.rb +++ b/middleman-core/lib/middleman-core/version.rb @@ -4,10 +4,10 @@ require "rubygems" module Middleman # Current Version # @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 # @private # @return [String] GEM_VERSION = ::Gem::Version.create(VERSION) unless const_defined?(:GEM_VERSION) -end \ No newline at end of file +end From 382107d40320dc8026f7aad304ccb2bb5a7cbd3b Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Wed, 8 Feb 2012 22:13:49 -0800 Subject: [PATCH 2/4] Get rid of build_reroute. The last thing using it was the sprockets extension, though it didn't appear to need it since removing the build_reroute call leaves tests passing, and the test site I tried still generated JS from CoffeeScript just fine. --- .../middleman-core/core_extensions/builder.rb | 38 ------------------- .../core_extensions/sprockets.rb | 14 +------ 2 files changed, 1 insertion(+), 51 deletions(-) diff --git a/middleman-core/lib/middleman-core/core_extensions/builder.rb b/middleman-core/lib/middleman-core/core_extensions/builder.rb index 7f67222b..4b901b69 100644 --- a/middleman-core/lib/middleman-core/core_extensions/builder.rb +++ b/middleman-core/lib/middleman-core/core_extensions/builder.rb @@ -6,45 +6,7 @@ module Middleman::CoreExtensions::Builder # @private def registered(app) app.define_hook :after_build - app.extend ClassMethods - app.send :include, InstanceMethods - app.delegate :build_reroute, :to => :"self.class" end alias :included :registered 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] - 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 diff --git a/middleman-more/lib/middleman-more/core_extensions/sprockets.rb b/middleman-more/lib/middleman-more/core_extensions/sprockets.rb index 370534ea..cc1acaf8 100644 --- a/middleman-more/lib/middleman-more/core_extensions/sprockets.rb +++ b/middleman-more/lib/middleman-more/core_extensions/sprockets.rb @@ -13,18 +13,6 @@ module Middleman::CoreExtensions::Sprockets app.set :js_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 app.ready do # Create sprockets env for JS @@ -172,4 +160,4 @@ module Middleman::CoreExtensions::Sprockets super(exception) end end -end \ No newline at end of file +end From fdbd301a324cb7a5c2abf386be48dfc5e42a9418 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Wed, 8 Feb 2012 23:00:29 -0800 Subject: [PATCH 3/4] Get rid of Middleman::Sitemap::Store#all_paths and #each in favor of #pages. Dealing with page objects all over the place instead of paths provides a lot of opportunities for cleaner code. --- .../lib/middleman-core/cli/build.rb | 23 +++++------ .../lib/middleman-core/sitemap/page.rb | 38 +++++++++---------- .../lib/middleman-core/sitemap/store.rb | 29 +++++--------- 3 files changed, 37 insertions(+), 53 deletions(-) diff --git a/middleman-core/lib/middleman-core/cli/build.rb b/middleman-core/lib/middleman-core/cli/build.rb index a16fd4b1..d43d29fe 100644 --- a/middleman-core/lib/middleman-core/cli/build.rb +++ b/middleman-core/lib/middleman-core/cli/build.rb @@ -197,10 +197,11 @@ module Middleman::Cli # 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) - @app.sitemap.all_paths.select do |p| - File.extname(p) == ".css" + # Pre-request CSS to give Compass a chance to build sprites + @app.sitemap.pages.select do |p| + p.ext == ".css" 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 # 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 # css files - # TODO: deal with pages, not paths - paths = @app.sitemap.all_paths.sort do |a, b| - a_ext = File.extname(a) - b_ext = File.extname(b) - - a_idx = sort_order.index(a_ext) || 100 - b_idx = sort_order.index(b_ext) || 100 + pages = @app.sitemap.pages.sort do |a, b| + a_idx = sort_order.index(a.ext) || 100 + b_idx = sort_order.index(b.ext) || 100 a_idx <=> b_idx end # Loop over all the paths and build them. - paths.each do |path| - page = @app.sitemap.page(path) - + pages.each do |page| 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) diff --git a/middleman-core/lib/middleman-core/sitemap/page.rb b/middleman-core/lib/middleman-core/sitemap/page.rb index b14faca7..c8886e36 100644 --- a/middleman-core/lib/middleman-core/sitemap/page.rb +++ b/middleman-core/lib/middleman-core/sitemap/page.rb @@ -72,7 +72,7 @@ module Middleman::Sitemap @_template ||= ::Middleman::Sitemap::Template.new(self) end - # Extension of the path + # Extension of the path (i.e. '.js') # @return [String] def ext File.extname(path) @@ -235,29 +235,27 @@ module Middleman::Sitemap if eponymous_directory? base_path = eponymous_directory_path - prefix = /^#{base_path.sub("/", "\\/")}/ + prefix = %r|^#{base_path.sub("/", "\\/")}| else base_path = path.sub("#{app.index_file}", "") - prefix = /^#{base_path.sub("/", "\\/")}/ + prefix = %r|^#{base_path.sub("/", "\\/")}| end - store.all_paths.select do |sub_path| - sub_path =~ prefix - end.select do |sub_path| - path != sub_path - end.select do |sub_path| - inner_path = sub_path.sub(prefix, "") - parts = inner_path.split("/") - if parts.length == 1 - true - elsif parts.length == 2 - parts.last == app.index_file - else - false - end - end.map do |p| - store.page(p) - end.reject { |p| p.ignored? } + store.pages.select do |sub_page| + if sub_page == self || sub_page.path !~ prefix || sub_page.ignored? + false + else + inner_path = sub_page.path.sub(prefix, "") + parts = inner_path.split("/") + if parts.length == 1 + true + elsif parts.length == 2 + parts.last == app.index_file + else + false + end + end + end end # This page's sibling pages diff --git a/middleman-core/lib/middleman-core/sitemap/store.rb b/middleman-core/lib/middleman-core/sitemap/store.rb index b14dac67..f307b82d 100644 --- a/middleman-core/lib/middleman-core/sitemap/store.rb +++ b/middleman-core/lib/middleman-core/sitemap/store.rb @@ -24,6 +24,12 @@ module Middleman::Sitemap @reroute_callbacks = [] end + # A list of all pages + # @return [Array] + def pages + @pages.values + end + # Check to see if we know about a specific path # @param [String] path # @return [Boolean] @@ -82,22 +88,7 @@ module Middleman::Sitemap def page_by_destination(destination_path) # TODO: memoize this destination_path = normalize_path(destination_path) - @pages.values.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] - def all_paths - @pages.keys + pages.find {|p| p.destination_path == destination_path } end # Whether a path is ignored @@ -119,7 +110,7 @@ module Middleman::Sitemap # Get a list of ignored paths # @return [Array] def ignored_paths - @pages.values.select(&:ignored?).map(&:path) + pages.select(&:ignored?).map(&:path) end # Whether the given path is generic @@ -133,7 +124,7 @@ module Middleman::Sitemap # @return [Array] def generic_paths app.cache.fetch :generic_paths do - @pages.values.select(&:generic?).map(&:path) + pages.select(&:generic?).map(&:path) end end @@ -148,7 +139,7 @@ module Middleman::Sitemap # @return [Array] def proxied_paths app.cache.fetch :proxied_paths do - @pages.values.select(&:proxy?).map(&:path) + pages.select(&:proxy?).map(&:path) end end From d483d8bc54b4b47943a08fd3cdcf912de1d79062 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Thu, 9 Feb 2012 09:39:24 -0800 Subject: [PATCH 4/4] Memoize Sitemap::Page#destination_path --- middleman-core/lib/middleman-core/sitemap/page.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/middleman-core/lib/middleman-core/sitemap/page.rb b/middleman-core/lib/middleman-core/sitemap/page.rb index c8886e36..8b25b084 100644 --- a/middleman-core/lib/middleman-core/sitemap/page.rb +++ b/middleman-core/lib/middleman-core/sitemap/page.rb @@ -192,8 +192,9 @@ module Middleman::Sitemap # This path can be affected by proxy callbacks. # @return [String] def destination_path - # TODO: memoize this value - store.reroute_callbacks.inject(self.path) do |destination, callback| + # memoizing this means that reroute callbacks should be in place before the sitemap + # gets built + @destination_path ||= store.reroute_callbacks.inject(self.path) do |destination, callback| callback.call(destination, self) end end