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-05-25 01:31:21 +02:00
require " active_support/core_ext/integer/inflections "
2012-07-15 20:04:45 +02:00
require " active_support/core_ext/float/rounding "
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 "
2012-10-14 02:57:48 +02:00
require " middleman-core/configuration "
2012-06-09 20:30:22 +02:00
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-10-14 02:57:48 +02:00
# Global configuration
include Configuration :: Global
2012-04-14 22:51:02 +02:00
# Uses callbacks
include Hooks
2012-08-14 00:39:06 +02:00
2012-04-14 22:51:02 +02:00
# Before request hook
define_hook :before
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
# Ready (all loading and parsing of extensions complete) hook
define_hook :ready
2012-06-09 20:30:22 +02:00
2012-10-14 07:37:24 +02:00
# Mix-in helper methods. Accepts either a list of Modules
# and/or a block to be evaluated
# @return [void]
def self . helpers ( * extensions , & block )
class_eval ( & block ) if block_given?
include ( * extensions ) if extensions . any?
2012-04-14 22:51:02 +02:00
end
2012-05-01 22:11:42 +02:00
delegate :helpers , :to = > :" self.class "
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
# Root project directory (overwritten in middleman build/server)
# @return [String]
2012-10-14 07:37:24 +02:00
def self . root
ENV [ " MM_ROOT " ] || Dir . pwd
end
delegate :root , :to = > :" self.class "
2012-06-09 20:30:22 +02:00
2012-05-26 03:18:51 +02:00
# Pathname-addressed root
2012-10-14 02:57:48 +02:00
def self . root_path
2012-10-14 07:37:24 +02:00
Pathname ( root )
2012-05-26 03:18:51 +02:00
end
2012-10-14 02:57:48 +02:00
delegate :root_path , :to = > :" self.class "
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
# Name of the source directory
# @return [String]
2012-10-14 04:54:55 +02:00
config . define_setting :source , " source " , 'Name of the source directory'
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
# Middleman environment. Defaults to :development, set to :build by the build process
# @return [String]
2012-10-14 04:54:55 +02:00
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'
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
# Which file should be used for directory indexes
# @return [String]
2012-10-14 04:54:55 +02:00
config . define_setting :index_file , " index.html " , 'Which file should be used for directory indexes'
2011-11-18 04:56:55 +01:00
2012-07-13 03:27:05 +02:00
# Whether to strip the index file name off links to directory indexes
# @return [Boolean]
2012-10-14 04:54:55 +02:00
config . define_setting :strip_index_file , true , 'Whether to strip the index file name off links to directory indexes'
2012-07-13 03:27:05 +02:00
# Whether to include a trailing slash when stripping the index file
2012-07-12 04:33:33 +02:00
# @return [Boolean]
2012-10-14 04:54:55 +02:00
config . define_setting :trailing_slash , true , 'Whether to include a trailing slash when stripping the index file'
2012-07-12 04:33:33 +02:00
2012-05-24 23:29:29 +02:00
# Location of javascripts within source.
2012-04-14 22:51:02 +02:00
# @return [String]
2012-10-14 04:54:55 +02:00
config . define_setting :js_dir , " javascripts " , 'Location of javascripts within source'
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
# Location of stylesheets within source. Used by Compass.
# @return [String]
2012-10-14 04:54:55 +02:00
config . define_setting :css_dir , " stylesheets " , 'Location of stylesheets within source'
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
# Location of images within source. Used by HTML helpers and Compass.
# @return [String]
2012-10-14 04:54:55 +02:00
config . define_setting :images_dir , " images " , 'Location of images within source'
2011-11-18 04:56:55 +01:00
2012-06-09 20:30:22 +02:00
# Location of fonts within source. Used by Compass.
# @return [String]
2012-10-14 04:54:55 +02:00
config . define_setting :fonts_dir , " fonts " , 'Location of fonts within source'
2012-06-09 20:30:22 +02:00
2013-04-05 13:58:41 +02:00
# Location of partials within source. Used by renderers.
# @return [String]
config . define_setting :partials_dir , " " , 'Location of partials within source'
2012-04-14 22:51:02 +02:00
# Where to build output files
# @return [String]
2012-10-14 04:54:55 +02:00
config . define_setting :build_dir , " build " , 'Where to build output files'
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
# Default prefix for building paths. Used by HTML helpers and Compass.
# @return [String]
2012-10-14 04:54:55 +02:00
config . define_setting :http_prefix , " / " , 'Default prefix for building paths'
2011-11-24 06:59:53 +01:00
2012-04-14 22:51:02 +02:00
# Default layout name
# @return [String, Symbold]
2012-10-14 04:54:55 +02:00
config . define_setting :layout , :_auto_layout , 'Default layout name'
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
# Activate custom features and extensions
include Middleman :: CoreExtensions :: Extensions
2012-06-09 20:30:22 +02:00
2012-06-16 00:07:44 +02:00
# Manage Ruby string encodings
include Middleman :: CoreExtensions :: RubyEncoding
2012-05-01 22:11:42 +02:00
# Basic Rack Request Handling
2012-09-29 23:12:26 +02:00
register Middleman :: CoreExtensions :: Request
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
# Handle exceptions
register Middleman :: CoreExtensions :: ShowExceptions
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
# Add Builder Callbacks
register Middleman :: CoreExtensions :: Builder
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
# Add Watcher Callbacks
register Middleman :: CoreExtensions :: FileWatcher
2012-06-09 20:30:22 +02: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
2012-06-09 20:30:22 +02:00
2012-05-19 23:29:38 +02:00
# Parse YAML from templates. Must be before sitemap so sitemap
# extensions see updated frontmatter!
register Middleman :: CoreExtensions :: FrontMatter
2012-04-14 22:51:02 +02:00
# Sitemap
register Middleman :: Sitemap
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
# Setup external helpers
register Middleman :: CoreExtensions :: ExternalHelpers
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
# with_layout and page routing
register Middleman :: CoreExtensions :: Routing
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
# Initialize the Middleman project
def initialize ( & block )
# Clear the static class cache
cache . clear
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
# Setup the default values from calls to set before initialization
2013-02-11 01:43:37 +01:00
self . class . config . load_settings ( self . class . superclass . config . all_settings )
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
# Evaluate a passed block if given
instance_exec ( & block ) if block_given?
2012-06-09 20:30:22 +02:00
2012-10-14 04:54:55 +02:00
config [ :source ] = ENV [ " MM_SOURCE " ] if ENV [ " MM_SOURCE " ]
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
super
end
2012-06-09 20:30:22 +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
2012-10-14 04:54:55 +02:00
def development? ; config [ :environment ] == :development ; end
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
# Whether we're in build mode
# @return [Boolean] If we're in build mode
2012-10-14 04:54:55 +02:00
def build? ; config [ :environment ] == :build ; end
2011-11-18 04:56:55 +01:00
2012-09-16 08:36:00 +02:00
# The full path to the source directory
#
# @return [String]
def source_dir
2012-10-14 07:37:24 +02:00
File . join ( root , config [ :source ] )
2012-09-16 08:36:00 +02:00
end
2012-07-15 20:04:45 +02:00
delegate :logger , :instrument , :to = > :: Middleman :: Util
2012-06-09 20:30:22 +02:00
2012-07-03 08:39:30 +02:00
# Work around this bug: http://bugs.ruby-lang.org/issues/4521
# where Ruby will call to_s/inspect while printing exception
# messages, which can take a long time (minutes at full CPU)
# if the object is huge or has cyclic references, like this.
def to_s
2012-09-13 19:51:16 +02:00
" # <Middleman::Application:0x #{ object_id } > "
2012-07-03 08:39:30 +02:00
end
2013-04-03 07:19:17 +02:00
alias :inspect :to_s # Ruby 2.0 calls inspect for NoMethodError instead of to_s
2012-07-03 08:39:30 +02: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 )
2012-09-16 08:36:00 +02:00
resource = sitemap . find_resource_by_destination_path ( path )
if ! resource
# Try it with /index.html at the end
2012-10-14 04:54:55 +02:00
indexed_path = File . join ( path . sub ( %r{ /$ } , '' ) , config [ :index_file ] )
2012-09-16 08:36:00 +02:00
resource = sitemap . find_resource_by_destination_path ( indexed_path )
end
if resource
'/' + resource . destination_path
else
'/' + Middleman :: Util . normalize_path ( path )
2011-11-20 03:53:18 +01:00
end
2011-11-27 05:09:14 +01:00
end
2012-06-09 20:30:22 +02:00
2011-11-18 04:56:55 +01:00
end
2012-05-17 06:19:03 +02:00
end