Merge pull request #4974 from ayastreb/frontmatter_defaults
Merge pull request 4974
This commit is contained in:
		
						commit
						dc4b91b0c8
					
				| 
						 | 
				
			
			@ -11,7 +11,6 @@ AllCops:
 | 
			
		|||
  - lib/jekyll/deprecator.rb
 | 
			
		||||
  - lib/jekyll/document.rb
 | 
			
		||||
  - lib/jekyll/filters.rb
 | 
			
		||||
  - lib/jekyll/frontmatter_defaults.rb
 | 
			
		||||
  - lib/jekyll/regenerator.rb
 | 
			
		||||
  - lib/jekyll/renderer.rb
 | 
			
		||||
  - lib/jekyll/site.rb
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,37 +11,42 @@ module Jekyll
 | 
			
		|||
    end
 | 
			
		||||
 | 
			
		||||
    def update_deprecated_types(set)
 | 
			
		||||
      return set unless set.key?('scope') && set['scope'].key?('type')
 | 
			
		||||
      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'
 | 
			
		||||
      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']
 | 
			
		||||
          set["scope"]["type"]
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
      set
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def ensure_time!(set)
 | 
			
		||||
      return set unless set.key?('values') && set['values'].key?('date')
 | 
			
		||||
      return set if set['values']['date'].is_a?(Time)
 | 
			
		||||
      set['values']['date'] = Utils.parse_date(set['values']['date'], "An invalid date format was found in a front-matter default set: #{set}")
 | 
			
		||||
      return set unless set.key?("values") && set["values"].key?("date")
 | 
			
		||||
      return set if set["values"]["date"].is_a?(Time)
 | 
			
		||||
      set["values"]["date"] = Utils.parse_date(
 | 
			
		||||
        set["values"]["date"],
 | 
			
		||||
        "An invalid date format was found in a front-matter default set: #{set}"
 | 
			
		||||
      )
 | 
			
		||||
      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
 | 
			
		||||
    # type - a symbol indicating whether a :page, a :post or a :draft calls this method
 | 
			
		||||
    # path - the path (relative to the source) of the page,
 | 
			
		||||
    # post or :draft the default is used in
 | 
			
		||||
    # type - a symbol indicating whether a :page,
 | 
			
		||||
    # a :post or a :draft calls this method
 | 
			
		||||
    #
 | 
			
		||||
    # Returns the default value or nil if none was found
 | 
			
		||||
    def find(path, type, setting)
 | 
			
		||||
| 
						 | 
				
			
			@ -49,9 +54,9 @@ module Jekyll
 | 
			
		|||
      old_scope = nil
 | 
			
		||||
 | 
			
		||||
      matching_sets(path, type).each do |set|
 | 
			
		||||
        if set['values'].key?(setting) && has_precedence?(old_scope, set['scope'])
 | 
			
		||||
          value = set['values'][setting]
 | 
			
		||||
          old_scope = set['scope']
 | 
			
		||||
        if set["values"].key?(setting) && has_precedence?(old_scope, set["scope"])
 | 
			
		||||
          value = set["values"][setting]
 | 
			
		||||
          old_scope = set["scope"]
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
      value
 | 
			
		||||
| 
						 | 
				
			
			@ -67,11 +72,11 @@ module Jekyll
 | 
			
		|||
      defaults = {}
 | 
			
		||||
      old_scope = nil
 | 
			
		||||
      matching_sets(path, type).each do |set|
 | 
			
		||||
        if has_precedence?(old_scope, set['scope'])
 | 
			
		||||
          defaults = Utils.deep_merge_hashes(defaults, set['values'])
 | 
			
		||||
          old_scope = set['scope']
 | 
			
		||||
        if has_precedence?(old_scope, set["scope"])
 | 
			
		||||
          defaults = Utils.deep_merge_hashes(defaults, set["values"])
 | 
			
		||||
          old_scope = set["scope"]
 | 
			
		||||
        else
 | 
			
		||||
          defaults = Utils.deep_merge_hashes(set['values'], defaults)
 | 
			
		||||
          defaults = Utils.deep_merge_hashes(set["values"], defaults)
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
      defaults
 | 
			
		||||
| 
						 | 
				
			
			@ -91,9 +96,9 @@ module Jekyll
 | 
			
		|||
    end
 | 
			
		||||
 | 
			
		||||
    def applies_path?(scope, path)
 | 
			
		||||
      return true if !scope.key?('path') || scope['path'].empty?
 | 
			
		||||
      return true if !scope.key?("path") || scope["path"].empty?
 | 
			
		||||
 | 
			
		||||
      scope_path = Pathname.new(scope['path'])
 | 
			
		||||
      scope_path = Pathname.new(scope["path"])
 | 
			
		||||
      Pathname.new(sanitize_path(path)).ascend do |ascended_path|
 | 
			
		||||
        if ascended_path.to_s == scope_path.to_s
 | 
			
		||||
          return true
 | 
			
		||||
| 
						 | 
				
			
			@ -113,7 +118,7 @@ module Jekyll
 | 
			
		|||
    # Returns true if either of the above conditions are satisfied,
 | 
			
		||||
    #   otherwise returns false
 | 
			
		||||
    def applies_type?(scope, type)
 | 
			
		||||
      !scope.key?('type') || scope['type'].eql?(type.to_s)
 | 
			
		||||
      !scope.key?("type") || scope["type"].eql?(type.to_s)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    # Checks if a given set of default values is valid
 | 
			
		||||
| 
						 | 
				
			
			@ -122,7 +127,7 @@ module Jekyll
 | 
			
		|||
    #
 | 
			
		||||
    # Returns true if the set is valid and can be used in this class
 | 
			
		||||
    def valid?(set)
 | 
			
		||||
      set.is_a?(Hash) && set['values'].is_a?(Hash)
 | 
			
		||||
      set.is_a?(Hash) && set["values"].is_a?(Hash)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    # Determines if a new scope has precedence over an old one
 | 
			
		||||
| 
						 | 
				
			
			@ -131,27 +136,29 @@ module Jekyll
 | 
			
		|||
    # new_scope - the new scope hash
 | 
			
		||||
    #
 | 
			
		||||
    # Returns true if the new scope has precedence over the older
 | 
			
		||||
    # rubocop: disable PredicateName
 | 
			
		||||
    def has_precedence?(old_scope, new_scope)
 | 
			
		||||
      return true if old_scope.nil?
 | 
			
		||||
 | 
			
		||||
      new_path = sanitize_path(new_scope['path'])
 | 
			
		||||
      old_path = sanitize_path(old_scope['path'])
 | 
			
		||||
      new_path = sanitize_path(new_scope["path"])
 | 
			
		||||
      old_path = sanitize_path(old_scope["path"])
 | 
			
		||||
 | 
			
		||||
      if new_path.length != old_path.length
 | 
			
		||||
        new_path.length >= old_path.length
 | 
			
		||||
      elsif new_scope.key? 'type'
 | 
			
		||||
      elsif new_scope.key?("type")
 | 
			
		||||
        true
 | 
			
		||||
      else
 | 
			
		||||
        !old_scope.key? 'type'
 | 
			
		||||
        !old_scope.key? "type"
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
    # rubocop: enable PredicateName
 | 
			
		||||
 | 
			
		||||
    # Collects a list of sets that match the given path and type
 | 
			
		||||
    #
 | 
			
		||||
    # Returns an array of hashes
 | 
			
		||||
    def matching_sets(path, type)
 | 
			
		||||
      valid_sets.select do |set|
 | 
			
		||||
        !set.key?('scope') || applies?(set['scope'], path, type)
 | 
			
		||||
        !set.key?("scope") || applies?(set["scope"], path, type)
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -162,7 +169,7 @@ module Jekyll
 | 
			
		|||
    #
 | 
			
		||||
    # Returns an array of hashes
 | 
			
		||||
    def valid_sets
 | 
			
		||||
      sets = @site.config['defaults']
 | 
			
		||||
      sets = @site.config["defaults"]
 | 
			
		||||
      return [] unless sets.is_a?(Array)
 | 
			
		||||
 | 
			
		||||
      sets.map do |set|
 | 
			
		||||
| 
						 | 
				
			
			@ -170,7 +177,7 @@ module Jekyll
 | 
			
		|||
          ensure_time!(update_deprecated_types(set))
 | 
			
		||||
        else
 | 
			
		||||
          Jekyll.logger.warn "Defaults:", "An invalid front-matter default set was found:"
 | 
			
		||||
          Jekyll.logger.warn "#{set}"
 | 
			
		||||
          Jekyll.logger.warn set.to_s
 | 
			
		||||
          nil
 | 
			
		||||
        end
 | 
			
		||||
      end.compact
 | 
			
		||||
| 
						 | 
				
			
			@ -181,7 +188,7 @@ module Jekyll
 | 
			
		|||
      if path.nil? || path.empty?
 | 
			
		||||
        ""
 | 
			
		||||
      else
 | 
			
		||||
        path.gsub(/\A\//, '').gsub(/([^\/])\z/, '\1')
 | 
			
		||||
        path.gsub(%r!\A/!, "").gsub(%r!([^/])\z!, '\1')
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue