From 13aec48137b18e6007e43b5009c05e664c18aa83 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 16 Sep 2016 14:40:54 -0700 Subject: [PATCH 1/8] Add ThemeAssetsReader which reads assets from a theme If the theme includes the 'assets' directory, it will be walked and items will be added to the site based on the normal rules of Jekyll: if there is YAML front matter, it will be added as a (convertible) Page, otherwise it will be added as a StaticFile. --- lib/jekyll.rb | 1 + lib/jekyll/page.rb | 6 ++++- lib/jekyll/reader.rb | 1 + lib/jekyll/readers/theme_assets_reader.rb | 31 +++++++++++++++++++++++ lib/jekyll/theme.rb | 12 ++++++--- 5 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 lib/jekyll/readers/theme_assets_reader.rb diff --git a/lib/jekyll.rb b/lib/jekyll.rb index a9fe696f..3c4def1a 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -55,6 +55,7 @@ module Jekyll autoload :PostReader, "jekyll/readers/post_reader" autoload :PageReader, "jekyll/readers/page_reader" autoload :StaticFileReader, "jekyll/readers/static_file_reader" + autoload :ThemeAssetsReader, "jekyll/readers/theme_assets_reader" autoload :LogAdapter, "jekyll/log_adapter" autoload :Page, "jekyll/page" autoload :PluginManager, "jekyll/plugin_manager" diff --git a/lib/jekyll/page.rb b/lib/jekyll/page.rb index 4e3efe3f..00fdec23 100644 --- a/lib/jekyll/page.rb +++ b/lib/jekyll/page.rb @@ -40,7 +40,11 @@ module Jekyll @base = base @dir = dir @name = name - @path = site.in_source_dir(base, dir, 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 process(name) read_yaml(File.join(base, dir), name) diff --git a/lib/jekyll/reader.rb b/lib/jekyll/reader.rb index abbe923f..7bde499d 100644 --- a/lib/jekyll/reader.rb +++ b/lib/jekyll/reader.rb @@ -18,6 +18,7 @@ module Jekyll sort_files! @site.data = DataReader.new(site).read(site.config["data_dir"]) CollectionReader.new(site).read + ThemeAssetsReader.new(site).read end # Sorts posts, pages, and static files. diff --git a/lib/jekyll/readers/theme_assets_reader.rb b/lib/jekyll/readers/theme_assets_reader.rb new file mode 100644 index 00000000..85151d0f --- /dev/null +++ b/lib/jekyll/readers/theme_assets_reader.rb @@ -0,0 +1,31 @@ +module Jekyll + class ThemeAssetsReader + attr_reader :site + def initialize(site) + @site = site + end + + def read + return unless site.theme && site.theme.assets_path + + Find.find(site.theme.assets_path) do |path| + next if File.directory?(path) + 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 + end + end + end + end +end diff --git a/lib/jekyll/theme.rb b/lib/jekyll/theme.rb index 0dd73f78..4b0eb2db 100644 --- a/lib/jekyll/theme.rb +++ b/lib/jekyll/theme.rb @@ -18,15 +18,19 @@ module Jekyll end def includes_path - path_for :includes + path_for :_includes end def layouts_path - path_for :layouts + path_for :_layouts end def sass_path - path_for :sass + path_for :_sass + end + + def assets_path + path_for :assets end def configure_sass @@ -43,7 +47,7 @@ module Jekyll end def realpath_for(folder) - File.realpath(Jekyll.sanitized_path(root, "_#{folder}")) + File.realpath(Jekyll.sanitized_path(root, folder.to_s)) rescue Errno::ENOENT, Errno::EACCES, Errno::ELOOP nil end From 87b9cfe2b5871807ee4269bdbf870028f244ff27 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 16 Sep 2016 14:57:58 -0700 Subject: [PATCH 2/8] ThemeBuilder: add 'assets' to list of scaffold directories --- lib/jekyll/theme_builder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/theme_builder.rb b/lib/jekyll/theme_builder.rb index 33477185..68a5eeab 100644 --- a/lib/jekyll/theme_builder.rb +++ b/lib/jekyll/theme_builder.rb @@ -1,6 +1,6 @@ class Jekyll::ThemeBuilder SCAFFOLD_DIRECTORIES = %w( - _layouts _includes _sass + assets _layouts _includes _sass ).freeze attr_reader :name, :path, :code_of_conduct From cf26bf5db0fd2a50ae2bf5cc207bd3d6d633cc0c Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 16 Sep 2016 15:36:33 -0700 Subject: [PATCH 3/8] TestTheme: update tests for 'path_for' now that it's no longer prepending the underscore --- test/test_theme.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_theme.rb b/test/test_theme.rb index a448ce30..a24a4c88 100644 --- a/test/test_theme.rb +++ b/test/test_theme.rb @@ -43,7 +43,7 @@ class TestTheme < JekyllUnitTest should "generate folder paths" do expected = File.expand_path("./_sass", @expected_root) - assert_equal expected, @theme.send(:path_for, :sass) + assert_equal expected, @theme.send(:path_for, :_sass) end should "not allow paths outside of the theme root" do @@ -56,7 +56,7 @@ class TestTheme < JekyllUnitTest should "return the resolved path when a symlink & resolved path exists" do expected = File.expand_path("./_layouts", @expected_root) - assert_equal expected, @theme.send(:path_for, :symlink) + assert_equal expected, @theme.send(:path_for, :_symlink) end end From 6d7f305e7c40229149a01cc118b4ea569842e2c9 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 16 Sep 2016 16:04:35 -0700 Subject: [PATCH 4/8] Add tests for assets directory support. --- .../{style.scss => test-theme-black.scss} | 0 .../test-theme/assets/img/another-logo.png | 1 + test/fixtures/test-theme/assets/img/logo.png | Bin 0 -> 3514 bytes test/fixtures/test-theme/assets/style.scss | 3 + test/test_theme.rb | 4 +- test/test_theme_assets_reader.rb | 61 ++++++++++++++++++ 6 files changed, 67 insertions(+), 2 deletions(-) rename test/fixtures/test-theme/_sass/{style.scss => test-theme-black.scss} (100%) create mode 120000 test/fixtures/test-theme/assets/img/another-logo.png create mode 100644 test/fixtures/test-theme/assets/img/logo.png create mode 100644 test/fixtures/test-theme/assets/style.scss create mode 100644 test/test_theme_assets_reader.rb diff --git a/test/fixtures/test-theme/_sass/style.scss b/test/fixtures/test-theme/_sass/test-theme-black.scss similarity index 100% rename from test/fixtures/test-theme/_sass/style.scss rename to test/fixtures/test-theme/_sass/test-theme-black.scss diff --git a/test/fixtures/test-theme/assets/img/another-logo.png b/test/fixtures/test-theme/assets/img/another-logo.png new file mode 120000 index 00000000..bd36e718 --- /dev/null +++ b/test/fixtures/test-theme/assets/img/another-logo.png @@ -0,0 +1 @@ +logo.png \ No newline at end of file diff --git a/test/fixtures/test-theme/assets/img/logo.png b/test/fixtures/test-theme/assets/img/logo.png new file mode 100644 index 0000000000000000000000000000000000000000..0d1cbe53d68031a7bb33ca2fd765d7cffb34f000 GIT binary patch literal 3514 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 "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}" + 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}" + end + + context "with a valid theme" do + should "read all assets" do + ThemeAssetsReader.new(@site).read + assert_file_with_relative_path @site.static_files, "assets/img/logo.png" + assert_file_with_relative_path @site.pages, "assets/style.scss" + end + + should "convert pages" do + @site.process + + file = @site.pages.find { |f| f.relative_path == "assets/style.scss" } + refute_nil file + assert_equal @site.in_dest_dir("assets/style.css"), file.destination(@site.dest) + assert_includes file.output, ".sample {\n color: black; }" + end + end + + context "with a valid theme without an assets dir" do + should "not read any assets" do + allow(Theme).to receive(:realpath_for).with(:sass).and_return(nil) + site = fixture_site("theme" => "test-theme") + ThemeAssetsReader.new(site).read + refute_file_with_relative_path @site.static_files, "assets/img/logo.png" + refute_file_with_relative_path @site.pages, "assets/style.scss" + end + end + + context "with no theme" do + should "not read any assets" do + site = fixture_site("theme" => nil) + ThemeAssetsReader.new(site).read + 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 From 74baeb889a923ee46a29a3010caf605f2fbba453 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Sun, 18 Sep 2016 15:04:15 -0400 Subject: [PATCH 5/8] ThemeAssetsReader: fix tests so everything passes. --- .rubocop.yml | 2 +- features/theme.feature | 2 +- lib/jekyll/page.rb | 10 ++--- lib/jekyll/readers/theme_assets_reader.rb | 38 +++++++++++++------ .../test-theme/_sass/test-theme-red.scss | 3 ++ test/fixtures/test-theme/assets/style.scss | 2 +- test/test_theme.rb | 2 +- test/test_theme_assets_reader.rb | 18 ++++----- 8 files changed, 48 insertions(+), 29 deletions(-) create mode 100644 test/fixtures/test-theme/_sass/test-theme-red.scss diff --git a/.rubocop.yml b/.rubocop.yml index 15bcb5df..b0fef47a 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 0e05d693..7729a0eb 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 00fdec23..3e3c7ec8 100644 --- a/lib/jekyll/page.rb +++ b/lib/jekyll/page.rb @@ -40,11 +40,11 @@ module Jekyll @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 85151d0f..035c0607 100644 --- a/lib/jekyll/readers/theme_assets_reader.rb +++ b/lib/jekyll/readers/theme_assets_reader.rb @@ -13,19 +13,35 @@ module Jekyll 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 00000000..0307e17a --- /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 408f04f7..47c4a2f1 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 82b4224a..fd380d95 100644 --- a/test/test_theme.rb +++ b/test/test_theme.rb @@ -37,7 +37,7 @@ class TestTheme < JekyllUnitTest [: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 d964acf3..ecafa0f1 100644 --- a/test/test_theme_assets_reader.rb +++ b/test/test_theme_assets_reader.rb @@ -3,22 +3,24 @@ require "helper" 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 @@ class TestThemeAssetsReader < JekyllUnitTest 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 From 7309ecf8e188ee1a329a9b260b2a6678a54d8c38 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Mon, 19 Sep 2016 13:47:51 -0700 Subject: [PATCH 6/8] Theme: for various path helpers, use strings. Symbols confuse people. --- lib/jekyll/theme.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/jekyll/theme.rb b/lib/jekyll/theme.rb index 4b0eb2db..803004f3 100644 --- a/lib/jekyll/theme.rb +++ b/lib/jekyll/theme.rb @@ -18,19 +18,19 @@ module Jekyll end def includes_path - path_for :_includes + path_for "_includes".freeze end def layouts_path - path_for :_layouts + path_for "_layouts".freeze end def sass_path - path_for :_sass + path_for "_sass".freeze end def assets_path - path_for :assets + path_for "assets".freeze end def configure_sass From b78827cecbc5c158a9e7d4242ecfe663af8c8c7d Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Mon, 19 Sep 2016 13:57:36 -0700 Subject: [PATCH 7/8] Add documentation for assets. --- site/_docs/themes.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/site/_docs/themes.md b/site/_docs/themes.md index 988f6ebd..c7d6d574 100644 --- a/site/_docs/themes.md +++ b/site/_docs/themes.md @@ -27,6 +27,7 @@ Jekyll themes set default layouts, includes, and stylesheets, that can be overri Jekyll will look first to your site's content, before looking to the theme's defaults, for any requested file in the following folders: +* `/assets` * `/_layouts` * `/_includes` * `/_sass` @@ -68,6 +69,12 @@ Theme layouts and includes work just like they work in any Jekyll site. Place la For example, if your theme has a `/_layouts/page.html` file, and a page has `layout: page` in its YAML front matter, Jekyll will first look to the site's `_layouts` folder for a the `page` layout, and if none exists, will use your theme's `page` layout. +### Assets + +Any file in `/assets` will be copied over to the user's site upon build unless they have a file with the same relative path. You may ship any kind of asset here: SCSS, an image, a webfont, etc. These files behave just like pages and static files in Jekyll: if the file has [YAML front matter]({{ site.baseurl }}/docs/frontmatter/) at the top, then it will be rendered. If it does not have YAML front matter, it will simply be copied over into the resulting site. This allows theme creators to ship a default `/assets/styles.scss` file which their layouts can depend on as `/assets/styles.css`. + +All files in `/assets` will be output into the compiled site in the `/assets` folder just as you'd expect from using Jekyll on your sites. + ### Stylesheets Your theme's stylesheets should be placed in your theme's `/_sass` folder, again, just as you would when authoring a Jekyll site. Your theme's styles can be included in the user's stylesheet using the `@import` directive. From 29d8fee4ce9162019ef35facb4a5a44eb42c6920 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Tue, 20 Sep 2016 13:12:34 -0700 Subject: [PATCH 8/8] Add test to ensure that the /assets theme reader doesn't clobber preexisting site files. --- .../test-theme/assets/application.coffee | 3 +++ test/source/assets/application.coffee | 3 +++ test/test_filters.rb | 2 +- test/test_site.rb | 1 + test/test_theme_assets_reader.rb | 20 ++++++++++++++----- 5 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/test-theme/assets/application.coffee create mode 100644 test/source/assets/application.coffee diff --git a/test/fixtures/test-theme/assets/application.coffee b/test/fixtures/test-theme/assets/application.coffee new file mode 100644 index 00000000..02f33515 --- /dev/null +++ b/test/fixtures/test-theme/assets/application.coffee @@ -0,0 +1,3 @@ +--- +--- +alert "From your theme." diff --git a/test/source/assets/application.coffee b/test/source/assets/application.coffee new file mode 100644 index 00000000..b9f5e1ef --- /dev/null +++ b/test/source/assets/application.coffee @@ -0,0 +1,3 @@ +--- +--- +alert "From your site." diff --git a/test/test_filters.rb b/test/test_filters.rb index b5f0a395..d6ff9eac 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -475,7 +475,7 @@ class TestFilters < JekyllUnitTest g["items"].is_a?(Array), "The list of grouped items for '' is not an Array." ) - assert_equal 14, g["items"].size + assert_equal 15, g["items"].size end end end diff --git a/test/test_site.rb b/test/test_site.rb index 83d6e456..1f0d30a1 100644 --- a/test/test_site.rb +++ b/test/test_site.rb @@ -180,6 +180,7 @@ class TestSite < JekyllUnitTest %#\ +.md .htaccess about.html + application.coffee bar.html coffeescript.coffee contacts.html diff --git a/test/test_theme_assets_reader.rb b/test/test_theme_assets_reader.rb index ecafa0f1..fc335248 100644 --- a/test/test_theme_assets_reader.rb +++ b/test/test_theme_assets_reader.rb @@ -25,6 +25,7 @@ class TestThemeAssetsReader < JekyllUnitTest context "with a valid theme" do should "read all assets" do + @site.reset ThemeAssetsReader.new(@site).read assert_file_with_relative_path @site.static_files, "assets/img/logo.png" assert_file_with_relative_path @site.pages, "assets/style.scss" @@ -38,15 +39,24 @@ class TestThemeAssetsReader < JekyllUnitTest assert_equal @site.in_dest_dir("assets/style.css"), file.destination(@site.dest) assert_includes file.output, ".sample {\n color: black; }" end + + should "not overwrite site content with the same relative path" do + @site.reset + @site.read + + file = @site.pages.find { |f| f.relative_path == "assets/application.coffee" } + refute_nil file + assert_includes file.content, "alert \"From your site.\"" + end end context "with a valid theme without an assets dir" do should "not read any assets" do - allow(Theme).to receive(:realpath_for).with(:sass).and_return(nil) site = fixture_site("theme" => "test-theme") + allow(site.theme).to receive(:assets_path).and_return(nil) ThemeAssetsReader.new(site).read - refute_file_with_relative_path @site.static_files, "assets/img/logo.png" - refute_file_with_relative_path @site.pages, "assets/style.scss" + refute_file_with_relative_path site.static_files, "assets/img/logo.png" + refute_file_with_relative_path site.pages, "assets/style.scss" end end @@ -54,8 +64,8 @@ class TestThemeAssetsReader < JekyllUnitTest should "not read any assets" do site = fixture_site("theme" => nil) ThemeAssetsReader.new(site).read - refute_file_with_relative_path @site.static_files, "assets/img/logo.png" - refute_file_with_relative_path @site.pages, "assets/style.scss" + 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