Put errors/exceptions into Jekyll::Errors module

This commit is contained in:
Parker Moore 2014-07-12 13:47:59 -07:00
parent a97ae67552
commit c7cc36abdb
13 changed files with 165 additions and 146 deletions

View File

@ -51,7 +51,7 @@ module Jekyll
# Returns nothing # Returns nothing
def process_site(site) def process_site(site)
site.process site.process
rescue Jekyll::FatalException => e rescue Jekyll::Errors::FatalException => e
Jekyll.logger.error "ERROR:", "YOUR SITE COULD NOT BE BUILT:" Jekyll.logger.error "ERROR:", "YOUR SITE COULD NOT BE BUILT:"
Jekyll.logger.error "", "------------------------------------" Jekyll.logger.error "", "------------------------------------"
Jekyll.logger.error "", e.message Jekyll.logger.error "", e.message

View File

@ -11,22 +11,41 @@ module Jekyll
@parser = @parser =
case @config['markdown'].downcase case @config['markdown'].downcase
when 'redcarpet' then RedcarpetParser.new(@config) when 'redcarpet' then RedcarpetParser.new(@config)
when 'kramdown' then KramdownParser.new(@config) when 'kramdown' then KramdownParser.new(@config)
when 'rdiscount' then RDiscountParser.new(@config) when 'rdiscount' then RDiscountParser.new(@config)
when 'maruku' then MarukuParser.new(@config) when 'maruku' then MarukuParser.new(@config)
else else
# So they can't try some tricky bullshit or go down the ancestor chain, I hope. # So they can't try some tricky bullshit or go down the ancestor chain, I hope.
if allowed_custom_class?(@config['markdown']) if allowed_custom_class?(@config['markdown'])
self.class.const_get(@config['markdown']).new(@config) self.class.const_get(@config['markdown']).new(@config)
else else
Jekyll.logger.error "Invalid Markdown Processor:", "#{@config['markdown']}" Jekyll.logger.error "Invalid Markdown Processor:", "#{@config['markdown']}"
Jekyll.logger.error "", "Valid options are [ maruku | rdiscount | kramdown | redcarpet ]" Jekyll.logger.error "", "Valid options are [ #{valid_processors.join(" | ")} ]"
raise FatalException, "Invalid Markdown Processor: #{@config['markdown']}" raise Errors::FatalException, "Invalid Markdown Processor: #{@config['markdown']}"
end end
end end
@setup = true @setup = true
end end
def valid_processors
%w[
maruku
rdiscount
kramdown
redcarpet
] + third_party_processors
end
def third_party_processors
self.class.constants - %w[
KramdownParser
MarukuParser
RDiscountParser
RedcarpetParser
PRIORITIES
].map(&:to_sym)
end
def matches(ext) def matches(ext)
rgx = '^\.(' + @config['markdown_ext'].gsub(',','|') +')$' rgx = '^\.(' + @config['markdown_ext'].gsub(',','|') +')$'
ext =~ Regexp.new(rgx, Regexp::IGNORECASE) ext =~ Regexp.new(rgx, Regexp::IGNORECASE)

View File

@ -8,7 +8,7 @@ module Jekyll
rescue LoadError rescue LoadError
STDERR.puts 'You are missing a library required for Markdown. Please run:' STDERR.puts 'You are missing a library required for Markdown. Please run:'
STDERR.puts ' $ [sudo] gem install kramdown' STDERR.puts ' $ [sudo] gem install kramdown'
raise FatalException.new("Missing dependency: kramdown") raise Errors::FatalException.new("Missing dependency: kramdown")
end end
def convert(content) def convert(content)

View File

@ -15,7 +15,7 @@ module Jekyll
rescue LoadError rescue LoadError
STDERR.puts 'You are missing a library required for Markdown. Please run:' STDERR.puts 'You are missing a library required for Markdown. Please run:'
STDERR.puts ' $ [sudo] gem install maruku' STDERR.puts ' $ [sudo] gem install maruku'
raise FatalException.new("Missing dependency: maruku") raise Errors::FatalException.new("Missing dependency: maruku")
end end
def load_divs_library def load_divs_library

View File

@ -13,7 +13,7 @@ module Jekyll
rescue LoadError rescue LoadError
STDERR.puts 'You are missing a library required for Textile. Please run:' STDERR.puts 'You are missing a library required for Textile. Please run:'
STDERR.puts ' $ [sudo] gem install RedCloth' STDERR.puts ' $ [sudo] gem install RedCloth'
raise FatalException.new("Missing dependency: RedCloth") raise Errors::FatalException.new("Missing dependency: RedCloth")
end end
def matches(ext) def matches(ext)

View File

@ -1,5 +1,8 @@
module Jekyll module Jekyll
class FatalException < StandardError module Errors
class FatalException < RuntimeError
end
class MissingDependencyException < FatalException class MissingDependencyException < FatalException
end end
end end

View File

@ -1,4 +1,3 @@
require 'jekyll/convertible'
require 'forwardable' require 'forwardable'
module Jekyll module Jekyll

View File

@ -1,147 +1,145 @@
module Jekyll module Jekyll
class Configuration # This class handles custom defaults for YAML frontmatter settings.
# This class handles custom defaults for YAML frontmatter settings. # These are set in _config.yml and apply both to internal use (e.g. layout)
# These are set in _config.yml and apply both to internal use (e.g. layout) # and the data available to liquid.
# and the data available to liquid. #
# It is exposed via the frontmatter_defaults method on the site class.
class FrontmatterDefaults
# Initializes a new instance.
def initialize(site)
@site = site
end
# Finds a default value for a given setting, filtered by path and type
# #
# It is exposed via the frontmatter_defaults method on the site class. # path - the path (relative to the source) of the page, post or :draft the default is used in
class FrontmatterDefaults # type - a symbol indicating whether a :page, a :post or a :draft calls this method
# Initializes a new instance. #
def initialize(site) # Returns the default value or nil if none was found
@site = site def find(path, type, setting)
end value = nil
old_scope = nil
# Finds a default value for a given setting, filtered by path and type matching_sets(path, type).each do |set|
# if set['values'].has_key?(setting) && has_precedence?(old_scope, set['scope'])
# path - the path (relative to the source) of the page, post or :draft the default is used in value = set['values'][setting]
# type - a symbol indicating whether a :page, a :post or a :draft calls this method old_scope = set['scope']
#
# Returns the default value or nil if none was found
def find(path, type, setting)
value = nil
old_scope = nil
matching_sets(path, type).each do |set|
if set['values'].has_key?(setting) && has_precedence?(old_scope, set['scope'])
value = set['values'][setting]
old_scope = set['scope']
end
end
value
end
# Collects a hash with all default values for a page or post
#
# path - the relative path of the page or post
# type - a symbol indicating the type (:post, :page or :draft)
#
# Returns a hash with all default values (an empty hash if there are none)
def all(path, type)
defaults = {}
old_scope = nil
matching_sets(path, type).each do |set|
if has_precedence?(old_scope, set['scope'])
defaults = Utils.deep_merge_hashes(defaults, set['values'])
old_scope = set['scope']
else
defaults = Utils.deep_merge_hashes(set['values'], defaults)
end
end
defaults
end
private
# Checks if a given default setting scope matches the given path and type
#
# scope - the hash indicating the scope, as defined in _config.yml
# path - the path to check for
# type - the type (:post, :page or :draft) to check for
#
# Returns true if the scope applies to the given path and type
def applies?(scope, path, type)
applies_path?(scope, path) && applies_type?(scope, type)
end
def applies_path?(scope, path)
return true if scope['path'].empty?
scope_path = Pathname.new(scope['path'])
Pathname.new(sanitize_path(path)).ascend do |path|
if path == scope_path
return true
end
end end
end end
value
end
def applies_type?(scope, type) # Collects a hash with all default values for a page or post
!scope.has_key?('type') || scope['type'] == type.to_s #
end # path - the relative path of the page or post
# type - a symbol indicating the type (:post, :page or :draft)
# Checks if a given set of default values is valid #
# # Returns a hash with all default values (an empty hash if there are none)
# set - the default value hash, as defined in _config.yml def all(path, type)
# defaults = {}
# Returns true if the set is valid and can be used in this class old_scope = nil
def valid?(set) matching_sets(path, type).each do |set|
set.is_a?(Hash) && set['scope'].is_a?(Hash) && set['scope']['path'].is_a?(String) && set['values'].is_a?(Hash) if has_precedence?(old_scope, set['scope'])
end defaults = Utils.deep_merge_hashes(defaults, set['values'])
old_scope = set['scope']
# Determines if a new scope has precedence over an old one
#
# old_scope - the old scope hash, or nil if there's none
# new_scope - the new scope hash
#
# Returns true if the new scope has precedence over the older
def has_precedence?(old_scope, new_scope)
return true if old_scope.nil?
new_path = sanitize_path(new_scope['path'])
old_path = sanitize_path(old_scope['path'])
if new_path.length != old_path.length
new_path.length >= old_path.length
elsif new_scope.has_key? 'type'
true
else else
!old_scope.has_key? 'type' defaults = Utils.deep_merge_hashes(set['values'], defaults)
end end
end end
defaults
end
# Collects a list of sets that match the given path and type private
#
# Returns an array of hashes # Checks if a given default setting scope matches the given path and type
def matching_sets(path, type) #
valid_sets.select do |set| # scope - the hash indicating the scope, as defined in _config.yml
applies?(set['scope'], path, type) # path - the path to check for
# type - the type (:post, :page or :draft) to check for
#
# Returns true if the scope applies to the given path and type
def applies?(scope, path, type)
applies_path?(scope, path) && applies_type?(scope, type)
end
def applies_path?(scope, path)
return true if scope['path'].empty?
scope_path = Pathname.new(scope['path'])
Pathname.new(sanitize_path(path)).ascend do |path|
if path == scope_path
return true
end end
end end
end
# Returns a list of valid sets def applies_type?(scope, type)
# !scope.has_key?('type') || scope['type'] == type.to_s
# This is not cached to allow plugins to modify the configuration end
# and have their changes take effect
#
# Returns an array of hashes
def valid_sets
sets = @site.config['defaults']
return [] unless sets.is_a?(Array)
sets.select do |set| # Checks if a given set of default values is valid
unless valid?(set) #
Jekyll.logger.warn "Default:", "An invalid default set was found" # set - the default value hash, as defined in _config.yml
end #
valid?(set) # Returns true if the set is valid and can be used in this class
end def valid?(set)
set.is_a?(Hash) && set['scope'].is_a?(Hash) && set['scope']['path'].is_a?(String) && set['values'].is_a?(Hash)
end
# Determines if a new scope has precedence over an old one
#
# old_scope - the old scope hash, or nil if there's none
# new_scope - the new scope hash
#
# Returns true if the new scope has precedence over the older
def has_precedence?(old_scope, new_scope)
return true if old_scope.nil?
new_path = sanitize_path(new_scope['path'])
old_path = sanitize_path(old_scope['path'])
if new_path.length != old_path.length
new_path.length >= old_path.length
elsif new_scope.has_key? 'type'
true
else
!old_scope.has_key? 'type'
end end
end
# Sanitizes the given path by removing a leading and addding a trailing slash # Collects a list of sets that match the given path and type
def sanitize_path(path) #
if path.nil? || path.empty? # Returns an array of hashes
"" def matching_sets(path, type)
else valid_sets.select do |set|
path.gsub(/\A\//, '').gsub(/([^\/])\z/, '\1/') applies?(set['scope'], path, type)
end
end
# Returns a list of valid sets
#
# This is not cached to allow plugins to modify the configuration
# and have their changes take effect
#
# Returns an array of hashes
def valid_sets
sets = @site.config['defaults']
return [] unless sets.is_a?(Array)
sets.select do |set|
unless valid?(set)
Jekyll.logger.warn "Default:", "An invalid default set was found"
end end
valid?(set)
end
end
# Sanitizes the given path by removing a leading and addding a trailing slash
def sanitize_path(path)
if path.nil? || path.empty?
""
else
path.gsub(/\A\//, '').gsub(/([^\/])\z/, '\1/')
end end
end end
end end

View File

@ -166,7 +166,7 @@ module Jekyll
path = File.join(@dir || "", name) path = File.join(@dir || "", name)
msg = "Post '#{path}' does not have a valid date.\n" msg = "Post '#{path}' does not have a valid date.\n"
msg << "Fix the date, or exclude the file or directory from being processed" msg << "Fix the date, or exclude the file or directory from being processed"
raise FatalException.new(msg) raise Errors::FatalException.new(msg)
end end
# The generated directory into which the post will be placed # The generated directory into which the post will be placed

View File

@ -80,7 +80,7 @@ module Jekyll
dest_pathname = Pathname.new(dest) dest_pathname = Pathname.new(dest)
Pathname.new(source).ascend do |path| Pathname.new(source).ascend do |path|
if path == dest_pathname if path == dest_pathname
raise FatalException.new "Destination directory cannot be or contain the Source directory." raise Errors::FatalException.new "Destination directory cannot be or contain the Source directory."
end end
end end
end end
@ -443,7 +443,7 @@ module Jekyll
end end
def frontmatter_defaults def frontmatter_defaults
@frontmatter_defaults ||= Configuration::FrontmatterDefaults.new(self) @frontmatter_defaults ||= FrontmatterDefaults.new(self)
end end
private private

View File

@ -44,7 +44,7 @@ class TestCommand < Test::Unit::TestCase
context "when fatal error occurs" do context "when fatal error occurs" do
should "exit with non-zero error code" do should "exit with non-zero error code" do
site = Object.new site = Object.new
stub(site).process { raise Jekyll::FatalException } stub(site).process { raise Jekyll::Errors::FatalException }
error = assert_raise(SystemExit) { Command.process_site(site) } error = assert_raise(SystemExit) { Command.process_site(site) }
assert_not_equal 0, error.status assert_not_equal 0, error.status
end end

View File

@ -83,7 +83,7 @@ class TestPost < Test::Unit::TestCase
end end
should "raise a good error on invalid post date" do should "raise a good error on invalid post date" do
assert_raise Jekyll::FatalException do assert_raise Jekyll::Errors::FatalException do
@post.process("2009-27-03-foo-bar.textile") @post.process("2009-27-03-foo-bar.textile")
end end
end end

View File

@ -225,7 +225,7 @@ class TestSite < Test::Unit::TestCase
Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => source_dir}) Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => source_dir})
end end
assert_raise Jekyll::FatalException do assert_raise Jekyll::Errors::FatalException do
site = Site.new(Jekyll.configuration) site = Site.new(Jekyll.configuration)
end end
end end
@ -235,7 +235,7 @@ class TestSite < Test::Unit::TestCase
Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => File.join(source_dir, "..")}) Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => File.join(source_dir, "..")})
end end
assert_raise Jekyll::FatalException do assert_raise Jekyll::Errors::FatalException do
site = Site.new(Jekyll.configuration) site = Site.new(Jekyll.configuration)
end end
end end
@ -332,7 +332,7 @@ class TestSite < Test::Unit::TestCase
bad_processor = "Custom::Markdown" bad_processor = "Custom::Markdown"
s = Site.new(Jekyll.configuration.merge({ 'markdown' => bad_processor })) s = Site.new(Jekyll.configuration.merge({ 'markdown' => bad_processor }))
assert_raise Jekyll::FatalException do assert_raise Jekyll::Errors::FatalException do
s.process s.process
end end
@ -352,7 +352,7 @@ class TestSite < Test::Unit::TestCase
should 'throw FatalException at process time' do should 'throw FatalException at process time' do
bad_processor = 'not a processor name' bad_processor = 'not a processor name'
s = Site.new(Jekyll.configuration.merge({ 'markdown' => bad_processor })) s = Site.new(Jekyll.configuration.merge({ 'markdown' => bad_processor }))
assert_raise Jekyll::FatalException do assert_raise Jekyll::Errors::FatalException do
s.process s.process
end end
end end