diff --git a/jekyll.gemspec b/jekyll.gemspec index ee48478c..c3dafc33 100644 --- a/jekyll.gemspec +++ b/jekyll.gemspec @@ -32,6 +32,7 @@ Gem::Specification.new do |s| s.add_runtime_dependency('safe_yaml', "~> 0.9.7") s.add_runtime_dependency('colorator', "~> 0.1") s.add_runtime_dependency('redcarpet', "~> 2.3.0") + s.add_runtime_dependency('toml', '~> 0.0.4') s.add_development_dependency('rake', "~> 10.1") s.add_development_dependency('rdoc', "~> 3.11") diff --git a/lib/jekyll.rb b/lib/jekyll.rb index 633994e4..2ec3c8ec 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -26,6 +26,7 @@ require 'pathname' require 'liquid' require 'maruku' require 'colorator' +require 'toml' # internal requires require 'jekyll/core_ext' diff --git a/lib/jekyll/configuration.rb b/lib/jekyll/configuration.rb index 442fa5e6..a72e51d8 100644 --- a/lib/jekyll/configuration.rb +++ b/lib/jekyll/configuration.rb @@ -99,6 +99,17 @@ module Jekyll override['source'] || self['source'] || DEFAULTS['source'] end + def safe_load_file(filename) + case File.extname(filename) + when '.toml' + TOML.load_file(filename) + when /\.y(a)?ml/ + YAML.safe_load_file(filename) + else + raise ArgumentError, "No parser for '#{filename}' is available. Use a .toml or .y(a)ml file instead." + end + end + # Public: Generate list of configuration files from the override # # override - the command-line options hash @@ -121,8 +132,8 @@ module Jekyll # # Returns this configuration, overridden by the values in the file def read_config_file(file) - next_config = YAML.safe_load_file(file) - raise ArgumentError.new("Configuration file: (INVALID) #{file}".yellow) if !next_config.is_a?(Hash) + next_config = safe_load_file(file) + raise ArgumentError.new("Configuration file: (INVALID) #{file}".yellow) unless next_config.is_a?(Hash) Jekyll.logger.info "Configuration file:", file next_config rescue SystemCallError @@ -131,7 +142,7 @@ module Jekyll {} else Jekyll.logger.error "Fatal:", "The configuration file '#{file}' could not be found." - raise LoadError + raise LoadError, "The Configuration file '#{file}' could not be found." end end diff --git a/test/source/_config.dev.toml b/test/source/_config.dev.toml new file mode 100644 index 00000000..7ac863f8 --- /dev/null +++ b/test/source/_config.dev.toml @@ -0,0 +1,2 @@ +baseurl = "/you-beautiful-blog-you" +title = "My magnificent site, wut" diff --git a/test/test_configuration.rb b/test/test_configuration.rb index c57408e5..d911b779 100644 --- a/test/test_configuration.rb +++ b/test/test_configuration.rb @@ -30,7 +30,7 @@ class TestConfiguration < Test::Unit::TestCase @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]} + @multiple_files = {"config" => %w[config/site.yml config/deploy.toml configuration.yml]} end should "always return an array" do @@ -45,7 +45,7 @@ class TestConfiguration < Test::Unit::TestCase 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) + assert_equal %w[config/site.yml config/deploy.toml configuration.yml], @config.config_files(@multiple_files) end end context "#backwards_compatibilize" do @@ -131,6 +131,7 @@ class TestConfiguration < Test::Unit::TestCase @paths = { :default => File.join(Dir.pwd, '_config.yml'), :other => File.join(Dir.pwd, '_config.live.yml'), + :toml => source_dir('_config.dev.toml'), :empty => "" } end @@ -153,12 +154,20 @@ class TestConfiguration < Test::Unit::TestCase assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({ "config" => @paths[:empty] }) end + should "successfully load a TOML file" do + Jekyll.logger.log_level = Jekyll::Stevenson::WARN + assert_equal Jekyll::Configuration::DEFAULTS.merge({ "baseurl" => "/you-beautiful-blog-you", "title" => "My magnificent site, wut" }), Jekyll.configuration({ "config" => [@paths[:toml]] }) + Jekyll.logger.log_level = Jekyll::Stevenson::INFO + end + should "load multiple config files" do mock(YAML).safe_load_file(@paths[:default]) { Hash.new } mock(YAML).safe_load_file(@paths[:other]) { Hash.new } + mock(TOML).load_file(@paths[:toml]) { Hash.new } mock($stdout).puts("Configuration file: #{@paths[:default]}") mock($stdout).puts("Configuration file: #{@paths[:other]}") - assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({ "config" => [@paths[:default], @paths[:other]] }) + mock($stdout).puts("Configuration file: #{@paths[:toml]}") + assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({ "config" => [@paths[:default], @paths[:other], @paths[:toml]] }) end should "load multiple config files and last config should win" do