Extensionize liquid, bring back provides_metadata_for_path
This commit is contained in:
parent
c285848866
commit
d83d6e077c
4 changed files with 54 additions and 24 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -4,24 +4,20 @@ require 'liquid'
|
|||
module Middleman
|
||||
module Renderers
|
||||
# Liquid Renderer
|
||||
module Liquid
|
||||
# Setup extension
|
||||
class << self
|
||||
# Once registerd
|
||||
def registered(app)
|
||||
class Liquid < Middleman::Extension
|
||||
# After config, setup liquid partial paths
|
||||
app.after_configuration do
|
||||
def after_configuration
|
||||
::Liquid::Template.file_system = ::Liquid::LocalFileSystem.new(source_dir)
|
||||
end
|
||||
|
||||
def manipulate_resource_list(resources)
|
||||
resources.each do |resource|
|
||||
# Convert data object into a hash for liquid
|
||||
sitemap.provides_metadata %r{\.liquid$} do
|
||||
{ locals: { data: data.to_h } }
|
||||
if resource.source_file =~ %r{\.liquid$}
|
||||
resource.add_metadata locals: { data: app.data.to_h }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
alias_method :included, :registered
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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]
|
||||
|
|
Loading…
Reference in a new issue