Use plural 'type' in front matter defaults for pages/posts/drafts
Fixes #2657
This commit is contained in:
parent
f190a960a1
commit
60c29561f2
|
@ -129,11 +129,11 @@ module Jekyll
|
|||
# Returns the type of self.
|
||||
def type
|
||||
if is_a?(Post)
|
||||
:post
|
||||
:posts
|
||||
elsif is_a?(Page)
|
||||
:page
|
||||
:pages
|
||||
elsif is_a?(Draft)
|
||||
:draft
|
||||
:drafts
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
module Jekyll
|
||||
module Deprecator
|
||||
def self.process(args)
|
||||
extend self
|
||||
|
||||
def process(args)
|
||||
no_subcommand(args)
|
||||
arg_is_present? args, "--server", "The --server command has been replaced by the \
|
||||
'serve' subcommand."
|
||||
|
@ -16,24 +18,29 @@ module Jekyll
|
|||
arg_is_present? args, "--url", "The 'url' setting can only be set in your config files."
|
||||
end
|
||||
|
||||
def self.no_subcommand(args)
|
||||
def no_subcommand(args)
|
||||
if args.size > 0 && args.first =~ /^--/ && !%w[--help --version].include?(args.first)
|
||||
deprecation_message "Jekyll now uses subcommands instead of just \
|
||||
switches. Run `jekyll --help' to find out more."
|
||||
end
|
||||
end
|
||||
|
||||
def self.arg_is_present?(args, deprecated_argument, message)
|
||||
def arg_is_present?(args, deprecated_argument, message)
|
||||
if args.include?(deprecated_argument)
|
||||
deprecation_message(message)
|
||||
end
|
||||
end
|
||||
|
||||
def self.deprecation_message(message)
|
||||
def deprecation_message(message)
|
||||
Jekyll.logger.error "Deprecation:", message
|
||||
end
|
||||
|
||||
def self.gracefully_require(gem_name)
|
||||
def defaults_deprecate_type(old, current)
|
||||
Jekyll.logger.warn "Defaults:", "The '#{old}' type has become '#{current}'."
|
||||
Jekyll.logger.warn "Defaults:", "Please update your front-matter defaults to use 'type: #{current}'."
|
||||
end
|
||||
|
||||
def gracefully_require(gem_name)
|
||||
Array(gem_name).each do |name|
|
||||
begin
|
||||
require name
|
||||
|
|
|
@ -10,6 +10,21 @@ module Jekyll
|
|||
@site = site
|
||||
end
|
||||
|
||||
def update_deprecated_types(scope)
|
||||
if scope['type'].eql?('page')
|
||||
defaults_deprecate_type('page', 'pages')
|
||||
scope['type'] = 'pages'
|
||||
elsif scope['type'].eql?('post')
|
||||
defaults_deprecate_type('post', 'posts')
|
||||
type.to_s.eql?('posts')
|
||||
scope['type'] = 'posts'
|
||||
elsif scope['type'].eql?('draft')
|
||||
defaults_deprecate_type('draft', 'drafts')
|
||||
scope['type'] = 'drafts'
|
||||
end
|
||||
scope
|
||||
end
|
||||
|
||||
# Finds a default value for a given setting, filtered by path and type
|
||||
#
|
||||
# path - the path (relative to the source) of the page, post or :draft the default is used in
|
||||
|
@ -73,8 +88,19 @@ module Jekyll
|
|||
end
|
||||
end
|
||||
|
||||
# Determines whether the scope applies to type.
|
||||
# The scope applies to the type if:
|
||||
# 1. no 'type' is specified
|
||||
# 2. the 'type' in the scope is the same as the type asked about
|
||||
#
|
||||
# scope - the Hash defaults set being asked about application
|
||||
# type - the type of the document being processed / asked about
|
||||
# its defaults.
|
||||
#
|
||||
# Returns true if either of the above conditions are satisfied,
|
||||
# otherwise returns false
|
||||
def applies_type?(scope, type)
|
||||
!scope.key?('type') || scope['type'] == type.to_s
|
||||
!scope.key('type') || scope['type'].eql?(type.to_s)
|
||||
end
|
||||
|
||||
# Checks if a given set of default values is valid
|
||||
|
@ -126,12 +152,15 @@ module Jekyll
|
|||
sets = @site.config['defaults']
|
||||
return [] unless sets.is_a?(Array)
|
||||
|
||||
sets.select do |set|
|
||||
unless valid?(set)
|
||||
Jekyll.logger.warn "Default:", "An invalid default set was found"
|
||||
end
|
||||
valid?(set)
|
||||
sets.map do |set|
|
||||
if valid?(set)
|
||||
update_deprecated_types(set)
|
||||
else
|
||||
Jekyll.logger.warn "Defaults:", "An invalid front-matter default set was found:"
|
||||
Jekyll.logger.warn "#{set}"
|
||||
nil
|
||||
end
|
||||
end.compact
|
||||
end
|
||||
|
||||
# Sanitizes the given path by removing a leading and addding a trailing slash
|
||||
|
|
|
@ -304,13 +304,13 @@ defaults:
|
|||
-
|
||||
scope:
|
||||
path: "" # an empty string here means all files in the project
|
||||
type: "post"
|
||||
type: "posts"
|
||||
values:
|
||||
layout: "default"
|
||||
{% endhighlight %}
|
||||
|
||||
Now, this will only set the layout for files where the type is `post`.
|
||||
The different types that are available to you are `page`, `post`, `draft` or any collection in your site. While `type` is optional, you must specify a value for `path` when creating a `scope/values` pair.
|
||||
Now, this will only set the layout for files where the type is `posts`.
|
||||
The different types that are available to you are `pages`, `posts`, `drafts` or any collection in your site. While `type` is optional, you must specify a value for `path` when creating a `scope/values` pair.
|
||||
|
||||
As mentioned earlier, you can set multiple scope/values pairs for `defaults`.
|
||||
|
||||
|
@ -319,17 +319,16 @@ defaults:
|
|||
-
|
||||
scope:
|
||||
path: ""
|
||||
type: "post"
|
||||
type: "posts"
|
||||
values:
|
||||
layout: "my-site"
|
||||
-
|
||||
scope:
|
||||
path: "projects"
|
||||
type: "page"
|
||||
type: "pages"
|
||||
values:
|
||||
layout: "project" # overrides previous default layout
|
||||
author: "Mr. Hyde"
|
||||
category: "project"
|
||||
{% endhighlight %}
|
||||
|
||||
With these defaults, all posts would use the `my-site` layout. Any html files that exist in the `projects/` folder will use the `project` layout, if it exists. Those files will also have the `page.author` [liquid variable](../variables/) set to `Mr. Hyde` as well as have the category for the page set to `project`.
|
||||
|
@ -343,7 +342,7 @@ defaults:
|
|||
-
|
||||
scope:
|
||||
path: ""
|
||||
type: "my_collection" # a collection in your site
|
||||
type: "my_collection" # a collection in your site, in plural form
|
||||
values:
|
||||
layout: "default"
|
||||
{% endhighlight %}
|
||||
|
@ -365,7 +364,7 @@ defaults:
|
|||
-
|
||||
scope:
|
||||
path: "projects"
|
||||
type: "page"
|
||||
type: "pages"
|
||||
values:
|
||||
layout: "project"
|
||||
author: "Mr. Hyde"
|
||||
|
|
Loading…
Reference in New Issue