Extract plugin management into its own class.

This commit is contained in:
Parker Moore 2014-04-01 18:10:51 -04:00
parent 9603d8a013
commit f418ea5fc3
2 changed files with 82 additions and 40 deletions

View File

@ -0,0 +1,79 @@
module Jekyll
class PluginManager
attr_reader :site
# Create an instance of this class.
#
# site - the instance of Jekyll::Site we're concerned with
#
# Returns nothing
def initialize(site)
@site = site
end
# Require all the plugins which are allowed.
#
# Returns nothing
def conscientious_require
require_plugin_files
require_gems
end
# Require each of the gem plugins specified.
#
# Returns nothing.
def require_gems
site.gems.each do |gem|
if plugin_allowed?(gem)
require gem
end
end
end
# Check whether a gem plugin is allowed to be used during this build.
#
# gem_name - the name of the gem
#
# Returns true if the gem name is in the whitelist or if the site is not
# in safe mode.
def plugin_allowed?(gem_name)
!site.safe || whitelist.include?(gem_name)
end
# Build an array of allowed plugin gem names.
#
# Returns an array of strings, each string being the name of a gem name
# that is allowed to be used.
def whitelist
@whitelist ||= Array[site.config['whitelist']].flatten
end
# Require all .rb files if safe mode is off
#
# Returns nothing.
def require_plugin_files
# If safe mode is off, load in any Ruby files under the plugins
# directory.
unless site.safe
plugins.each do |plugins|
Dir[File.join(plugins, "**", "*.rb")].sort.each do |f|
require f
end
end
end
end
# Static: Setup the plugin search path
#
# path_from_site - the plugin path from the Site configuration
#
# Returns an Array of plugin search paths
def self.plugins_path(path_from_site)
if (path_from_site == Jekyll::Configuration::DEFAULTS['plugins'])
[File.join(source, path_from_site)]
else
Array(path_from_site).map { |d| File.expand_path(d) }
end
end
end
end

View File

@ -19,7 +19,7 @@ module Jekyll
self.source = File.expand_path(config['source'])
self.dest = File.expand_path(config['destination'])
self.plugins = plugins_path
self.plugins = Jekyll::PluginManager.plugins_path(config['plugins'])
self.permalink_style = config['permalink'].to_sym
self.file_read_opts = {}
@ -63,17 +63,7 @@ module Jekyll
def setup
ensure_not_in_dest
# If safe mode is off, load in any Ruby files under the plugins
# directory.
unless safe
plugins.each do |plugins|
Dir[File.join(plugins, "**/*.rb")].sort.each do |f|
require f
end
end
end
require_gems
Jekyll::PluginManager.new(self).conscientious_require
self.converters = instantiate_subclasses(Jekyll::Converter)
self.generators = instantiate_subclasses(Jekyll::Generator)
@ -90,33 +80,6 @@ module Jekyll
end
end
def require_gems
gems.each do |gem|
if plugin_allowed?(gem)
require gem
end
end
end
def plugin_allowed?(gem_name)
whitelist.include?(gem_name) || !safe
end
def whitelist
@whitelist ||= Array[config['whitelist']].flatten
end
# Internal: Setup the plugin search path
#
# Returns an Array of plugin search paths
def plugins_path
if (config['plugins'] == Jekyll::Configuration::DEFAULTS['plugins'])
[File.join(source, config['plugins'])]
else
Array(config['plugins']).map { |d| File.expand_path(d) }
end
end
# Read Site data from disk and load it into internal data structures.
#
# Returns nothing.
@ -307,7 +270,7 @@ module Jekyll
# See Site#post_attr_hash for type info.
def site_payload
{"jekyll" => { "version" => Jekyll::VERSION },
"site" => config.merge({
"site" => config.merge({
"time" => time,
"posts" => posts.sort { |a, b| b <=> a },
"pages" => pages,