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:
Shigeya Suzuki 2013-08-24 14:40:55 +09:00
parent 97e052df63
commit c625ddf6cd
3 changed files with 31 additions and 7 deletions

View File

@ -20,6 +20,12 @@ module Jekyll
self.content || ''
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.
#
# base - The String path to the dir containing the file.
@ -29,10 +35,8 @@ module Jekyll
# Returns nothing.
def read_yaml(base, name, opts = {})
begin
opts = (self.site ? self.site.file_read_opts : {}).merge(opts)
self.content = File.read(File.join(base, name), opts)
self.content = File.read_with_options(File.join(base, name),
merged_file_read_opts(opts))
if self.content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
self.content = $POSTMATCH
self.data = YAML.safe_load($1)

View File

@ -69,3 +69,18 @@ module Enumerable
any? { |exp| File.fnmatch?(exp, e) }
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

View File

@ -70,6 +70,11 @@ eos
end
end
# Grab file read opts in the context
def file_read_opts(context)
context.registers[:site].file_read_opts
end
def render(context)
dir = File.join(context.registers[:site].source, INCLUDES_DIR)
if error = validate_dir(dir, context.registers[:site].safe)
@ -81,7 +86,7 @@ eos
return error
end
partial = Liquid::Template.parse(source(file))
partial = Liquid::Template.parse(source(file, context))
context.stack do
context['include'] = parse_params(context) if @params
@ -108,8 +113,8 @@ eos
end
# This method allows to modify the file content by inheriting from the class.
def source(file)
File.read(file, context.registers[:site].file_read_opts)
def source(file, context)
File.read_with_options(file, file_read_opts(context))
end
end
end