diff --git a/.rubocop.yml b/.rubocop.yml index 15bcb5dfb..b0fef47a2 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -16,7 +16,7 @@ Lint/UnreachableCode: Lint/UselessAccessModifier: Enabled: false Metrics/AbcSize: - Max: 20 + Max: 21 Metrics/ClassLength: Exclude: - !ruby/regexp /features\/.*.rb$/ diff --git a/features/theme.feature b/features/theme.feature index 0e05d6937..7729a0ebb 100644 --- a/features/theme.feature +++ b/features/theme.feature @@ -17,7 +17,7 @@ Feature: Writing themes Scenario: A theme with SCSS Given I have a configuration file with "theme" set to "test-theme" And I have a css directory - And I have a "css/main.scss" page that contains "@import 'style';" + And I have a "css/main.scss" page that contains "@import 'test-theme-black';" When I run jekyll build Then I should get a zero exit status And the _site directory should exist diff --git a/lib/jekyll/page.rb b/lib/jekyll/page.rb index 00fdec230..3e3c7ec86 100644 --- a/lib/jekyll/page.rb +++ b/lib/jekyll/page.rb @@ -40,11 +40,11 @@ def initialize(site, base, dir, name) @base = base @dir = dir @name = name - if site.in_theme_dir(base) == base # we're in a theme - @path = site.in_theme_dir(base, dir, name) - else - @path = site.in_source_dir(base, dir, name) - end + @path = if site.in_theme_dir(base) == base # we're in a theme + site.in_theme_dir(base, dir, name) + else + site.in_source_dir(base, dir, name) + end process(name) read_yaml(File.join(base, dir), name) diff --git a/lib/jekyll/readers/theme_assets_reader.rb b/lib/jekyll/readers/theme_assets_reader.rb index 85151d0f5..035c06078 100644 --- a/lib/jekyll/readers/theme_assets_reader.rb +++ b/lib/jekyll/readers/theme_assets_reader.rb @@ -13,19 +13,35 @@ def read if File.symlink?(path) Jekyll.logger.warn "Theme reader:", "Ignored symlinked asset: #{path}" else - base = site.theme.root - dir = File.dirname(path.sub("#{site.theme.root}/", "")) - name = File.basename(path) - relative_path = File.join(*[dir, name].compact) - if Utils.has_yaml_header?(path) - next if site.pages.any? { |file| file.relative_path == relative_path } - site.pages << Jekyll::Page.new(site, base, dir, name) - else - next if site.static_files.any? { |file| file.relative_path == relative_path } - site.static_files << Jekyll::StaticFile.new(site, base, dir, name) - end + read_theme_asset(path) end end end + + private + def read_theme_asset(path) + base = site.theme.root + dir = File.dirname(path.sub("#{site.theme.root}/", "")) + name = File.basename(path) + + if Utils.has_yaml_header?(path) + append_unless_exists site.pages, + Jekyll::Page.new(site, base, dir, name) + else + append_unless_exists site.static_files, + Jekyll::StaticFile.new(site, base, dir, name) + end + end + + def append_unless_exists(haystack, new_item) + if haystack.any? { |file| file.relative_path == new_item.relative_path } + Jekyll.logger.debug "Theme:", + "Ignoring #{new_item.relative_path} in theme due to existing file " \ + "with that path in site." + return + end + + haystack << new_item + end end end diff --git a/test/fixtures/test-theme/_sass/test-theme-red.scss b/test/fixtures/test-theme/_sass/test-theme-red.scss new file mode 100644 index 000000000..0307e17ab --- /dev/null +++ b/test/fixtures/test-theme/_sass/test-theme-red.scss @@ -0,0 +1,3 @@ +.sample { + color: red; +} diff --git a/test/fixtures/test-theme/assets/style.scss b/test/fixtures/test-theme/assets/style.scss index 408f04f76..47c4a2f13 100644 --- a/test/fixtures/test-theme/assets/style.scss +++ b/test/fixtures/test-theme/assets/style.scss @@ -1,3 +1,3 @@ --- --- -@import "test-theme-{{ site.theme-color }}"; +@import "test-theme-{{ site.theme-color | default: "red" }}"; diff --git a/test/test_theme.rb b/test/test_theme.rb index 82b4224a6..fd380d950 100644 --- a/test/test_theme.rb +++ b/test/test_theme.rb @@ -37,7 +37,7 @@ def setup [:assets, :_layouts, :_includes, :_sass].each do |folder| should "know the #{folder} path" do expected = File.expand_path(folder.to_s, @expected_root) - assert_equal expected, @theme.public_send("#{folder}_path") + assert_equal expected, @theme.public_send("#{folder.to_s.tr("_", "")}_path") end end diff --git a/test/test_theme_assets_reader.rb b/test/test_theme_assets_reader.rb index d964acf34..ecafa0f11 100644 --- a/test/test_theme_assets_reader.rb +++ b/test/test_theme_assets_reader.rb @@ -3,22 +3,24 @@ class TestThemeAssetsReader < JekyllUnitTest def setup @site = fixture_site( - "theme" => "test-theme", + "theme" => "test-theme", "theme-color" => "black" ) assert @site.theme end def assert_file_with_relative_path(haystack, relative_path) - assert haystack.any? { |f| - f.relative_path == relative_path - }, "Site should read in the #{relative_path} file, but it was not found in #{haystack.inspect}" + assert haystack.any? { |f| + f.relative_path == relative_path + }, "Site should read in the #{relative_path} file, " \ + "but it was not found in #{haystack.inspect}" end def refute_file_with_relative_path(haystack, relative_path) - refute haystack.any? { |f| - f.relative_path == relative_path - }, "Site should not have read in the #{relative_path} file, but it was found in #{haystack.inspect}" + refute haystack.any? { |f| + f.relative_path == relative_path + }, "Site should not have read in the #{relative_path} file, " \ + "but it was found in #{haystack.inspect}" end context "with a valid theme" do @@ -55,7 +57,5 @@ def refute_file_with_relative_path(haystack, relative_path) refute_file_with_relative_path @site.static_files, "assets/img/logo.png" refute_file_with_relative_path @site.pages, "assets/style.scss" end - end - end