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.
:: I18n . config . enforce_available_locales = false
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
2013-12-28 01:26:31 +01:00
require 'vendored-middleman-deps/hooks-0.2.0/lib/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'
2014-01-01 03:21:30 +01:00
require 'middleman-core/sitemap/store'
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
2014-01-01 03:21:30 +01:00
require 'middleman-core/config_context'
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
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
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
2013-12-28 01:26:31 +01:00
ENV [ 'MM_ROOT' ] || Dir . pwd
2012-10-14 07:37:24 +02:00
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]
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
2014-01-01 23:50:42 +01:00
include Middleman :: CoreExtensions :: Request
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
# Handle exceptions
2014-01-01 23:50:42 +01:00
include Middleman :: CoreExtensions :: ShowExceptions
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
# Add Watcher Callbacks
2014-01-01 23:50:42 +01:00
include Middleman :: CoreExtensions :: FileWatcher
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
# Activate Data package
2014-01-01 23:50:42 +01:00
include Middleman :: CoreExtensions :: Data
2012-04-21 08:38:59 +02:00
2012-04-14 22:51:02 +02:00
# Setup custom rendering
2014-01-01 23:50:42 +01:00
include 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
2014-01-01 03:21:30 +01:00
# Sitemap Config options and public api
2014-01-01 23:50:42 +01:00
include Middleman :: Sitemap
2012-06-09 20:30:22 +02:00
2012-04-14 22:51:02 +02:00
# Setup external helpers
2014-01-01 23:50:42 +01:00
include Middleman :: CoreExtensions :: ExternalHelpers
2012-06-09 20:30:22 +02:00
2014-01-01 03:21:30 +01:00
# Reference to Logger singleton
2013-12-31 23:41:17 +01:00
attr_reader :logger
2014-01-01 03:21:30 +01:00
# New container for config.rb commands
attr_reader :config_context
# Reference to Sitemap
attr_reader :sitemap
# Template cache
attr_reader :cache
2012-04-14 22:51:02 +02:00
# Initialize the Middleman project
def initialize ( & block )
2014-01-01 03:21:30 +01:00
@cache = :: Tilt :: Cache . new
2013-12-31 23:41:17 +01:00
@logger = :: Middleman :: Logger . singleton
2014-01-01 03:21:30 +01:00
@config_context = ConfigContext . new ( self )
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
2014-01-01 03:21:30 +01:00
# Initialize the Sitemap
@sitemap = :: Middleman :: Sitemap :: Store . new ( self )
2013-04-10 07:42:04 +02:00
if Object . const_defined? ( :Encoding )
Encoding . default_internal = config [ :encoding ]
Encoding . default_external = config [ :encoding ]
end
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
2014-01-01 23:50:42 +01:00
# Built-in extensions
activate :default_helpers
activate :lorem
if defined? ( Middleman :: CoreExtensions :: Compass )
activate :compass
end
# Evaluate a passed block if given
@config_context . instance_exec ( & block ) if block_given?
2012-04-14 22:51:02 +02:00
super
end
2012-06-09 20:30:22 +02:00
2014-01-01 03:21:30 +01:00
def add_to_config_context ( name , & func )
@config_context . define_singleton_method ( name , & func )
2012-04-14 22:51:02 +02:00
end
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
2013-12-31 23:41:17 +01: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
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
2011-11-18 04:56:55 +01:00
end
2012-05-17 06:19:03 +02:00
end