Invoke File.read with or without options depends on Ruby version
- Extract option fetch method as a separate method - Added File.read_with_options method to use - With performance fix
This commit is contained in:
parent
97e052df63
commit
c625ddf6cd
|
@ -20,6 +20,12 @@ module Jekyll
|
||||||
self.content || ''
|
self.content || ''
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns merged optin hash for File.read of self.site (if exists)
|
||||||
|
# and a given param
|
||||||
|
def merged_file_read_opts(opts)
|
||||||
|
(self.site ? self.site.file_read_opts : {}).merge(opts)
|
||||||
|
end
|
||||||
|
|
||||||
# Read the YAML frontmatter.
|
# Read the YAML frontmatter.
|
||||||
#
|
#
|
||||||
# base - The String path to the dir containing the file.
|
# base - The String path to the dir containing the file.
|
||||||
|
@ -29,10 +35,8 @@ module Jekyll
|
||||||
# Returns nothing.
|
# Returns nothing.
|
||||||
def read_yaml(base, name, opts = {})
|
def read_yaml(base, name, opts = {})
|
||||||
begin
|
begin
|
||||||
opts = (self.site ? self.site.file_read_opts : {}).merge(opts)
|
self.content = File.read_with_options(File.join(base, name),
|
||||||
|
merged_file_read_opts(opts))
|
||||||
self.content = File.read(File.join(base, name), opts)
|
|
||||||
|
|
||||||
if self.content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
|
if self.content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
|
||||||
self.content = $POSTMATCH
|
self.content = $POSTMATCH
|
||||||
self.data = YAML.safe_load($1)
|
self.data = YAML.safe_load($1)
|
||||||
|
|
|
@ -69,3 +69,18 @@ module Enumerable
|
||||||
any? { |exp| File.fnmatch?(exp, e) }
|
any? { |exp| File.fnmatch?(exp, e) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Ruby 1.8's File.read don't support option.
|
||||||
|
# read_with_options ignore optional parameter for 1.8,
|
||||||
|
# and act as alias for 1.9 or later.
|
||||||
|
class File
|
||||||
|
if RUBY_VERSION < '1.9'
|
||||||
|
def self.read_with_options(path, opts = {})
|
||||||
|
self.read(path)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
def self.read_with_options(path, opts = {})
|
||||||
|
self.read(path, opts)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
|
@ -70,6 +70,11 @@ eos
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Grab file read opts in the context
|
||||||
|
def file_read_opts(context)
|
||||||
|
context.registers[:site].file_read_opts
|
||||||
|
end
|
||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
dir = File.join(context.registers[:site].source, INCLUDES_DIR)
|
dir = File.join(context.registers[:site].source, INCLUDES_DIR)
|
||||||
if error = validate_dir(dir, context.registers[:site].safe)
|
if error = validate_dir(dir, context.registers[:site].safe)
|
||||||
|
@ -81,7 +86,7 @@ eos
|
||||||
return error
|
return error
|
||||||
end
|
end
|
||||||
|
|
||||||
partial = Liquid::Template.parse(source(file))
|
partial = Liquid::Template.parse(source(file, context))
|
||||||
|
|
||||||
context.stack do
|
context.stack do
|
||||||
context['include'] = parse_params(context) if @params
|
context['include'] = parse_params(context) if @params
|
||||||
|
@ -108,8 +113,8 @@ eos
|
||||||
end
|
end
|
||||||
|
|
||||||
# This method allows to modify the file content by inheriting from the class.
|
# This method allows to modify the file content by inheriting from the class.
|
||||||
def source(file)
|
def source(file, context)
|
||||||
File.read(file, context.registers[:site].file_read_opts)
|
File.read_with_options(file, file_read_opts(context))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue