middleman/middleman-core/lib/middleman-core.rb

297 lines
9.3 KiB
Ruby
Raw Normal View History

2011-12-21 02:03:36 +01:00
require "rbconfig"
# Using Thor's indifferent hash access
require "thor"
require "active_support"
2011-07-27 06:05:13 +02:00
# Setup our load paths
libdir = File.expand_path(File.dirname(__FILE__))
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
class String
def camelize
self.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end
end
# Simple callback library
require "middleman-core/vendor/hooks-0.2.0/lib/hooks"
require "middleman-core/version"
2011-02-11 08:14:33 +01:00
# Top-level Middleman object
2010-03-03 07:00:46 +01:00
module Middleman
WINDOWS = !!(RUBY_PLATFORM =~ /(mingw|bccwin|wince|mswin32)/i) unless const_defined?(:WINDOWS)
JRUBY = !!(RbConfig::CONFIG["RUBY_INSTALL_NAME"] =~ /^jruby/i) unless const_defined?(:JRUBY)
DARWIN = RbConfig::CONFIG['target_os'] =~ /darwin/i unless const_defined?(:DARWIN)
LINUX = RbConfig::CONFIG['target_os'] =~ /linux/i unless const_defined?(:LINUX)
2012-02-11 10:35:49 +01:00
2011-02-11 08:14:33 +01:00
# Auto-load modules on-demand
autoload :Base, "middleman-core/base"
autoload :Cache, "middleman-core/cache"
autoload :Templates, "middleman-core/templates"
autoload :Watcher, "middleman-core/watcher"
2012-02-11 10:35:49 +01:00
2011-12-21 21:13:28 +01:00
module Cli
autoload :Base, "middleman-core/cli"
autoload :Build, "middleman-core/cli/build"
autoload :Init, "middleman-core/cli/init"
autoload :Server, "middleman-core/cli/server"
end
2012-02-11 10:35:49 +01:00
2011-02-11 08:14:33 +01:00
# Custom Renderers
module Renderers
autoload :ERb, "middleman-core/renderers/erb"
end
2012-02-11 10:35:49 +01:00
module Sitemap
autoload :Store, "middleman-core/sitemap/store"
autoload :Page, "middleman-core/sitemap/page"
autoload :Template, "middleman-core/sitemap/template"
end
2012-02-11 10:35:49 +01:00
module CoreExtensions
2011-11-09 00:38:15 +01:00
# File Change Notifier
autoload :FileWatcher, "middleman-core/core_extensions/file_watcher"
2012-02-11 10:35:49 +01:00
2011-11-09 00:38:15 +01:00
# In-memory Sitemap
autoload :Sitemap, "middleman-core/core_extensions/sitemap"
2012-02-11 10:35:49 +01:00
# Add Builder callbacks
autoload :Builder, "middleman-core/core_extensions/builder"
2012-02-11 10:35:49 +01:00
# Custom Feature API
autoload :Extensions, "middleman-core/core_extensions/extensions"
2012-02-11 10:35:49 +01:00
2011-07-06 19:40:17 +02:00
# Asset Path Pipeline
autoload :Assets, "middleman-core/core_extensions/assets"
2012-02-11 10:35:49 +01:00
# Data looks at the data/ folder for YAML files and makes them available
# to dynamic requests.
autoload :Data, "middleman-core/core_extensions/data"
2012-02-11 10:35:49 +01:00
# Parse YAML from templates
autoload :FrontMatter, "middleman-core/core_extensions/front_matter"
2012-02-11 10:35:49 +01:00
# External helpers looks in the helpers/ folder for helper modules
autoload :ExternalHelpers, "middleman-core/core_extensions/external_helpers"
2012-02-11 10:35:49 +01:00
# DefaultHelpers are the built-in dynamic template helpers.
autoload :DefaultHelpers, "middleman-core/core_extensions/default_helpers"
# Extended version of Padrino's rendering
autoload :Rendering, "middleman-core/core_extensions/rendering"
2012-02-11 10:35:49 +01:00
2011-07-06 19:46:06 +02:00
# Pass custom options to views
autoload :Routing, "middleman-core/core_extensions/routing"
2012-02-11 10:35:49 +01:00
# Catch and show exceptions at the Rack level
autoload :ShowExceptions, "middleman-core/core_extensions/show_exceptions"
2012-03-11 03:57:00 +01:00
# i18n
autoload :I18n, "middleman-core/core_extensions/i18n"
end
2012-02-11 10:35:49 +01:00
module Extensions
# Provide Apache-style index.html files for directories
autoload :DirectoryIndexes, "middleman-core/extensions/directory_indexes"
2011-07-10 22:55:40 +02:00
# Lorem provides a handful of helpful prototyping methods to generate
# words, paragraphs, fake images, names and email addresses.
autoload :Lorem, "middleman-core/extensions/lorem"
2012-02-11 10:35:49 +01:00
# AutomaticImageSizes inspects the images used in your dynamic templates
# and automatically adds width and height attributes to their HTML
# elements.
autoload :AutomaticImageSizes, "middleman-core/extensions/automatic_image_sizes"
2012-02-11 10:35:49 +01:00
# AssetHost allows you to setup multiple domains to host your static
# assets. Calls to asset paths in dynamic templates will then rotate
# through each of the asset servers to better spread the load.
autoload :AssetHost, "middleman-core/extensions/asset_host"
end
2012-02-11 10:35:49 +01:00
# Backwards compatibility namespace
module Features
end
module Extensions
class << self
def registered
@_registered ||= {}
end
# Register a new extension. Choose a name which will be
# used to activate the extension in config.rb, like this:
#
# activate :my_extension
#
# Provide your extension module either as the namespace
# parameter, or return it from the block:
#
# @param [Symbol] name The name of the extension
# @param [Module] namespace The extension module
# @param [String] version A RubyGems-style version string stating
# the versions of middleman this extension
# is compatible with.
# @yield Instead of passing a module in namespace, you can provide
# a block which returns your extension module. This gives
# you the ability to require other files only when the
# extension is activated.
def register(name, namespace=nil, version=nil, &block)
2012-02-11 10:35:49 +01:00
# If we've already got a matching extension that passed the
# version check, bail out.
2012-02-11 10:35:49 +01:00
return if registered.has_key?(name.to_sym) &&
!registered[name.to_sym].is_a?(String)
if block_given?
version = namespace
end
passed_version_check = true
if !version.nil?
requirement = ::Gem::Requirement.create(version)
if !requirement.satisfied_by?(Middleman::GEM_VERSION)
passed_version_check = false
end
end
registered[name.to_sym] = if !passed_version_check
"== #{name} failed version check. Requested #{version}, got #{Middleman::VERSION}"
elsif block_given?
block
elsif namespace
namespace
end
end
def load(name)
name = name.to_sym
return nil unless registered.has_key?(name)
extension = registered[name]
if extension.is_a?(Proc)
extension = extension.call() || nil
registered[name] = extension
end
extension
end
end
end
2012-02-11 10:35:49 +01:00
2011-11-24 06:59:53 +01:00
# Where to look in gems for extensions to auto-register
EXTENSION_FILE = File.join("lib", "middleman_extension.rb") unless const_defined?(:EXTENSION_FILE)
2012-02-11 10:35:49 +01:00
2011-11-21 06:21:19 +01:00
class << self
2012-02-11 10:35:49 +01:00
# Recursively convert a normal Hash into a HashWithIndifferentAccess
#
# @private
# @param [Hash] data Normal hash
# @return [Thor::CoreExt::HashWithIndifferentAccess]
def recursively_enhance(data)
if data.is_a? Hash
data = ::Thor::CoreExt::HashWithIndifferentAccess.new(data)
data.each do |key, val|
data[key] = recursively_enhance(val)
end
data
elsif data.is_a? Array
data.each_with_index do |val, i|
data[i] = recursively_enhance(val)
end
data
else
data
end
end
2012-02-11 10:35:49 +01:00
2011-11-24 06:59:53 +01:00
# Automatically load extensions from available RubyGems
# which contain the EXTENSION_FILE
#
# @private
2011-11-21 06:21:19 +01:00
def load_extensions_in_path
if defined?(Bundler)
Bundler.require
else
extensions = rubygems_latest_specs.select do |spec|
spec_has_file?(spec, EXTENSION_FILE)
end
2012-02-11 10:35:49 +01:00
extensions.each do |spec|
require spec.name
end
2011-11-21 06:21:19 +01:00
end
2011-08-19 02:33:45 +02:00
end
2012-02-11 10:35:49 +01:00
2011-11-24 06:59:53 +01:00
# Backwards compatible means of finding all the latest gemspecs
# available on the system
#
# @private
# @return [Array] Array of latest Gem::Specification
2011-11-21 06:21:19 +01:00
def rubygems_latest_specs
# If newer Rubygems
if ::Gem::Specification.respond_to? :latest_specs
::Gem::Specification.latest_specs
else
::Gem.source_index.latest_specs
end
2011-08-29 20:53:11 +02:00
end
2012-02-11 10:35:49 +01:00
2011-11-24 06:59:53 +01:00
# Where a given Gem::Specification has a specific file. Used
# to discover extensions and Sprockets-supporting gems.
#
# @private
# @param [Gem::Specification]
# @param [String] Path to look for
# @return [Boolean] Whether the file exists
2011-11-21 06:21:19 +01:00
def spec_has_file?(spec, path)
full_path = File.join(spec.full_gem_path, path)
File.exists?(full_path)
end
2012-02-11 10:35:49 +01:00
2011-11-24 06:59:53 +01:00
# Create a new Class which is based on Middleman::Base
2012-02-11 10:35:49 +01:00
# Used to create a safe sandbox into which extensions and
2011-11-24 06:59:53 +01:00
# configuration can be included later without impacting
# other classes and instances.
#
# @return [Class]
2011-11-21 06:21:19 +01:00
def server(&block)
@@servercounter ||= 1
@@servercounter += 1
const_set("MiddlemanBase#{@@servercounter}", Class.new(Middleman::Base))
2011-11-21 06:21:19 +01:00
end
2012-02-11 10:35:49 +01:00
2011-11-24 06:59:53 +01:00
# Creates a new Rack::Server
#
# @param [Hash] options to pass to Rack::Server.new
# @return [Rack::Server]
2011-11-21 06:21:19 +01:00
def start_server(options={})
require "webrick"
2012-02-11 10:35:49 +01:00
2011-11-21 06:21:19 +01:00
opts = {
:Port => options[:port] || 4567,
2011-11-24 07:05:53 +01:00
:Host => options[:host] || "0.0.0.0",
2011-11-21 06:21:19 +01:00
:AccessLog => []
}
2012-02-11 10:35:49 +01:00
2012-02-26 00:53:23 +01:00
# opts[:Logger] = WEBrick::Log::new("/dev/null", 7) if !options[:logging]
2012-02-11 10:35:49 +01:00
2011-11-28 07:04:19 +01:00
app_class = options[:app] ||= ::Middleman.server.inst
2011-11-21 06:21:19 +01:00
opts[:app] = app_class
2012-02-11 10:35:49 +01:00
# Disable for Beta 1. See if people notice.
2012-02-26 00:53:23 +01:00
require "thin"
::Thin::Logging.silent = !options[:logging]
opts[:server] = 'thin'
# opts[:server] = 'webrick'
2012-02-11 10:35:49 +01:00
2011-11-21 06:21:19 +01:00
server = ::Rack::Server.new(opts)
server.start
server
end
2011-08-09 23:37:55 +02:00
end
2012-02-11 10:35:49 +01:00
end