2011-12-20 20:03:36 -05:00
|
|
|
require "rbconfig"
|
|
|
|
|
2011-12-26 14:24:33 -05:00
|
|
|
# Using Thor's indifferent hash access
|
|
|
|
require "thor"
|
|
|
|
|
2011-12-29 18:09:51 -05:00
|
|
|
require "active_support"
|
|
|
|
|
2011-07-27 00:05:13 -04:00
|
|
|
# Setup our load paths
|
2011-12-24 14:30:41 -05:00
|
|
|
libdir = File.expand_path(File.dirname(__FILE__))
|
2009-10-22 20:25:15 -04:00
|
|
|
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
|
|
|
|
|
2011-12-29 18:09:51 -05:00
|
|
|
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"
|
|
|
|
|
2011-02-11 02:14:33 -05:00
|
|
|
# Top-level Middleman object
|
2010-03-03 01:00:46 -05:00
|
|
|
module Middleman
|
2011-12-29 22:04:39 -05:00
|
|
|
WINDOWS = !!(RUBY_PLATFORM =~ /(mingw|bccwin|wince|mswin32)/i) unless const_defined?(:WINDOWS)
|
|
|
|
JRUBY = !!(RbConfig::CONFIG["RUBY_INSTALL_NAME"] =~ /^jruby/i) unless const_defined?(:JRUBY)
|
2011-12-20 20:03:36 -05:00
|
|
|
|
2011-02-11 02:14:33 -05:00
|
|
|
# Auto-load modules on-demand
|
2011-12-29 18:09:51 -05:00
|
|
|
autoload :Base, "middleman-core/base"
|
|
|
|
autoload :Cache, "middleman-core/cache"
|
|
|
|
autoload :Templates, "middleman-core/templates"
|
2012-01-01 20:47:38 -05:00
|
|
|
autoload :Watcher, "middleman-core/watcher"
|
2010-09-06 21:48:25 -04:00
|
|
|
|
2011-12-21 15:13:28 -05:00
|
|
|
module Cli
|
2011-12-29 18:09:51 -05:00
|
|
|
autoload :Base, "middleman-core/cli"
|
|
|
|
autoload :Build, "middleman-core/cli/build"
|
|
|
|
autoload :Init, "middleman-core/cli/init"
|
|
|
|
autoload :Server, "middleman-core/cli/server"
|
2011-12-21 14:03:45 -05:00
|
|
|
end
|
|
|
|
|
2011-02-11 02:14:33 -05:00
|
|
|
# Custom Renderers
|
2010-09-06 21:48:25 -04:00
|
|
|
module Renderers
|
2011-12-29 18:09:51 -05:00
|
|
|
autoload :ERb, "middleman-core/renderers/erb"
|
2010-09-06 21:48:25 -04:00
|
|
|
end
|
2011-07-06 12:50:34 -04:00
|
|
|
|
2011-11-20 20:05:29 -05:00
|
|
|
module Sitemap
|
2011-12-29 18:09:51 -05:00
|
|
|
autoload :Store, "middleman-core/sitemap/store"
|
|
|
|
autoload :Page, "middleman-core/sitemap/page"
|
|
|
|
autoload :Template, "middleman-core/sitemap/template"
|
2011-11-20 20:05:29 -05:00
|
|
|
end
|
2011-11-24 00:59:53 -05:00
|
|
|
|
2011-07-06 12:50:34 -04:00
|
|
|
module CoreExtensions
|
2011-11-08 18:38:15 -05:00
|
|
|
# File Change Notifier
|
2011-12-29 18:09:51 -05:00
|
|
|
autoload :FileWatcher, "middleman-core/core_extensions/file_watcher"
|
2011-11-08 01:34:02 -05:00
|
|
|
|
2011-11-08 18:38:15 -05:00
|
|
|
# In-memory Sitemap
|
2011-12-29 18:09:51 -05:00
|
|
|
autoload :Sitemap, "middleman-core/core_extensions/sitemap"
|
2011-11-08 01:34:02 -05:00
|
|
|
|
2011-08-04 22:43:42 -04:00
|
|
|
# Add Builder callbacks
|
2011-12-29 18:09:51 -05:00
|
|
|
autoload :Builder, "middleman-core/core_extensions/builder"
|
2011-08-04 22:43:42 -04:00
|
|
|
|
2011-07-06 13:15:31 -04:00
|
|
|
# Custom Feature API
|
2011-12-29 18:09:51 -05:00
|
|
|
autoload :Extensions, "middleman-core/core_extensions/extensions"
|
2011-07-06 13:15:31 -04:00
|
|
|
|
2011-07-06 13:40:17 -04:00
|
|
|
# Asset Path Pipeline
|
2011-12-29 18:09:51 -05:00
|
|
|
autoload :Assets, "middleman-core/core_extensions/assets"
|
2011-07-06 12:50:34 -04:00
|
|
|
|
|
|
|
# Data looks at the data/ folder for YAML files and makes them available
|
|
|
|
# to dynamic requests.
|
2011-12-29 18:09:51 -05:00
|
|
|
autoload :Data, "middleman-core/core_extensions/data"
|
2011-07-06 13:15:31 -04:00
|
|
|
|
|
|
|
# Parse YAML from templates
|
2011-12-29 18:09:51 -05:00
|
|
|
autoload :FrontMatter, "middleman-core/core_extensions/front_matter"
|
2011-07-06 13:15:31 -04:00
|
|
|
|
2011-12-29 18:09:51 -05:00
|
|
|
# DefaultHelpers are the built-in dynamic template helpers.
|
|
|
|
autoload :DefaultHelpers, "middleman-core/core_extensions/default_helpers"
|
|
|
|
|
2011-07-06 13:15:31 -04:00
|
|
|
# Extended version of Padrino's rendering
|
2011-12-29 18:09:51 -05:00
|
|
|
autoload :Rendering, "middleman-core/core_extensions/rendering"
|
2011-07-10 18:10:49 -04:00
|
|
|
|
2011-07-06 13:46:06 -04:00
|
|
|
# Pass custom options to views
|
2011-12-29 18:09:51 -05:00
|
|
|
autoload :Routing, "middleman-core/core_extensions/routing"
|
2011-12-09 14:11:17 -05:00
|
|
|
|
|
|
|
# Catch and show exceptions at the Rack level
|
2011-12-29 18:09:51 -05:00
|
|
|
autoload :ShowExceptions, "middleman-core/core_extensions/show_exceptions"
|
2011-07-06 12:50:34 -04:00
|
|
|
end
|
2011-12-29 18:09:51 -05:00
|
|
|
|
2011-11-21 03:23:16 -05:00
|
|
|
module Extensions
|
2011-12-29 18:09:51 -05:00
|
|
|
# Provide Apache-style index.html files for directories
|
2011-12-29 22:04:39 -05:00
|
|
|
autoload :DirectoryIndexes, "middleman-core/extensions/directory_indexes"
|
2011-07-06 13:15:31 -04:00
|
|
|
|
2011-07-10 16:55:40 -04:00
|
|
|
# Lorem provides a handful of helpful prototyping methods to generate
|
|
|
|
# words, paragraphs, fake images, names and email addresses.
|
2011-12-29 22:04:39 -05:00
|
|
|
autoload :Lorem, "middleman-core/extensions/lorem"
|
|
|
|
|
|
|
|
# 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"
|
|
|
|
|
|
|
|
# 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"
|
2011-12-29 18:09:51 -05:00
|
|
|
end
|
|
|
|
|
2011-12-29 22:04:39 -05:00
|
|
|
module Extensions
|
2011-12-04 14:58:45 -05:00
|
|
|
class << self
|
|
|
|
def registered
|
|
|
|
@_registered ||= {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def register(name, namespace=nil, version=nil, &block)
|
|
|
|
# If we've already got a matching extension that passed the
|
|
|
|
# version check, bail out.
|
|
|
|
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
|
2011-12-29 22:04:39 -05:00
|
|
|
"== #{name} failed version check. Requested #{version}, got #{Middleman::VERSION}"
|
2011-12-04 14:58:45 -05:00
|
|
|
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
|
2011-07-13 03:38:04 -04:00
|
|
|
end
|
|
|
|
|
2011-11-24 00:59:53 -05:00
|
|
|
# Where to look in gems for extensions to auto-register
|
2011-12-29 22:04:39 -05:00
|
|
|
EXTENSION_FILE = File.join("lib", "middleman_extension.rb") unless const_defined?(:EXTENSION_FILE)
|
2011-11-24 00:59:53 -05:00
|
|
|
|
2011-11-21 00:21:19 -05:00
|
|
|
class << self
|
2011-11-24 00:59:53 -05:00
|
|
|
|
2011-12-26 14:24:33 -05: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
|
|
|
|
|
2011-11-24 00:59:53 -05:00
|
|
|
# Automatically load extensions from available RubyGems
|
|
|
|
# which contain the EXTENSION_FILE
|
|
|
|
#
|
|
|
|
# @private
|
2011-11-21 00:21:19 -05:00
|
|
|
def load_extensions_in_path
|
|
|
|
extensions = rubygems_latest_specs.select do |spec|
|
|
|
|
spec_has_file?(spec, EXTENSION_FILE)
|
|
|
|
end
|
2011-12-29 22:04:39 -05:00
|
|
|
|
2011-11-21 00:21:19 -05:00
|
|
|
extensions.each do |spec|
|
|
|
|
require spec.name
|
|
|
|
end
|
2011-08-18 20:33:45 -04:00
|
|
|
end
|
|
|
|
|
2011-11-24 00:59:53 -05: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 00:21:19 -05: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 14:53:11 -04:00
|
|
|
end
|
|
|
|
|
2011-11-24 00:59:53 -05: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 00:21:19 -05:00
|
|
|
def spec_has_file?(spec, path)
|
|
|
|
full_path = File.join(spec.full_gem_path, path)
|
|
|
|
File.exists?(full_path)
|
|
|
|
end
|
2011-08-29 14:53:11 -04:00
|
|
|
|
2011-11-24 00:59:53 -05:00
|
|
|
# Create a new Class which is based on Middleman::Base
|
|
|
|
# Used to create a safe sandbox into which extensions and
|
|
|
|
# configuration can be included later without impacting
|
|
|
|
# other classes and instances.
|
|
|
|
#
|
|
|
|
# @return [Class]
|
2011-11-21 00:21:19 -05:00
|
|
|
def server(&block)
|
|
|
|
Class.new(Middleman::Base)
|
|
|
|
end
|
2011-08-09 17:37:55 -04:00
|
|
|
|
2011-11-24 00:59:53 -05:00
|
|
|
# Creates a new Rack::Server
|
|
|
|
#
|
|
|
|
# @param [Hash] options to pass to Rack::Server.new
|
|
|
|
# @return [Rack::Server]
|
2011-11-21 00:21:19 -05:00
|
|
|
def start_server(options={})
|
2011-12-29 22:04:39 -05:00
|
|
|
require "webrick"
|
|
|
|
|
2011-11-21 00:21:19 -05:00
|
|
|
opts = {
|
|
|
|
:Port => options[:port] || 4567,
|
2011-11-24 01:05:53 -05:00
|
|
|
:Host => options[:host] || "0.0.0.0",
|
2011-11-21 00:21:19 -05:00
|
|
|
:AccessLog => []
|
|
|
|
}
|
2011-12-29 22:04:39 -05:00
|
|
|
|
|
|
|
opts[:Logger] = WEBrick::Log::new("/dev/null", 7) if !options[:logging]
|
2011-08-09 17:37:55 -04:00
|
|
|
|
2011-11-28 01:04:19 -05:00
|
|
|
app_class = options[:app] ||= ::Middleman.server.inst
|
2011-11-21 00:21:19 -05:00
|
|
|
opts[:app] = app_class
|
2011-12-24 19:09:07 -05:00
|
|
|
|
2011-12-29 18:09:51 -05:00
|
|
|
# Disable for Beta 1. See if people notice.
|
|
|
|
# require "thin"
|
|
|
|
# ::Thin::Logging.silent = !options[:logging]
|
|
|
|
# opts[:server] = 'thin'
|
2011-12-29 22:04:39 -05:00
|
|
|
opts[:server] = 'webrick'
|
2011-12-24 19:09:07 -05:00
|
|
|
|
2011-11-21 00:21:19 -05:00
|
|
|
server = ::Rack::Server.new(opts)
|
|
|
|
server.start
|
|
|
|
server
|
|
|
|
end
|
2011-08-09 17:37:55 -04:00
|
|
|
end
|
2011-12-29 18:09:51 -05:00
|
|
|
end
|