middleman/lib/middleman.rb

248 lines
7.8 KiB
Ruby
Raw Normal View History

2011-12-21 02:03:36 +01:00
require "rbconfig"
2011-07-27 06:05:13 +02:00
# Setup our load paths
libdir = File.dirname(__FILE__)
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
2011-02-11 08:14:33 +01:00
# Top-level Middleman object
2010-03-03 07:00:46 +01:00
module Middleman
2011-12-21 02:03:36 +01:00
WINDOWS = !!(RUBY_PLATFORM =~ /(mingw|bccwin|wince|mswin32)/i)
JRUBY = !!(RbConfig::CONFIG["RUBY_INSTALL_NAME"] =~ /^jruby/i)
2011-02-11 08:14:33 +01:00
# Auto-load modules on-demand
autoload :Base, "middleman/base"
autoload :Cache, "middleman/cache"
autoload :Templates, "middleman/templates"
autoload :Guard, "middleman/guard"
2011-12-21 21:13:28 +01:00
module Cli
autoload :Base, "middleman/cli"
autoload :Build, "middleman/cli/build"
2011-12-21 21:13:28 +01:00
autoload :Init, "middleman/cli/init"
autoload :Server, "middleman/cli/server"
end
2011-02-11 08:14:33 +01:00
# Custom Renderers
module Renderers
autoload :Haml, "middleman/renderers/haml"
autoload :Sass, "middleman/renderers/sass"
autoload :Markdown, "middleman/renderers/markdown"
2011-10-27 18:24:48 +02:00
autoload :ERb, "middleman/renderers/erb"
autoload :Liquid, "middleman/renderers/liquid"
end
module Sitemap
autoload :Store, "middleman/sitemap/store"
autoload :Page, "middleman/sitemap/page"
autoload :Template, "middleman/sitemap/template"
end
2011-11-24 06:59:53 +01:00
module CoreExtensions
2011-11-09 00:38:15 +01:00
# File Change Notifier
2011-11-08 07:34:02 +01:00
autoload :FileWatcher, "middleman/core_extensions/file_watcher"
2011-11-09 00:38:15 +01:00
# In-memory Sitemap
autoload :Sitemap, "middleman/core_extensions/sitemap"
2011-11-08 07:34:02 +01:00
# Add Builder callbacks
autoload :Builder, "middleman/core_extensions/builder"
# Custom Feature API
autoload :Extensions, "middleman/core_extensions/extensions"
2011-07-06 19:40:17 +02:00
# Asset Path Pipeline
autoload :Assets, "middleman/core_extensions/assets"
# DefaultHelpers are the built-in dynamic template helpers.
autoload :DefaultHelpers, "middleman/core_extensions/default_helpers"
# Data looks at the data/ folder for YAML files and makes them available
# to dynamic requests.
autoload :Data, "middleman/core_extensions/data"
# Parse YAML from templates
autoload :FrontMatter, "middleman/core_extensions/front_matter"
# Extended version of Padrino's rendering
autoload :Rendering, "middleman/core_extensions/rendering"
# Compass framework for Sass
autoload :Compass, "middleman/core_extensions/compass"
# Sprockets 2
autoload :Sprockets, "middleman/core_extensions/sprockets"
2011-07-06 19:46:06 +02:00
# Pass custom options to views
autoload :Routing, "middleman/core_extensions/routing"
# Catch and show exceptions at the Rack level
autoload :ShowExceptions, "middleman/core_extensions/show_exceptions"
end
module Extensions
# RelativeAssets allow any asset path in dynamic templates to be either
# relative to the root of the project or use an absolute URL.
autoload :RelativeAssets, "middleman/extensions/relative_assets"
2011-07-10 22:55:40 +02: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/extensions/asset_host"
# CacheBuster adds a query string to assets in dynamic templates to avoid
# browser caches failing to update to your new content.
autoload :CacheBuster, "middleman/extensions/cache_buster"
2011-07-10 22:55:40 +02:00
# AutomaticImageSizes inspects the images used in your dynamic templates
# and automatically adds width and height attributes to their HTML
# elements.
autoload :AutomaticImageSizes, "middleman/extensions/automatic_image_sizes"
# MinifyCss uses the YUI compressor to shrink CSS files
autoload :MinifyCss, "middleman/extensions/minify_css"
# MinifyJavascript uses the YUI compressor to shrink JS files
autoload :MinifyJavascript, "middleman/extensions/minify_javascript"
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/extensions/lorem"
2011-07-14 03:45:15 +02:00
# Automatically convert filename.html files into filename/index.html
autoload :DirectoryIndexes, "middleman/extensions/directory_indexes"
2011-11-08 09:00:33 +01: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
"== #{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
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")
2011-11-24 06:59:53 +01:00
2011-11-21 06:21:19 +01:00
class << self
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
extensions = rubygems_latest_specs.select do |spec|
spec_has_file?(spec, EXTENSION_FILE)
end
2011-08-19 02:33:45 +02:00
2011-11-21 06:21:19 +01:00
extensions.each do |spec|
require spec.name
end
2011-08-19 02:33:45 +02:00
end
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
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
2011-08-29 20:53:11 +02:00
2011-11-24 06:59:53 +01: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 06:21:19 +01:00
def server(&block)
Class.new(Middleman::Base)
end
2011-08-09 23:37:55 +02: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={})
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 => []
}
2011-08-09 23:37:55 +02: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
2011-12-21 02:03:36 +01:00
opts[:server] = if ::Middleman::JRUBY
'webrick' # Maybe Kirk?
else
require "thin"
::Thin::Logging.silent = !options[:is_logging]
'thin'
end
2011-08-09 23:55:48 +02: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
2010-08-06 18:59:38 +02:00
end
2011-11-24 06:59:53 +01:00
# Make the VERSION string available
require "middleman/version"
2011-11-24 06:59:53 +01:00
# Automatically discover extensions in RubyGems
2011-11-08 02:58:07 +01:00
Middleman.load_extensions_in_path