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
|
# Liquid Support
|
||||||
begin
|
begin
|
||||||
require 'middleman-core/renderers/liquid'
|
require 'middleman-core/renderers/liquid'
|
||||||
app.send :include, Middleman::Renderers::Liquid
|
Middleman::Extensions.register :liquid, Middleman::Renderers::Liquid
|
||||||
|
activate :liquid
|
||||||
rescue LoadError
|
rescue LoadError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -4,23 +4,19 @@ require 'liquid'
|
||||||
module Middleman
|
module Middleman
|
||||||
module Renderers
|
module Renderers
|
||||||
# Liquid Renderer
|
# Liquid Renderer
|
||||||
module Liquid
|
class Liquid < Middleman::Extension
|
||||||
# Setup extension
|
# After config, setup liquid partial paths
|
||||||
class << self
|
def after_configuration
|
||||||
# Once registerd
|
::Liquid::Template.file_system = ::Liquid::LocalFileSystem.new(source_dir)
|
||||||
def registered(app)
|
end
|
||||||
# After config, setup liquid partial paths
|
|
||||||
app.after_configuration do
|
|
||||||
::Liquid::Template.file_system = ::Liquid::LocalFileSystem.new(source_dir)
|
|
||||||
|
|
||||||
# Convert data object into a hash for liquid
|
def manipulate_resource_list(resources)
|
||||||
sitemap.provides_metadata %r{\.liquid$} do
|
resources.each do |resource|
|
||||||
{ locals: { data: data.to_h } }
|
# Convert data object into a hash for liquid
|
||||||
end
|
if resource.source_file =~ %r{\.liquid$}
|
||||||
|
resource.add_metadata locals: { data: app.data.to_h }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
alias_method :included, :registered
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -11,15 +11,12 @@ module Middleman
|
||||||
include Middleman::Sitemap::Extensions::Traversal
|
include Middleman::Sitemap::Extensions::Traversal
|
||||||
include Middleman::Sitemap::Extensions::ContentType
|
include Middleman::Sitemap::Extensions::ContentType
|
||||||
|
|
||||||
# @return [Middleman::Sitemap::Store]
|
|
||||||
#attr_reader :store
|
|
||||||
|
|
||||||
# The source path of this resource (relative to the source directory,
|
# The source path of this resource (relative to the source directory,
|
||||||
# without template extensions)
|
# without template extensions)
|
||||||
# @return [String]
|
# @return [String]
|
||||||
attr_reader :path
|
attr_reader :path
|
||||||
|
|
||||||
# The output path for this resource
|
# The output path in the build directory for this resource
|
||||||
# @return [String]
|
# @return [String]
|
||||||
attr_accessor :destination_path
|
attr_accessor :destination_path
|
||||||
|
|
||||||
|
@ -78,15 +75,15 @@ module Middleman
|
||||||
end
|
end
|
||||||
|
|
||||||
# Merge in new metadata specific to this resource.
|
# 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={})
|
def add_metadata(meta={})
|
||||||
@local_metadata.deep_merge!(meta.dup)
|
@local_metadata.deep_merge!(meta.dup)
|
||||||
end
|
end
|
||||||
|
|
||||||
# The output/preview URL for this resource
|
|
||||||
# @return [String]
|
|
||||||
attr_accessor :destination_path
|
|
||||||
|
|
||||||
# Extension of the path (i.e. '.js')
|
# Extension of the path (i.e. '.js')
|
||||||
# @return [String]
|
# @return [String]
|
||||||
def ext
|
def ext
|
||||||
|
|
|
@ -25,8 +25,10 @@ module Middleman
|
||||||
@app = app
|
@app = app
|
||||||
@resources = []
|
@resources = []
|
||||||
@_cached_metadata = {}
|
@_cached_metadata = {}
|
||||||
|
# TODO: Should this be a set or hash?
|
||||||
@resource_list_manipulators = []
|
@resource_list_manipulators = []
|
||||||
@needs_sitemap_rebuild = true
|
@needs_sitemap_rebuild = true
|
||||||
|
@provides_metadata_for_path = Set.new
|
||||||
|
|
||||||
@lock = Monitor.new
|
@lock = Monitor.new
|
||||||
reset_lookup_cache!
|
reset_lookup_cache!
|
||||||
|
@ -125,6 +127,40 @@ module Middleman
|
||||||
@resources_not_ignored = nil
|
@resources_not_ignored = nil
|
||||||
end
|
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
|
# Get the URL path for an on-disk file
|
||||||
# @param [String] file
|
# @param [String] file
|
||||||
# @return [String]
|
# @return [String]
|
||||||
|
|
Loading…
Reference in a new issue