From 69396d34c1fdbdc2cf6b0ae0934f925bf39b80c8 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sun, 27 Apr 2014 21:54:53 -0700 Subject: [PATCH 01/21] Start gutting the provides_metadata methods and move some of frontmatter over --- .../core_extensions/front_matter.rb | 47 ++++++++------ .../lib/middleman-core/sitemap/resource.rb | 12 +++- .../lib/middleman-core/sitemap/store.rb | 62 +------------------ 3 files changed, 39 insertions(+), 82 deletions(-) diff --git a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb index 5310d37e..0d90a577 100644 --- a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb +++ b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb @@ -29,29 +29,36 @@ module Middleman::CoreExtensions app.files.deleted { |file| ext.clear_data(file) } end + # Modify each resource to add data & options from frontmatter. + def manipulate_resource_list(resources) + resources.each do |resource| + fmdata = data(resource.path).first + + # Copy over special options + opts = fmdata.extract!(:layout, :layout_engine, :renderer_options) + if opts.has_key?(:renderer_options) + opts[:renderer_options].symbolize_keys! + end + + ignored = fmdata.delete(:ignored) + + # TODO: Enhance data? NOOOO + # TODO: stringify-keys? immutable/freeze? + + resource.add_metadata options: opts, page: fmdata + + # TODO: resource.ignore! if ignored + + # TODO: Save new template here somewhere? + end + end + def after_configuration app.ignore %r{\.frontmatter$} + # TODO: Add to file watcher ignore? - ::Middleman::Sitemap::Resource.send :include, ResourceInstanceMethods - - app.sitemap.provides_metadata do |path| - fmdata = data(path).first - - data = {} - - [:layout, :layout_engine].each do |opt| - data[opt] = fmdata[opt] unless fmdata[opt].nil? - end - - if fmdata[:renderer_options] - data[:renderer_options] = {} - fmdata[:renderer_options].each do |k, v| - data[:renderer_options][k.to_sym] = v - end - end - - { options: data } - end + # TODO: Replace all of this functionality + #::Middleman::Sitemap::Resource.send :include, ResourceInstanceMethods end module ResourceInstanceMethods diff --git a/middleman-core/lib/middleman-core/sitemap/resource.rb b/middleman-core/lib/middleman-core/sitemap/resource.rb index 4a68d3fa..0c42c059 100644 --- a/middleman-core/lib/middleman-core/sitemap/resource.rb +++ b/middleman-core/lib/middleman-core/sitemap/resource.rb @@ -46,7 +46,11 @@ module Middleman @source_file = source_file @destination_path = @path - @local_metadata = { options: {}, locals: {} } + # Options are generally rendering/sitemap options + # Locals are local variables for rendering this resource's template + # Page are data that is exposed through this resource's data member. + # Note: It is named 'page' for backwards compatibility with older MM. + @local_metadata = { options: {}, locals: {}, page: {} } end # Whether this resource has a template file @@ -70,6 +74,12 @@ module Middleman result end + # Data about this resource, populated from frontmatter or extensions. + # @return [HashWithIndifferentAccess] + def data + metadata[:page] + end + # Merge in new metadata specific to this resource. # @param [Hash] meta A metadata block like provides_metadata_for_path takes def add_metadata(meta={}) diff --git a/middleman-core/lib/middleman-core/sitemap/store.rb b/middleman-core/lib/middleman-core/sitemap/store.rb index 992cae00..eab6291d 100644 --- a/middleman-core/lib/middleman-core/sitemap/store.rb +++ b/middleman-core/lib/middleman-core/sitemap/store.rb @@ -19,9 +19,6 @@ module Middleman # which is the path relative to the source directory, minus any template # extensions. All "path" parameters used in this class are source paths. class Store - # @return [Middleman::Application] - attr_accessor :app - # Initialize with parent app # @param [Middleman::Application] app def initialize(app) @@ -54,7 +51,7 @@ module Middleman register_resource_list_manipulator(k, m.new(self)) end - app.config_context.class.send :delegate, :sitemap, to: :app + @app.config_context.class.send :delegate, :sitemap, to: :app end # Register an object which can transform the sitemap resource list. Best to register @@ -128,63 +125,6 @@ module Middleman @resources_not_ignored = nil end - # Register a handler to provide metadata on a file path - # @param [Regexp] matcher - # @return [Array>] - def provides_metadata(matcher=nil, &block) - @_provides_metadata ||= [] - @_provides_metadata << [block, matcher] if block_given? - @_provides_metadata - end - - # Get the metadata for a specific file - # @param [String] source_file - # @return [Hash] - def metadata_for_file(source_file) - blank_metadata = { options: {}, locals: {} } - - provides_metadata.reduce(blank_metadata) do |result, (callback, matcher)| - next result if matcher && !source_file.match(matcher) - - metadata = callback.call(source_file).dup - result.deep_merge(metadata) - end - end - - # Register a handler to provide metadata on a url path - # @param [Regexp] matcher - # @return [Array>] - def provides_metadata_for_path(matcher=nil, &block) - @_provides_metadata_for_path ||= [] - if block_given? - @_provides_metadata_for_path << [block, matcher] - @_cached_metadata = {} - end - @_provides_metadata_for_path - end - - # Get the metadata for a specific URL - # @param [String] request_path - # @return [Hash] - def metadata_for_path(request_path) - return @_cached_metadata[request_path] if @_cached_metadata[request_path] - - blank_metadata = { options: {}, locals: {} } - - @_cached_metadata[request_path] = provides_metadata_for_path.reduce(blank_metadata) do |result, (callback, matcher)| - case matcher - when Regexp - next result unless request_path =~ matcher - when String - next result unless File.fnmatch('/' + Util.strip_leading_slash(matcher), "/#{request_path}") - end - - metadata = callback.call(request_path).dup - - result.deep_merge(metadata) - end - end - # Get the URL path for an on-disk file # @param [String] file # @return [String] From c285848866c48c3f98d92f891d828104be8e8811 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sun, 27 Apr 2014 22:24:27 -0700 Subject: [PATCH 02/21] Clean up i18n a bit, stake out some territory around routing and resource --- .../core_extensions/front_matter.rb | 1 - .../middleman-core/core_extensions/i18n.rb | 37 ++++--------------- .../middleman-core/core_extensions/routing.rb | 5 +++ .../lib/middleman-core/sitemap/resource.rb | 31 ++++++---------- 4 files changed, 24 insertions(+), 50 deletions(-) diff --git a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb index 0d90a577..5685cc3b 100644 --- a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb +++ b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb @@ -55,7 +55,6 @@ module Middleman::CoreExtensions def after_configuration app.ignore %r{\.frontmatter$} - # TODO: Add to file watcher ignore? # TODO: Replace all of this functionality #::Middleman::Sitemap::Resource.send :include, ResourceInstanceMethods diff --git a/middleman-core/lib/middleman-core/core_extensions/i18n.rb b/middleman-core/lib/middleman-core/core_extensions/i18n.rb index 41e2ba0f..6b2d46ff 100644 --- a/middleman-core/lib/middleman-core/core_extensions/i18n.rb +++ b/middleman-core/lib/middleman-core/core_extensions/i18n.rb @@ -42,7 +42,6 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension # Don't output localizable files app.ignore File.join(options[:templates_dir], '**') - app.sitemap.provides_metadata_for_path(&method(:metadata_for_path)) app.files.changed(&method(:on_file_changed)) app.files.deleted(&method(:on_file_changed)) end @@ -56,14 +55,12 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension delegate :logger, to: :app def langs - @_langs ||= known_languages + @langs ||= known_languages end # Update the main sitemap resource list # @return [void] def manipulate_resource_list(resources) - @_localization_data = {} - new_resources = [] resources.each do |resource| @@ -81,6 +78,10 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension new_resources << build_resource(path, resource.path, page_id, lang) end end + + # This is for backwards compatibility with the old provides_metadata-based code + # that used to be in this extension, but I don't know how much sense it makes. + resource.add_metadata options: { lang: @mount_at_root }, locals: { lang: @mount_at_root } end resources + new_resources @@ -90,7 +91,7 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension def on_file_changed(file) if @locales_regex =~ file - @_langs = nil # Clear langs cache + @langs = nil # Clear langs cache ::I18n.reload! end end @@ -112,24 +113,6 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension end end - def metadata_for_path(url) - if d = localization_data(url) - lang, page_id = d - else - # Default to the @mount_at_root lang - page_id = nil - lang = @mount_at_root - end - - { - locals: { - lang: lang, - page_id: page_id - }, - options: { lang: lang } - } - end - def known_languages if options[:langs] Array(options[:langs]).map(&:to_sym) @@ -144,11 +127,6 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension end end - def localization_data(path) - @_localization_data ||= {} - @_localization_data[path] - end - # Parse locale extension filename # @return [lang, path, basename] # will return +nil+ if no locale extension @@ -183,10 +161,9 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension path = path.sub(options[:templates_dir] + '/', '') - @_localization_data[path] = [lang, path, localized_page_id] - p = ::Middleman::Sitemap::Resource.new(app.sitemap, path) p.proxy_to(source_path) + p.add_metadata locals: { lang: lang, page_id: path }, options: { lang: lang } ::I18n.locale = old_locale p diff --git a/middleman-core/lib/middleman-core/core_extensions/routing.rb b/middleman-core/lib/middleman-core/core_extensions/routing.rb index 04f05197..cc4d3e3c 100644 --- a/middleman-core/lib/middleman-core/core_extensions/routing.rb +++ b/middleman-core/lib/middleman-core/core_extensions/routing.rb @@ -14,7 +14,9 @@ module Middleman options = opts.dup # Default layout + # TODO: This seems wrong options[:layout] = @app.config[:layout] if options[:layout].nil? + # TODO: You can set options and locals, but not data metadata = { options: options, locals: options.delete(:locals) || {} } # If the url is a regexp @@ -37,6 +39,9 @@ module Middleman end # Setup a metadata matcher for rendering those options + # TODO: How to get rid of this? Perhaps a separate extension that manipulates + # in this sort of data? + # This is harder because sitemap isn't available. @app.sitemap.provides_metadata_for_path(url) { |_| metadata } end end diff --git a/middleman-core/lib/middleman-core/sitemap/resource.rb b/middleman-core/lib/middleman-core/sitemap/resource.rb index 0c42c059..c80a90b4 100644 --- a/middleman-core/lib/middleman-core/sitemap/resource.rb +++ b/middleman-core/lib/middleman-core/sitemap/resource.rb @@ -11,12 +11,8 @@ module Middleman include Middleman::Sitemap::Extensions::Traversal include Middleman::Sitemap::Extensions::ContentType - # @return [Middleman::Application] - attr_reader :app - delegate :logger, :instrument, to: :app - # @return [Middleman::Sitemap::Store] - attr_reader :store + #attr_reader :store # The source path of this resource (relative to the source directory, # without template extensions) @@ -29,9 +25,8 @@ module Middleman # Set the on-disk source file for this resource # @return [String] - # attr_reader :source_file - def source_file + # TODO: Make this work when get_source_file doesn't exist @source_file || get_source_file end @@ -63,9 +58,10 @@ module Middleman # Get the metadata for both the current source_file and the current path # @return [Hash] def metadata - result = store.metadata_for_path(path).dup + # TODO: Get rid of all this + result = @store.metadata_for_path(path).dup - file_meta = store.metadata_for_file(source_file).dup + file_meta = @store.metadata_for_file(source_file).dup result.deep_merge!(file_meta) local_meta = @local_metadata.dup @@ -77,6 +73,7 @@ module Middleman # Data about this resource, populated from frontmatter or extensions. # @return [HashWithIndifferentAccess] def data + # TODO: Upconvert/freeze at this point? metadata[:page] end @@ -96,18 +93,14 @@ module Middleman File.extname(path) end - def request_path - destination_path - end - # Render this resource # @return [String] def render(opts={}, locs={}) return ::Middleman::FileRenderer.new(@app, source_file).template_data_for_file unless template? - relative_source = Pathname(source_file).relative_path_from(Pathname(app.root)) + relative_source = Pathname(source_file).relative_path_from(Pathname(@app.root)) - instrument 'render.resource', path: relative_source, destination_path: destination_path do + @app.instrument 'render.resource', path: relative_source, destination_path: destination_path do md = metadata.dup opts = md[:options].deep_merge(opts) locs = md[:locals].deep_merge(locs) @@ -128,11 +121,11 @@ module Middleman # @return [String] def url url_path = destination_path - if app.config[:strip_index_file] - url_path = url_path.sub(/(^|\/)#{Regexp.escape(app.config[:index_file])}$/, - app.config[:trailing_slash] ? '/' : '') + if @app.config[:strip_index_file] + url_path = url_path.sub(/(^|\/)#{Regexp.escape(@app.config[:index_file])}$/, + @app.config[:trailing_slash] ? '/' : '') end - File.join(app.config[:http_prefix], url_path) + File.join(@app.config[:http_prefix], url_path) end # Whether the source file is binary. From d83d6e077c3fbcbb5f5cffa3c51911c006b8921f Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sun, 27 Apr 2014 22:50:19 -0700 Subject: [PATCH 03/21] Extensionize liquid, bring back provides_metadata_for_path --- .../core_extensions/rendering.rb | 3 +- .../lib/middleman-core/renderers/liquid.rb | 24 ++++++------- .../lib/middleman-core/sitemap/resource.rb | 15 ++++---- .../lib/middleman-core/sitemap/store.rb | 36 +++++++++++++++++++ 4 files changed, 54 insertions(+), 24 deletions(-) diff --git a/middleman-core/lib/middleman-core/core_extensions/rendering.rb b/middleman-core/lib/middleman-core/core_extensions/rendering.rb index 53f72265..d045f1a4 100644 --- a/middleman-core/lib/middleman-core/core_extensions/rendering.rb +++ b/middleman-core/lib/middleman-core/core_extensions/rendering.rb @@ -48,7 +48,8 @@ module Middleman # Liquid Support begin require 'middleman-core/renderers/liquid' - app.send :include, Middleman::Renderers::Liquid + Middleman::Extensions.register :liquid, Middleman::Renderers::Liquid + activate :liquid rescue LoadError end diff --git a/middleman-core/lib/middleman-core/renderers/liquid.rb b/middleman-core/lib/middleman-core/renderers/liquid.rb index 4fc93204..7f9f5157 100644 --- a/middleman-core/lib/middleman-core/renderers/liquid.rb +++ b/middleman-core/lib/middleman-core/renderers/liquid.rb @@ -4,23 +4,19 @@ require 'liquid' module Middleman module Renderers # Liquid Renderer - module Liquid - # Setup extension - class << self - # Once registerd - def registered(app) - # After config, setup liquid partial paths - app.after_configuration do - ::Liquid::Template.file_system = ::Liquid::LocalFileSystem.new(source_dir) + class Liquid < Middleman::Extension + # After config, setup liquid partial paths + def after_configuration + ::Liquid::Template.file_system = ::Liquid::LocalFileSystem.new(source_dir) + end - # Convert data object into a hash for liquid - sitemap.provides_metadata %r{\.liquid$} do - { locals: { data: data.to_h } } - end + def manipulate_resource_list(resources) + resources.each do |resource| + # Convert data object into a hash for liquid + if resource.source_file =~ %r{\.liquid$} + resource.add_metadata locals: { data: app.data.to_h } end end - - alias_method :included, :registered end end end diff --git a/middleman-core/lib/middleman-core/sitemap/resource.rb b/middleman-core/lib/middleman-core/sitemap/resource.rb index c80a90b4..345b0360 100644 --- a/middleman-core/lib/middleman-core/sitemap/resource.rb +++ b/middleman-core/lib/middleman-core/sitemap/resource.rb @@ -11,15 +11,12 @@ module Middleman include Middleman::Sitemap::Extensions::Traversal include Middleman::Sitemap::Extensions::ContentType - # @return [Middleman::Sitemap::Store] - #attr_reader :store - # The source path of this resource (relative to the source directory, # without template extensions) # @return [String] attr_reader :path - # The output path for this resource + # The output path in the build directory for this resource # @return [String] attr_accessor :destination_path @@ -78,15 +75,15 @@ module Middleman end # Merge in new metadata specific to this resource. - # @param [Hash] meta A metadata block like provides_metadata_for_path takes + # @param [Hash] meta A metadata block with keys :options, :locals, :page. + # Options are generally rendering/sitemap options + # Locals are local variables for rendering this resource's template + # Page are data that is exposed through this resource's data member. + # Note: It is named 'page' for backwards compatibility with older MM. def add_metadata(meta={}) @local_metadata.deep_merge!(meta.dup) end - # The output/preview URL for this resource - # @return [String] - attr_accessor :destination_path - # Extension of the path (i.e. '.js') # @return [String] def ext diff --git a/middleman-core/lib/middleman-core/sitemap/store.rb b/middleman-core/lib/middleman-core/sitemap/store.rb index eab6291d..882698de 100644 --- a/middleman-core/lib/middleman-core/sitemap/store.rb +++ b/middleman-core/lib/middleman-core/sitemap/store.rb @@ -25,8 +25,10 @@ module Middleman @app = app @resources = [] @_cached_metadata = {} + # TODO: Should this be a set or hash? @resource_list_manipulators = [] @needs_sitemap_rebuild = true + @provides_metadata_for_path = Set.new @lock = Monitor.new reset_lookup_cache! @@ -125,6 +127,40 @@ module Middleman @resources_not_ignored = nil end + # Register a handler to provide metadata on a url path + # Extensions authors should prefer adding metadata to Resources via a + # sitemap manipulator and Resource#add_metadata. + # + # @param [Regexp, String] matcher or glob string + def provides_metadata_for_path(matcher=nil, &block) + if block_given? + @provides_metadata_for_path << [block, matcher] + @_cached_metadata = {} + end + nil + end + + # Get the metadata for a specific source path + # @param [String] path Source path of a resource + # @return [Hash] + def metadata_for_path(path) + return @_cached_metadata[path] if @_cached_metadata.has_key?(path) + + blank_metadata = { options: {}, locals: {}, page: {} } + + @_cached_metadata[path] = @provides_metadata_for_path.inject(blank_metadata) do |result, (callback, matcher)| + case matcher + when Regexp + next result unless path =~ matcher + when String + next result unless File.fnmatch('/' + Util.strip_leading_slash(matcher), "/#{path}") + end + + metadata = callback.call(path) + result.deep_merge(metadata) + end + end + # Get the URL path for an on-disk file # @param [String] file # @return [String] From 5c04c2f42bb5355c946a86cc14dcc0113b6fc8a2 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sun, 27 Apr 2014 23:02:20 -0700 Subject: [PATCH 04/21] More trimming --- .../lib/middleman-core/sitemap/resource.rb | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/middleman-core/lib/middleman-core/sitemap/resource.rb b/middleman-core/lib/middleman-core/sitemap/resource.rb index 345b0360..ee907023 100644 --- a/middleman-core/lib/middleman-core/sitemap/resource.rb +++ b/middleman-core/lib/middleman-core/sitemap/resource.rb @@ -55,16 +55,8 @@ module Middleman # Get the metadata for both the current source_file and the current path # @return [Hash] def metadata - # TODO: Get rid of all this - result = @store.metadata_for_path(path).dup - - file_meta = @store.metadata_for_file(source_file).dup - result.deep_merge!(file_meta) - - local_meta = @local_metadata.dup - result.deep_merge!(local_meta) - - result + # TODO: The only reason we call metadata_for_page is to power the "page" DSL + @store.metadata_for_path(path).deep_merge @local_metadata end # Data about this resource, populated from frontmatter or extensions. @@ -81,7 +73,7 @@ module Middleman # Page are data that is exposed through this resource's data member. # Note: It is named 'page' for backwards compatibility with older MM. def add_metadata(meta={}) - @local_metadata.deep_merge!(meta.dup) + @local_metadata.deep_merge!(meta) end # Extension of the path (i.e. '.js') @@ -98,7 +90,7 @@ module Middleman relative_source = Pathname(source_file).relative_path_from(Pathname(@app.root)) @app.instrument 'render.resource', path: relative_source, destination_path: destination_path do - md = metadata.dup + md = metadata opts = md[:options].deep_merge(opts) locs = md[:locals].deep_merge(locs) locs[:current_path] ||= destination_path From adfad92f8f2b90c975a1db771e664ef5453d1cec Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sun, 27 Apr 2014 23:21:12 -0700 Subject: [PATCH 05/21] Get rid of raw_data --- .../core_extensions/front_matter.rb | 39 ++----------------- .../extensions/directory_indexes.rb | 7 +--- .../sitemap/extensions/content_type.rb | 4 +- .../sitemap/extensions/ignores.rb | 10 +++-- .../sitemap/extensions/redirects.rb | 8 ---- .../sitemap/extensions/request_endpoints.rb | 6 --- .../lib/middleman-core/sitemap/resource.rb | 33 +++++++++++----- 7 files changed, 36 insertions(+), 71 deletions(-) diff --git a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb index 5685cc3b..e37163e8 100644 --- a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb +++ b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb @@ -35,7 +35,9 @@ module Middleman::CoreExtensions fmdata = data(resource.path).first # Copy over special options - opts = fmdata.extract!(:layout, :layout_engine, :renderer_options) + # TODO: Should we make people put these under "options" instead of having + # special known keys? + opts = fmdata.extract!(:layout, :layout_engine, :renderer_options, :directory_index, :content_type) if opts.has_key?(:renderer_options) opts[:renderer_options].symbolize_keys! end @@ -55,41 +57,6 @@ module Middleman::CoreExtensions def after_configuration app.ignore %r{\.frontmatter$} - - # TODO: Replace all of this functionality - #::Middleman::Sitemap::Resource.send :include, ResourceInstanceMethods - end - - module ResourceInstanceMethods - def ignored? - if !proxy? && raw_data[:ignored] == true - true - else - super - end - end - - # This page's frontmatter without being enhanced for access by either symbols or strings. - # Used internally - # @private - # @return [Hash] - def raw_data - app.extensions[:front_matter].data(source_file).first - end - - # This page's frontmatter - # @return [Hash] - def data - @enhanced_data ||= ::Middleman::Util.recursively_enhance(raw_data).freeze - end - - # Override Resource#content_type to take into account frontmatter - def content_type - # Allow setting content type in frontmatter too - raw_data.fetch :content_type do - super - end - end end # Get the template data from a path diff --git a/middleman-core/lib/middleman-core/extensions/directory_indexes.rb b/middleman-core/lib/middleman-core/extensions/directory_indexes.rb index 68e9f958..b63eb64e 100644 --- a/middleman-core/lib/middleman-core/extensions/directory_indexes.rb +++ b/middleman-core/lib/middleman-core/extensions/directory_indexes.rb @@ -16,11 +16,8 @@ class Middleman::Extensions::DirectoryIndexes < ::Middleman::Extension resource.destination_path.end_with?(new_index_path) || File.extname(index_file) != resource.ext - # Check if frontmatter turns directory_index off - next if resource.raw_data[:directory_index] == false - - # Check if file metadata (options set by "page" in config.rb) turns directory_index off - next if resource.metadata[:options][:directory_index] == false + # Check if file metadata (options set by "page" in config.rb or frontmatter) turns directory_index off + next if resource.options[:directory_index] == false resource.destination_path = resource.destination_path.chomp(File.extname(index_file)) + new_index_path end diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/content_type.rb b/middleman-core/lib/middleman-core/sitemap/extensions/content_type.rb index 619ec225..23325012 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/content_type.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/content_type.rb @@ -5,8 +5,8 @@ module Middleman::Sitemap::Extensions module ContentType # The preferred MIME content type for this resource def content_type - # Allow explcitly setting content type from page/proxy options - meta_type = metadata[:options][:content_type] + # Allow explcitly setting content type from page/proxy options or frontmatter + meta_type = options[:content_type] return meta_type if meta_type # Look up mime type based on extension diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/ignores.rb b/middleman-core/lib/middleman-core/sitemap/extensions/ignores.rb index 414cd7e9..0edb3bd6 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/ignores.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/ignores.rb @@ -51,10 +51,12 @@ module Middleman # Whether the Resource is ignored # @return [Boolean] def ignored? - @app.sitemap.ignored?(path) || - (!proxy? && - @app.sitemap.ignored?(source_file.sub("#{@app.source_dir}/", '')) - ) + # Check frontmatter/data + return true if !proxy? && data[:ignored] == true + # Ignore based on the source path (without template extensions) + return true if @app.sitemap.ignored?(path) + # This allows files to be ignored by their source file name (with template extensions) + !proxy? && @app.sitemap.ignored?(source_file.sub("#{@app.source_dir}/", '')) end end end diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb b/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb index cfff4197..dec6d825 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb @@ -75,18 +75,10 @@ module Middleman end end - # def request_path - # @request_path - # end - def binary? false end - def raw_data - {} - end - def ignored? false end diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb b/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb index 7c4b85c3..265c5d92 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb @@ -63,16 +63,10 @@ module Middleman return output.call if output end - attr_reader :request_path - def binary? false end - def raw_data - {} - end - def ignored? false end diff --git a/middleman-core/lib/middleman-core/sitemap/resource.rb b/middleman-core/lib/middleman-core/sitemap/resource.rb index ee907023..5b039cc6 100644 --- a/middleman-core/lib/middleman-core/sitemap/resource.rb +++ b/middleman-core/lib/middleman-core/sitemap/resource.rb @@ -52,6 +52,16 @@ module Middleman !::Tilt[source_file].nil? end + # Merge in new metadata specific to this resource. + # @param [Hash] meta A metadata block with keys :options, :locals, :page. + # Options are generally rendering/sitemap options + # Locals are local variables for rendering this resource's template + # Page are data that is exposed through this resource's data member. + # Note: It is named 'page' for backwards compatibility with older MM. + def add_metadata(meta={}) + @local_metadata.deep_merge!(meta) + end + # Get the metadata for both the current source_file and the current path # @return [Hash] def metadata @@ -62,18 +72,21 @@ module Middleman # Data about this resource, populated from frontmatter or extensions. # @return [HashWithIndifferentAccess] def data - # TODO: Upconvert/freeze at this point? - metadata[:page] + # TODO: Should this really be a HashWithIndifferentAccess? + ::Middleman::Util.recursively_enhance(metadata[:page]).freeze end - # Merge in new metadata specific to this resource. - # @param [Hash] meta A metadata block with keys :options, :locals, :page. - # Options are generally rendering/sitemap options - # Locals are local variables for rendering this resource's template - # Page are data that is exposed through this resource's data member. - # Note: It is named 'page' for backwards compatibility with older MM. - def add_metadata(meta={}) - @local_metadata.deep_merge!(meta) + # Options about how this resource is rendered, such as its :layout, + # :renderer_options, and whether or not to use :directory_indexes. + # @return [Hash] + def options + metadata[:options] + end + + # Local variable mappings that are used when rendering the template for this resource. + # @return [Hash] + def locals + metadata[:locals] end # Extension of the path (i.e. '.js') From d687677e38e845f28d9b893053979ed8c5580b8d Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sun, 27 Apr 2014 23:25:10 -0700 Subject: [PATCH 06/21] Mess around with liquid --- middleman-core/lib/middleman-core/core_extensions/rendering.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/middleman-core/lib/middleman-core/core_extensions/rendering.rb b/middleman-core/lib/middleman-core/core_extensions/rendering.rb index d045f1a4..42854825 100644 --- a/middleman-core/lib/middleman-core/core_extensions/rendering.rb +++ b/middleman-core/lib/middleman-core/core_extensions/rendering.rb @@ -48,8 +48,7 @@ module Middleman # Liquid Support begin require 'middleman-core/renderers/liquid' - Middleman::Extensions.register :liquid, Middleman::Renderers::Liquid - activate :liquid + Middleman::Extensions.register :liquid, Middleman::Renderers::Liquid, auto_activate: :before_configuration rescue LoadError end From 78b7bbb92aa23385c759fb0d6ee98619c3f50c5a Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 10 May 2014 18:03:56 -0700 Subject: [PATCH 07/21] TODO metadata for path extension --- .../sitemap/extensions/metadata_for_path.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 middleman-core/lib/middleman-core/sitemap/extensions/metadata_for_path.rb diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/metadata_for_path.rb b/middleman-core/lib/middleman-core/sitemap/extensions/metadata_for_path.rb new file mode 100644 index 00000000..10463cab --- /dev/null +++ b/middleman-core/lib/middleman-core/sitemap/extensions/metadata_for_path.rb @@ -0,0 +1,17 @@ +module Middleman + module Sitemap + module Extensions + + # Add metadata to Resources based on path matchers. This exists + # entirely to support the "page" method in config.rb. + + # TODO: This requires the concept of priority for sitemap manipulators + # in order for it to always run after all other manipulators. + class MetadataForPath + def initialize(sitemap) + @app = sitemap.app + end + end + end + end +end From 213c67296906076029823fd087e0cae8c4490b9a Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Tue, 27 May 2014 23:05:37 -0700 Subject: [PATCH 08/21] Fix a lot of breakage caused by removing/hiding accessors --- middleman-cli/lib/middleman-cli/build.rb | 2 +- .../lib/middleman-core/renderers/liquid.rb | 2 +- .../middleman-core/sitemap/extensions/ignores.rb | 8 ++++---- .../middleman-core/sitemap/extensions/on_disk.rb | 4 ++-- .../middleman-core/sitemap/extensions/proxies.rb | 8 ++++---- .../sitemap/extensions/redirects.rb | 8 ++++++-- .../sitemap/extensions/request_endpoints.rb | 4 ++-- .../sitemap/extensions/traversal.rb | 16 ++++++++-------- .../lib/middleman-core/sitemap/store.rb | 15 +++++++++------ 9 files changed, 37 insertions(+), 30 deletions(-) diff --git a/middleman-cli/lib/middleman-cli/build.rb b/middleman-cli/lib/middleman-cli/build.rb index 60538431..80916a02 100644 --- a/middleman-cli/lib/middleman-cli/build.rb +++ b/middleman-cli/lib/middleman-cli/build.rb @@ -254,7 +254,7 @@ module Middleman::Cli FileUtils.cp(resource.source_file, output_file) else begin - response = @rack.get(URI.escape(resource.request_path)) + response = @rack.get(URI.escape(resource.destination_path)) if response.status == 200 base.create_file(output_file, binary_encode(response.body)) diff --git a/middleman-core/lib/middleman-core/renderers/liquid.rb b/middleman-core/lib/middleman-core/renderers/liquid.rb index 7f9f5157..a6fb2ef8 100644 --- a/middleman-core/lib/middleman-core/renderers/liquid.rb +++ b/middleman-core/lib/middleman-core/renderers/liquid.rb @@ -7,7 +7,7 @@ module Middleman class Liquid < Middleman::Extension # After config, setup liquid partial paths def after_configuration - ::Liquid::Template.file_system = ::Liquid::LocalFileSystem.new(source_dir) + ::Liquid::Template.file_system = ::Liquid::LocalFileSystem.new(app.source_dir) end def manipulate_resource_list(resources) diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/ignores.rb b/middleman-core/lib/middleman-core/sitemap/extensions/ignores.rb index 0edb3bd6..5a4f36b1 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/ignores.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/ignores.rb @@ -3,15 +3,15 @@ module Middleman module Extensions # Class to handle managing ignores class Ignores - def initialize(sitemap) - @app = sitemap.app + def initialize(app, sitemap) + @app = app @app.add_to_config_context :ignore, &method(:create_ignore) - @app.define_singleton_method(:ignore, &method(:create_ignore)) + @app.define_singleton_method :ignore, &method(:create_ignore) # Array of callbacks which can ass ignored @ignored_callbacks = [] - sitemap.define_singleton_method(:ignored?, &method(:ignored?)) + sitemap.define_singleton_method :ignored?, &method(:ignored?) ::Middleman::Sitemap::Resource.send :include, IgnoreResourceInstanceMethods end diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb b/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb index 2c51063e..b259c43e 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb @@ -7,9 +7,9 @@ module Middleman attr_accessor :sitemap attr_accessor :waiting_for_ready - def initialize(sitemap) + def initialize(app, sitemap) @sitemap = sitemap - @app = @sitemap.app + @app = app @file_paths_on_disk = Set.new scoped_self = self diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb b/middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb index 4c4ca5d2..37f76894 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb @@ -4,8 +4,8 @@ module Middleman # Manages the list of proxy configurations and manipulates the sitemap # to include new resources based on those configurations class Proxies - def initialize(sitemap) - @app = sitemap.app + def initialize(app) + @app = app @app.add_to_config_context :proxy, &method(:create_proxy) @app.define_singleton_method(:proxy, &method(:create_proxy)) @@ -109,10 +109,10 @@ module Middleman # if there is no resource. # @return [Sitemap::Resource] def proxied_to_resource - proxy_resource = store.find_resource_by_path(proxied_to) + proxy_resource = @store.find_resource_by_path(proxied_to) unless proxy_resource - raise "Path #{path} proxies to unknown file #{proxied_to}:#{store.resources.map(&:path)}" + raise "Path #{path} proxies to unknown file #{proxied_to}:#{@store.resources.map(&:path)}" end if proxy_resource.proxy? diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb b/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb index dec6d825..058595f2 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb @@ -6,8 +6,8 @@ module Middleman # Manages the list of proxy configurations and manipulates the sitemap # to include new resources based on those configurations class Redirects - def initialize(sitemap) - @app = sitemap.app + def initialize(app) + @app = app @app.add_to_config_context :redirect, &method(:create_redirect) @redirects = {} @@ -86,6 +86,10 @@ module Middleman def metadata @local_metadata.dup end + + def get_source_file + '' + end end end end diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb b/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb index 265c5d92..cc980879 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb @@ -4,8 +4,8 @@ module Middleman class RequestEndpoints # Manages the list of proxy configurations and manipulates the sitemap # to include new resources based on those configurations - def initialize(sitemap) - @app = sitemap.app + def initialize(app) + @app = app @app.add_to_config_context :endpoint, &method(:create_endpoint) @endpoints = {} diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/traversal.rb b/middleman-core/lib/middleman-core/sitemap/extensions/traversal.rb index 50a0df6f..140d8f74 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/traversal.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/traversal.rb @@ -7,7 +7,7 @@ module Middleman def parent parts = path.split('/') tail = parts.pop - is_index = (tail == app.config[:index_file]) + is_index = (tail == @app.config[:index_file]) return nil if is_index && parts.length < 1 @@ -21,7 +21,7 @@ module Middleman found else parts.pop if is_index - store.find_resource_by_destination_path("#{parts.join('/')}/#{app.config[:index_file]}") + store.find_resource_by_destination_path("#{parts.join('/')}/#{@app.config[:index_file]}") end end @@ -34,11 +34,11 @@ module Middleman base_path = eponymous_directory_path prefix = %r{^#{base_path.sub("/", "\\/")}} else - base_path = path.sub("#{app.config[:index_file]}", '') + base_path = path.sub("#{@app.config[:index_file]}", '') prefix = %r{^#{base_path.sub("/", "\\/")}} end - store.resources.select do |sub_resource| + @store.resources.select do |sub_resource| if sub_resource.path == path || sub_resource.path !~ prefix false else @@ -47,7 +47,7 @@ module Middleman if parts.length == 1 true elsif parts.length == 2 - parts.last == app.config[:index_file] + parts.last == @app.config[:index_file] else false end @@ -65,17 +65,17 @@ module Middleman # Whether this resource is either a directory index, or has the same name as an existing directory in the source # @return [Boolean] def directory_index? - path.include?(app.config[:index_file]) || path =~ /\/$/ || eponymous_directory? + path.include?(@app.config[:index_file]) || path =~ /\/$/ || eponymous_directory? end # Whether the resource has the same name as a directory in the source # (e.g., if the resource is named 'gallery.html' and a path exists named 'gallery/', this would return true) # @return [Boolean] def eponymous_directory? - if !path.end_with?("/#{app.config[:index_file]}") && destination_path.end_with?("/#{app.config[:index_file]}") + if !path.end_with?("/#{@app.config[:index_file]}") && destination_path.end_with?("/#{@app.config[:index_file]}") return true end - full_path = File.join(app.source_dir, eponymous_directory_path) + full_path = File.join(@app.source_dir, eponymous_directory_path) File.exist?(full_path) && File.directory?(full_path) end diff --git a/middleman-core/lib/middleman-core/sitemap/store.rb b/middleman-core/lib/middleman-core/sitemap/store.rb index 882698de..2de8ecf5 100644 --- a/middleman-core/lib/middleman-core/sitemap/store.rb +++ b/middleman-core/lib/middleman-core/sitemap/store.rb @@ -19,6 +19,9 @@ module Middleman # which is the path relative to the source directory, minus any template # extensions. All "path" parameters used in this class are source paths. class Store + # @return [Middleman::Application] + attr_reader :app + # Initialize with parent app # @param [Middleman::Application] app def initialize(app) @@ -34,23 +37,23 @@ module Middleman reset_lookup_cache! # Handle ignore commands - Middleman::Sitemap::Extensions::Ignores.new(self) + Middleman::Sitemap::Extensions::Ignores.new(@app, self) # Extensions { # Register classes which can manipulate the main site map list - on_disk: Middleman::Sitemap::Extensions::OnDisk, + on_disk: Middleman::Sitemap::Extensions::OnDisk.new(@app, self), # Request Endpoints - request_endpoints: Middleman::Sitemap::Extensions::RequestEndpoints, + request_endpoints: Middleman::Sitemap::Extensions::RequestEndpoints.new(@app), # Proxies - proxies: Middleman::Sitemap::Extensions::Proxies, + proxies: Middleman::Sitemap::Extensions::Proxies.new(@app), # Redirects - redirects: Middleman::Sitemap::Extensions::Redirects + redirects: Middleman::Sitemap::Extensions::Redirects.new(@app) }.each do |k, m| - register_resource_list_manipulator(k, m.new(self)) + register_resource_list_manipulator(k, m) end @app.config_context.class.send :delegate, :sitemap, to: :app From bf8f02d5631b2eb7e547202f36f041a9784d607b Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Wed, 28 May 2014 00:00:36 -0700 Subject: [PATCH 09/21] Turn routing into an extension --- .../lib/middleman-core/config_context.rb | 5 -- .../lib/middleman-core/core_extensions.rb | 6 ++ .../core_extensions/extensions.rb | 3 + .../middleman-core/core_extensions/routing.rb | 73 ++++++++++++++----- .../sitemap/extensions/metadata_for_path.rb | 17 ----- .../sitemap/extensions/redirects.rb | 6 +- .../sitemap/extensions/request_endpoints.rb | 4 +- .../lib/middleman-core/sitemap/resource.rb | 11 +-- .../lib/middleman-core/sitemap/store.rb | 36 --------- 9 files changed, 70 insertions(+), 91 deletions(-) delete mode 100644 middleman-core/lib/middleman-core/sitemap/extensions/metadata_for_path.rb diff --git a/middleman-core/lib/middleman-core/config_context.rb b/middleman-core/lib/middleman-core/config_context.rb index 96f30d1d..90922ea9 100644 --- a/middleman-core/lib/middleman-core/config_context.rb +++ b/middleman-core/lib/middleman-core/config_context.rb @@ -1,10 +1,5 @@ -require 'middleman-core/core_extensions/routing' - module Middleman class ConfigContext - # page routing - include Middleman::CoreExtensions::Routing - attr_reader :app # Whitelist methods that can reach out. diff --git a/middleman-core/lib/middleman-core/core_extensions.rb b/middleman-core/lib/middleman-core/core_extensions.rb index e85afac3..4ff25339 100644 --- a/middleman-core/lib/middleman-core/core_extensions.rb +++ b/middleman-core/lib/middleman-core/core_extensions.rb @@ -45,6 +45,12 @@ Middleman::Extensions.register :lorem, auto_activate: :before_configuration do Middleman::Extensions::Lorem end +Middleman::Extensions.register :routing, auto_activate: :before_configuration do + require 'middleman-core/core_extensions/routing' + Middleman::CoreExtensions::Routing +end + + ### # Setup Optional Extensions ### diff --git a/middleman-core/lib/middleman-core/core_extensions/extensions.rb b/middleman-core/lib/middleman-core/core_extensions/extensions.rb index 7a76776c..2cfb6c41 100644 --- a/middleman-core/lib/middleman-core/core_extensions/extensions.rb +++ b/middleman-core/lib/middleman-core/core_extensions/extensions.rb @@ -105,6 +105,9 @@ module Middleman run_hook :before_configuration + # Evaluate a passed block if given + config_context.instance_exec(&block) if block_given? + # Check for and evaluate local configuration in `config.rb` local_config = File.join(root, 'config.rb') if File.exist? local_config diff --git a/middleman-core/lib/middleman-core/core_extensions/routing.rb b/middleman-core/lib/middleman-core/core_extensions/routing.rb index cc4d3e3c..07150f7e 100644 --- a/middleman-core/lib/middleman-core/core_extensions/routing.rb +++ b/middleman-core/lib/middleman-core/core_extensions/routing.rb @@ -1,16 +1,55 @@ # Routing extension module Middleman module CoreExtensions - module Routing - # The page method allows the layout to be set on a specific path + class Routing < Extension + # This should always run late, but not as late as :directory_indexes, + # so it can add metadata to any pages generated by other extensions + self.resource_list_manipulator_priority = 90 + + def initialize(app, options_hash={}, &block) + super + + @page_configs = [] + end + + def before_configuration + app.add_to_config_context :page, &method(:page) + end + + def manipulate_resource_list(resources) + resources.each do |resource| + @page_configs.each do |matcher, metadata| + case matcher + when Regexp + next unless resource.path =~ matcher + when String + next unless File.fnmatch('/' + Util.strip_leading_slash(matcher), "/#{resource.path}") + end + + resource.add_metadata metadata + end + end + end + + # The page method allows options to be set for a given source path, regex, or glob. + # Options that may be set include layout, locals, proxy, andx ignore. # - # page "/about.html", layout: false - # page "/", layout: :homepage_layout + # @example + # page '/about.html', layout: false + # @example + # page '/index.html', layout: :homepage_layout + # @example + # page '/foo.html', locals: { foo: 'bar' } # - # @param [String] url - # @param [Hash] opts + # @param [String, Regexp] path A source path, or a Regexp/glob that can match multiple resources. + # @params [Hash] opts Options to apply to all matching resources. Undocumented options are passed on as page metadata to be used by extensions. + # @option opts [Symbol, Boolean, String] layout The layout name to use (e.g. `:article`) or `false` to disable layout. + # @option opts [Boolean] directory_indexes Whether or not the `:directory_indexes` extension applies to these paths. + # @option opts [Hash] locals Local variables for the template. These will be available when the template renders. + # @option opts [String] proxy The source path for a template to proxy this path to. Only valid when a single path is provided. Prefer using the `proxy` method to do this. + # @option opts [Boolean] ignore Set to `true` to ignore the provided path(s). Only valid when a single path is provided. Prefer using the `ignore` method to do this. # @return [void] - def page(url, opts={}) + def page(path, opts={}) options = opts.dup # Default layout @@ -19,30 +58,26 @@ module Middleman # TODO: You can set options and locals, but not data metadata = { options: options, locals: options.delete(:locals) || {} } - # If the url is a regexp - unless url.is_a?(Regexp) || url.include?('*') + # If the path is a regexp + unless path.is_a?(Regexp) || path.include?('*') # Normalized path - url = '/' + Middleman::Util.normalize_path(url) - if url.end_with?('/') || File.directory?(File.join(@app.source_dir, url)) - url = File.join(url, @app.config[:index_file]) + path = '/' + Middleman::Util.normalize_path(path) + if path.end_with?('/') || File.directory?(File.join(@app.source_dir, path)) + path = File.join(path, @app.config[:index_file]) end # Setup proxy if target = options.delete(:proxy) # TODO: deprecate proxy through page? - @app.proxy(url, target, opts.dup) + @app.proxy(path, target, opts.dup) return elsif options.delete(:ignore) # TODO: deprecate ignore through page? - @app.ignore(url) + @app.ignore(path) end end - # Setup a metadata matcher for rendering those options - # TODO: How to get rid of this? Perhaps a separate extension that manipulates - # in this sort of data? - # This is harder because sitemap isn't available. - @app.sitemap.provides_metadata_for_path(url) { |_| metadata } + @page_configs << [path, metadata] end end end diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/metadata_for_path.rb b/middleman-core/lib/middleman-core/sitemap/extensions/metadata_for_path.rb deleted file mode 100644 index 10463cab..00000000 --- a/middleman-core/lib/middleman-core/sitemap/extensions/metadata_for_path.rb +++ /dev/null @@ -1,17 +0,0 @@ -module Middleman - module Sitemap - module Extensions - - # Add metadata to Resources based on path matchers. This exists - # entirely to support the "page" method in config.rb. - - # TODO: This requires the concept of priority for sitemap manipulators - # in order for it to always run after all other manipulators. - class MetadataForPath - def initialize(sitemap) - @app = sitemap.app - end - end - end - end -end diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb b/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb index 058595f2..60e5bdbb 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb @@ -53,7 +53,7 @@ module Middleman end def render(*) - url = ::Middleman::Util.url_for(store.app, @request_path, + url = ::Middleman::Util.url_for(@store.app, @request_path, relative: false, find_resource: true ) @@ -83,10 +83,6 @@ module Middleman false end - def metadata - @local_metadata.dup - end - def get_source_file '' end diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb b/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb index cc980879..18bc996e 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb @@ -71,8 +71,8 @@ module Middleman false end - def metadata - @local_metadata.dup + def get_source_file + '' end end end diff --git a/middleman-core/lib/middleman-core/sitemap/resource.rb b/middleman-core/lib/middleman-core/sitemap/resource.rb index 5b039cc6..16ac10f0 100644 --- a/middleman-core/lib/middleman-core/sitemap/resource.rb +++ b/middleman-core/lib/middleman-core/sitemap/resource.rb @@ -42,7 +42,7 @@ module Middleman # Locals are local variables for rendering this resource's template # Page are data that is exposed through this resource's data member. # Note: It is named 'page' for backwards compatibility with older MM. - @local_metadata = { options: {}, locals: {}, page: {} } + @metadata = { options: {}, locals: {}, page: {} } end # Whether this resource has a template file @@ -59,15 +59,12 @@ module Middleman # Page are data that is exposed through this resource's data member. # Note: It is named 'page' for backwards compatibility with older MM. def add_metadata(meta={}) - @local_metadata.deep_merge!(meta) + @metadata.deep_merge!(meta) end - # Get the metadata for both the current source_file and the current path + # The metadata for this resource # @return [Hash] - def metadata - # TODO: The only reason we call metadata_for_page is to power the "page" DSL - @store.metadata_for_path(path).deep_merge @local_metadata - end + attr_reader :metadata # Data about this resource, populated from frontmatter or extensions. # @return [HashWithIndifferentAccess] diff --git a/middleman-core/lib/middleman-core/sitemap/store.rb b/middleman-core/lib/middleman-core/sitemap/store.rb index 2de8ecf5..c12a1e6f 100644 --- a/middleman-core/lib/middleman-core/sitemap/store.rb +++ b/middleman-core/lib/middleman-core/sitemap/store.rb @@ -27,11 +27,9 @@ module Middleman def initialize(app) @app = app @resources = [] - @_cached_metadata = {} # TODO: Should this be a set or hash? @resource_list_manipulators = [] @needs_sitemap_rebuild = true - @provides_metadata_for_path = Set.new @lock = Monitor.new reset_lookup_cache! @@ -130,40 +128,6 @@ module Middleman @resources_not_ignored = nil end - # Register a handler to provide metadata on a url path - # Extensions authors should prefer adding metadata to Resources via a - # sitemap manipulator and Resource#add_metadata. - # - # @param [Regexp, String] matcher or glob string - def provides_metadata_for_path(matcher=nil, &block) - if block_given? - @provides_metadata_for_path << [block, matcher] - @_cached_metadata = {} - end - nil - end - - # Get the metadata for a specific source path - # @param [String] path Source path of a resource - # @return [Hash] - def metadata_for_path(path) - return @_cached_metadata[path] if @_cached_metadata.has_key?(path) - - blank_metadata = { options: {}, locals: {}, page: {} } - - @_cached_metadata[path] = @provides_metadata_for_path.inject(blank_metadata) do |result, (callback, matcher)| - case matcher - when Regexp - next result unless path =~ matcher - when String - next result unless File.fnmatch('/' + Util.strip_leading_slash(matcher), "/#{path}") - end - - metadata = callback.call(path) - result.deep_merge(metadata) - end - end - # Get the URL path for an on-disk file # @param [String] file # @return [String] From 096f5ee35657d8e3c683896f0dbe53dbc1aa81db Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Fri, 30 May 2014 21:50:10 -0700 Subject: [PATCH 10/21] Use source file, not path, to look up frontmatter data --- .../lib/middleman-core/core_extensions/front_matter.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb index e37163e8..8b0cbb11 100644 --- a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb +++ b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb @@ -32,7 +32,9 @@ module Middleman::CoreExtensions # Modify each resource to add data & options from frontmatter. def manipulate_resource_list(resources) resources.each do |resource| - fmdata = data(resource.path).first + next if resource.source_file.blank? + + fmdata = data(resource.source_file).first.dup # Copy over special options # TODO: Should we make people put these under "options" instead of having @@ -76,6 +78,8 @@ module Middleman::CoreExtensions data = external_data.deep_merge(data) end + + [data, content] end end From 0309753561469817041882ffb578c74e3e388437 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Fri, 30 May 2014 22:04:32 -0700 Subject: [PATCH 11/21] Change when file listeners are registered in order to make cache invalidation work --- .../middleman-core/sitemap/extensions/on_disk.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb b/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb index b259c43e..de9569ce 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb @@ -15,14 +15,16 @@ module Middleman scoped_self = self @waiting_for_ready = true - # Register file change callback - @app.files.changed do |file| - scoped_self.touch_file(file) - end + @app.before_configuration do + # Register file change callback + files.changed do |file| + scoped_self.touch_file(file) + end - # Register file delete callback - @app.files.deleted do |file| - scoped_self.remove_file(file) + # Register file delete callback + files.deleted do |file| + scoped_self.remove_file(file) + end end @app.ready do From 5760d64ef9e6417efc5b09f453cce7b5833e6076 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Fri, 30 May 2014 22:22:24 -0700 Subject: [PATCH 12/21] More ignores --- .../lib/middleman-core/core_extensions/front_matter.rb | 3 +++ .../lib/middleman-core/sitemap/extensions/ignores.rb | 9 +++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb index 8b0cbb11..faf0bb12 100644 --- a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb +++ b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb @@ -52,6 +52,9 @@ module Middleman::CoreExtensions resource.add_metadata options: opts, page: fmdata # TODO: resource.ignore! if ignored + # TODO: This doesn't really work because resources can't themselves be ignored / when + # an ignore rule is in place it's forever + resource.ignore! if ignored == true && !resource.proxy? # TODO: Save new template here somewhere? end diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/ignores.rb b/middleman-core/lib/middleman-core/sitemap/extensions/ignores.rb index 5a4f36b1..b12e6c73 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/ignores.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/ignores.rb @@ -48,11 +48,16 @@ module Middleman # Helpers methods for Resources module IgnoreResourceInstanceMethods + # Ignore a resource directly, without going through the whole + # ignore filter stuff. + def ignore! + @ignored = true + end + # Whether the Resource is ignored # @return [Boolean] def ignored? - # Check frontmatter/data - return true if !proxy? && data[:ignored] == true + return true if @ignored # Ignore based on the source path (without template extensions) return true if @app.sitemap.ignored?(path) # This allows files to be ignored by their source file name (with template extensions) From b0ea4e7608ecb347a0c3c732583304a502bf5bf1 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Fri, 30 May 2014 22:32:39 -0700 Subject: [PATCH 13/21] Tweak resource manipulator order --- .../lib/middleman-core/core_extensions/front_matter.rb | 3 +++ middleman-core/lib/middleman-core/core_extensions/routing.rb | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb index faf0bb12..42e8ee98 100644 --- a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb +++ b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb @@ -10,6 +10,9 @@ require 'active_support/json' # Extensions namespace module Middleman::CoreExtensions class FrontMatter < ::Middleman::Extension + # Try to run after routing but before directory_indexes + self.resource_list_manipulator_priority = 90 + YAML_ERRORS = [StandardError] # https://github.com/tenderlove/psych/issues/23 diff --git a/middleman-core/lib/middleman-core/core_extensions/routing.rb b/middleman-core/lib/middleman-core/core_extensions/routing.rb index 07150f7e..9769c943 100644 --- a/middleman-core/lib/middleman-core/core_extensions/routing.rb +++ b/middleman-core/lib/middleman-core/core_extensions/routing.rb @@ -4,7 +4,7 @@ module Middleman class Routing < Extension # This should always run late, but not as late as :directory_indexes, # so it can add metadata to any pages generated by other extensions - self.resource_list_manipulator_priority = 90 + self.resource_list_manipulator_priority = 80 def initialize(app, options_hash={}, &block) super From 2beb774eb96070f7ba5960d6ed352796686fa127 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Fri, 30 May 2014 22:46:15 -0700 Subject: [PATCH 14/21] Put back request_path, I get it now --- middleman-cli/lib/middleman-cli/build.rb | 2 +- .../middleman-core/sitemap/extensions/request_endpoints.rb | 7 ++++--- middleman-core/lib/middleman-core/sitemap/resource.rb | 5 +++++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/middleman-cli/lib/middleman-cli/build.rb b/middleman-cli/lib/middleman-cli/build.rb index 80916a02..60538431 100644 --- a/middleman-cli/lib/middleman-cli/build.rb +++ b/middleman-cli/lib/middleman-cli/build.rb @@ -254,7 +254,7 @@ module Middleman::Cli FileUtils.cp(resource.source_file, output_file) else begin - response = @rack.get(URI.escape(resource.destination_path)) + response = @rack.get(URI.escape(resource.request_path)) if response.status == 200 base.create_file(output_file, binary_encode(response.body)) diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb b/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb index 18bc996e..b89ec083 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb @@ -49,12 +49,13 @@ module Middleman class EndpointResource < ::Middleman::Sitemap::Resource attr_accessor :output - def initialize(store, path, source_file) - @request_path = ::Middleman::Util.normalize_path(source_file) - + def initialize(store, path, request_path) super(store, path) + @request_path = ::Middleman::Util.normalize_path(request_path) end + attr_reader :request_path + def template? true end diff --git a/middleman-core/lib/middleman-core/sitemap/resource.rb b/middleman-core/lib/middleman-core/sitemap/resource.rb index 16ac10f0..62551ad5 100644 --- a/middleman-core/lib/middleman-core/sitemap/resource.rb +++ b/middleman-core/lib/middleman-core/sitemap/resource.rb @@ -20,6 +20,11 @@ module Middleman # @return [String] attr_accessor :destination_path + # The path to use when requesting this resource. Normally it's + # the same as {#destination_path} but it can be overridden in subclasses. + # @return [String] + alias_method :request_path, :destination_path + # Set the on-disk source file for this resource # @return [String] def source_file From cb2b13778e1c7253a61b279817b6a0e1bab2f78a Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Fri, 30 May 2014 23:03:42 -0700 Subject: [PATCH 15/21] Fix i18n --- middleman-core/lib/middleman-core/core_extensions/i18n.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/middleman-core/lib/middleman-core/core_extensions/i18n.rb b/middleman-core/lib/middleman-core/core_extensions/i18n.rb index 6b2d46ff..6f4f0012 100644 --- a/middleman-core/lib/middleman-core/core_extensions/i18n.rb +++ b/middleman-core/lib/middleman-core/core_extensions/i18n.rb @@ -27,9 +27,9 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension end def after_configuration - app.files.reload_path(app.config[:locals_dir] || options[:data]) + app.files.reload_path(app.config[:locales_dir] || options[:data]) - @locales_glob = File.join(app.config[:locals_dir] || options[:data], '**', '*.{rb,yml,yaml}') + @locales_glob = File.join(app.config[:locales_dir] || options[:data], '**', '*.{rb,yml,yaml}') @locales_regex = convert_glob_to_regex(@locales_glob) @maps = {} @@ -81,7 +81,7 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension # This is for backwards compatibility with the old provides_metadata-based code # that used to be in this extension, but I don't know how much sense it makes. - resource.add_metadata options: { lang: @mount_at_root }, locals: { lang: @mount_at_root } + #resource.add_metadata options: { lang: @mount_at_root }, locals: { lang: @mount_at_root } end resources + new_resources From 85cebdb7e94ab901ab2db975e98db96aebff3625 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Fri, 30 May 2014 23:19:33 -0700 Subject: [PATCH 16/21] rubocop --- middleman-core/lib/middleman-core/core_extensions.rb | 1 - .../lib/middleman-core/core_extensions/front_matter.rb | 9 +-------- .../lib/middleman-core/core_extensions/i18n.rb | 4 +++- .../lib/middleman-core/sitemap/extensions/redirects.rb | 1 + .../sitemap/extensions/request_endpoints.rb | 1 + 5 files changed, 6 insertions(+), 10 deletions(-) diff --git a/middleman-core/lib/middleman-core/core_extensions.rb b/middleman-core/lib/middleman-core/core_extensions.rb index 4ff25339..1177eefe 100644 --- a/middleman-core/lib/middleman-core/core_extensions.rb +++ b/middleman-core/lib/middleman-core/core_extensions.rb @@ -50,7 +50,6 @@ Middleman::Extensions.register :routing, auto_activate: :before_configuration do Middleman::CoreExtensions::Routing end - ### # Setup Optional Extensions ### diff --git a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb index 42e8ee98..234e5a01 100644 --- a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb +++ b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb @@ -43,9 +43,7 @@ module Middleman::CoreExtensions # TODO: Should we make people put these under "options" instead of having # special known keys? opts = fmdata.extract!(:layout, :layout_engine, :renderer_options, :directory_index, :content_type) - if opts.has_key?(:renderer_options) - opts[:renderer_options].symbolize_keys! - end + opts[:renderer_options].symbolize_keys! if opts.key?(:renderer_options) ignored = fmdata.delete(:ignored) @@ -54,9 +52,6 @@ module Middleman::CoreExtensions resource.add_metadata options: opts, page: fmdata - # TODO: resource.ignore! if ignored - # TODO: This doesn't really work because resources can't themselves be ignored / when - # an ignore rule is in place it's forever resource.ignore! if ignored == true && !resource.proxy? # TODO: Save new template here somewhere? @@ -84,8 +79,6 @@ module Middleman::CoreExtensions data = external_data.deep_merge(data) end - - [data, content] end end diff --git a/middleman-core/lib/middleman-core/core_extensions/i18n.rb b/middleman-core/lib/middleman-core/core_extensions/i18n.rb index 6f4f0012..c6284226 100644 --- a/middleman-core/lib/middleman-core/core_extensions/i18n.rb +++ b/middleman-core/lib/middleman-core/core_extensions/i18n.rb @@ -81,7 +81,9 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension # This is for backwards compatibility with the old provides_metadata-based code # that used to be in this extension, but I don't know how much sense it makes. - #resource.add_metadata options: { lang: @mount_at_root }, locals: { lang: @mount_at_root } + unless resource.options[:lang] + resource.add_metadata options: { lang: @mount_at_root }, locals: { lang: @mount_at_root } + end end resources + new_resources diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb b/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb index 60e5bdbb..3f4dafd1 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb @@ -83,6 +83,7 @@ module Middleman false end + # rubocop:disable AccessorMethodName def get_source_file '' end diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb b/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb index b89ec083..4f5a646e 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb @@ -72,6 +72,7 @@ module Middleman false end + # rubocop:disable AccessorMethodName def get_source_file '' end From e6ec5f31de38130e2a2c7676f459c19e3171393e Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Fri, 30 May 2014 23:32:48 -0700 Subject: [PATCH 17/21] Fix meta pages --- .../lib/middleman-core/meta_pages/sitemap_resource.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/middleman-core/lib/middleman-core/meta_pages/sitemap_resource.rb b/middleman-core/lib/middleman-core/meta_pages/sitemap_resource.rb index e42176df..27fc03fa 100644 --- a/middleman-core/lib/middleman-core/meta_pages/sitemap_resource.rb +++ b/middleman-core/lib/middleman-core/meta_pages/sitemap_resource.rb @@ -44,11 +44,10 @@ module Middleman data = @resource.data props['Data'] = data.inspect unless data.empty? - meta = @resource.metadata - options = meta[:options] + options = @resource.options props['Options'] = options.inspect unless options.empty? - locals = meta[:locals].keys + locals = @resource.locals.keys props['Locals'] = locals.join(', ') unless locals.empty? props From 416428444c8d607d69f4c2a41fb1aaf74deb3e9e Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Fri, 30 May 2014 23:32:58 -0700 Subject: [PATCH 18/21] Let users add metadata via #page --- middleman-core/lib/middleman-core/core_extensions/routing.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/middleman-core/lib/middleman-core/core_extensions/routing.rb b/middleman-core/lib/middleman-core/core_extensions/routing.rb index 9769c943..004fdc2c 100644 --- a/middleman-core/lib/middleman-core/core_extensions/routing.rb +++ b/middleman-core/lib/middleman-core/core_extensions/routing.rb @@ -46,6 +46,7 @@ module Middleman # @option opts [Symbol, Boolean, String] layout The layout name to use (e.g. `:article`) or `false` to disable layout. # @option opts [Boolean] directory_indexes Whether or not the `:directory_indexes` extension applies to these paths. # @option opts [Hash] locals Local variables for the template. These will be available when the template renders. + # @option opts [Hash] data Extra metadata to add to the page. This is the same as frontmatter, though frontmatter will take precedence over metadata defined here. Available via {Resource#data}. # @option opts [String] proxy The source path for a template to proxy this path to. Only valid when a single path is provided. Prefer using the `proxy` method to do this. # @option opts [Boolean] ignore Set to `true` to ignore the provided path(s). Only valid when a single path is provided. Prefer using the `ignore` method to do this. # @return [void] @@ -56,7 +57,7 @@ module Middleman # TODO: This seems wrong options[:layout] = @app.config[:layout] if options[:layout].nil? # TODO: You can set options and locals, but not data - metadata = { options: options, locals: options.delete(:locals) || {} } + metadata = { options: options, locals: options.delete(:locals) || {}, page: options.delete(:data) || {} } # If the path is a regexp unless path.is_a?(Regexp) || path.include?('*') From f07bed4ecfb239148ee788658f5599fcb3180c50 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 14 Jun 2014 12:38:44 -0700 Subject: [PATCH 19/21] Fix custom layouts tests --- .../features/custom_layouts.feature | 34 ++++++++++++++----- .../step_definitions/page_layout_steps.rb | 6 ---- .../core_extensions/extensions.rb | 3 -- 3 files changed, 25 insertions(+), 18 deletions(-) delete mode 100644 middleman-core/features/step_definitions/page_layout_steps.rb diff --git a/middleman-core/features/custom_layouts.feature b/middleman-core/features/custom_layouts.feature index 09c7faa1..27f7df08 100644 --- a/middleman-core/features/custom_layouts.feature +++ b/middleman-core/features/custom_layouts.feature @@ -1,14 +1,22 @@ Feature: Custom layouts In order easily switch between relative and absolute paths - + Scenario: Using custom :layout attribute - Given page "/custom-layout.html" has layout "custom" + Given a fixture app "custom-layout-app2" + And a file named "config.rb" with: + """ + page '/custom-layout.html', layout: :custom + """ And the Server is running at "custom-layout-app2" When I go to "/custom-layout.html" Then I should see "Custom Layout" - + Scenario: Using custom :layout attribute with folders - Given page "/custom-layout-dir/" has layout "custom" + Given a fixture app "custom-layout-app2" + And a file named "config.rb" with: + """ + page '/custom-layout-dir/', layout: :custom + """ And the Server is running at "custom-layout-app2" When I go to "/custom-layout-dir" Then I should see "Custom Layout" @@ -16,9 +24,13 @@ Feature: Custom layouts Then I should see "Custom Layout" When I go to "/custom-layout-dir/index.html" Then I should see "Custom Layout" - + Scenario: Using custom :layout attribute with folders - Given page "/custom-layout-dir" has layout "custom" + Given a fixture app "custom-layout-app2" + And a file named "config.rb" with: + """ + page '/custom-layout-dir', layout: :custom + """ And the Server is running at "custom-layout-app2" When I go to "/custom-layout-dir" Then I should see "Custom Layout" @@ -26,9 +38,13 @@ Feature: Custom layouts Then I should see "Custom Layout" When I go to "/custom-layout-dir/index.html" Then I should see "Custom Layout" - + Scenario: Using custom :layout attribute with folders - Given page "/custom-layout-dir/index.html" has layout "custom" + Given a fixture app "custom-layout-app2" + And a file named "config.rb" with: + """ + page '/custom-layout-dir/index.html', layout: :custom + """ And the Server is running at "custom-layout-app2" When I go to "/custom-layout-dir" Then I should see "Custom Layout" @@ -36,7 +52,7 @@ Feature: Custom layouts Then I should see "Custom Layout" When I go to "/custom-layout-dir/index.html" Then I should see "Custom Layout" - + Scenario: Setting layout inside a matching page block Given the Server is running at "page-helper-layout-block-app" When I go to "/index.html" diff --git a/middleman-core/features/step_definitions/page_layout_steps.rb b/middleman-core/features/step_definitions/page_layout_steps.rb deleted file mode 100644 index 46a27026..00000000 --- a/middleman-core/features/step_definitions/page_layout_steps.rb +++ /dev/null @@ -1,6 +0,0 @@ -Given /^page "([^\"]*)" has layout "([^\"]*)"$/ do |url, layout| - @initialize_commands ||= [] - @initialize_commands << lambda { - page(url, layout: layout.to_sym) - } -end diff --git a/middleman-core/lib/middleman-core/core_extensions/extensions.rb b/middleman-core/lib/middleman-core/core_extensions/extensions.rb index 2cfb6c41..dbe8715b 100644 --- a/middleman-core/lib/middleman-core/core_extensions/extensions.rb +++ b/middleman-core/lib/middleman-core/core_extensions/extensions.rb @@ -83,9 +83,6 @@ module Middleman # Search the root of the project for required files $LOAD_PATH.unshift(root) unless $LOAD_PATH.include?(root) - # Evaluate a passed block if given - config_context.instance_exec(&block) if block_given? - super ::Middleman::Extension.clear_after_extension_callbacks From f63feaf017173eaffbf34d63a44845ce8614e78d Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 14 Jun 2014 13:05:32 -0700 Subject: [PATCH 20/21] Do not use ShowExceptions during tests, it confuses things --- .../lib/middleman-core/core_extensions/show_exceptions.rb | 6 ++++-- .../lib/middleman-core/step_definitions/server_steps.rb | 7 +++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/middleman-core/lib/middleman-core/core_extensions/show_exceptions.rb b/middleman-core/lib/middleman-core/core_extensions/show_exceptions.rb index 3f039ca6..a28e6a7f 100644 --- a/middleman-core/lib/middleman-core/core_extensions/show_exceptions.rb +++ b/middleman-core/lib/middleman-core/core_extensions/show_exceptions.rb @@ -1,14 +1,16 @@ +require 'rack/showexceptions' + # Support rack/showexceptions during development module Middleman::CoreExtensions class ShowExceptions < ::Middleman::Extension def initialize(app, options_hash={}, &block) super - require 'rack/showexceptions' + app.config.define_setting :show_exceptions, true, 'Whether to catch and display exceptions' end def after_configuration - app.use ::Rack::ShowExceptions + app.use ::Rack::ShowExceptions if app.config[:show_exceptions] end end end diff --git a/middleman-core/lib/middleman-core/step_definitions/server_steps.rb b/middleman-core/lib/middleman-core/step_definitions/server_steps.rb index 4ea87dc8..94905abe 100644 --- a/middleman-core/lib/middleman-core/step_definitions/server_steps.rb +++ b/middleman-core/lib/middleman-core/step_definitions/server_steps.rb @@ -41,12 +41,11 @@ Given /^the Server is running$/ do ENV['MM_ROOT'] = root_dir initialize_commands = @initialize_commands || [] + initialize_commands.unshift lambda { config[:show_exceptions] = false } @server_inst = Middleman::Application.server.inst do - app.initialized do - initialize_commands.each do |p| - config_context.instance_exec(&p) - end + initialize_commands.each do |p| + instance_exec(&p) end end From 2ef842a730628b4f3dab9e77a4c1ef381ec7b78f Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 14 Jun 2014 13:08:03 -0700 Subject: [PATCH 21/21] Fix references to store in traversal --- .../lib/middleman-core/sitemap/extensions/traversal.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/traversal.rb b/middleman-core/lib/middleman-core/sitemap/extensions/traversal.rb index 140d8f74..fe0dfb55 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/traversal.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/traversal.rb @@ -13,7 +13,7 @@ module Middleman test_expr = parts.join('\\/') # eponymous reverse-lookup - found = store.resources.find do |candidate| + found = @store.resources.find do |candidate| candidate.path =~ %r!^#{test_expr}(?:\.[a-zA-Z0-9]+|\/)$! end @@ -21,7 +21,7 @@ module Middleman found else parts.pop if is_index - store.find_resource_by_destination_path("#{parts.join('/')}/#{@app.config[:index_file]}") + @store.find_resource_by_destination_path("#{parts.join('/')}/#{@app.config[:index_file]}") end end