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.
|
# Returns the type of self.
|
||||||
def type
|
def type
|
||||||
if is_a?(Post)
|
if is_a?(Post)
|
||||||
:post
|
:posts
|
||||||
elsif is_a?(Page)
|
elsif is_a?(Page)
|
||||||
:page
|
:pages
|
||||||
elsif is_a?(Draft)
|
elsif is_a?(Draft)
|
||||||
:draft
|
:drafts
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
module Jekyll
|
module Jekyll
|
||||||
module Deprecator
|
module Deprecator
|
||||||
def self.process(args)
|
extend self
|
||||||
|
|
||||||
|
def process(args)
|
||||||
no_subcommand(args)
|
no_subcommand(args)
|
||||||
arg_is_present? args, "--server", "The --server command has been replaced by the \
|
arg_is_present? args, "--server", "The --server command has been replaced by the \
|
||||||
'serve' subcommand."
|
'serve' subcommand."
|
||||||
|
@ -16,24 +18,29 @@ module Jekyll
|
||||||
arg_is_present? args, "--url", "The 'url' setting can only be set in your config files."
|
arg_is_present? args, "--url", "The 'url' setting can only be set in your config files."
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.no_subcommand(args)
|
def no_subcommand(args)
|
||||||
if args.size > 0 && args.first =~ /^--/ && !%w[--help --version].include?(args.first)
|
if args.size > 0 && args.first =~ /^--/ && !%w[--help --version].include?(args.first)
|
||||||
deprecation_message "Jekyll now uses subcommands instead of just \
|
deprecation_message "Jekyll now uses subcommands instead of just \
|
||||||
switches. Run `jekyll --help' to find out more."
|
switches. Run `jekyll --help' to find out more."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.arg_is_present?(args, deprecated_argument, message)
|
def arg_is_present?(args, deprecated_argument, message)
|
||||||
if args.include?(deprecated_argument)
|
if args.include?(deprecated_argument)
|
||||||
deprecation_message(message)
|
deprecation_message(message)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.deprecation_message(message)
|
def deprecation_message(message)
|
||||||
Jekyll.logger.error "Deprecation:", message
|
Jekyll.logger.error "Deprecation:", message
|
||||||
end
|
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|
|
Array(gem_name).each do |name|
|
||||||
begin
|
begin
|
||||||
require name
|
require name
|
||||||
|
|
|
@ -10,6 +10,21 @@ module Jekyll
|
||||||
@site = site
|
@site = site
|
||||||
end
|
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
|
# 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
|
# 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
|
||||||
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)
|
def applies_type?(scope, type)
|
||||||
!scope.key?('type') || scope['type'] == type.to_s
|
!scope.key('type') || scope['type'].eql?(type.to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Checks if a given set of default values is valid
|
# Checks if a given set of default values is valid
|
||||||
|
@ -126,12 +152,15 @@ module Jekyll
|
||||||
sets = @site.config['defaults']
|
sets = @site.config['defaults']
|
||||||
return [] unless sets.is_a?(Array)
|
return [] unless sets.is_a?(Array)
|
||||||
|
|
||||||
sets.select do |set|
|
sets.map do |set|
|
||||||
unless valid?(set)
|
if valid?(set)
|
||||||
Jekyll.logger.warn "Default:", "An invalid default set was found"
|
update_deprecated_types(set)
|
||||||
|
else
|
||||||
|
Jekyll.logger.warn "Defaults:", "An invalid front-matter default set was found:"
|
||||||
|
Jekyll.logger.warn "#{set}"
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
valid?(set)
|
end.compact
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Sanitizes the given path by removing a leading and addding a trailing slash
|
# Sanitizes the given path by removing a leading and addding a trailing slash
|
||||||
|
|
|
@ -304,13 +304,13 @@ defaults:
|
||||||
-
|
-
|
||||||
scope:
|
scope:
|
||||||
path: "" # an empty string here means all files in the project
|
path: "" # an empty string here means all files in the project
|
||||||
type: "post"
|
type: "posts"
|
||||||
values:
|
values:
|
||||||
layout: "default"
|
layout: "default"
|
||||||
{% endhighlight %}
|
{% endhighlight %}
|
||||||
|
|
||||||
Now, this will only set the layout for files where the type is `post`.
|
Now, this will only set the layout for files where the type is `posts`.
|
||||||
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.
|
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`.
|
As mentioned earlier, you can set multiple scope/values pairs for `defaults`.
|
||||||
|
|
||||||
|
@ -319,17 +319,16 @@ defaults:
|
||||||
-
|
-
|
||||||
scope:
|
scope:
|
||||||
path: ""
|
path: ""
|
||||||
type: "post"
|
type: "posts"
|
||||||
values:
|
values:
|
||||||
layout: "my-site"
|
layout: "my-site"
|
||||||
-
|
-
|
||||||
scope:
|
scope:
|
||||||
path: "projects"
|
path: "projects"
|
||||||
type: "page"
|
type: "pages"
|
||||||
values:
|
values:
|
||||||
layout: "project" # overrides previous default layout
|
layout: "project" # overrides previous default layout
|
||||||
author: "Mr. Hyde"
|
author: "Mr. Hyde"
|
||||||
category: "project"
|
|
||||||
{% endhighlight %}
|
{% 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`.
|
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:
|
scope:
|
||||||
path: ""
|
path: ""
|
||||||
type: "my_collection" # a collection in your site
|
type: "my_collection" # a collection in your site, in plural form
|
||||||
values:
|
values:
|
||||||
layout: "default"
|
layout: "default"
|
||||||
{% endhighlight %}
|
{% endhighlight %}
|
||||||
|
@ -365,7 +364,7 @@ defaults:
|
||||||
-
|
-
|
||||||
scope:
|
scope:
|
||||||
path: "projects"
|
path: "projects"
|
||||||
type: "page"
|
type: "pages"
|
||||||
values:
|
values:
|
||||||
layout: "project"
|
layout: "project"
|
||||||
author: "Mr. Hyde"
|
author: "Mr. Hyde"
|
||||||
|
|
Loading…
Reference in New Issue