Merge pull request #959 from imathis/env-handling

Handle Octopress Environment
This commit is contained in:
Brandon Mathis 2013-01-27 14:37:07 -08:00
commit 0065592cfb
3 changed files with 89 additions and 83 deletions

View File

@ -15,6 +15,7 @@ markdown: rdiscount
pygments: false # Jekyll's default Python Pygments have been replaced by pygments.rb.
# Set to true to use Albino + Python Pygments
env: production
blog_index_dir: source # directory for your blog's index page (if you put your index in source/blog/index.html, set this to 'source/blog')
stash_dir: _stash # directory to stash posts for speedy generation
posts_dir: _posts # directory for blog files

View File

@ -1,7 +1,12 @@
$:.unshift File.dirname(__FILE__) # For use/testing when no gem is installed
module Octopress
end
require "octopress/core_ext"
require "octopress/configuration"
module Octopress
# Static: Fetches the Octopress environment
def self.env
ENV["OCTOPRESS_ENV"] || Configuration.read_config("defaults/jekyll.yml")[:env]
end
end

View File

@ -1,86 +1,86 @@
require 'yaml'
class Octopress::Configuration
def self.config_dir(*subdirs)
File.absolute_path(File.join(File.dirname(__FILE__), '../', '../' '_config', *subdirs))
end
# Static: Reads the configuration of the specified file
#
# path - the String path to the configuration file, relative to ./_config
#
# Returns a Hash of the items in the configuration file (symbol keys)
def self.read_config(path)
full_path = self.config_dir(path)
if File.exists? full_path
begin
configs = YAML.load(File.open(full_path))
if configs.nil?
configs
else
configs.to_symbol_keys
end
rescue => e
puts "Error reading configuration file '#{full_path}':"
puts e.message, e.backtrace
exit(-1)
end
else
raise ArgumentError, "File at '#{full_path}' does not exist."
module Octopress
module Configuration
def self.config_dir(*subdirs)
File.absolute_path(File.join(File.dirname(__FILE__), '../', '../' '_config', *subdirs))
end
end
# Static: Writes the contents of a set of configurations to a path in the config directory
#
# path - the String path to the configuration file, relative to ./_config
# obj - the object to be dumped into the specified file in YAML form
#
# Returns
def self.write_config(path, obj)
YAML.dump(obj.to_string_keys, File.open(self.config_dir(path), 'w'))
end
# Static: Reads all the configuration files into one hash
#
# Returns a Hash of all the configuration files, with each key being a symbol
def self.read_configuration
configs = {}
Dir.glob(self.config_dir('defaults', '**', '*.yml')) do |filename|
file_yaml = YAML.load(File.read(filename))
unless file_yaml.nil?
configs = file_yaml.deep_merge(configs)
end
end
Dir.glob(self.config_dir('*.yml')) do |filename|
file_yaml = YAML.load(File.read(filename))
unless file_yaml.nil?
configs = file_yaml.deep_merge(configs)
end
end
configs.to_symbol_keys
end
# Static: Writes configuration files necessary for generation of the Jekyll site
#
# Returns a Hash of the items which were written to the Jekyll configuration file
def self.write_configs_for_generation
config = self.read_configuration
jekyll_configs = {}
File.open("_config.yml", "w") do |f|
jekyll_configs = config.to_string_keys.to_yaml :canonical => false
f.write(jekyll_configs)
end
jekyll_configs
end
# Static: Removes configuration files required for site generation
#
# Returns the number of files deleted
def self.remove_configs_for_generation
File.unlink("_config.yml")
# Static: Reads the configuration of the specified file
#
# path - the String path to the configuration file, relative to ./_config
#
# Returns a Hash of the items in the configuration file (symbol keys)
def self.read_config(path)
full_path = self.config_dir(path)
if File.exists? full_path
begin
configs = YAML.load(File.open(full_path))
if configs.nil?
configs
else
configs.to_symbol_keys
end
rescue => e
puts "Error reading configuration file '#{full_path}':"
puts e.message, e.backtrace
exit(-1)
end
else
raise ArgumentError, "File at '#{full_path}' does not exist."
end
end
# Static: Writes the contents of a set of configurations to a path in the config directory
#
# path - the String path to the configuration file, relative to ./_config
# obj - the object to be dumped into the specified file in YAML form
#
# Returns the Hash for the configuration file.
def self.write_config(path, obj)
YAML.dump(obj.to_string_keys, File.open(self.config_dir(path), 'w'))
end
# Static: Reads all the configuration files into one hash
#
# Returns a Hash of all the configuration files, with each key being a symbol
def self.read_configuration
configs = {}
Dir.glob(self.config_dir('defaults', '**', '*.yml')) do |filename|
file_yaml = YAML.load(File.read(filename))
unless file_yaml.nil?
configs = file_yaml.deep_merge(configs)
end
end
Dir.glob(self.config_dir('*.yml')) do |filename|
file_yaml = YAML.load(File.read(filename))
unless file_yaml.nil?
configs = file_yaml.deep_merge(configs)
end
end
configs.to_symbol_keys
end
# Static: Writes configuration files necessary for generation of the Jekyll site
#
# Returns a Hash of the items which were written to the Jekyll configuration file
def self.write_configs_for_generation
config = self.read_configuration
jekyll_configs = {}
File.open("_config.yml", "w") do |f|
jekyll_configs = config.to_string_keys.to_yaml :canonical => false
f.write(jekyll_configs)
end
jekyll_configs
end
# Static: Removes configuration files required for site generation
#
# Returns the number of files deleted
def self.remove_configs_for_generation
File.unlink("_config.yml")
end
end
end