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-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-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-04-14 22:51:02 +02:00
|
|
|
class << self
|
2012-07-17 02:24:13 +02:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# 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
|
2012-06-09 20:30:22 +02:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Access class-wide defaults
|
|
|
|
#
|
|
|
|
# @private
|
|
|
|
# @return [Hash] Hash of default values
|
|
|
|
def defaults
|
|
|
|
@defaults ||= {}
|
|
|
|
end
|
2012-06-09 20:30:22 +02: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
|
2012-06-09 20:30:22 +02:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
@inst.set(key, value, &block) if @inst
|
|
|
|
end
|
|
|
|
end
|
2012-06-09 20:30:22 +02:00
|
|
|
|
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
|
|
|
# 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
|
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]
|
|
|
|
set :root, ENV["MM_ROOT"] || Dir.pwd
|
2012-06-09 20:30:22 +02:00
|
|
|
|
2012-05-26 03:18:51 +02:00
|
|
|
# Pathname-addressed root
|
|
|
|
def root_path
|
|
|
|
@_root_path ||= Pathname.new(root)
|
|
|
|
end
|
2012-06-09 20:30:22 +02:00
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Name of the source directory
|
|
|
|
# @return [String]
|
|
|
|
set :source, "source"
|
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]
|
|
|
|
set :environment, (ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development
|
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]
|
|
|
|
set :index_file, "index.html"
|
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]
|
|
|
|
set :strip_index_file, true
|
|
|
|
|
|
|
|
# Whether to include a trailing slash when stripping the index file
|
2012-07-12 04:33:33 +02:00
|
|
|
# @return [Boolean]
|
|
|
|
set :trailing_slash, true
|
|
|
|
|
2012-05-24 23:29:29 +02:00
|
|
|
# Location of javascripts within source.
|
2012-04-14 22:51:02 +02:00
|
|
|
# @return [String]
|
|
|
|
set :js_dir, "javascripts"
|
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]
|
|
|
|
set :css_dir, "stylesheets"
|
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]
|
|
|
|
set :images_dir, "images"
|
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]
|
|
|
|
set :fonts_dir, "fonts"
|
|
|
|
|
2012-04-14 22:51:02 +02:00
|
|
|
# Where to build output files
|
|
|
|
# @return [String]
|
|
|
|
set :build_dir, "build"
|
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]
|
|
|
|
set :http_prefix, "/"
|
2011-11-24 06:59:53 +01:00
|
|
|
|
2012-06-16 00:07:44 +02:00
|
|
|
# Default string encoding for templates and output.
|
|
|
|
# @return [String]
|
|
|
|
set :encoding, "utf-8"
|
|
|
|
|
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
|
|
|
# Default layout name
|
|
|
|
# @return [String, Symbold]
|
|
|
|
set :layout, :_auto_layout
|
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
|
|
|
|
self.class.superclass.defaults.each { |k,v| set(k,v) }
|
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-09-16 08:36:00 +02:00
|
|
|
set :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
|
|
|
|
def development?; 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
|
|
|
|
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
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-09-16 08:36:00 +02:00
|
|
|
# The full path to the source directory
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
def source_dir
|
|
|
|
File.join(root, source)
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
indexed_path = File.join(path.sub(%r{/$}, ''), 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
|
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
|