add inline code docs

This commit is contained in:
maul.esel 2013-09-11 17:49:34 +02:00
parent 0874c14b2c
commit 5775603f49
1 changed files with 39 additions and 0 deletions

View File

@ -1,10 +1,22 @@
module Jekyll module Jekyll
class Configuration class Configuration
# This class handles custom defaults for YAML frontmatter settings.
# These are set in _config.yml and apply both to internal use (e.g. layout)
# and the data available to liquid.
#
# It is exposed via the frontmatter_defaults method on the site class.
class FrontmatterDefaults class FrontmatterDefaults
# Initializes a new instance.
def initialize(site) def initialize(site)
@site = site @site = site
end end
# Finds a default value for a given setting, filtered by path and type
#
# path - the path (relative to the source) of the page, post or :draft the default is used in
# type - a symbol indicating whether a :page, a :post or a :draft calls this method
#
# Returns the default value or nil if none was found
def find(path, type, setting) def find(path, type, setting)
value = nil value = nil
matching_sets(path, type).each do |set| matching_sets(path, type).each do |set|
@ -13,6 +25,12 @@ module Jekyll
value value
end 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) def all(path, type)
defaults = {} defaults = {}
matching_sets(path, type).each do |set| matching_sets(path, type).each do |set|
@ -23,20 +41,41 @@ module Jekyll
private 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) def applies?(scope, path, type)
(scope['path'].empty? || path.starts_with?(scope['path'])) && (!scope.has_key?('type') || scope['type'] == type.to_s) (scope['path'].empty? || path.starts_with?(scope['path'])) && (!scope.has_key?('type') || scope['type'] == type.to_s)
end end
# Checks if a given set of default values is valid
#
# set - the default value hash, as defined in _config.yml
#
# Returns true if the set is valid and can be used in this class
def valid?(set) def valid?(set)
set.is_a?(Hash) && set['scope'].is_a?(Hash) && set['scope']['path'].is_a?(String) && set['values'].is_a?(Hash) set.is_a?(Hash) && set['scope'].is_a?(Hash) && set['scope']['path'].is_a?(String) && set['values'].is_a?(Hash)
end end
# Collects a list of sets that match the given path and type
#
# Returns an array of hashes
def matching_sets(path, type) def matching_sets(path, type)
valid_sets.select do |set| valid_sets.select do |set|
applies?(set['scope'], path, type) applies?(set['scope'], path, type)
end end
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 def valid_sets
sets = @site.config['defaults'] sets = @site.config['defaults']
return [] unless sets.is_a?(Array) return [] unless sets.is_a?(Array)