Merge pull request #2732 from jekyll/type-should-be-similar-in-number

This commit is contained in:
Parker Moore 2014-08-12 12:46:42 -04:00
commit 871e425fa0
5 changed files with 90 additions and 25 deletions

View File

@ -128,12 +128,12 @@ module Jekyll
#
# Returns the type of self.
def type
if is_a?(Post)
:post
if is_a?(Draft)
:drafts
elsif is_a?(Post)
:posts
elsif is_a?(Page)
:page
elsif is_a?(Draft)
:draft
:pages
end
end

View File

@ -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

View File

@ -10,6 +10,26 @@ module Jekyll
@site = site
end
def update_deprecated_types(set)
return set unless set.key?('scope') && set['scope'].key?('type')
set['scope']['type'] = case set['scope']['type']
when 'page'
Deprecator.defaults_deprecate_type('page', 'pages')
'pages'
when 'post'
Deprecator.defaults_deprecate_type('post', 'posts')
'posts'
when 'draft'
Deprecator.defaults_deprecate_type('draft', 'drafts')
'drafts'
else
set['scope']['type']
end
set
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 +93,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 +157,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

View File

@ -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"

View File

@ -53,7 +53,7 @@ class TestFrontMatterDefaults < Test::Unit::TestCase
end
end
context "A site with front matter defaults with no path" do
context "A site with front matter defaults with no path and a deprecated type" do
setup do
@site = Site.new(Jekyll.configuration({
"source" => source_dir,
@ -78,6 +78,31 @@ class TestFrontMatterDefaults < Test::Unit::TestCase
end
end
context "A site with front matter defaults with no path" do
setup do
@site = Site.new(Jekyll.configuration({
"source" => source_dir,
"destination" => dest_dir,
"defaults" => [{
"scope" => {
"type" => "pages"
},
"values" => {
"key" => "val"
}
}]
}))
@site.process
@affected = @site.pages
@not_affected = @site.posts
end
should "affect only the specified type and all paths" do
assert_equal @affected.reject { |page| page.data["key"] == "val" }, []
assert_equal @not_affected.reject { |page| page.data["key"] == "val" }, @not_affected
end
end
context "A site with front matter defaults with no path or type" do
setup do
@site = Site.new(Jekyll.configuration({