Merge pull request #620 from bhollis/config

Move config settings into a separate object
This commit is contained in:
Thomas Reynolds 2012-10-14 12:52:19 -07:00
commit af81b588d5
30 changed files with 374 additions and 186 deletions

View file

@ -12,10 +12,14 @@ require "middleman-core/vendor/hooks-0.2.0/lib/hooks"
require "middleman-core/sitemap" require "middleman-core/sitemap"
require "middleman-core/core_extensions" require "middleman-core/core_extensions"
require "middleman-core/configuration"
# Core Middleman Class # Core Middleman Class
module Middleman module Middleman
class Application class Application
# Global configuration
include Configuration::Global
# Uses callbacks # Uses callbacks
include Hooks include Hooks
@ -25,115 +29,75 @@ module Middleman
# Ready (all loading and parsing of extensions complete) hook # Ready (all loading and parsing of extensions complete) hook
define_hook :ready define_hook :ready
class << self
# Mix-in helper methods. Accepts either a list of Modules # Mix-in helper methods. Accepts either a list of Modules
# and/or a block to be evaluated # and/or a block to be evaluated
# @return [void] # @return [void]
def helpers(*extensions, &block) def self.helpers(*extensions, &block)
class_eval(&block) if block_given? class_eval(&block) if block_given?
include(*extensions) if extensions.any? include(*extensions) if extensions.any?
end end
# Access class-wide defaults
#
# @private
# @return [Hash] Hash of default values
def defaults
@defaults ||= {}
end
# Set class-wide defaults
#
# @param [Symbol] key Unique key name
# @param value Default value
# @return [void]
def set(key, value=nil, &block)
@defaults ||= {}
@defaults[key] = value
@inst.set(key, value, &block) if @inst
end
end
delegate :helpers, :to => :"self.class" delegate :helpers, :to => :"self.class"
# Set attributes (global variables)
#
# @param [Symbol] key Name of the attribue
# @param value Attribute value
# @return [void]
def set(key, value=nil, &block)
setter = "#{key}=".to_sym
self.class.send(:attr_accessor, key) if !respond_to?(setter)
value = block if block_given?
send(setter, value)
end
# Root project directory (overwritten in middleman build/server) # Root project directory (overwritten in middleman build/server)
# @return [String] # @return [String]
set :root, ENV["MM_ROOT"] || Dir.pwd def self.root
ENV["MM_ROOT"] || Dir.pwd
end
delegate :root, :to => :"self.class"
# Pathname-addressed root # Pathname-addressed root
def root_path def self.root_path
@_root_path ||= Pathname.new(root) Pathname(root)
end end
delegate :root_path, :to => :"self.class"
# Name of the source directory # Name of the source directory
# @return [String] # @return [String]
set :source, "source" config.define_setting :source, "source", 'Name of the source directory'
# Middleman environment. Defaults to :development, set to :build by the build process # Middleman environment. Defaults to :development, set to :build by the build process
# @return [String] # @return [String]
set :environment, (ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development config.define_setting :environment, ((ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development), 'Middleman environment. Defaults to :development, set to :build by the build process'
# Which file should be used for directory indexes # Which file should be used for directory indexes
# @return [String] # @return [String]
set :index_file, "index.html" config.define_setting :index_file, "index.html", 'Which file should be used for directory indexes'
# Whether to strip the index file name off links to directory indexes # Whether to strip the index file name off links to directory indexes
# @return [Boolean] # @return [Boolean]
set :strip_index_file, true config.define_setting :strip_index_file, true, 'Whether to strip the index file name off links to directory indexes'
# Whether to include a trailing slash when stripping the index file # Whether to include a trailing slash when stripping the index file
# @return [Boolean] # @return [Boolean]
set :trailing_slash, true config.define_setting :trailing_slash, true, 'Whether to include a trailing slash when stripping the index file'
# Location of javascripts within source. # Location of javascripts within source.
# @return [String] # @return [String]
set :js_dir, "javascripts" config.define_setting :js_dir, "javascripts", 'Location of javascripts within source'
# Location of stylesheets within source. Used by Compass. # Location of stylesheets within source. Used by Compass.
# @return [String] # @return [String]
set :css_dir, "stylesheets" config.define_setting :css_dir, "stylesheets", 'Location of stylesheets within source'
# Location of images within source. Used by HTML helpers and Compass. # Location of images within source. Used by HTML helpers and Compass.
# @return [String] # @return [String]
set :images_dir, "images" config.define_setting :images_dir, "images", 'Location of images within source'
# Location of fonts within source. Used by Compass. # Location of fonts within source. Used by Compass.
# @return [String] # @return [String]
set :fonts_dir, "fonts" config.define_setting :fonts_dir, "fonts", 'Location of fonts within source'
# Where to build output files # Where to build output files
# @return [String] # @return [String]
set :build_dir, "build" config.define_setting :build_dir, "build", 'Where to build output files'
# Default prefix for building paths. Used by HTML helpers and Compass. # Default prefix for building paths. Used by HTML helpers and Compass.
# @return [String] # @return [String]
set :http_prefix, "/" config.define_setting :http_prefix, "/", 'Default prefix for building paths'
# Default string encoding for templates and output.
# @return [String]
set :encoding, "utf-8"
# Whether to catch and display exceptions
# @return [Boolean]
set :show_exceptions, true
# Default layout name # Default layout name
# @return [String, Symbold] # @return [String, Symbold]
set :layout, :_auto_layout config.define_setting :layout, :_auto_layout, 'Default layout name'
# Activate custom features and extensions # Activate custom features and extensions
include Middleman::CoreExtensions::Extensions include Middleman::CoreExtensions::Extensions
@ -178,12 +142,12 @@ module Middleman
cache.clear cache.clear
# Setup the default values from calls to set before initialization # Setup the default values from calls to set before initialization
self.class.superclass.defaults.each { |k,v| set(k,v) } self.class.superclass.config.to_h.each { |k,v| self.class.config.define_setting(k,v) }
# Evaluate a passed block if given # Evaluate a passed block if given
instance_exec(&block) if block_given? instance_exec(&block) if block_given?
set :source, ENV["MM_SOURCE"] if ENV["MM_SOURCE"] config[:source] = ENV["MM_SOURCE"] if ENV["MM_SOURCE"]
super super
end end
@ -199,24 +163,17 @@ module Middleman
# Whether we're in development mode # Whether we're in development mode
# @return [Boolean] If we're in dev mode # @return [Boolean] If we're in dev mode
def development?; environment == :development; end def development?; config[:environment] == :development; end
# Whether we're in build mode # Whether we're in build mode
# @return [Boolean] If we're in build mode # @return [Boolean] If we're in build mode
def build?; environment == :build; end def build?; config[:environment] == :build; end
# Backwards compatibilty with old Sinatra template interface
#
# @return [Middleman::Application]
def settings
self
end
# The full path to the source directory # The full path to the source directory
# #
# @return [String] # @return [String]
def source_dir def source_dir
File.join(root, source) File.join(root, config[:source])
end end
delegate :logger, :instrument, :to => ::Middleman::Util delegate :logger, :instrument, :to => ::Middleman::Util
@ -239,7 +196,7 @@ module Middleman
if !resource if !resource
# Try it with /index.html at the end # Try it with /index.html at the end
indexed_path = File.join(path.sub(%r{/$}, ''), index_file) indexed_path = File.join(path.sub(%r{/$}, ''), config[:index_file])
resource = sitemap.find_resource_by_destination_path(indexed_path) resource = sitemap.find_resource_by_destination_path(indexed_path)
end end

View file

@ -83,7 +83,7 @@ module Middleman::Cli
# @return [Middleman::Application] # @return [Middleman::Application]
def shared_instance(verbose=false, instrument=false) def shared_instance(verbose=false, instrument=false)
@_shared_instance ||= ::Middleman::Application.server.inst do @_shared_instance ||= ::Middleman::Application.server.inst do
set :environment, :build config[:environment] = :build
logger(verbose ? 0 : 1, instrument) logger(verbose ? 0 : 1, instrument)
end end
end end
@ -114,7 +114,7 @@ module Middleman::Cli
# @param [Middleman::Sitemap::Resource] resource # @param [Middleman::Sitemap::Resource] resource
# @return [String] The full path of the file that was written # @return [String] The full path of the file that was written
def render_to_file(resource) def render_to_file(resource)
build_dir = self.class.shared_instance.build_dir build_dir = self.class.shared_instance.config[:build_dir]
output_file = File.join(build_dir, resource.destination_path) output_file = File.join(build_dir, resource.destination_path)
begin begin

View file

@ -0,0 +1,231 @@
module Middleman
module Configuration
# Access to a global configuration manager for the whole Middleman project,
# plus backwards compatibility mechanisms for older Middleman projects.
module Global
def self.included(app)
app.send :extend, ClassMethods
end
module ClassMethods
# Global configuration for the whole Middleman project.
# @return [ConfigurationManager]
def config
@_config ||= ConfigurationManager.new
end
# Set attributes (global variables)
#
# @deprecated Prefer accessing settings through "config".
#
# @param [Symbol] key Name of the attribue
# @param value Attribute value
# @return [void]
def set(key, default=nil, &block)
config.define_setting(key, default)
@inst.set(key, default, &block) if @inst
end
# Access global settings as methods, to preserve compatibility with
# old Middleman.
#
# @deprecated Prefer accessing settings through "config".
def method_missing(method, *args)
if config.defines_setting? method
config[method]
else
super
end
end
# Needed so that method_missing makes sense
def respond_to?(method, include_private = false)
super || config.defines_setting?(method)
end
end
def config
self.class.config
end
# Backwards compatibilty with old Sinatra template interface
#
# @deprecated Prefer accessing settings through "config".
#
# @return [ConfigurationManager]
alias :settings :config
# Set attributes (global variables)
#
# @deprecated Prefer accessing settings through "config".
#
# @param [Symbol] key Name of the attribue
# @param value Attribute value
# @return [void]
def set(key, value=nil, &block)
value = block if block_given?
config[key] = value
end
# Access global settings as methods, to preserve compatibility with
# old Middleman.
#
# @deprecated Prefer accessing settings through "config".
def method_missing(method, *args)
if config.defines_setting? method
config[method]
else
super
end
end
# Needed so that method_missing makes sense
def respond_to?(method, include_private = false)
super || config.defines_setting?(method)
end
end
# A class that manages a collection of documented settings.
# Can be used by extensions as well as the main Middleman
# application. Extensions should probably finalize their instance
# after defining all the settings they want to expose.
class ConfigurationManager
def initialize
# A hash from setting key to ConfigSetting instance.
@settings = {}
@finalized = false
end
# Get all settings, sorted by key, as ConfigSetting objects.
# @return [Array<ConfigSetting>]
def all_settings
@settings.values.sort_by(&:key)
end
# Get a full ConfigSetting object for the setting with the give key.
# @return [ConfigSetting]
def setting(key)
@settings[key]
end
# Get the value of a setting by key. Returns nil if there is no such setting.
# @return [Object]
def [](key)
setting = @settings[key]
setting ? setting.value : nil
end
# Set the value of a setting by key. Creates the setting if it doesn't exist.
# @param [Symbol] key
# @param [Object] val
def []=(key, val)
setting = @settings[key] || define_setting(key)
setting.value = val
end
# Allow configuration settings to be read and written via methods
def method_missing(method, *args)
if defines_setting?(method) && args.size == 0
self[method]
elsif method =~ /^(\w+)=$/ && args.size == 1
self[$1.to_sym] = args[0]
else
super
end
end
# Needed so that method_missing makes sense
def respond_to?(method, include_private = false)
super || defines_setting?(method) || (method =~ /^(\w+)=$/ && defines_setting?($1))
end
# Does this configuration manager know about the setting identified by key?
# @param [Symbol] key
# @return [Boolean]
def defines_setting?(key)
@settings.has_key?(key)
end
# Define a new setting, with optional default and user-friendly description.
# Once the configuration manager is finalized, no new settings may be defined.
#
# @param [Symbol] key
# @param [Object] default
# @param [String] description
# @return [ConfigSetting]
def define_setting(key, default=nil, description=nil)
raise "Setting #{key} doesn't exist" if @finalized
raise "Setting #{key} already defined" if @settings.has_key?(key)
raise "Setting key must be a Symbol" unless key.is_a? Symbol
@settings[key] = ConfigSetting.new(key, default, description)
end
# Switch the configuration manager is finalized, it switches to read-only
# mode and no new settings may be defined.
def finalize!
@finalized = true
end
# Deep duplicate of the configuration manager
def dup
copy = ConfigurationManager.new
@settings.each do |key, setting|
copy_setting = copy.define_setting setting.key, setting.default, setting.description
copy_setting.value = setting.value if setting.value_set?
end
copy
end
def to_h
hash = {}
@settings.each do |key, setting|
hash[key] = setting.value
end
hash
end
def to_s
to_h.inspect
end
end
# An individual configuration setting, with an optional default and description.
# Also models whether or not a value has been set.
class ConfigSetting
# The name of this setting
attr_accessor :key
# The default value for this setting
attr_accessor :default
# A human-friendly description of the setting
attr_accessor :description
def initialize(key, default, description)
@value_set = false
self.key = key
self.default = default
self.description = description
end
# The user-supplied value for this setting, overriding the default
def value=(value)
@value = value
@value_set = true
end
# The effective value of the setting, which may be the default
# if the user has not set a value themselves. Note that even if the
# user sets the value to nil it will override the default.
def value
value_set? ? @value : default
end
# Whether or not there has been a value set beyond the default
def value_set?
@value_set
end
end
end
end

View file

@ -13,7 +13,7 @@ module Middleman
require "yaml" require "yaml"
require "active_support/json" require "active_support/json"
app.set :data_dir, "data" app.config.define_setting :data_dir, "data", "The directory data files are stored in"
app.send :include, InstanceMethods app.send :include, InstanceMethods
end end
alias :included :registered alias :included :registered
@ -24,12 +24,12 @@ module Middleman
# Setup data files before anything else so they are available when # Setup data files before anything else so they are available when
# parsing config.rb # parsing config.rb
def initialize def initialize
self.files.changed DataStore.matcher do |file| files.changed DataStore.matcher do |file|
self.data.touch_file(file) if file.match(%r{^#{self.data_dir}\/}) data.touch_file(file) if file.match(%r{^#{config[:data_dir]}\/})
end end
self.files.deleted DataStore.matcher do |file| files.deleted DataStore.matcher do |file|
self.data.remove_file(file) if file.match(%r{^#{self.data_dir}\/}) data.remove_file(file) if file.match(%r{^#{config[data_dir]}\/})
end end
super super

View file

@ -37,19 +37,13 @@ module Middleman
class << self class << self
# @private # @private
def registered(app) def registered(app)
# Using for version parsing
require "rubygems"
app.define_hook :after_configuration app.define_hook :after_configuration
app.define_hook :before_configuration app.define_hook :before_configuration
app.define_hook :build_config app.define_hook :build_config
app.define_hook :development_config app.define_hook :development_config
if ENV["AUTOLOAD_SPROCKETS"] app.config.define_setting :autoload_sprockets, true, 'Automatically load sprockets at startup?'
app.set :autoload_sprockets, (ENV["AUTOLOAD_SPROCKETS"] == "true") app.config[:autoload_sprockets] = (ENV["AUTOLOAD_SPROCKETS"] == "true") if ENV["AUTOLOAD_SPROCKETS"]
else
app.set :autoload_sprockets, true
end
app.extend ClassMethods app.extend ClassMethods
app.send :include, InstanceMethods app.send :include, InstanceMethods
@ -137,7 +131,7 @@ module Middleman
instance_eval File.read(local_config), local_config, 1 instance_eval File.read(local_config), local_config, 1
end end
if autoload_sprockets if config[:autoload_sprockets]
begin begin
require "middleman-sprockets" require "middleman-sprockets"
activate(:sprockets) activate(:sprockets)

View file

@ -9,20 +9,20 @@ module Middleman
# once registered # once registered
def registered(app) def registered(app)
# Setup a default helpers paths # Setup a default helpers paths
app.set :helpers_dir, "helpers" app.config.define_setting :helpers_dir, "helpers", 'Directory to autoload helper modules from'
app.set :helpers_filename_glob, "**{,/*/**}/*.rb" app.config.define_setting :helpers_filename_glob, "**.rb", 'Glob pattern for matching helper ruby files'
app.set :helpers_filename_to_module_name_proc, Proc.new { |filename| app.config.define_setting :helpers_filename_to_module_name_proc, Proc.new { |filename|
basename = File.basename(filename, File.extname(filename)) basename = File.basename(filename, File.extname(filename))
basename.camelcase basename.camelcase
} }, 'Proc implementing the conversion from helper filename to module name'
# After config # After config
app.after_configuration do app.after_configuration do
helpers_path = File.expand_path(helpers_dir, root) helpers_path = File.join(root, config[:helpers_dir])
next unless File.exists?(helpers_path) next unless File.exists?(helpers_path)
Dir[File.join(helpers_path, helpers_filename_glob)].each do |filename| Dir[File.join(helpers_path, config[:helpers_filename_glob])].each do |filename|
module_name = helpers_filename_to_module_name_proc.call(filename) module_name = config[:helpers_filename_to_module_name_proc].call(filename)
next unless module_name next unless module_name
require filename require filename

View file

@ -29,7 +29,7 @@ module Middleman
# Before parsing config, load the data/ directory # Before parsing config, load the data/ directory
app.before_configuration do app.before_configuration do
files.reload_path(data_dir) files.reload_path(config[:data_dir])
end end
# After config, load everything else # After config, load everything else

View file

@ -62,7 +62,7 @@ module Middleman::CoreExtensions
def clear_data(file) def clear_data(file)
# Copied from Sitemap::Store#file_to_path, but without # Copied from Sitemap::Store#file_to_path, but without
# removing the file extension # removing the file extension
file = File.expand_path(file, @app.root) file = File.join(@app.root, file)
prefix = @app.source_dir.sub(/\/$/, "") + "/" prefix = @app.source_dir.sub(/\/$/, "") + "/"
return unless file.include?(prefix) return unless file.include?(prefix)
path = file.sub(prefix, "") path = file.sub(prefix, "")

View file

@ -257,7 +257,7 @@ module Middleman
extension_class = ::Tilt[ext] extension_class = ::Tilt[ext]
::Tilt.mappings.each do |ext, engines| ::Tilt.mappings.each do |ext, engines|
next unless engines.include? extension_class next unless engines.include? extension_class
engine_options = respond_to?(ext.to_sym) ? send(ext.to_sym) : {} engine_options = config[ext.to_sym] || {}
options.merge!(engine_options) options.merge!(engine_options)
end end
@ -272,7 +272,7 @@ module Middleman
# @return [String] # @return [String]
def fetch_layout(engine, opts) def fetch_layout(engine, opts)
# The layout name comes from either the system default or the options # The layout name comes from either the system default or the options
local_layout = opts.has_key?(:layout) ? opts[:layout] : layout local_layout = opts.has_key?(:layout) ? opts[:layout] : config[:layout]
return false unless local_layout return false unless local_layout
# Look for engine-specific options # Look for engine-specific options

View file

@ -28,12 +28,12 @@ module Middleman
# @param [String, Symbol] layout_name # @param [String, Symbol] layout_name
# @return [void] # @return [void]
def with_layout(layout_name, &block) def with_layout(layout_name, &block)
old_layout = layout old_layout = config[:layout]
set :layout, layout_name config[:layout] = layout_name
instance_exec(&block) if block_given? instance_exec(&block) if block_given?
ensure ensure
set :layout, old_layout config[:layout] = old_layout
end end
# The page method allows the layout to be set on a specific path # The page method allows the layout to be set on a specific path
@ -48,7 +48,7 @@ module Middleman
blocks = Array(block) blocks = Array(block)
# Default layout # Default layout
opts[:layout] = layout if opts[:layout].nil? opts[:layout] = config[:layout] if opts[:layout].nil?
# If the url is a regexp # If the url is a regexp
if url.is_a?(Regexp) || url.include?("*") if url.is_a?(Regexp) || url.include?("*")
@ -64,7 +64,7 @@ module Middleman
# Normalized path # Normalized path
url = '/' + Middleman::Util.normalize_path(url) url = '/' + Middleman::Util.normalize_path(url)
if url.end_with?('/') || File.directory?(File.join(source_dir, url)) if url.end_with?('/') || File.directory?(File.join(source_dir, url))
url = File.join(url, index_file) url = File.join(url, config[:index_file])
end end
# Setup proxy # Setup proxy

View file

@ -6,6 +6,10 @@ module Middleman::CoreExtensions::RubyEncoding
# Once registerd # Once registerd
def registered(app) def registered(app)
# Default string encoding for templates and output.
# @return [String]
app.config.define_setting :encoding, "utf-8", 'Default string encoding for templates and output'
app.send :include, InstanceMethods app.send :include, InstanceMethods
end end
@ -15,8 +19,8 @@ module Middleman::CoreExtensions::RubyEncoding
module InstanceMethods module InstanceMethods
def initialize def initialize
if Object.const_defined?(:Encoding) if Object.const_defined?(:Encoding)
Encoding.default_internal = encoding Encoding.default_internal = config[:encoding]
Encoding.default_external = encoding Encoding.default_external = config[:encoding]
end end
super super

View file

@ -11,10 +11,14 @@ module Middleman
# Once registered # Once registered
def registered(app) def registered(app)
# Whether to catch and display exceptions
# @return [Boolean]
app.config.define_setting :show_exceptions, true, 'Whether to catch and display exceptions'
# When in dev # When in dev
app.configure :development do app.configure :development do
# Include middlemare # Include middlemare
if show_exceptions if config[:show_exceptions]
use ::Middleman::CoreExtensions::ShowExceptions::Middleware use ::Middleman::CoreExtensions::ShowExceptions::Middleware
end end
end end

View file

@ -71,7 +71,7 @@ module Middleman
opts = @options opts = @options
@app =::Middleman::Application.server.inst do @app =::Middleman::Application.server.inst do
if opts[:environment] if opts[:environment]
set :environment, opts[:environment].to_sym config[:environment] = opts[:environment].to_sym
end end
logger(opts[:debug] ? 0 : 1, opts[:instrumenting] || false) logger(opts[:debug] ? 0 : 1, opts[:instrumenting] || false)

View file

@ -8,8 +8,8 @@ module Middleman
# once registered # once registered
def registered(app) def registered(app)
# Setup a default ERb engine # Setup a default ERb engine
app.set :erb_engine, :erb app.config.define_setting :erb_engine, :erb, 'The engine to use for rendering ERb templates'
app.set :erb_engine_prefix, ::Tilt app.config.define_setting :erb_engine_prefix, ::Tilt, 'The parent module for ERb template engines'
app.before_configuration do app.before_configuration do
template_extensions :erb => :html template_extensions :erb => :html
@ -19,14 +19,14 @@ module Middleman
app.after_configuration do app.after_configuration do
# Find the user's prefered engine # Find the user's prefered engine
# Convert symbols to classes # Convert symbols to classes
if erb_engine.is_a? Symbol if config[:erb_engine].is_a? Symbol
engine = engine.to_s engine = engine.to_s
engine = engine == "erb" ? "ERB" : engine.camelize engine = engine == "erb" ? "ERB" : engine.camelize
erb_engine = erb_engine_prefix.const_get("#{engine}Template") config[:erb_engine] = config[:erb_engine_prefix].const_get("#{engine}Template")
end end
# Tell Tilt to use the preferred engine # Tell Tilt to use the preferred engine
::Tilt.prefer(erb_engine) ::Tilt.prefer(config[:erb_engine])
end end
end end
alias :included :registered alias :included :registered

View file

@ -11,15 +11,15 @@ module Middleman
# Once registered # Once registered
def registered(app) def registered(app)
# Default sass options # Default less options
app.set :less, {} app.config.define_setting :less, {}, 'LESS compiler options'
app.before_configuration do app.before_configuration do
template_extensions :less => :css template_extensions :less => :css
end end
app.after_configuration do app.after_configuration do
::Less.paths << File.expand_path(css_dir, source_dir) ::Less.paths << File.join(source_dir, config[:css_dir])
end end
# Tell Tilt to use it as well (for inline sass blocks) # Tell Tilt to use it as well (for inline sass blocks)

View file

@ -10,8 +10,8 @@ module Middleman
# Once registered # Once registered
def registered(app) def registered(app)
# Set our preference for a markdown engine # Set our preference for a markdown engine
app.set :markdown_engine, :maruku app.config.define_setting :markdown_engine, :maruku, 'Preferred markdown engine'
app.set :markdown_engine_prefix, ::Tilt app.config.define_setting :markdown_engine_prefix, ::Tilt, 'The parent module for markdown template engines'
app.before_configuration do app.before_configuration do
template_extensions :markdown => :html, template_extensions :markdown => :html,
@ -26,24 +26,24 @@ module Middleman
begin begin
# Look for the user's preferred engine # Look for the user's preferred engine
if markdown_engine == :redcarpet if config[:markdown_engine] == :redcarpet
require "middleman-core/renderers/redcarpet" require "middleman-core/renderers/redcarpet"
::Tilt.prefer(::Middleman::Renderers::RedcarpetTemplate) ::Tilt.prefer(::Middleman::Renderers::RedcarpetTemplate)
elsif !markdown_engine.nil? elsif !config[:markdown_engine].nil?
# Map symbols to classes # Map symbols to classes
markdown_engine_klass = if markdown_engine.is_a? Symbol markdown_engine_klass = if config[:markdown_engine].is_a? Symbol
engine = markdown_engine.to_s engine = config[:markdown_engine].to_s
engine = engine == "rdiscount" ? "RDiscount" : engine.camelize engine = engine == "rdiscount" ? "RDiscount" : engine.camelize
markdown_engine_prefix.const_get("#{engine}Template") config[:markdown_engine_prefix].const_get("#{engine}Template")
else else
markdown_engine_prefix config[:markdown_engine_prefix]
end end
# Tell tilt to use that engine # Tell tilt to use that engine
::Tilt.prefer(markdown_engine_klass) ::Tilt.prefer(markdown_engine_klass)
end end
rescue LoadError rescue LoadError
logger.warn "Requested Markdown engine (#{markdown_engine}) not found. Maybe the gem needs to be installed and required?" logger.warn "Requested Markdown engine (#{config[:markdown_engine]}) not found. Maybe the gem needs to be installed and required?"
end end
end end
end end

View file

@ -12,16 +12,16 @@ module Middleman
# Once registered # Once registered
def registered(app) def registered(app)
# Default sass options # Default sass options
app.set :sass, {} app.config.define_setting :sass, {}, 'Sass engine options'
# Location of SASS .sass_cache directory. # Location of SASS .sass_cache directory.
# @return [String] # @return [String]
# set :sass_cache_path, "/tmp/middleman-app-name/sass_cache" app.config.define_setting :sass_cache_path, nil, 'Location of sass cache' # runtime compile of path
app.set(:sass_cache_path) { File.join(app.root_path, '.sass_cache') } # runtime compile of path
app.before_configuration do app.before_configuration do
template_extensions :scss => :css, template_extensions :scss => :css,
:sass => :css :sass => :css
config[:sass_cache_path] = File.join(app.root_path, '.sass_cache')
end end
# Tell Tilt to use it as well (for inline sass blocks) # Tell Tilt to use it as well (for inline sass blocks)
@ -76,11 +76,11 @@ module Middleman
more_opts = { :filename => eval_file, :line => line, :syntax => syntax } more_opts = { :filename => eval_file, :line => line, :syntax => syntax }
if @context.is_a?(::Middleman::Application) && file if @context.is_a?(::Middleman::Application) && file
location_of_sass_file = File.expand_path(@context.source, @context.root) location_of_sass_file = @context.source_dir
parts = basename.split('.') parts = basename.split('.')
parts.pop parts.pop
more_opts[:css_filename] = File.join(location_of_sass_file, @context.css_dir, parts.join(".")) more_opts[:css_filename] = File.join(location_of_sass_file, @context.config[:css_dir], parts.join("."))
end end
options.merge(more_opts) options.merge(more_opts)

View file

@ -20,10 +20,10 @@ module Middleman
app.register Middleman::Sitemap::Extensions::Ignores app.register Middleman::Sitemap::Extensions::Ignores
# Set to automatically convert some characters into a directory # Set to automatically convert some characters into a directory
app.set :automatic_directory_matcher, nil app.config.define_setting :automatic_directory_matcher, nil, 'Set to automatically convert some characters into a directory'
# Setup callbacks which can exclude paths from the sitemap # Setup callbacks which can exclude paths from the sitemap
app.set :ignored_sitemap_matchers, { app.config.define_setting :ignored_sitemap_matchers, {
# dotfiles and folders in the root # dotfiles and folders in the root
:root_dotfiles => proc { |file| file.match(%r{^\.}) }, :root_dotfiles => proc { |file| file.match(%r{^\.}) },
@ -38,7 +38,7 @@ module Middleman
:layout => proc { |file| :layout => proc { |file|
file.match(%r{^source/layout\.}) || file.match(%r{^source/layouts/}) file.match(%r{^source/layout\.}) || file.match(%r{^source/layouts/})
} }
} }, 'Callbacks that can exclude paths from the sitemap'
# Include instance methods # Include instance methods
app.send :include, InstanceMethods app.send :include, InstanceMethods

View file

@ -45,7 +45,7 @@ module Middleman
path = @sitemap.file_to_path(file) path = @sitemap.file_to_path(file)
return false unless path return false unless path
ignored = @app.ignored_sitemap_matchers.any? do |name, callback| ignored = @app.config[:ignored_sitemap_matchers].any? do |name, callback|
callback.call(file) callback.call(file)
end end
@ -85,7 +85,7 @@ module Middleman
::Middleman::Sitemap::Resource.new( ::Middleman::Sitemap::Resource.new(
@sitemap, @sitemap,
@sitemap.file_to_path(file), @sitemap.file_to_path(file),
File.expand_path(file, @app.root) File.join(@app.root, file)
) )
end end
end end

View file

@ -152,7 +152,7 @@ module Middleman
# @param [String] file # @param [String] file
# @return [String] # @return [String]
def file_to_path(file) def file_to_path(file)
file = File.expand_path(file, @app.root) file = File.join(@app.root, file)
prefix = @app.source_dir.sub(/\/$/, "") + "/" prefix = @app.source_dir.sub(/\/$/, "") + "/"
return false unless file.start_with?(prefix) return false unless file.start_with?(prefix)
@ -160,8 +160,8 @@ module Middleman
path = file.sub(prefix, "") path = file.sub(prefix, "")
# Replace a file name containing automatic_directory_matcher with a folder # Replace a file name containing automatic_directory_matcher with a folder
unless @app.automatic_directory_matcher.nil? unless @app.config[:automatic_directory_matcher].nil?
path = path.gsub(@app.automatic_directory_matcher, "/") path = path.gsub(@app.config[:automatic_directory_matcher], "/")
end end
extensionless_path(path) extensionless_path(path)

View file

@ -10,9 +10,12 @@ end
Given /^an empty app$/ do Given /^an empty app$/ do
step %Q{a directory named "empty_app"} step %Q{a directory named "empty_app"}
step %Q{I cd to "empty_app"} step %Q{I cd to "empty_app"}
ENV["MM_ROOT"] = nil
end end
Given /^a fixture app "([^\"]*)"$/ do |path| Given /^a fixture app "([^\"]*)"$/ do |path|
ENV["MM_ROOT"] = nil
# This step can be reentered from several places but we don't want # This step can be reentered from several places but we don't want
# to keep re-copying and re-cd-ing into ever-deeper directories # to keep re-copying and re-cd-ing into ever-deeper directories
next if File.basename(current_dir) == path next if File.basename(current_dir) == path

View file

@ -40,9 +40,10 @@ Given /^the Server is running$/ do
ENV["MM_SOURCE"] = "" ENV["MM_SOURCE"] = ""
end end
ENV["MM_ROOT"] = root_dir
initialize_commands = @initialize_commands || [] initialize_commands = @initialize_commands || []
initialize_commands.unshift lambda { initialize_commands.unshift lambda {
set :root, root_dir
set :environment, @current_env || :development set :environment, @current_env || :development
set :show_exceptions, false set :show_exceptions, false
} }

View file

@ -7,9 +7,6 @@ module MyExtension
# Called when user `activate`s your extension # Called when user `activate`s your extension
def registered(app, options={}) def registered(app, options={})
# Setup extension-specific config
app.set :config_variable, false
# Include class methods # Include class methods
# app.extend ClassMethods # app.extend ClassMethods
@ -18,9 +15,6 @@ module MyExtension
app.after_configuration do app.after_configuration do
# Do something # Do something
# config_variable is now either the default or the user's
# setting from config.rb
end end
end end
alias :included :registered alias :included :registered

View file

@ -33,7 +33,7 @@ module Middleman
if resource = sitemap.find_resource_by_path(path) if resource = sitemap.find_resource_by_path(path)
resource.url resource.url
else else
File.join(http_prefix, path) File.join(config[:http_prefix], path)
end end
end end
end end

View file

@ -19,38 +19,38 @@ module Middleman
# Location of SASS/SCSS files external to source directory. # Location of SASS/SCSS files external to source directory.
# @return [Array] # @return [Array]
# set :sass_assets_paths, ["#{root}/assets/sass/", "/path/2/external/sass/repository/"] # config[:sass_assets_paths] = ["#{root}/assets/sass/", "/path/2/external/sass/repository/"]
app.set :sass_assets_paths, [] app.config.define_setting :sass_assets_paths, [], 'Paths to extra SASS/SCSS files'
app.after_configuration do app.after_configuration do
::Compass.configuration do |config| ::Compass.configuration do |compass_config|
config.project_path = source_dir compass_config.project_path = source_dir
config.environment = :development compass_config.environment = :development
config.cache_path = sass_cache_path compass_config.cache_path = config[:sass_cache_path]
config.sass_dir = css_dir compass_config.sass_dir = config[:css_dir]
config.additional_import_paths = sass_assets_paths compass_config.additional_import_paths = config[:sass_assets_paths]
config.css_dir = css_dir compass_config.css_dir = config[:css_dir]
config.javascripts_dir = js_dir compass_config.javascripts_dir = config[:js_dir]
config.fonts_dir = fonts_dir compass_config.fonts_dir = config[:fonts_dir]
config.images_dir = images_dir compass_config.images_dir = config[:images_dir]
config.http_path = http_prefix compass_config.http_path = config[:http_prefix]
# Disable this initially, the cache_buster extension will # Disable this initially, the cache_buster extension will
# re-enable it if requested. # re-enable it if requested.
config.asset_cache_buster :none compass_config.asset_cache_buster :none
# Disable this initially, the relative_assets extension will # Disable this initially, the relative_assets extension will
# re-enable it if requested. # re-enable it if requested.
config.relative_assets = false compass_config.relative_assets = false
# Default output style # Default output style
config.output_style = :nested compass_config.output_style = :nested
# No line-comments in test mode (changing paths mess with sha1) # No line-comments in test mode (changing paths mess with sha1)
config.line_comments = false if ENV["TEST"] compass_config.line_comments = false if ENV["TEST"]
if respond_to?(:asset_host) && asset_host.is_a?(Proc) if config.defines_setting?(:asset_host) && config[:asset_host].is_a?(Proc)
config.asset_host(&asset_host) compass_config.asset_host(&config[:asset_host])
end end
end end

View file

@ -20,7 +20,7 @@ module Middleman
app.helpers Helpers app.helpers Helpers
app.set :relative_links, false app.config.define_setting :relative_links, false, 'Whether to generate relative links instead of absolute ones'
end end
alias :included :registered alias :included :registered
end end
@ -110,7 +110,7 @@ module Middleman
# :relative option which, if set to true, will produce # :relative option which, if set to true, will produce
# relative URLs instead of absolute URLs. You can also add # relative URLs instead of absolute URLs. You can also add
# #
# set :relative_links, true # config[:relative_links] = true
# #
# to config.rb to have all links default to relative. # to config.rb to have all links default to relative.
def link_to(*args, &block) def link_to(*args, &block)
@ -136,10 +136,10 @@ module Middleman
resource = sitemap.find_resource_by_path(url) resource = sitemap.find_resource_by_path(url)
# Allow people to turn on relative paths for all links with set :relative_links, true # Allow people to turn on relative paths for all links with config[:relative_links] = true
# but still override on a case by case basis with the :relative parameter. # but still override on a case by case basis with the :relative parameter.
effective_relative = relative || false effective_relative = relative || false
if relative.nil? && relative_links if relative.nil? && config[:relative_links]
effective_relative = true effective_relative = true
end end

View file

@ -9,11 +9,11 @@ module Middleman
# Once registerd # Once registerd
def registered(app, options={}) def registered(app, options={})
app.set :locales_dir, "locales" app.config.define_setting :locales_dir, "locales", 'The directory holding your locale configurations'
# Needed for helpers as well # Needed for helpers as well
app.after_configuration do app.after_configuration do
locales_glob = File.join(locales_dir, "*.yml"); locales_glob = File.join(config[:locales_dir], "*.yml");
::I18n.load_path += Dir[File.join(root, locales_glob)] ::I18n.load_path += Dir[File.join(root, locales_glob)]
::I18n.reload! ::I18n.reload!

View file

@ -11,7 +11,7 @@ module Middleman
# Once registered # Once registered
def registered(app) def registered(app)
# Default to no host # Default to no host
app.set :asset_host, false app.config.define_setting :asset_host, false, 'The asset host to use, or false for no asset host, or a Proc to determine asset host'
# Include methods # Include methods
app.send :include, InstanceMethods app.send :include, InstanceMethods
@ -30,12 +30,12 @@ module Middleman
# @return [String] # @return [String]
def asset_url(path, prefix="") def asset_url(path, prefix="")
original_output = super original_output = super
return original_output unless asset_host return original_output unless config[:asset_host]
asset_prefix = if asset_host.is_a?(Proc) asset_prefix = if config[:asset_host].is_a?(Proc)
asset_host.call(original_output) config[:asset_host].call(original_output)
elsif asset_host.is_a?(String) elsif config[:asset_host].is_a?(String)
asset_host config[:asset_host]
end end
File.join(asset_prefix, original_output) File.join(asset_prefix, original_output)

View file

@ -10,13 +10,13 @@ module Middleman
# Once registered # Once registered
def registered(app, options={}) def registered(app, options={})
app.set :css_compressor, false app.config.define_setting :css_compressor, nil, 'Set the CSS compressor to use. Deprecated in favor of the :compressor option when activating :minify_css'
ignore = Array(options[:ignore]) << /\.min\./ ignore = Array(options[:ignore]) << /\.min\./
inline = options[:inline] || false inline = options[:inline] || false
app.after_configuration do app.after_configuration do
chosen_compressor = css_compressor || options[:compressor] || begin chosen_compressor = config[:css_compressor] || options[:compressor] || begin
require "middleman-more/extensions/minify_css/rainpress" require "middleman-more/extensions/minify_css/rainpress"
::Rainpress ::Rainpress
end end

View file

@ -10,7 +10,7 @@ module Middleman
# Once registered # Once registered
def registered(app, options={}) def registered(app, options={})
app.set :js_compressor, false app.config.define_setting :js_compressor, nil, 'Set the JS compressor to use. Deprecated in favor of the :compressor option when activating :minify_js'
ignore = Array(options[:ignore]) << /\.min\./ ignore = Array(options[:ignore]) << /\.min\./
inline = options[:inline] || false inline = options[:inline] || false