2011-11-24 06:59:53 +01:00
|
|
|
# Using Tilt for templating
|
2011-11-18 04:56:55 +01:00
|
|
|
require "tilt"
|
2011-11-24 06:59:53 +01:00
|
|
|
|
2011-12-30 00:09:51 +01:00
|
|
|
# Use ActiveSupport JSON
|
2011-09-09 08:06:22 +02:00
|
|
|
require "active_support/json"
|
2012-04-14 22:51:02 +02:00
|
|
|
|
|
|
|
# Simple callback library
|
|
|
|
require "middleman-core/vendor/hooks-0.2.0/lib/hooks"
|
|
|
|
|
|
|
|
require "middleman-core/sitemap"
|
2012-04-02 05:52:22 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
require "middleman-core/core_extensions"
|
|
|
|
|
2011-11-24 06:59:53 +01:00
|
|
|
# Core Middleman Class
|
2012-04-14 22:51:02 +02:00
|
|
|
module Middleman
|
2012-04-14 23:04:10 +02:00
|
|
|
class Application
|
2012-04-14 22:51:02 +02:00
|
|
|
# Uses callbacks
|
|
|
|
include Hooks
|
2011-11-24 06:59:53 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Before request hook
|
|
|
|
define_hook :before
|
2011-11-24 06:59:53 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Ready (all loading and parsing of extensions complete) hook
|
|
|
|
define_hook :ready
|
2011-11-24 06:59:53 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
class << self
|
|
|
|
# Mix-in helper methods. Accepts either a list of Modules
|
|
|
|
# and/or a block to be evaluated
|
|
|
|
# @return [void]
|
|
|
|
def helpers(*extensions, &block)
|
|
|
|
class_eval(&block) if block_given?
|
|
|
|
include(*extensions) if extensions.any?
|
|
|
|
end
|
2011-11-18 04:56:55 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Access class-wide defaults
|
|
|
|
#
|
|
|
|
# @private
|
|
|
|
# @return [Hash] Hash of default values
|
|
|
|
def defaults
|
|
|
|
@defaults ||= {}
|
|
|
|
end
|
2011-11-18 04:56:55 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Set class-wide defaults
|
|
|
|
#
|
2012-05-02 20:18:16 +02:00
|
|
|
# @param [Symbol] key Unique key name
|
|
|
|
# @param value Default value
|
2012-04-14 22:51:02 +02:00
|
|
|
# @return [void]
|
|
|
|
def set(key, value=nil, &block)
|
|
|
|
@defaults ||= {}
|
|
|
|
@defaults[key] = value
|
|
|
|
|
|
|
|
@inst.set(key, value, &block) if @inst
|
|
|
|
end
|
|
|
|
end
|
2012-05-01 22:11:42 +02:00
|
|
|
|
|
|
|
delegate :helpers, :to => :"self.class"
|
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Set attributes (global variables)
|
2011-11-24 06:59:53 +01:00
|
|
|
#
|
2012-05-02 20:18:16 +02:00
|
|
|
# @param [Symbol] key Name of the attribue
|
|
|
|
# @param value Attribute value
|
2011-12-18 05:12:13 +01:00
|
|
|
# @return [void]
|
2011-12-25 19:06:45 +01:00
|
|
|
def set(key, value=nil, &block)
|
2012-04-14 22:51:02 +02:00
|
|
|
setter = "#{key}=".to_sym
|
|
|
|
self.class.send(:attr_accessor, key) if !respond_to?(setter)
|
|
|
|
value = block if block_given?
|
|
|
|
send(setter, value)
|
2011-11-14 06:57:53 +01:00
|
|
|
end
|
2011-11-18 04:56:55 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Root project directory (overwritten in middleman build/server)
|
|
|
|
# @return [String]
|
|
|
|
set :root, ENV["MM_ROOT"] || Dir.pwd
|
2011-11-24 06:59:53 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Name of the source directory
|
|
|
|
# @return [String]
|
|
|
|
set :source, "source"
|
2011-11-24 06:59:53 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Middleman environment. Defaults to :development, set to :build by the build process
|
|
|
|
# @return [String]
|
|
|
|
set :environment, (ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development
|
2011-11-24 06:59:53 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Whether logging is active, disabled by default
|
|
|
|
# @return [String]
|
|
|
|
set :logging, false
|
2011-11-18 04:56:55 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Which file should be used for directory indexes
|
|
|
|
# @return [String]
|
|
|
|
set :index_file, "index.html"
|
2011-11-18 04:56:55 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Location of javascripts within source. Used by Sprockets.
|
|
|
|
# @return [String]
|
|
|
|
set :js_dir, "javascripts"
|
2011-11-24 06:59:53 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Location of stylesheets within source. Used by Compass.
|
|
|
|
# @return [String]
|
|
|
|
set :css_dir, "stylesheets"
|
2011-11-24 06:59:53 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Location of images within source. Used by HTML helpers and Compass.
|
|
|
|
# @return [String]
|
|
|
|
set :images_dir, "images"
|
2011-11-18 04:56:55 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Where to build output files
|
|
|
|
# @return [String]
|
|
|
|
set :build_dir, "build"
|
2011-11-18 04:56:55 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Default prefix for building paths. Used by HTML helpers and Compass.
|
|
|
|
# @return [String]
|
|
|
|
set :http_prefix, "/"
|
2011-11-24 06:59:53 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Whether to catch and display exceptions
|
|
|
|
# @return [Boolean]
|
|
|
|
set :show_exceptions, true
|
2011-12-09 20:11:17 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Automatically loaded extensions
|
|
|
|
# @return [Array<Symbol>]
|
|
|
|
set :default_extensions, [ :lorem ]
|
2011-11-18 04:56:55 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Default layout name
|
|
|
|
# @return [String, Symbold]
|
|
|
|
set :layout, :_auto_layout
|
2011-11-18 04:56:55 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Activate custom features and extensions
|
|
|
|
include Middleman::CoreExtensions::Extensions
|
2012-05-01 22:11:42 +02:00
|
|
|
|
|
|
|
# Basic Rack Request Handling
|
|
|
|
register Middleman::CoreExtensions::Request
|
2011-12-09 20:11:17 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Handle exceptions
|
|
|
|
register Middleman::CoreExtensions::ShowExceptions
|
2011-11-17 20:43:04 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Add Builder Callbacks
|
|
|
|
register Middleman::CoreExtensions::Builder
|
2011-11-18 04:56:55 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Add Watcher Callbacks
|
|
|
|
register Middleman::CoreExtensions::FileWatcher
|
2011-11-18 04:56:55 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Activate Data package
|
|
|
|
register Middleman::CoreExtensions::Data
|
2012-04-21 08:38:59 +02:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Setup custom rendering
|
|
|
|
register Middleman::CoreExtensions::Rendering
|
2011-11-18 04:56:55 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Sitemap
|
|
|
|
register Middleman::Sitemap
|
2011-11-27 01:17:18 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Setup external helpers
|
|
|
|
register Middleman::CoreExtensions::ExternalHelpers
|
2012-01-15 22:43:26 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Setup default helpers
|
|
|
|
register Middleman::CoreExtensions::DefaultHelpers
|
2011-11-18 04:56:55 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Setup asset path pipeline
|
|
|
|
register Middleman::CoreExtensions::Assets
|
2011-11-18 04:56:55 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# with_layout and page routing
|
|
|
|
register Middleman::CoreExtensions::Routing
|
2011-11-18 04:56:55 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# i18n
|
|
|
|
register Middleman::CoreExtensions::I18n
|
2012-03-11 03:57:00 +01:00
|
|
|
|
2012-05-09 06:05:55 +02:00
|
|
|
# Parse YAML from templates
|
|
|
|
register Middleman::CoreExtensions::FrontMatter
|
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Built-in Extensions
|
2012-05-07 23:41:39 +02:00
|
|
|
|
|
|
|
# Provide Apache-style index.html files for directories
|
|
|
|
Middleman::Extensions.register(:directory_indexes) do
|
|
|
|
require "middleman-core/extensions/directory_indexes"
|
|
|
|
Middleman::Extensions::DirectoryIndexes
|
|
|
|
end
|
|
|
|
|
|
|
|
# Lorem provides a handful of helpful prototyping methods to generate
|
|
|
|
# words, paragraphs, fake images, names and email addresses.
|
|
|
|
Middleman::Extensions.register(:lorem) do
|
|
|
|
require "middleman-core/extensions/lorem"
|
|
|
|
Middleman::Extensions::Lorem
|
|
|
|
end
|
|
|
|
|
|
|
|
# AutomaticImageSizes inspects the images used in your dynamic templates
|
|
|
|
# and automatically adds width and height attributes to their HTML
|
|
|
|
# elements.
|
|
|
|
Middleman::Extensions.register(:automatic_image_sizes) do
|
|
|
|
require "middleman-core/extensions/automatic_image_sizes"
|
|
|
|
Middleman::Extensions::AutomaticImageSizes
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
Middleman::Extensions.register(:asset_host) do
|
|
|
|
require "middleman-core/extensions/asset_host"
|
|
|
|
Middleman::Extensions::AssetHost
|
|
|
|
end
|
2011-11-24 06:59:53 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Initialize the Middleman project
|
|
|
|
def initialize(&block)
|
|
|
|
# Current path defaults to nil, used in views.
|
|
|
|
self.current_path = nil
|
2011-11-21 02:30:53 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Clear the static class cache
|
|
|
|
cache.clear
|
2011-11-27 01:17:18 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Setup the default values from calls to set before initialization
|
|
|
|
self.class.superclass.defaults.each { |k,v| set(k,v) }
|
2011-11-18 04:56:55 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Evaluate a passed block if given
|
|
|
|
instance_exec(&block) if block_given?
|
2011-11-18 22:38:18 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Build expanded source path once paths have been parsed
|
|
|
|
path = root.dup
|
|
|
|
source_path = ENV["MM_SOURCE"] || self.source
|
|
|
|
path = File.join(root, source_path) unless source_path.empty?
|
|
|
|
set :source_dir, path
|
2011-11-24 06:59:53 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
super
|
|
|
|
end
|
2011-07-13 09:38:04 +02:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Shared cache instance
|
|
|
|
#
|
|
|
|
# @private
|
2012-04-14 23:06:49 +02:00
|
|
|
# @return [Middleman::Util::Cache] The cache
|
2012-04-14 22:51:02 +02:00
|
|
|
def self.cache
|
2012-04-14 23:06:49 +02:00
|
|
|
@_cache ||= ::Middleman::Util::Cache.new
|
2012-04-14 22:51:02 +02:00
|
|
|
end
|
|
|
|
delegate :cache, :to => :"self.class"
|
2012-01-17 00:02:38 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Whether we're in development mode
|
|
|
|
# @return [Boolean] If we're in dev mode
|
|
|
|
def development?; environment == :development; end
|
2011-11-17 20:43:04 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Whether we're in build mode
|
|
|
|
# @return [Boolean] If we're in build mode
|
|
|
|
def build?; environment == :build; end
|
2011-11-18 04:56:55 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Backwards compatibilty with old Sinatra template interface
|
|
|
|
#
|
2012-04-14 23:04:10 +02:00
|
|
|
# @return [Middleman::Application]
|
2012-04-14 22:51:02 +02:00
|
|
|
def settings
|
|
|
|
self
|
|
|
|
end
|
2011-11-23 19:59:37 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Whether we're logging
|
|
|
|
#
|
|
|
|
# @return [Boolean] If we're logging
|
|
|
|
def logging?
|
|
|
|
logging
|
|
|
|
end
|
2011-11-18 09:34:56 +01:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Expand a path to include the index file if it's a directory
|
|
|
|
#
|
|
|
|
# @private
|
|
|
|
# @param [String] path Request path
|
|
|
|
# @return [String] Path with index file if necessary
|
|
|
|
def full_path(path)
|
|
|
|
cache.fetch(:full_path, path) do
|
|
|
|
parts = path ? path.split('/') : []
|
|
|
|
if parts.last.nil? || parts.last.split('.').length == 1
|
|
|
|
path = File.join(path, index_file)
|
|
|
|
end
|
|
|
|
"/" + path.sub(%r{^/}, '')
|
2011-11-20 03:53:18 +01:00
|
|
|
end
|
2011-11-27 05:09:14 +01:00
|
|
|
end
|
2011-11-18 04:56:55 +01:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
class << self
|
2012-04-14 23:04:10 +02:00
|
|
|
# Create a new Class which is based on Middleman::Application
|
2012-04-14 22:51:02 +02:00
|
|
|
# Used to create a safe sandbox into which extensions and
|
|
|
|
# configuration can be included later without impacting
|
|
|
|
# other classes and instances.
|
|
|
|
#
|
|
|
|
# @return [Class]
|
|
|
|
def server(&block)
|
2012-04-23 04:01:00 +02:00
|
|
|
@@servercounter ||= 0
|
2012-04-14 22:51:02 +02:00
|
|
|
@@servercounter += 1
|
2012-04-14 23:04:10 +02:00
|
|
|
const_set("MiddlemanApplication#{@@servercounter}", Class.new(Middleman::Application))
|
2011-11-19 06:35:25 +01:00
|
|
|
end
|
2012-04-14 22:51:02 +02:00
|
|
|
|
|
|
|
# Creates a new Rack::Server
|
|
|
|
#
|
|
|
|
# @param [Hash] options to pass to Rack::Server.new
|
|
|
|
# @return [Rack::Server]
|
|
|
|
def start_server(options={})
|
|
|
|
opts = {
|
|
|
|
:Port => options[:port] || 4567,
|
|
|
|
:Host => options[:host] || "0.0.0.0",
|
|
|
|
:AccessLog => []
|
|
|
|
}
|
|
|
|
|
|
|
|
app_class = options[:app] ||= ::Middleman.server.inst
|
|
|
|
opts[:app] = app_class
|
|
|
|
|
2012-05-01 06:50:31 +02:00
|
|
|
require "webrick"
|
|
|
|
opts[:Logger] = WEBrick::Log::new("/dev/null", 7) if !options[:logging]
|
|
|
|
opts[:server] = 'webrick'
|
2012-04-14 22:51:02 +02:00
|
|
|
|
|
|
|
server = ::Rack::Server.new(opts)
|
|
|
|
server.start
|
|
|
|
server
|
2011-11-19 06:35:25 +01:00
|
|
|
end
|
|
|
|
end
|
2012-05-01 22:11:42 +02:00
|
|
|
end
|