2011-11-24 06:59:53 +01:00
# Using Tilt for templating
2013-12-28 01:26:31 +01:00
require 'tilt'
2011-11-24 06:59:53 +01:00
2013-05-19 22:23:49 +02:00
# i18n Built-in
2013-12-28 01:26:31 +01:00
require 'i18n'
2013-05-19 22:23:49 +02:00
2013-12-26 01:52:29 +01:00
# Don't fail on invalid locale, that's not what our current
# users expect.
2014-02-19 03:30:29 +01:00
:: I18n . enforce_available_locales = false
2013-12-26 01:52:29 +01:00
2011-12-30 00:09:51 +01:00
# Use ActiveSupport JSON
2013-12-28 01:26:31 +01:00
require 'active_support/json'
require 'active_support/core_ext/integer/inflections'
2012-04-14 22:51:02 +02:00
# Simple callback library
2014-02-02 11:18:25 +01:00
require 'hooks'
2012-04-14 22:51:02 +02:00
2013-12-31 23:41:17 +01:00
# Our custom logger
require 'middleman-core/logger'
2013-12-28 01:26:31 +01:00
require 'middleman-core/sitemap'
2012-04-02 05:52:22 +02:00
2013-12-28 01:26:31 +01:00
require 'middleman-core/configuration'
require 'middleman-core/core_extensions'
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
2014-02-02 11:12:57 +01:00
include Hooks :: InstanceHooks
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
2014-02-05 06:03:24 +01:00
# Runs before the build is started
define_hook :before_build
2013-04-10 07:42:04 +02:00
# Runs after the build is finished
define_hook :after_build
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
2014-04-29 19:44:24 +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
2013-12-28 01:26:31 +01:00
ENV [ 'MM_ROOT' ] || Dir . pwd
2012-10-14 07:37:24 +02:00
end
2014-04-29 19:44:24 +02:00
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
2014-04-29 19:44:24 +02:00
delegate :root_path , to : :" self.class "
2012-06-09 20:30:22 +02:00
2015-03-02 03:17:22 +01:00
# Which port preview should start on.
# @return [Fixnum]
config . define_setting :port , 4567 , 'The preview server port'
2015-05-03 05:54:58 +02:00
# Whether to serve the preview server over HTTPS.
# @return [Boolean]
config . define_setting :https , false , 'Serve the preview server over SSL/TLS'
# The (optional) path to the SSL cert to use for the preview server.
# @return [String]
config . define_setting :ssl_certificate , nil , 'Path to an X.509 certificate to use for the preview server'
# The (optional) private key for the certificate in :ssl_certificate.
# @return [String]
config . define_setting :ssl_private_key , nil , " Path to an RSA private key for the preview server's certificate "
2012-04-14 22:51:02 +02:00
# Name of the source directory
# @return [String]
2013-12-28 01:26:31 +01: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]
2013-12-28 01:26:31 +01: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]
2013-12-28 01:26:31 +01: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]
2013-12-28 01:26:31 +01: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]
2013-12-28 01:26:31 +01: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]
2013-12-28 01:26:31 +01: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]
2013-12-28 01:26:31 +01:00
config . define_setting :partials_dir , '' , 'Location of partials within source'
2013-04-05 13:58:41 +02:00
2013-06-02 00:25:44 +02:00
# Location of layouts within source. Used by renderers.
# @return [String]
2013-12-28 01:26:31 +01:00
config . define_setting :layouts_dir , 'layouts' , 'Location of layouts within source'
2013-06-02 00:25:44 +02:00
2012-04-14 22:51:02 +02:00
# Where to build output files
# @return [String]
2013-12-28 01:26:31 +01: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]
2013-12-28 01:26:31 +01: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
2013-04-10 07:42:04 +02:00
# Default string encoding for templates and output.
# @return [String]
2013-12-28 01:26:31 +01:00
config . define_setting :encoding , 'utf-8' , 'Default string encoding for templates and output'
2013-04-10 07:42:04 +02:00
2013-11-20 03:10:26 +01:00
# Should Padrino include CRSF tag
# @return [Boolean]
config . define_setting :protect_from_csrf , false , 'Should Padrino include CRSF tag'
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-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 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
2013-04-10 07:42:04 +02:00
include Middleman :: CoreExtensions :: Routing
2012-06-09 20:30:22 +02:00
2014-04-08 01:47:06 +02:00
# Reference to Logger singleton
def logger
:: Middleman :: Logger . singleton
end
2013-12-31 23:41:17 +01: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
2013-04-10 07:42:04 +02:00
if Object . const_defined? ( :Encoding )
Encoding . default_internal = config [ :encoding ]
Encoding . default_external = config [ :encoding ]
end
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
2013-12-28 01:26:31 +01: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
2013-05-29 16:59:03 +02:00
@_cache || = :: Tilt :: Cache . new
2012-04-14 22:51:02 +02:00
end
2014-04-29 19:44:24 +02:00
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
2013-12-31 23:41:17 +01: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
2013-12-31 23:41:17 +01: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
2014-04-29 19:44:24 +02:00
delegate :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
2014-04-29 01:02:18 +02:00
alias_method :inspect , :to_s # Ruby 2.0 calls inspect for NoMethodError instead of to_s
2012-07-03 08:39:30 +02:00
2014-02-02 11:12:57 +01:00
# Hooks clones _hooks from the class to the instance.
# https://github.com/apotonick/hooks/blob/master/lib/hooks/instance_hooks.rb#L10
# Middleman expects the same list of hooks for class and instance hooks:
def _hooks
self . class . _hooks
end
2011-11-18 04:56:55 +01:00
end
2012-05-17 06:19:03 +02:00
end
2013-05-19 22:23:49 +02:00
2013-05-19 22:53:37 +02:00
Middleman :: CoreExtensions :: DefaultHelpers . activate
2013-05-19 22:23:49 +02:00
Middleman :: CoreExtensions :: Internationalization . register ( :i18n )
if defined? ( Middleman :: CoreExtensions :: Compass )
Middleman :: CoreExtensions :: Compass . activate
end
Middleman :: Extensions :: Lorem . activate