From 49ffbbd4c761a67fc930acd257ae932111464b28 Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Thu, 16 May 2019 21:14:39 +0530 Subject: [PATCH] Add `type` attribute to Document instances (#7406) Merge pull request 7406 --- lib/jekyll/document.rb | 11 +++++------ lib/jekyll/excerpt.rb | 2 +- test/test_document.rb | 4 ++++ test/test_excerpt.rb | 9 +++++++++ 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/jekyll/document.rb b/lib/jekyll/document.rb index 38867b02..a9eb8ed2 100644 --- a/lib/jekyll/document.rb +++ b/lib/jekyll/document.rb @@ -5,7 +5,7 @@ module Jekyll include Comparable extend Forwardable - attr_reader :path, :site, :extname, :collection + attr_reader :path, :site, :extname, :collection, :type attr_accessor :content, :output def_delegator :self, :read_post_data, :post_read @@ -44,6 +44,8 @@ module Jekyll @path = path @extname = File.extname(path) @collection = relations[:collection] + @type = @collection.label.to_sym + @has_yaml_header = nil if draft? @@ -53,7 +55,7 @@ module Jekyll end data.default_proc = proc do |_, key| - site.frontmatter_defaults.find(relative_path, collection.label, key) + site.frontmatter_defaults.find(relative_path, type, key) end trigger_hooks(:post_init) @@ -462,10 +464,7 @@ module Jekyll end def merge_defaults - defaults = @site.frontmatter_defaults.all( - relative_path, - collection.label.to_sym - ) + defaults = @site.frontmatter_defaults.all(relative_path, type) merge_data!(defaults, :source => "front matter defaults") unless defaults.empty? end diff --git a/lib/jekyll/excerpt.rb b/lib/jekyll/excerpt.rb index 69b107f5..dee4613f 100644 --- a/lib/jekyll/excerpt.rb +++ b/lib/jekyll/excerpt.rb @@ -10,7 +10,7 @@ module Jekyll def_delegators :@doc, :site, :name, :ext, :extname, - :collection, :related_posts, + :collection, :related_posts, :type, :coffeescript_file?, :yaml_file?, :url, :next_doc, :previous_doc diff --git a/test/test_document.rb b/test/test_document.rb index 1e135418..041328d5 100644 --- a/test/test_document.rb +++ b/test/test_document.rb @@ -58,6 +58,10 @@ class TestDocument < JekyllUnitTest assert_equal "configuration", @document.basename_without_ext end + should "know its type" do + assert_equal :methods, @document.type + end + should "know whether it's a YAML file" do assert_equal false, @document.yaml_file? end diff --git a/test/test_excerpt.rb b/test/test_excerpt.rb index fbe6f63d..c57def0b 100644 --- a/test/test_excerpt.rb +++ b/test/test_excerpt.rb @@ -56,6 +56,15 @@ class TestExcerpt < JekyllUnitTest end end + context "#type" do + should "return the post's type" do + assert_equal @excerpt.type, @post.type + end + should "return a symbol" do + assert_same @excerpt.type.class, Symbol + end + end + context "#to_s" do should "return rendered output" do assert_equal @excerpt.output, @excerpt.to_s