Skip to content

Commit

Permalink
Add ThemeAssetsReader which reads assets from a theme
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
parkr committed Sep 16, 2016
1 parent 562ffe7 commit 13aec48
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/jekyll.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 5 additions & 1 deletion lib/jekyll/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ def initialize(site, base, dir, name)
@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)
Expand Down
1 change: 1 addition & 0 deletions lib/jekyll/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def read
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.
Expand Down
31 changes: 31 additions & 0 deletions lib/jekyll/readers/theme_assets_reader.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 8 additions & 4 deletions lib/jekyll/theme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ def root
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
Expand All @@ -43,7 +47,7 @@ def path_for(folder)
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
Expand Down

0 comments on commit 13aec48

Please sign in to comment.