From a77c92aebe89a127ef4d9c39b39df892798c7e6a Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Tue, 1 Apr 2014 19:06:42 -0400 Subject: [PATCH] Replace load-in of YAML data with Jekyll::Document logic. COLLECTIONS IS COMING --- lib/jekyll.rb | 1 + lib/jekyll/document.rb | 83 ++++++++++++++++++++++++++++++++++++++++++ lib/jekyll/site.rb | 2 +- test/test_site.rb | 12 +++--- 4 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 lib/jekyll/document.rb diff --git a/lib/jekyll.rb b/lib/jekyll.rb index e2dd369c..d133f0b4 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -34,6 +34,7 @@ require 'jekyll/utils' require 'jekyll/stevenson' require 'jekyll/deprecator' require 'jekyll/configuration' +require 'jekyll/document' require 'jekyll/plugin_manager' require 'jekyll/site' require 'jekyll/convertible' diff --git a/lib/jekyll/document.rb b/lib/jekyll/document.rb new file mode 100644 index 00000000..50fbb056 --- /dev/null +++ b/lib/jekyll/document.rb @@ -0,0 +1,83 @@ +module Jekyll + class Document + + attr_reader :path + attr_accessor :content + + # Create a new Document. + # + # site - the Jekyll::Site instance to which this Document belongs + # path - the path to the file + # + # Returns nothing. + def initialize(site, path) + @site = site + @path = path + end + + # Fetch the Document's data. + # + # Returns a Hash containing the data. An empty hash is returned if + # no data was read. + def data + @data ||= Hash.new + end + + def extname + File.extname(path) + end + + def yaml_file? + %w[.yaml .yml].include?(extname) + end + + # Returns merged option hash for File.read of self.site (if exists) + # and a given param + # + # opts - override options + # + # Return + def merged_file_read_opts(opts) + (site ? site.file_read_opts : {}).merge(opts) + end + + # Whether the file is published or not, as indicated in YAML front-matter + # + # Returns true if the 'published' key is specified in the YAML front-matter and not `false`. + def published? + !(data.has_key?('published') && data['published'] == false) + end + + # Read in the file and assign the content and data based on the file contents. + # + # Returns nothing. + def read + if yaml_file? + @data = SafeYAML.load_file(path) + else + begin + @content = File.read(path, merged_file_read_opts(opts)) + if content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m + @content = $POSTMATCH + @data = SafeYAML.load($1) + end + rescue SyntaxError => e + puts "YAML Exception reading #{path}: #{e.message}" + rescue Exception => e + puts "Error reading file #{path}: #{e.message}" + end + end + end + + # Create a Liquid-understandable version of this Document. + # + # Returns a Hash representing this Document's data. + def to_liquid + data.merge({ + "content" => content, + "path" => path + }) + end + + end +end diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 14224898..4bb8f838 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -177,7 +177,7 @@ module Jekyll next if File.symlink?(path) && safe key = sanitize_filename(File.basename(entry, '.*')) - self.data[key] = SafeYAML.load_file(path) + (self.data[key] = Jekyll::Document.new(self, path)).read end end diff --git a/test/test_site.rb b/test/test_site.rb index c5d78594..8b805860 100644 --- a/test/test_site.rb +++ b/test/test_site.rb @@ -364,8 +364,8 @@ class TestSite < Test::Unit::TestCase file_content = SafeYAML.load_file(File.join(source_dir, '_data', 'members.yaml')) - assert_equal site.data['members'], file_content - assert_equal site.site_payload['site']['data']['members'], file_content + assert_equal site.data['members'].data, file_content + assert_equal site.site_payload['site']['data']['members'].data, file_content end should 'auto load yml files' do @@ -374,8 +374,8 @@ class TestSite < Test::Unit::TestCase file_content = SafeYAML.load_file(File.join(source_dir, '_data', 'languages.yml')) - assert_equal site.data['languages'], file_content - assert_equal site.site_payload['site']['data']['languages'], file_content + assert_equal site.data['languages'].data, file_content + assert_equal site.site_payload['site']['data']['languages'].data, file_content end should "load symlink files in unsafe mode" do @@ -384,8 +384,8 @@ class TestSite < Test::Unit::TestCase file_content = SafeYAML.load_file(File.join(source_dir, '_data', 'products.yml')) - assert_equal site.data['products'], file_content - assert_equal site.site_payload['site']['data']['products'], file_content + assert_equal site.data['products'].data, file_content + assert_equal site.site_payload['site']['data']['products'].data, file_content end should "not load symlink files in safe mode" do