Merge pull request #1765 from mojombo/tomlify

Add support for TOML config files
This commit is contained in:
Matt Rogers 2013-12-04 18:28:20 -08:00
commit 052a577938
5 changed files with 30 additions and 6 deletions

View File

@ -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")

View File

@ -26,6 +26,7 @@ require 'pathname'
require 'liquid'
require 'maruku'
require 'colorator'
require 'toml'
# internal requires
require 'jekyll/core_ext'

View File

@ -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

View File

@ -0,0 +1,2 @@
baseurl = "/you-beautiful-blog-you"
title = "My magnificent site, wut"

View File

@ -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