diff --git a/lib/jekyll/collection.rb b/lib/jekyll/collection.rb index 8e0b379c..95e4c8a1 100644 --- a/lib/jekyll/collection.rb +++ b/lib/jekyll/collection.rb @@ -15,6 +15,7 @@ module Jekyll Dir.glob(File.join(directory, "**", "*.*")).each do |file_path| if allowed_document?(file_path) doc = Jekyll::Document.new(file_path, { site: site, collection: self }) + doc.read docs << doc end end diff --git a/lib/jekyll/document.rb b/lib/jekyll/document.rb index 198b77fb..e637fbd1 100644 --- a/lib/jekyll/document.rb +++ b/lib/jekyll/document.rb @@ -28,6 +28,10 @@ module Jekyll Pathname.new(path).relative_path_from(Pathname.new(site.source)).to_s end + def basename(suffix = "") + File.basename(path, suffix) + end + def extname File.extname(path) end @@ -56,7 +60,7 @@ module Jekyll # Read in the file and assign the content and data based on the file contents. # # Returns nothing. - def read + def read(opts = {}) if yaml_file? @data = SafeYAML.load_file(path) else diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index de3d027e..8fe10776 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -175,16 +175,9 @@ module Jekyll entries = Dir.chdir(base) { Dir['*.{yaml,yml}'] } entries.delete_if { |e| File.directory?(File.join(base, e)) } data_collection = Jekyll::Collection.new(self, "data") - - entries.each do |entry| - path = File.join(source, dir, entry) - next if File.symlink?(path) && safe - - key = sanitize_filename(File.basename(entry, '.*')) - - doc = Jekyll::Document.new(path, { site: self, collection: data_collection }) - doc.read - + data_collection.read + data_collection.docs.each do |doc| + key = sanitize_filename(doc.basename(".*")) self.data[key] = doc.data end end diff --git a/test/source/_methods/um_hi.md b/test/source/_methods/um_hi.md index c549c8b4..9ebb5325 120000 --- a/test/source/_methods/um_hi.md +++ b/test/source/_methods/um_hi.md @@ -1 +1 @@ -test/source/_methods/sanitized_path.md \ No newline at end of file +./site/generate.md \ No newline at end of file diff --git a/test/test_document.rb b/test/test_document.rb new file mode 100644 index 00000000..75bf2a84 --- /dev/null +++ b/test/test_document.rb @@ -0,0 +1,45 @@ +require 'helper' + +class TestDocument < Test::Unit::TestCase + + context "" do + setup do + @site = Site.new(Jekyll.configuration({ + "collections" => ["methods"], + "source" => source_dir, + "destination" => dest_dir + })) + @site.process + @document = @site.collections["methods"].docs.first + end + + should "know its relative path" do + assert_equal "_methods/configuration.md", @document.relative_path + end + + should "knows its extname" do + assert_equal ".md", @document.extname + end + + should "know its basename" do + assert_equal "configuration.md", @document.basename + end + + should "allow the suffix to be specified for the basename" do + assert_equal "configuration", @document.basename(".*") + end + + should "know whether its a yaml file" do + assert_equal false, @document.yaml_file? + end + + should "know its data" do + assert_equal({ + "title" => "Jekyll.configuration", + "whatever" => "foo.bar" + }, @document.data) + end + + end + +end