From d83d6e077c3fbcbb5f5cffa3c51911c006b8921f Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sun, 27 Apr 2014 22:50:19 -0700 Subject: [PATCH] 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]