From b8cf7cf96d0199921d3b7e193f0c8613b1960186 Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Sat, 24 Sep 2022 19:24:56 +0530 Subject: [PATCH] Introduce `theme` drop to expose theme details (#9129) Merge pull request 9129 --- lib/jekyll/drops/theme_drop.rb | 36 ++++++++++++++++++++++++ lib/jekyll/drops/unified_payload_drop.rb | 4 +++ test/test_theme_drop.rb | 34 ++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 lib/jekyll/drops/theme_drop.rb create mode 100644 test/test_theme_drop.rb diff --git a/lib/jekyll/drops/theme_drop.rb b/lib/jekyll/drops/theme_drop.rb new file mode 100644 index 00000000..a641fbea --- /dev/null +++ b/lib/jekyll/drops/theme_drop.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +module Jekyll + module Drops + class ThemeDrop < Drop + delegate_methods :root, :version + delegate_method_as :runtime_dependencies, :dependencies + + def authors + @authors ||= gemspec.authors.join(", ") + end + + def version + @version ||= gemspec.version.to_s + end + + def description + @description ||= gemspec.description || gemspec.summary + end + + def metadata + @metadata ||= gemspec.metadata + end + + private + + def gemspec + @gemspec ||= @obj.send(:gemspec) + end + + def fallback_data + @fallback_data ||= {} + end + end + end +end diff --git a/lib/jekyll/drops/unified_payload_drop.rb b/lib/jekyll/drops/unified_payload_drop.rb index 24e62e58..709ad712 100644 --- a/lib/jekyll/drops/unified_payload_drop.rb +++ b/lib/jekyll/drops/unified_payload_drop.rb @@ -16,6 +16,10 @@ module Jekyll @site_drop ||= SiteDrop.new(@obj) end + def theme + @theme_drop ||= ThemeDrop.new(@obj.theme) if @obj.theme + end + private def fallback_data diff --git a/test/test_theme_drop.rb b/test/test_theme_drop.rb new file mode 100644 index 00000000..6ebbcf88 --- /dev/null +++ b/test/test_theme_drop.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require "helper" + +class TestThemeDrop < JekyllUnitTest + should "be initialized only for gem-based themes" do + assert_nil fixture_site.to_liquid.theme + end + + context "a theme drop" do + setup do + @drop = fixture_site("theme" => "test-theme").to_liquid.theme + end + + should "respond to `key?`" do + assert_respond_to @drop, :key? + end + + should "export relevant data to Liquid templates" do + expected = { + "authors" => "Jekyll", + "dependencies" => [], + "description" => "This is a theme used to test Jekyll", + "metadata" => {}, + "root" => theme_dir, + "version" => "0.1.0", + } + expected.each_key do |key| + assert @drop.key?(key) + assert_equal expected[key], @drop[key] + end + end + end +end