From 04fd68a232258e004caa59c4e3c6b9e008896f1a Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 12 Apr 2013 21:19:20 +0200 Subject: [PATCH 01/11] Move Jekyll::DEFAULTS to Jekyll::Configuration::DEFAULTS --- lib/jekyll.rb | 68 ++----------------------------------- lib/jekyll/site.rb | 2 +- test/test_configuration.rb | 16 ++++----- test/test_generated_site.rb | 6 ++-- test/test_page.rb | 2 +- test/test_pager.rb | 4 +-- test/test_post.rb | 6 ++-- test/test_site.rb | 32 ++++++++--------- test/test_tags.rb | 2 +- 9 files changed, 37 insertions(+), 101 deletions(-) diff --git a/lib/jekyll.rb b/lib/jekyll.rb index b3f6b22f..12082194 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -28,6 +28,7 @@ require 'pygments' # internal requires require 'jekyll/core_ext' +require 'jekyll/configuration' require 'jekyll/site' require 'jekyll/convertible' require 'jekyll/layout' @@ -54,76 +55,11 @@ SafeYAML::OPTIONS[:suppress_warnings] = true 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. diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index d5901886..6e23edf4 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -97,7 +97,7 @@ module Jekyll # # 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) } diff --git a/test/test_configuration.rb b/test/test_configuration.rb index 988d1c7d..64975552 100644 --- a/test/test_configuration.rb +++ b/test/test_configuration.rb @@ -9,20 +9,20 @@ class TestConfiguration < Test::Unit::TestCase should "fire warning with no _config.yml" do mock(YAML).safe_load_file(@path) { raise SystemCallError, "No such file or directory - #{@path}" } mock($stderr).puts("Configuration file: none") - assert_equal Jekyll::DEFAULTS, Jekyll.configuration({}) + assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({}) end should "load configuration as hash" do mock(YAML).safe_load_file(@path) { Hash.new } mock($stdout).puts("Configuration file: #{@path}") - assert_equal Jekyll::DEFAULTS, Jekyll.configuration({}) + assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({}) end should "fire warning with bad config" do mock(YAML).safe_load_file(@path) { Array.new } mock($stderr).puts(" WARNING: Error reading configuration. Using defaults (and options).") mock($stderr).puts("Configuration file: (INVALID) #{@path}") - assert_equal Jekyll::DEFAULTS, Jekyll.configuration({}) + assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({}) end end context "loading config from external file" do @@ -37,19 +37,19 @@ class TestConfiguration < Test::Unit::TestCase should "load default config if no config_file is set" do mock(YAML).safe_load_file(@paths[:default]) { Hash.new } mock($stdout).puts("Configuration file: #{@paths[:default]}") - assert_equal Jekyll::DEFAULTS, Jekyll.configuration({}) + assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({}) end should "load different config if specified" do mock(YAML).safe_load_file(@paths[:other]) { {"baseurl" => "http://wahoo.dev"} } mock($stdout).puts("Configuration file: #{@paths[:other]}") - assert_equal Jekyll::DEFAULTS.deep_merge({ "baseurl" => "http://wahoo.dev" }), Jekyll.configuration({ "config" => @paths[:other] }) + assert_equal Jekyll::Configuration::DEFAULTS.deep_merge({ "baseurl" => "http://wahoo.dev" }), Jekyll.configuration({ "config" => @paths[:other] }) end should "load default config if path passed is empty" do mock(YAML).safe_load_file(@paths[:default]) { Hash.new } mock($stdout).puts("Configuration file: #{@paths[:default]}") - assert_equal Jekyll::DEFAULTS, Jekyll.configuration({ "config" => @paths[:empty] }) + assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({ "config" => @paths[:empty] }) end should "load multiple config files" do @@ -57,7 +57,7 @@ class TestConfiguration < Test::Unit::TestCase mock(YAML).safe_load_file(@paths[:other]) { Hash.new } mock($stdout).puts("Configuration file: #{@paths[:default]}") mock($stdout).puts("Configuration file: #{@paths[:other]}") - assert_equal Jekyll::DEFAULTS, Jekyll.configuration({ "config" => [@paths[:default], @paths[:other]] }) + assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({ "config" => [@paths[:default], @paths[:other]] }) end should "load multiple config files and last config should win" do @@ -65,7 +65,7 @@ class TestConfiguration < Test::Unit::TestCase mock(YAML).safe_load_file(@paths[:other]) { {"baseurl" => "http://wahoo.dev"} } mock($stdout).puts("Configuration file: #{@paths[:default]}") mock($stdout).puts("Configuration file: #{@paths[:other]}") - assert_equal Jekyll::DEFAULTS.deep_merge({ "baseurl" => "http://wahoo.dev" }), Jekyll.configuration({ "config" => [@paths[:default], @paths[:other]] }) + assert_equal Jekyll::Configuration::DEFAULTS.deep_merge({ "baseurl" => "http://wahoo.dev" }), Jekyll.configuration({ "config" => [@paths[:default], @paths[:other]] }) end end end diff --git a/test/test_generated_site.rb b/test/test_generated_site.rb index 56413466..ca676861 100644 --- a/test/test_generated_site.rb +++ b/test/test_generated_site.rb @@ -5,7 +5,7 @@ class TestGeneratedSite < Test::Unit::TestCase setup do clear_dest stub(Jekyll).configuration do - Jekyll::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir}) + Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir}) end @site = Site.new(Jekyll.configuration) @@ -46,7 +46,7 @@ class TestGeneratedSite < Test::Unit::TestCase setup do clear_dest stub(Jekyll).configuration do - Jekyll::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'limit_posts' => 5}) + Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'limit_posts' => 5}) end @site = Site.new(Jekyll.configuration) @@ -62,7 +62,7 @@ class TestGeneratedSite < Test::Unit::TestCase assert_raise ArgumentError do clear_dest stub(Jekyll).configuration do - Jekyll::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'limit_posts' => 0}) + Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'limit_posts' => 0}) end @site = Site.new(Jekyll.configuration) diff --git a/test/test_page.rb b/test/test_page.rb index fda7cd9c..58e7b1fa 100644 --- a/test/test_page.rb +++ b/test/test_page.rb @@ -15,7 +15,7 @@ class TestPage < Test::Unit::TestCase context "A Page" do setup do clear_dest - stub(Jekyll).configuration { Jekyll::DEFAULTS } + stub(Jekyll).configuration { Jekyll::Configuration::DEFAULTS } @site = Site.new(Jekyll.configuration) end diff --git a/test/test_pager.rb b/test/test_pager.rb index 4d420f79..fe1156ca 100644 --- a/test/test_pager.rb +++ b/test/test_pager.rb @@ -14,7 +14,7 @@ class TestPager < Test::Unit::TestCase context "pagination disabled" do setup do stub(Jekyll).configuration do - Jekyll::DEFAULTS.merge({ + Jekyll::Configuration::DEFAULTS.merge({ 'source' => source_dir, 'destination' => dest_dir }) @@ -31,7 +31,7 @@ class TestPager < Test::Unit::TestCase context "pagination enabled for 2" do setup do stub(Jekyll).configuration do - Jekyll::DEFAULTS.merge({ + Jekyll::Configuration::DEFAULTS.merge({ 'source' => source_dir, 'destination' => dest_dir, 'paginate' => 2 diff --git a/test/test_post.rb b/test/test_post.rb index 62da1343..e57c7c6a 100644 --- a/test/test_post.rb +++ b/test/test_post.rb @@ -13,7 +13,7 @@ class TestPost < Test::Unit::TestCase context "A Post" do setup do clear_dest - stub(Jekyll).configuration { Jekyll::DEFAULTS } + stub(Jekyll).configuration { Jekyll::Configuration::DEFAULTS } @site = Site.new(Jekyll.configuration) end @@ -344,7 +344,7 @@ class TestPost < Test::Unit::TestCase context "when in a site" do setup do clear_dest - stub(Jekyll).configuration { Jekyll::DEFAULTS } + stub(Jekyll).configuration { Jekyll::Configuration::DEFAULTS } @site = Site.new(Jekyll.configuration) @site.posts = [setup_post('2008-02-02-published.textile'), setup_post('2009-01-27-categories.textile')] @@ -537,7 +537,7 @@ class TestPost < Test::Unit::TestCase context "converter file extension settings" do setup do - stub(Jekyll).configuration { Jekyll::DEFAULTS } + stub(Jekyll).configuration { Jekyll::Configuration::DEFAULTS } @site = Site.new(Jekyll.configuration) end diff --git a/test/test_site.rb b/test/test_site.rb index 57b29f16..e8eb104d 100644 --- a/test/test_site.rb +++ b/test/test_site.rb @@ -3,49 +3,49 @@ require 'helper' class TestSite < Test::Unit::TestCase context "configuring sites" do should "have an array for plugins by default" do - site = Site.new(Jekyll::DEFAULTS) + site = Site.new(Jekyll::Configuration::DEFAULTS) assert_equal [File.join(Dir.pwd, '_plugins')], site.plugins end should "look for plugins under the site directory by default" do - site = Site.new(Jekyll::DEFAULTS.merge({'source' => File.expand_path(source_dir)})) + site = Site.new(Jekyll::Configuration::DEFAULTS.merge({'source' => File.expand_path(source_dir)})) assert_equal [File.join(source_dir, '_plugins')], site.plugins end should "have an array for plugins if passed as a string" do - site = Site.new(Jekyll::DEFAULTS.merge({'plugins' => '/tmp/plugins'})) + site = Site.new(Jekyll::Configuration::DEFAULTS.merge({'plugins' => '/tmp/plugins'})) assert_equal ['/tmp/plugins'], site.plugins end should "have an array for plugins if passed as an array" do - site = Site.new(Jekyll::DEFAULTS.merge({'plugins' => ['/tmp/plugins', '/tmp/otherplugins']})) + site = Site.new(Jekyll::Configuration::DEFAULTS.merge({'plugins' => ['/tmp/plugins', '/tmp/otherplugins']})) assert_equal ['/tmp/plugins', '/tmp/otherplugins'], site.plugins end should "have an empty array for plugins if nothing is passed" do - site = Site.new(Jekyll::DEFAULTS.merge({'plugins' => []})) + site = Site.new(Jekyll::Configuration::DEFAULTS.merge({'plugins' => []})) assert_equal [], site.plugins end should "have an empty array for plugins if nil is passed" do - site = Site.new(Jekyll::DEFAULTS.merge({'plugins' => nil})) + site = Site.new(Jekyll::Configuration::DEFAULTS.merge({'plugins' => nil})) assert_equal [], site.plugins end should "expose default baseurl" do - site = Site.new(Jekyll::DEFAULTS) - assert_equal Jekyll::DEFAULTS['baseurl'], site.baseurl + site = Site.new(Jekyll::Configuration::DEFAULTS) + assert_equal Jekyll::Configuration::DEFAULTS['baseurl'], site.baseurl end should "expose baseurl passed in from config" do - site = Site.new(Jekyll::DEFAULTS.merge({'baseurl' => '/blog'})) + site = Site.new(Jekyll::Configuration::DEFAULTS.merge({'baseurl' => '/blog'})) assert_equal '/blog', site.baseurl end end context "creating sites" do setup do stub(Jekyll).configuration do - Jekyll::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir}) + Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir}) end @site = Site.new(Jekyll.configuration) end @@ -205,7 +205,7 @@ class TestSite < Test::Unit::TestCase should "filter symlink entries when safe mode enabled" do stub(Jekyll).configuration do - Jekyll::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'safe' => true}) + Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'safe' => true}) end site = Site.new(Jekyll.configuration) stub(File).symlink?('symlink.js') {true} @@ -221,7 +221,7 @@ class TestSite < Test::Unit::TestCase should "not include symlinks in safe mode" do stub(Jekyll).configuration do - Jekyll::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'safe' => true}) + Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'safe' => true}) end site = Site.new(Jekyll.configuration) @@ -232,7 +232,7 @@ class TestSite < Test::Unit::TestCase should "include symlinks in unsafe mode" do stub(Jekyll).configuration do - Jekyll::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'safe' => false}) + Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'safe' => false}) end site = Site.new(Jekyll.configuration) @@ -244,7 +244,7 @@ class TestSite < Test::Unit::TestCase context 'error handling' do should "raise if destination is included in source" do stub(Jekyll).configuration do - Jekyll::DEFAULTS.merge({'source' => source_dir, 'destination' => source_dir}) + Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => source_dir}) end assert_raise Jekyll::FatalException do @@ -254,7 +254,7 @@ class TestSite < Test::Unit::TestCase should "raise if destination is source" do stub(Jekyll).configuration do - Jekyll::DEFAULTS.merge({'source' => source_dir, 'destination' => File.join(source_dir, "..")}) + Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => File.join(source_dir, "..")}) end assert_raise Jekyll::FatalException do @@ -303,7 +303,7 @@ class TestSite < Test::Unit::TestCase end should 'remove orphaned files in destination - keep_files .svn' do - config = Jekyll::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'keep_files' => ['.svn']}) + config = Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'keep_files' => ['.svn']}) @site = Site.new(config) @site.process assert !File.exist?(dest_dir('.htpasswd')) diff --git a/test/test_tags.rb b/test/test_tags.rb index f7916fc5..0ba6b647 100644 --- a/test/test_tags.rb +++ b/test/test_tags.rb @@ -6,7 +6,7 @@ class TestTags < Test::Unit::TestCase def create_post(content, override = {}, converter_class = Jekyll::Converters::Markdown) stub(Jekyll).configuration do - Jekyll::DEFAULTS.deep_merge({'pygments' => true}).deep_merge(override) + Jekyll::Configuration::DEFAULTS.deep_merge({'pygments' => true}).deep_merge(override) end site = Site.new(Jekyll.configuration) From 64f224933b68ebcb3f6257ff96a70535011b42b7 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 12 Apr 2013 21:20:14 +0200 Subject: [PATCH 02/11] Add Jekyll::Configuration --- lib/jekyll/configuration.rb | 135 ++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 lib/jekyll/configuration.rb diff --git a/lib/jekyll/configuration.rb b/lib/jekyll/configuration.rb new file mode 100644 index 00000000..8fb476cf --- /dev/null +++ b/lib/jekyll/configuration.rb @@ -0,0 +1,135 @@ +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: Generate list of configuration files from the override§ + def config_files(override) + # _config.yml may override default source location, but until + # then, we need to know where to look for _config.yml + source = override['source'] || DEFAULTS['source'] + + # Get configuration from /_config.yml or / + 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 + config_files + end + + # Public: Read configuration and return merged Hash + # + # 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 + + 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 + end + + 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 From e2967c96cd0309a7be7702288690a02857c58a7a Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 12 Apr 2013 21:20:30 +0200 Subject: [PATCH 03/11] Jekyll.configuration should use the new Jekyll::Configuration class --- lib/jekyll.rb | 46 +++++----------------------------------------- 1 file changed, 5 insertions(+), 41 deletions(-) diff --git a/lib/jekyll.rb b/lib/jekyll.rb index 12082194..fad6c769 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -64,48 +64,12 @@ module Jekyll # # 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 /_config.yml or / - 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)) + config = config.backwards_compatibilize # Merge DEFAULTS < _config.yml < override - Jekyll::DEFAULTS.deep_merge(config).deep_merge(override) + config.deep_merge(override).stringify_keys end end From f2f20d18f01a8cf262b587c95074c0108d5e7202 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 12 Apr 2013 21:38:30 +0200 Subject: [PATCH 04/11] TomDoc Jekyll::Configuration --- lib/jekyll/configuration.rb | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/jekyll/configuration.rb b/lib/jekyll/configuration.rb index 8fb476cf..39cb6726 100644 --- a/lib/jekyll/configuration.rb +++ b/lib/jekyll/configuration.rb @@ -73,7 +73,11 @@ module Jekyll reduce({}) { |hsh,(k,v)| hsh.merge(k.to_s => v) } end - # Public: Generate list of configuration files from the override§ + # 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) # _config.yml may override default source location, but until # then, we need to know where to look for _config.yml @@ -90,7 +94,9 @@ module Jekyll # Public: Read configuration and return merged Hash # - # file - + # 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) @@ -98,7 +104,13 @@ module Jekyll $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 @@ -118,7 +130,11 @@ module Jekyll configuration 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 From 0c0b13e69b29cffca46694d29f85c8b624fb8f27 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 12 Apr 2013 21:56:45 +0200 Subject: [PATCH 05/11] Unit test for Configuration#stringify_keys --- test/test_configuration.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/test_configuration.rb b/test/test_configuration.rb index 64975552..0c57ec10 100644 --- a/test/test_configuration.rb +++ b/test/test_configuration.rb @@ -1,6 +1,30 @@ require 'helper' class TestConfiguration < Test::Unit::TestCase + context "#stringify_keys" do + setup do + @mixed_keys = Configuration[{ + 'markdown' => 'maruku', + :permalink => 'date', + 'baseurl' => '/', + :include => ['.htaccess'], + :source => './' + }] + @string_keys = Configuration[{ + 'markdown' => 'maruku', + 'permalink' => 'date', + 'baseurl' => '/', + 'include' => ['.htaccess'], + 'source' => './' + }] + end + should "stringify symbol keys" do + assert_equal @string_keys, @mixed_keys.stringify_keys + end + should "not mess with keys already strings" do + assert_equal @string_keys, @string_keys.stringify_keys + end + end context "loading configuration" do setup do @path = File.join(Dir.pwd, '_config.yml') From 0812c1b4c97bcbfb13f9ef75eb8eedea0d4faffd Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 12 Apr 2013 22:29:27 +0200 Subject: [PATCH 06/11] Add unit tests for Configuration#config_files --- test/test_configuration.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/test_configuration.rb b/test/test_configuration.rb index 0c57ec10..c8a2aac5 100644 --- a/test/test_configuration.rb +++ b/test/test_configuration.rb @@ -25,6 +25,30 @@ class TestConfiguration < Test::Unit::TestCase assert_equal @string_keys, @string_keys.stringify_keys end end + context "#config_files" do + setup do + @config = Configuration[Configuration::DEFAULTS] + @base_config = {"source" => source_dir} + @no_override = @base_config.merge({}) + @one_config_file = @base_config.merge({ "config" => "config.yml" }) + @multiple_files = @base_config.merge({ "config" => %w[config/site.yml config/deploy.yml configuration.yml] }) + end + + should "always return an array" do + assert @config.config_files(@no_override).is_a?(Array) + assert @config.config_files(@one_config_file).is_a?(Array) + assert @config.config_files(@multiple_files).is_a?(Array) + end + should "return the default config path if no config files are specified" do + assert_equal [File.join(source_dir, "_config.yml")], @config.config_files(@no_override) + end + should "return the config if given one config file" do + assert_equal %w[config.yml], @config.config_files(@one_config_file) + end + should "return an array of the config files if given many config files" do + assert_equal %w[config/site.yml config/deploy.yml configuration.yml], @config.config_files(@multiple_files) + end + end context "loading configuration" do setup do @path = File.join(Dir.pwd, '_config.yml') From 879b08a0b05c3775f365272d1983d56be667d7c2 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 12 Apr 2013 22:29:52 +0200 Subject: [PATCH 07/11] In Configuration#config_files, cascade to the Configuration's value of 'source' before the default --- lib/jekyll/configuration.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/jekyll/configuration.rb b/lib/jekyll/configuration.rb index 39cb6726..08b7dbf6 100644 --- a/lib/jekyll/configuration.rb +++ b/lib/jekyll/configuration.rb @@ -81,7 +81,7 @@ module Jekyll def config_files(override) # _config.yml may override default source location, but until # then, we need to know where to look for _config.yml - source = override['source'] || DEFAULTS['source'] + source = override['source'] || self['source'] || DEFAULTS['source'] # Get configuration from /_config.yml or / config_files = override.delete('config') @@ -127,7 +127,7 @@ module Jekyll "Using defaults (and options)." $stderr.puts "#{err}" end - + configuration end From 7c7bacffe76fd05e20f5f2b4d45fa57c3ca7533c Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 12 Apr 2013 22:37:01 +0200 Subject: [PATCH 08/11] Refactor setup block for test of Configuration#config_files --- test/test_configuration.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/test_configuration.rb b/test/test_configuration.rb index c8a2aac5..8793b29d 100644 --- a/test/test_configuration.rb +++ b/test/test_configuration.rb @@ -27,11 +27,10 @@ class TestConfiguration < Test::Unit::TestCase end context "#config_files" do setup do - @config = Configuration[Configuration::DEFAULTS] - @base_config = {"source" => source_dir} - @no_override = @base_config.merge({}) - @one_config_file = @base_config.merge({ "config" => "config.yml" }) - @multiple_files = @base_config.merge({ "config" => %w[config/site.yml config/deploy.yml configuration.yml] }) + @config = Configuration[{"source" => source_dir}] + @no_override = {} + @one_config_file = {"config" => "config.yml"} + @multiple_files = {"config" => %w[config/site.yml config/deploy.yml configuration.yml]} end should "always return an array" do From 0f600a322c0146d4334d4d79ea99f6c839a9ac54 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 12 Apr 2013 22:42:54 +0200 Subject: [PATCH 09/11] Added a unit test for Configuration#backwards_compatibilize --- test/test_configuration.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/test_configuration.rb b/test/test_configuration.rb index 8793b29d..6eb35fc4 100644 --- a/test/test_configuration.rb +++ b/test/test_configuration.rb @@ -48,6 +48,15 @@ class TestConfiguration < Test::Unit::TestCase assert_equal %w[config/site.yml config/deploy.yml configuration.yml], @config.config_files(@multiple_files) end end + context "#backwards_compatibilize" do + setup do + @config = Configuration[{"auto" => true}] + end + should "create 'watch' key for 'auto'" do + assert @config.backwards_compatibilize.has_key? "watch" + assert_equal true, @config.backwards_compatibilize["watch"] + end + end context "loading configuration" do setup do @path = File.join(Dir.pwd, '_config.yml') From bd8d271ce3153a1bf49223dfc3380ea079fc969b Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 12 Apr 2013 23:11:38 +0200 Subject: [PATCH 10/11] Moving the backwards-compatibilizing to Configuration#read_config_files. @mattr- --- lib/jekyll.rb | 1 - lib/jekyll/configuration.rb | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/jekyll.rb b/lib/jekyll.rb index fad6c769..78572491 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -67,7 +67,6 @@ module Jekyll config = Configuration[Configuration::DEFAULTS] override = Configuration[override].stringify_keys config = config.read_config_files(config.config_files(override)) - config = config.backwards_compatibilize # Merge DEFAULTS < _config.yml < override config.deep_merge(override).stringify_keys diff --git a/lib/jekyll/configuration.rb b/lib/jekyll/configuration.rb index 08b7dbf6..619b33ae 100644 --- a/lib/jekyll/configuration.rb +++ b/lib/jekyll/configuration.rb @@ -128,7 +128,7 @@ module Jekyll $stderr.puts "#{err}" end - configuration + configuration.backwards_compatibilize end # Public: Ensure the proper options are set in the configuration to allow for From 6881d3bfbffd8fd856e7f9146fc55a184b8e0953 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 12 Apr 2013 23:59:37 +0200 Subject: [PATCH 11/11] Refactored Configuration#config_files --- lib/jekyll/configuration.rb | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/jekyll/configuration.rb b/lib/jekyll/configuration.rb index 619b33ae..f0617744 100644 --- a/lib/jekyll/configuration.rb +++ b/lib/jekyll/configuration.rb @@ -73,22 +73,25 @@ module Jekyll 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) - # _config.yml may override default source location, but until - # then, we need to know where to look for _config.yml - source = override['source'] || self['source'] || DEFAULTS['source'] - # Get configuration from /_config.yml or / 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 + 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