middleman/middleman-core/lib/middleman-core/application.rb

217 lines
6.8 KiB
Ruby
Raw Normal View History

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
# Use ActiveSupport JSON
require "active_support/json"
require "active_support/core_ext/integer/inflections"
require "active_support/core_ext/float/rounding"
# Simple callback library
require "middleman-core/vendor/hooks-0.2.0/lib/hooks"
require "middleman-core/sitemap"
require "middleman-core/core_extensions"
require "middleman-core/configuration"
2011-11-24 06:59:53 +01:00
# Core Middleman Class
module Middleman
class Application
# Global configuration
include Configuration::Global
# Uses callbacks
include Hooks
# Before request hook
define_hook :before
# Ready (all loading and parsing of extensions complete) hook
define_hook :ready
# 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?
end
delegate :helpers, :to => :"self.class"
# Root project directory (overwritten in middleman build/server)
# @return [String]
def self.root
ENV["MM_ROOT"] || Dir.pwd
end
delegate :root, :to => :"self.class"
# Pathname-addressed root
def self.root_path
Pathname(root)
end
delegate :root_path, :to => :"self.class"
# 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'
# 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'
# 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
# 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'
# Whether to include a trailing slash when stripping the index file
# @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-05-24 23:29:29 +02:00
# Location of javascripts within source.
# @return [String]
2012-10-14 04:54:55 +02:00
config.define_setting :js_dir, "javascripts", 'Location of javascripts within source'
# 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'
# 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
# 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'
# Location of partials within source. Used by renderers.
# @return [String]
config.define_setting :partials_dir, "", 'Location of partials within source'
# 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'
# 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
# Default layout name
# @return [String, Symbold]
2012-10-14 04:54:55 +02:00
config.define_setting :layout, :_auto_layout, 'Default layout name'
# Activate custom features and extensions
include Middleman::CoreExtensions::Extensions
# Manage Ruby string encodings
include Middleman::CoreExtensions::RubyEncoding
# Basic Rack Request Handling
register Middleman::CoreExtensions::Request
# Handle exceptions
register Middleman::CoreExtensions::ShowExceptions
# Add Builder Callbacks
register Middleman::CoreExtensions::Builder
# Add Watcher Callbacks
register Middleman::CoreExtensions::FileWatcher
# Activate Data package
register Middleman::CoreExtensions::Data
# Setup custom rendering
register Middleman::CoreExtensions::Rendering
# Parse YAML from templates. Must be before sitemap so sitemap
# extensions see updated frontmatter!
register Middleman::CoreExtensions::FrontMatter
# Sitemap
register Middleman::Sitemap
# Setup external helpers
register Middleman::CoreExtensions::ExternalHelpers
# with_layout and page routing
register Middleman::CoreExtensions::Routing
# Initialize the Middleman project
def initialize(&block)
# Clear the static class cache
cache.clear
# Setup the default values from calls to set before initialization
self.class.config.load_settings(self.class.superclass.config.all_settings)
# Evaluate a passed block if given
instance_exec(&block) if block_given?
2012-10-14 04:54:55 +02:00
config[:source] = ENV["MM_SOURCE"] if ENV["MM_SOURCE"]
super
end
# Shared cache instance
#
# @private
2012-04-14 23:06:49 +02:00
# @return [Middleman::Util::Cache] The cache
def self.cache
2012-04-14 23:06:49 +02:00
@_cache ||= ::Middleman::Util::Cache.new
end
delegate :cache, :to => :"self.class"
# 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
# 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
# The full path to the source directory
#
# @return [String]
def source_dir
File.join(root, config[:source])
end
delegate :logger, :instrument, :to => ::Middleman::Util
# 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
"#<Middleman::Application:0x#{object_id}>"
end
alias :inspect :to_s # Ruby 2.0 calls inspect for NoMethodError instead of to_s
# 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)
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])
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
end
2011-11-18 04:56:55 +01:00
end
end