Skip to content

Commit

Permalink
read '_config.yml' if included with theme gem
Browse files Browse the repository at this point in the history
  • Loading branch information
ashmaroli committed Dec 13, 2016
1 parent 1d3a9d4 commit 7338f6b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/jekyll-data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@

# replace Jekyll::Reader with a subclass Jekyll::ThemeReader only if the site
# uses a gem-based theme else have this plugin disabled.
#
# Additionally, append to site's config hash with optional config hash from the
# theme gem by filling in keys not already defined.
Jekyll::Hooks.register :site, :after_init do |site|
if site.theme
site.config = Jekyll::Utils.deep_merge_hashes(
Jekyll::ThemeReader.new(site).read_theme_config, site.config
)
site.reader = Jekyll::ThemeReader.new(site)
else
Jekyll.logger.abort_with(
Expand Down
13 changes: 13 additions & 0 deletions lib/jekyll/theme_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ def read_theme_data
end
end

# Read the '_config.yml' file if present within the theme gem and
# return a data hash otherwise return a hash of Jekyll Defaults.
#
# Returns a hash
def read_theme_config
config = @site.in_theme_dir("_config.yml")
if File.exist?(config)
Configuration.new.read_config_file(config)
else
Configuration::DEFAULTS
end
end

private

# Private:
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/test-theme/_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
title: Test Theme
email: tester@domain.com
# the '>' means to ignore newlines until "baseurl:"
description: >
Powered by a test-theme.
This text has been inserted by your theme gem.
Override by defining a "description:" key in the config file at source.
baseurl: ""
markdown: kramdown
post_excerpts: enabled # "enabled" / "disabled"
17 changes: 17 additions & 0 deletions test/test_theme_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,21 @@ class TestThemeReader < JekyllDataTest
File.read(File.join(fixture_dir, "same_data_output.html"))
end
end

context "theme gem shipped with a '_config.yml'" do
setup do
@site = fixture_site(
"title" => "Config Test"
)
end

should "have its hash appended to site's config hash" do
assert_contains @site.config, %w(post_excerpts enabled)
end

should "have its hash added only where its not already set" do
refute_equal "Test Theme", @site.config["title"]
assert_equal "Config Test", @site.config["title"]
end
end
end

0 comments on commit 7338f6b

Please sign in to comment.