Merge pull request #2732 from jekyll/type-should-be-similar-in-number
This commit is contained in:
		
						commit
						871e425fa0
					
				| 
						 | 
					@ -128,12 +128,12 @@ module Jekyll
 | 
				
			||||||
    #
 | 
					    #
 | 
				
			||||||
    # Returns the type of self.
 | 
					    # Returns the type of self.
 | 
				
			||||||
    def type
 | 
					    def type
 | 
				
			||||||
      if is_a?(Post)
 | 
					      if is_a?(Draft)
 | 
				
			||||||
        :post
 | 
					        :drafts
 | 
				
			||||||
 | 
					      elsif is_a?(Post)
 | 
				
			||||||
 | 
					        :posts
 | 
				
			||||||
      elsif is_a?(Page)
 | 
					      elsif is_a?(Page)
 | 
				
			||||||
        :page
 | 
					        :pages
 | 
				
			||||||
      elsif is_a?(Draft)
 | 
					 | 
				
			||||||
        :draft
 | 
					 | 
				
			||||||
      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,26 @@ module Jekyll
 | 
				
			||||||
      @site = site
 | 
					      @site = site
 | 
				
			||||||
    end
 | 
					    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
 | 
					    # 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 +93,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 +157,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"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -53,7 +53,7 @@ class TestFrontMatterDefaults < Test::Unit::TestCase
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  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
 | 
					    setup do
 | 
				
			||||||
      @site = Site.new(Jekyll.configuration({
 | 
					      @site = Site.new(Jekyll.configuration({
 | 
				
			||||||
        "source"      => source_dir,
 | 
					        "source"      => source_dir,
 | 
				
			||||||
| 
						 | 
					@ -78,6 +78,31 @@ class TestFrontMatterDefaults < Test::Unit::TestCase
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  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
 | 
					  context "A site with front matter defaults with no path or type" do
 | 
				
			||||||
    setup do
 | 
					    setup do
 | 
				
			||||||
      @site = Site.new(Jekyll.configuration({
 | 
					      @site = Site.new(Jekyll.configuration({
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue