Replace load-in of YAML data with Jekyll::Document logic.
COLLECTIONS IS COMING
This commit is contained in:
parent
f0d4fefb6a
commit
a77c92aebe
|
@ -34,6 +34,7 @@ require 'jekyll/utils'
|
||||||
require 'jekyll/stevenson'
|
require 'jekyll/stevenson'
|
||||||
require 'jekyll/deprecator'
|
require 'jekyll/deprecator'
|
||||||
require 'jekyll/configuration'
|
require 'jekyll/configuration'
|
||||||
|
require 'jekyll/document'
|
||||||
require 'jekyll/plugin_manager'
|
require 'jekyll/plugin_manager'
|
||||||
require 'jekyll/site'
|
require 'jekyll/site'
|
||||||
require 'jekyll/convertible'
|
require 'jekyll/convertible'
|
||||||
|
|
|
@ -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
|
|
@ -177,7 +177,7 @@ module Jekyll
|
||||||
next if File.symlink?(path) && safe
|
next if File.symlink?(path) && safe
|
||||||
|
|
||||||
key = sanitize_filename(File.basename(entry, '.*'))
|
key = sanitize_filename(File.basename(entry, '.*'))
|
||||||
self.data[key] = SafeYAML.load_file(path)
|
(self.data[key] = Jekyll::Document.new(self, path)).read
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -364,8 +364,8 @@ class TestSite < Test::Unit::TestCase
|
||||||
|
|
||||||
file_content = SafeYAML.load_file(File.join(source_dir, '_data', 'members.yaml'))
|
file_content = SafeYAML.load_file(File.join(source_dir, '_data', 'members.yaml'))
|
||||||
|
|
||||||
assert_equal site.data['members'], file_content
|
assert_equal site.data['members'].data, file_content
|
||||||
assert_equal site.site_payload['site']['data']['members'], file_content
|
assert_equal site.site_payload['site']['data']['members'].data, file_content
|
||||||
end
|
end
|
||||||
|
|
||||||
should 'auto load yml files' do
|
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'))
|
file_content = SafeYAML.load_file(File.join(source_dir, '_data', 'languages.yml'))
|
||||||
|
|
||||||
assert_equal site.data['languages'], file_content
|
assert_equal site.data['languages'].data, file_content
|
||||||
assert_equal site.site_payload['site']['data']['languages'], file_content
|
assert_equal site.site_payload['site']['data']['languages'].data, file_content
|
||||||
end
|
end
|
||||||
|
|
||||||
should "load symlink files in unsafe mode" do
|
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'))
|
file_content = SafeYAML.load_file(File.join(source_dir, '_data', 'products.yml'))
|
||||||
|
|
||||||
assert_equal site.data['products'], file_content
|
assert_equal site.data['products'].data, file_content
|
||||||
assert_equal site.site_payload['site']['data']['products'], file_content
|
assert_equal site.site_payload['site']['data']['products'].data, file_content
|
||||||
end
|
end
|
||||||
|
|
||||||
should "not load symlink files in safe mode" do
|
should "not load symlink files in safe mode" do
|
||||||
|
|
Loading…
Reference in New Issue