Skip to content

Commit

Permalink
Read in static files info collection.files as StaticFiles.
Browse files Browse the repository at this point in the history
  • Loading branch information
parkr committed Aug 12, 2014
1 parent f16a5cb commit 530e049
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 33 deletions.
21 changes: 18 additions & 3 deletions lib/jekyll/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,28 @@ def docs
@docs ||= []
end

# Fetch the static files in this collection.
# Defaults to an empty array if no static files have been read in.
#
# Returns an array of Jekyll::StaticFile objects.
def files
@files ||= []
end

# Read the allowed documents into the collection's array of docs.
#
# Returns the sorted array of docs.
def read
filtered_entries.each do |file_path|
doc = Jekyll::Document.new(Jekyll.sanitized_path(directory, file_path), { site: site, collection: self })
doc.read
docs << doc
full_path = Jekyll.sanitized_path(directory, file_path)
if Utils.has_yaml_header? full_path
doc = Jekyll::Document.new(full_path, { site: site, collection: self })
doc.read
docs << doc
else
relative_dir = File.join(relative_directory, File.dirname(file_path)).chomp("/.")
files << StaticFile.new(site, site.source, relative_dir, File.basename(full_path), self)
end
end
docs.sort!
end
Expand Down Expand Up @@ -118,6 +132,7 @@ def to_liquid
metadata.merge({
"label" => label,
"docs" => docs,
"files" => files,
"directory" => directory,
"output" => write?,
"relative_directory" => relative_directory
Expand Down
14 changes: 3 additions & 11 deletions lib/jekyll/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,28 +97,20 @@ def coffeescript_file?
'.coffee'.eql?(extname)
end

# Determine whether the document has a YAML header.
#
# Returns true if the file starts with three dashes
def has_yaml_header?
@has_yaml_header unless @has_yaml_header.nil?
@has_yaml_header = !!(File.open(path, 'rb') { |f| f.read(5) } =~ /\A---\r?\n/)
end

# Determine whether the file should be rendered with Liquid.
#
# Returns false if the document is either an asset file or a yaml file,
# true otherwise.
def render_with_liquid?
!(coffeescript_file? || yaml_file?) && has_yaml_header?
!(coffeescript_file? || yaml_file?)
end

# Determine whether the file should be placed into layouts.
#
# Returns false if the document is either an asset file or a yaml file,
# true otherwise.
def place_in_layout?
!(asset_file? || yaml_file?) && has_yaml_header?
!(asset_file? || yaml_file?)
end

# The URL template where the document would be accessible.
Expand Down Expand Up @@ -214,7 +206,7 @@ def read(opts = {})
unless defaults.empty?
@data = defaults
end
@content = File.open(path, "rb", merged_file_read_opts(opts)) { |f| f.read }
@content = File.read(path, merged_file_read_opts(opts))
if content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
@content = $POSTMATCH
data_file = SafeYAML.load($1)
Expand Down
8 changes: 2 additions & 6 deletions lib/jekyll/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def read_directories(dir = '')
if File.directory?(f_abs)
f_rel = File.join(dir, f)
read_directories(f_rel) unless dest.sub(/\/$/, '') == f_abs
elsif has_yaml_header?(f_abs)
elsif Utils.has_yaml_header?(f_abs)
page = Page.new(self, source, dir, f)
pages << page if publisher.publish?(page)
else
Expand Down Expand Up @@ -432,7 +432,7 @@ def docs_to_write

def documents
collections.reduce(Set.new) do |docs, (_, collection)|
docs.merge(collection.docs)
docs + collection.docs + collection.files
end.to_a
end

Expand All @@ -454,10 +454,6 @@ def has_relative_page?
pages.any? { |page| page.uses_relative_permalinks }
end

def has_yaml_header?(file)
!!(File.open(file, 'rb') { |f| f.read(5) } =~ /\A---\r?\n/)
end

def limit_posts!
limit = posts.length < limit_posts ? posts.length : limit_posts
self.posts = posts[-limit, limit]
Expand Down
24 changes: 20 additions & 4 deletions lib/jekyll/static_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ class StaticFile
# base - The String path to the <source>.
# dir - The String path between <source> and the file.
# name - The String filename of the file.
def initialize(site, base, dir, name)
def initialize(site, base, dir, name, collection = nil)
@site = site
@base = base
@dir = dir
@name = name
@collection = collection
end

# Returns source file path.
Expand All @@ -23,7 +24,11 @@ def path

# Returns the source file path relative to the site source
def relative_path
@relative_path ||= path.sub(/\A#{@site.source}/, '')
@relative_path ||= File.join(*[@dir, @name].compact)
end

def extname
File.extname(path)
end

# Obtain destination path.
Expand All @@ -32,7 +37,11 @@ def relative_path
#
# Returns destination file path.
def destination(dest)
File.join(*[dest, @dir, @name].compact)
if @collection
File.join(*[dest, @dir.gsub(/\A_/, ''), @name].compact)
else
File.join(*[dest, @dir, @name].compact)
end
end

# Returns last modification time for this file.
Expand All @@ -47,6 +56,13 @@ def modified?
@@mtimes[path] != mtime
end

# Whether to write the file to the filesystem
#
# Returns true.
def write?
true
end

# Write the static file to the destination directory (if modified).
#
# dest - The String path to the destination dir.
Expand Down Expand Up @@ -75,7 +91,7 @@ def self.reset_cache

def to_liquid
{
"path" => relative_path,
"path" => File.join("", relative_path),
"modified_time" => mtime.to_s,
"extname" => File.extname(relative_path)
}
Expand Down
2 changes: 2 additions & 0 deletions test/source/_slides/example-slide-1.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
title: Example slide
layout: slide
---

Wooot
4 changes: 4 additions & 0 deletions test/source/_with.dots/file.with.dots.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

I'm a file with dots.
18 changes: 11 additions & 7 deletions test/test_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class TestDocument < Test::Unit::TestCase
}]
}))
@site.process
@document = @site.collections["slides"].docs.first
@document = @site.collections["slides"].docs.select{|d| d.is_a?(Document) }.first
end

should "know the frontmatter defaults" do
Expand Down Expand Up @@ -199,20 +199,24 @@ class TestDocument < Test::Unit::TestCase
"destination" => dest_dir
}))
@site.process
@document = @site.collections["slides"].docs.find { |doc| doc.relative_path == "_slides/octojekyll.png" }
@document = @site.collections["slides"].files.find { |doc| doc.relative_path == "_slides/octojekyll.png" }
@dest_file = dest_dir("slides/octojekyll.png")
end

should "be a document" do
assert !@document.nil?
should "be a static file" do
assert_equal true, @document.is_a?(StaticFile)
end

should "be set to write" do
assert @document.write?
end

should "not be rendered with Liquid" do
assert !@document.render_with_liquid?
should "be in the list of docs_to_write" do
assert @site.docs_to_write.include?(@document)
end

should "be output in the correct place" do
assert File.file? @dest_file
assert_equal true, File.file?(@dest_file)
end
end

Expand Down
4 changes: 2 additions & 2 deletions test/test_site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ def generate(site)

should "read pages with yaml front matter" do
abs_path = File.expand_path("about.html", @site.source)
assert_equal true, @site.send(:has_yaml_header?, abs_path)
assert_equal true, Utils.has_yaml_header?(abs_path)
end

should "enforce a strict 3-dash limit on the start of the YAML front-matter" do
abs_path = File.expand_path("pgp.key", @site.source)
assert_equal false, @site.send(:has_yaml_header?, abs_path)
assert_equal false, Utils.has_yaml_header?(abs_path)
end

should "expose jekyll version to site payload" do
Expand Down

0 comments on commit 530e049

Please sign in to comment.