Extracted `in_source_dir` from site.rb and into reader.rb.

Extracted `in_source_dir` from site.rb into reader.rb.
Updated all the references and tests.

Signed-off-by: Martin Jorn Rogalla <martin@martinrogalla.com>
This commit is contained in:
Martin Jorn Rogalla 2015-03-04 19:16:03 +01:00
parent 5e9b7343bf
commit bb9d43dff5
14 changed files with 67 additions and 40 deletions

View File

@ -55,6 +55,7 @@ module Jekyll
autoload :PluginManager, 'jekyll/plugin_manager'
autoload :Post, 'jekyll/post'
autoload :Publisher, 'jekyll/publisher'
autoload :Reader, 'jekyll/reader'
autoload :Regenerator, 'jekyll/regenerator'
autoload :RelatedPosts, 'jekyll/related_posts'
autoload :Renderer, 'jekyll/renderer'

View File

@ -89,7 +89,7 @@ module Jekyll
# Returns a String containing th directory name where the collection
# is stored on the filesystem.
def directory
@directory ||= site.in_source_dir(relative_directory)
@directory ||= site.reader.in_source_dir(relative_directory)
end
# The full path to the directory containing the collection, with
@ -102,7 +102,7 @@ module Jekyll
# is stored on the filesystem.
def collection_dir(*files)
return directory if files.empty?
site.in_source_dir(relative_directory, *files)
site.reader.in_source_dir(relative_directory, *files)
end
# Checks whether the directory "exists" for this collection.

View File

@ -43,7 +43,7 @@ module Jekyll
# Returns nothing.
def read_yaml(base, name, opts = {})
begin
self.content = File.read(site.in_source_dir(base, name),
self.content = File.read(site.reader.in_source_dir(base, name),
merged_file_read_opts(opts))
if content =~ /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m
self.content = $POSTMATCH
@ -209,8 +209,8 @@ module Jekyll
# Add layout to dependency tree
site.regenerator.add_dependency(
site.in_source_dir(path),
site.in_source_dir(layout.path)
site.reader.in_source_dir(path),
site.reader.in_source_dir(layout.path)
)
if layout = layouts[layout.data["layout"]]

View File

@ -15,7 +15,7 @@ module Jekyll
# Get the full path to the directory containing the draft files
def containing_dir(dir)
site.in_source_dir(dir, '_drafts')
site.reader.in_source_dir(dir, '_drafts')
end
# The path to the draft source file, relative to the site source

View File

@ -29,7 +29,7 @@ module Jekyll
@site = site
@base = base
@name = name
@path = site.in_source_dir(base, name)
@path = site.reader.in_source_dir(base, name)
self.data = {}

View File

@ -38,7 +38,7 @@ module Jekyll
end
def layout_directory_inside_source
site.in_source_dir(site.config['layouts'])
site.reader.in_source_dir(site.config['layouts'])
end
def layout_directory_in_cwd

View File

@ -82,7 +82,7 @@ module Jekyll
# Returns an Array of plugin search paths
def plugins_path
if (site.config['plugins'] == Jekyll::Configuration::DEFAULTS['plugins'])
[site.in_source_dir(site.config['plugins'])]
[site.reader.in_source_dir(site.config['plugins'])]
else
Array(site.config['plugins']).map { |d| File.expand_path(d) }
end

View File

@ -91,7 +91,7 @@ module Jekyll
# Get the full path to the directory containing the post files
def containing_dir(dir)
site.in_source_dir(dir, '_posts')
site.reader.in_source_dir(dir, '_posts')
end
# Read the YAML frontmatter.

38
lib/jekyll/reader.rb Normal file
View File

@ -0,0 +1,38 @@
# encoding: UTF-8
require 'csv'
module Jekyll
class Reader
# Public: Initialize a new Reader.
# @return [Object]
def initialize(source, dest)
@source = source
@dest = dest
end
# Public: Prefix a given path with the source directory.
#
# paths - (optional) path elements to a file or directory within the
# source directory
#
# Returns a path which is prefixed with the source directory.
def in_source_dir(*paths)
paths.reduce(@source) do |base, path|
Jekyll.sanitized_path(base, path)
end
end
# Public: Prefix a given path with the destination directory.
#
# paths - (optional) path elements to a file or directory within the
# destination directory
#
# Returns a path which is prefixed with the destination directory.
def in_dest_dir(*paths)
paths.reduce(@dest) do |base, path|
Jekyll.sanitized_path(base, path)
end
end
end
end

View File

@ -19,7 +19,7 @@ module Jekyll
case document
when Post, Page
document.asset_file? || document.data['regenerate'] ||
modified?(site.in_source_dir(document.relative_path))
modified?(site.reader.in_source_dir(document.relative_path))
when Document
!document.write? || document.data['regenerate'] || modified?(document.path)
else
@ -113,7 +113,7 @@ module Jekyll
#
# Returns the String path of the file.
def metadata_file
site.in_source_dir('.jekyll-metadata')
site.reader.in_source_dir('.jekyll-metadata')
end
# Check if metadata has been disabled

View File

@ -141,8 +141,8 @@ module Jekyll
# Add layout to dependency tree
site.regenerator.add_dependency(
site.in_source_dir(document.path),
site.in_source_dir(layout.path)
site.reader.in_source_dir(document.path),
site.reader.in_source_dir(layout.path)
) if document.write?
if layout = site.layouts[layout.data["layout"]]

View File

@ -10,7 +10,7 @@ module Jekyll
:show_drafts, :keep_files, :baseurl, :data, :file_read_opts,
:gems, :plugin_manager
attr_accessor :converters, :generators
attr_accessor :converters, :generators, :reader
attr_reader :regenerator
# Public: Initialize a new Site.
@ -28,6 +28,8 @@ module Jekyll
@source = File.expand_path(config['source']).freeze
@dest = File.expand_path(config['destination']).freeze
@reader = Reader.new(@source,@dest);
# Initialize incremental regenerator
@regenerator = Regenerator.new(self)
@ -97,18 +99,6 @@ module Jekyll
end
end
# Public: Prefix a given path with the source directory.
#
# paths - (optional) path elements to a file or directory within the
# source directory
#
# Returns a path which is prefixed with the source directory.
def in_source_dir(*paths)
paths.reduce(source) do |base, path|
Jekyll.sanitized_path(base, path)
end
end
# Public: Prefix a given path with the destination directory.
#
# paths - (optional) path elements to a file or directory within the
@ -116,9 +106,7 @@ module Jekyll
#
# Returns a path which is prefixed with the destination directory.
def in_dest_dir(*paths)
paths.reduce(dest) do |base, path|
Jekyll.sanitized_path(base, path)
end
reader.in_dest_dir(*paths)
end
# The list of collections and their corresponding Jekyll::Collection instances.
@ -165,7 +153,7 @@ module Jekyll
#
# Returns nothing.
def read_directories(dir = '')
base = in_source_dir(dir)
base = reader.in_source_dir(dir)
entries = Dir.chdir(base) { filter_entries(Dir.entries('.'), base) }
read_posts(dir)
@ -174,7 +162,7 @@ module Jekyll
limit_posts! if limit_posts > 0 # limit the posts if :limit_posts option is set
entries.each do |f|
f_abs = in_source_dir(base, f)
f_abs = reader.in_source_dir(base, f)
if File.directory?(f_abs)
f_rel = File.join(dir, f)
read_directories(f_rel) unless dest.sub(/\/$/, '') == f_abs
@ -232,7 +220,7 @@ module Jekyll
#
# Returns nothing
def read_data(dir)
base = in_source_dir(dir)
base = reader.in_source_dir(dir)
read_data_to(base, self.data)
end
@ -251,7 +239,7 @@ module Jekyll
end
entries.each do |entry|
path = in_source_dir(dir, entry)
path = reader.in_source_dir(dir, entry)
next if File.symlink?(path) && safe
key = sanitize_filename(File.basename(entry, '.*'))
@ -454,10 +442,10 @@ module Jekyll
#
# Returns the list of entries to process
def get_entries(dir, subfolder)
base = in_source_dir(dir, subfolder)
base = reader.in_source_dir(dir, subfolder)
return [] unless File.exist?(base)
entries = Dir.chdir(base) { filter_entries(Dir['**/*'], base) }
entries.delete_if { |e| File.directory?(in_source_dir(base, e)) }
entries.delete_if { |e| File.directory?(reader.in_source_dir(base, e)) }
end
# Aggregate post information

View File

@ -117,7 +117,7 @@ eos
# Add include to dependency tree
if context.registers[:page] and context.registers[:page].has_key? "path"
site.regenerator.add_dependency(
site.in_source_dir(context.registers[:page]["path"]),
site.reader.in_source_dir(context.registers[:page]["path"]),
path
)
end
@ -135,7 +135,7 @@ eos
end
def resolved_includes_dir(context)
context.registers[:site].in_source_dir(@includes_dir)
context.registers[:site].reader.in_source_dir(@includes_dir)
end
def validate_path(path, dir, safe)
@ -170,7 +170,7 @@ eos
end
def resolved_includes_dir(context)
context.registers[:site].in_source_dir(page_path(context))
context.registers[:site].reader.in_source_dir(page_path(context))
end
end
end

View File

@ -63,7 +63,7 @@ class TestRegenerator < JekyllUnitTest
}))
@site.process
@path = @site.in_source_dir(@site.pages.first.path)
@path = @site.reader.in_source_dir(@site.pages.first.path)
@regenerator = @site.regenerator
end