2012-01-14 21:46:22 +01:00
# Routing extension
2012-05-07 23:41:39 +02:00
module Middleman
module CoreExtensions
2016-01-12 19:35:12 +01:00
class Routing < ConfigExtension
2014-05-28 09:00:36 +02:00
# This should always run late, but not as late as :directory_indexes,
# so it can add metadata to any pages generated by other extensions
2015-09-19 23:07:42 +02:00
self . resource_list_manipulator_priority = 10
2014-05-28 09:00:36 +02:00
2015-05-02 20:48:21 +02:00
# Expose the `page` method to config.
expose_to_config :page
2016-01-12 19:35:12 +01:00
PageDescriptor = Struct . new ( :path , :metadata ) do
def execute_descriptor ( app , resources )
normalized_path = path . dup
2014-05-28 09:00:36 +02:00
2016-01-12 19:35:12 +01:00
if normalized_path . is_a? ( String ) && ! normalized_path . include? ( '*' )
# Normalize path
normalized_path = :: Middleman :: Util . normalize_path ( normalized_path )
if normalized_path . end_with? ( '/' ) || app . files . by_type ( :source ) . watchers . any? { | w | ( w . directory + Pathname ( normalized_path ) ) . directory? }
normalized_path = :: File . join ( normalized_path , app . config [ :index_file ] )
end
2014-05-28 09:00:36 +02:00
end
2016-01-12 19:35:12 +01:00
normalized_path = '/' + :: Middleman :: Util . strip_leading_slash ( normalized_path ) if normalized_path . is_a? ( String )
resources
2016-01-14 20:21:42 +01:00
. select { | r | :: Middleman :: Util . path_match ( normalized_path , " / #{ r . path } " ) }
. each { | r | r . add_metadata ( metadata ) }
2016-01-12 19:35:12 +01:00
resources
2014-05-28 09:00:36 +02:00
end
end
# The page method allows options to be set for a given source path, regex, or glob.
2015-11-12 00:30:26 +01:00
# Options that may be set include layout, locals, andx ignore.
2014-01-01 03:21:30 +01:00
#
2014-05-28 09:00:36 +02:00
# @example
# page '/about.html', layout: false
# @example
# page '/index.html', layout: :homepage_layout
# @example
# page '/foo.html', locals: { foo: 'bar' }
2014-01-01 03:21:30 +01:00
#
2014-05-28 09:00:36 +02:00
# @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.
2014-05-31 08:32:58 +02:00
# @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}.
2014-01-01 03:21:30 +01:00
# @return [void]
2016-01-12 19:35:12 +01:00
Contract Or [ String , Regexp ] , Hash = > PageDescriptor
2014-05-28 09:00:36 +02:00
def page ( path , opts = { } )
2014-01-04 00:49:54 +01:00
options = opts . dup
2014-01-01 03:21:30 +01:00
# Default layout
2014-06-17 06:44:14 +02:00
metadata = {
locals : options . delete ( :locals ) || { } ,
2016-01-12 19:35:12 +01:00
page : options . delete ( :data ) || { } ,
options : options
2014-06-17 06:44:14 +02:00
}
2012-08-14 00:39:06 +02:00
2016-01-12 19:35:12 +01:00
PageDescriptor . new ( path , metadata )
2014-01-01 03:21:30 +01:00
end
2011-07-06 19:46:06 +02:00
end
end
2012-08-14 00:39:06 +02:00
end