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('safe_yaml', "~> 0.9.7")
s.add_runtime_dependency('colorator', "~> 0.1") s.add_runtime_dependency('colorator', "~> 0.1")
s.add_runtime_dependency('redcarpet', "~> 2.3.0") 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('rake', "~> 10.1")
s.add_development_dependency('rdoc', "~> 3.11") s.add_development_dependency('rdoc', "~> 3.11")

View File

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

View File

@ -99,6 +99,17 @@ module Jekyll
override['source'] || self['source'] || DEFAULTS['source'] override['source'] || self['source'] || DEFAULTS['source']
end 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 # Public: Generate list of configuration files from the override
# #
# override - the command-line options hash # override - the command-line options hash
@ -121,8 +132,8 @@ module Jekyll
# #
# Returns this configuration, overridden by the values in the file # Returns this configuration, overridden by the values in the file
def read_config_file(file) def read_config_file(file)
next_config = YAML.safe_load_file(file) next_config = safe_load_file(file)
raise ArgumentError.new("Configuration file: (INVALID) #{file}".yellow) if !next_config.is_a?(Hash) raise ArgumentError.new("Configuration file: (INVALID) #{file}".yellow) unless next_config.is_a?(Hash)
Jekyll.logger.info "Configuration file:", file Jekyll.logger.info "Configuration file:", file
next_config next_config
rescue SystemCallError rescue SystemCallError
@ -131,7 +142,7 @@ module Jekyll
{} {}
else else
Jekyll.logger.error "Fatal:", "The configuration file '#{file}' could not be found." 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
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}] @config = Configuration[{"source" => source_dir}]
@no_override = {} @no_override = {}
@one_config_file = {"config" => "config.yml"} @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 end
should "always return an array" do 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) assert_equal %w[config.yml], @config.config_files(@one_config_file)
end end
should "return an array of the config files if given many config files" do 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
end end
context "#backwards_compatibilize" do context "#backwards_compatibilize" do
@ -131,6 +131,7 @@ class TestConfiguration < Test::Unit::TestCase
@paths = { @paths = {
:default => File.join(Dir.pwd, '_config.yml'), :default => File.join(Dir.pwd, '_config.yml'),
:other => File.join(Dir.pwd, '_config.live.yml'), :other => File.join(Dir.pwd, '_config.live.yml'),
:toml => source_dir('_config.dev.toml'),
:empty => "" :empty => ""
} }
end end
@ -153,12 +154,20 @@ class TestConfiguration < Test::Unit::TestCase
assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({ "config" => @paths[:empty] }) assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({ "config" => @paths[:empty] })
end 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 should "load multiple config files" do
mock(YAML).safe_load_file(@paths[:default]) { Hash.new } mock(YAML).safe_load_file(@paths[:default]) { Hash.new }
mock(YAML).safe_load_file(@paths[:other]) { 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[:default]}")
mock($stdout).puts("Configuration file: #{@paths[:other]}") 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 end
should "load multiple config files and last config should win" do should "load multiple config files and last config should win" do