Skip to content

Commit

Permalink
Merge pull request #953 from mojombo/refactor-configuration
Browse files Browse the repository at this point in the history
Refactor Jekyll Configuration
  • Loading branch information
parkr committed Apr 13, 2013
2 parents 4765338 + 6881d3b commit 891ccbd
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 142 deletions.
113 changes: 6 additions & 107 deletions lib/jekyll.rb
Expand Up @@ -28,6 +28,7 @@ def require_all(path)

# internal requires
require 'jekyll/core_ext'
require 'jekyll/configuration'
require 'jekyll/site'
require 'jekyll/convertible'
require 'jekyll/layout'
Expand All @@ -54,122 +55,20 @@ def require_all(path)
module Jekyll
VERSION = '1.0.0.beta4'

# Default options. Overriden by values in _config.yml.
# Strings rather than symbols are used for compatability with YAML.
DEFAULTS = {
'source' => Dir.pwd,
'destination' => File.join(Dir.pwd, '_site'),
'plugins' => '_plugins',
'layouts' => '_layouts',
'keep_files' => ['.git','.svn'],

'future' => true, # remove and make true just default
'pygments' => true, # remove and make true just default

'markdown' => 'maruku',
'permalink' => 'date',
'baseurl' => '/',
'include' => ['.htaccess'],
'paginate_path' => 'page:num',

'markdown_ext' => 'markdown,mkd,mkdn,md',
'textile_ext' => 'textile',

'port' => '4000',
'host' => '0.0.0.0',

'excerpt_separator' => "\n\n",

'maruku' => {
'use_tex' => false,
'use_divs' => false,
'png_engine' => 'blahtex',
'png_dir' => 'images/latex',
'png_url' => '/images/latex'
},

'rdiscount' => {
'extensions' => []
},

'redcarpet' => {
'extensions' => []
},

'kramdown' => {
'auto_ids' => true,
'footnote_nr' => 1,
'entity_output' => 'as_char',
'toc_levels' => '1..6',
'smart_quotes' => 'lsquo,rsquo,ldquo,rdquo',
'use_coderay' => false,

'coderay' => {
'coderay_wrap' => 'div',
'coderay_line_numbers' => 'inline',
'coderay_line_number_start' => 1,
'coderay_tab_width' => 4,
'coderay_bold_every' => 10,
'coderay_css' => 'style'
}
},

'redcloth' => {
'hard_breaks' => true
}
}

# Public: Generate a Jekyll configuration Hash by merging the default
# options with anything in _config.yml, and adding the given options on top.
#
# override - A Hash of config directives that override any options in both
# the defaults and the config file. See Jekyll::DEFAULTS for a
# the defaults and the config file. See Jekyll::Configuration::DEFAULTS for a
# list of option names and their defaults.
#
# Returns the final configuration Hash.
def self.configuration(override)
# Convert any symbol keys to strings and remove the old key/values
override = override.reduce({}) { |hsh,(k,v)| hsh.merge(k.to_s => v) }

# _config.yml may override default source location, but until
# then, we need to know where to look for _config.yml
source = override['source'] || Jekyll::DEFAULTS['source']

# Get configuration from <source>/_config.yml or <source>/<config_file>
config_files = override.delete('config')
config_files = File.join(source, "_config.yml") if config_files.to_s.empty?
unless config_files.is_a? Array
config_files = [config_files]
end

begin
config = {}
config_files.each do |config_file|
next_config = YAML.safe_load_file(config_file)
raise "Configuration file: (INVALID) #{config_file}" if !next_config.is_a?(Hash)
$stdout.puts "Configuration file: #{config_file}"
config = config.deep_merge(next_config)
end
rescue SystemCallError
# Errno:ENOENT = file not found
$stderr.puts "Configuration file: none"
config = {}
rescue => err
$stderr.puts " " +
"WARNING: Error reading configuration. " +
"Using defaults (and options)."
$stderr.puts "#{err}"
config = {}
end

# Provide backwards-compatibility
if config['auto']
$stderr.puts "Deprecation: ".rjust(20) + "'auto' has been changed to " +
"'watch'. Please update your configuration to use 'watch'."
config['watch'] = config['auto']
end
config = Configuration[Configuration::DEFAULTS]
override = Configuration[override].stringify_keys
config = config.read_config_files(config.config_files(override))

# Merge DEFAULTS < _config.yml < override
Jekyll::DEFAULTS.deep_merge(config).deep_merge(override)
config.deep_merge(override).stringify_keys
end
end
154 changes: 154 additions & 0 deletions lib/jekyll/configuration.rb
@@ -0,0 +1,154 @@
module Jekyll
class Configuration < Hash

# Default options. Overriden by values in _config.yml.
# Strings rather than symbols are used for compatability with YAML.
DEFAULTS = {
'source' => Dir.pwd,
'destination' => File.join(Dir.pwd, '_site'),
'plugins' => '_plugins',
'layouts' => '_layouts',
'keep_files' => ['.git','.svn'],

'future' => true, # remove and make true just default
'pygments' => true, # remove and make true just default

'markdown' => 'maruku',
'permalink' => 'date',
'baseurl' => '/',
'include' => ['.htaccess'],
'paginate_path' => 'page:num',

'markdown_ext' => 'markdown,mkd,mkdn,md',
'textile_ext' => 'textile',

'port' => '4000',
'host' => '0.0.0.0',

'excerpt_separator' => "\n\n",

'maruku' => {
'use_tex' => false,
'use_divs' => false,
'png_engine' => 'blahtex',
'png_dir' => 'images/latex',
'png_url' => '/images/latex'
},

'rdiscount' => {
'extensions' => []
},

'redcarpet' => {
'extensions' => []
},

'kramdown' => {
'auto_ids' => true,
'footnote_nr' => 1,
'entity_output' => 'as_char',
'toc_levels' => '1..6',
'smart_quotes' => 'lsquo,rsquo,ldquo,rdquo',
'use_coderay' => false,

'coderay' => {
'coderay_wrap' => 'div',
'coderay_line_numbers' => 'inline',
'coderay_line_number_start' => 1,
'coderay_tab_width' => 4,
'coderay_bold_every' => 10,
'coderay_css' => 'style'
}
},

'redcloth' => {
'hard_breaks' => true
}
}

# Public: Turn all keys into string
#
# Return a copy of the hash where all its keys are strings
def stringify_keys
reduce({}) { |hsh,(k,v)| hsh.merge(k.to_s => v) }
end

# Public: Directory of the Jekyll source folder
#
# override - the command-line options hash
#
# Returns the path to the Jekyll source directory
def source(override)
override['source'] || self['source'] || DEFAULTS['source']
end

# Public: Generate list of configuration files from the override
#
# override - the command-line options hash
#
# Returns an Array of config files
def config_files(override)
# Get configuration from <source>/_config.yml or <source>/<config_file>
config_files = override.delete('config')
config_files = File.join(source(override), "_config.yml") if config_files.to_s.empty?
config_files = [config_files] unless config_files.is_a? Array
config_files
end

# Public: Read configuration and return merged Hash
#
# file - the path to the YAML file to be read in
#
# Returns this configuration, overridden by the values in the file
def read_config_file(file)
configuration = dup
next_config = YAML.safe_load_file(file)
raise "Configuration file: (INVALID) #{file}" if !next_config.is_a?(Hash)
$stdout.puts "Configuration file: #{file}"
configuration.deep_merge(next_config)
end

# Public: Read in a list of configuration files and merge with this hash
#
# files - the list of configuration file paths
#
# Returns the full configuration, with the defaults overridden by the values in the
# configuration files
def read_config_files(files)
configuration = dup

begin
files.each do |config_file|
configuration = read_config_file(config_file)
end
rescue SystemCallError
# Errno:ENOENT = file not found
$stderr.puts "Configuration file: none"
rescue => err
$stderr.puts " " +
"WARNING: Error reading configuration. " +
"Using defaults (and options)."
$stderr.puts "#{err}"
end

configuration.backwards_compatibilize
end

# Public: Ensure the proper options are set in the configuration to allow for
# backwards-compatibility with Jekyll pre-1.0
#
# Returns the backwards-compatible configuration
def backwards_compatibilize
config = dup
# Provide backwards-compatibility
if config['auto']
$stderr.puts "Deprecation: ".rjust(20) + "'auto' has been changed to " +
"'watch'. Please update your configuration to use 'watch'."
config['watch'] = config['auto']
end

config
end

end
end
2 changes: 1 addition & 1 deletion lib/jekyll/site.rb
Expand Up @@ -97,7 +97,7 @@ def setup
#
# Returns an Array of plugin search paths
def plugins_path
if (config['plugins'] == Jekyll::DEFAULTS['plugins'])
if (config['plugins'] == Jekyll::Configuration::DEFAULTS['plugins'])
[File.join(self.source, config['plugins'])]
else
Array(config['plugins']).map { |d| File.expand_path(d) }
Expand Down

0 comments on commit 891ccbd

Please sign in to comment.