Class: Middleman::Application

Inherits:
Object
  • Object
show all
Includes:
Hooks, Configuration::Global, CoreExtensions::Extensions, CoreExtensions::Routing
Defined in:
lib/middleman-core/application.rb

Constant Summary

Constants included from Hooks

Hooks::VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CoreExtensions::Routing

#page, #with_layout

Methods included from CoreExtensions::Extensions

registered

Methods included from Hooks

included, #run_hook

Methods included from Configuration::Global

#config, included, #method_missing, #respond_to?, #set

Constructor Details

#initialize(&block) ⇒ Application

Initialize the Middleman project



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/middleman-core/application.rb', line 152

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)

  if Object.const_defined?(:Encoding)
    Encoding.default_internal = config[:encoding]
    Encoding.default_external = config[:encoding]
  end

  # Evaluate a passed block if given
  instance_exec(&block) if block_given?

  config[:source] = ENV["MM_SOURCE"] if ENV["MM_SOURCE"]

  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Middleman::Configuration::Global

Class Method Details

.cacheMiddleman::Util::Cache

Shared cache instance

Returns:

  • (Middleman::Util::Cache)

    The cache



176
177
178
# File 'lib/middleman-core/application.rb', line 176

def self.cache
  @_cache ||= ::Tilt::Cache.new
end

.helpers(*extensions, &block) ⇒ void

This method returns an undefined value.

Mix-in helper methods. Accepts either a list of Modules and/or a block to be evaluated



41
42
43
44
# File 'lib/middleman-core/application.rb', line 41

def self.helpers(*extensions, &block)
  class_eval(&block)   if block_given?
  include(*extensions) if extensions.any?
end

.rootString

Root project directory (overwritten in middleman build/server)

Returns:



49
50
51
# File 'lib/middleman-core/application.rb', line 49

def self.root
  ENV["MM_ROOT"] || Dir.pwd
end

.root_pathObject

Pathname-addressed root



55
56
57
# File 'lib/middleman-core/application.rb', line 55

def self.root_path
  Pathname(root)
end

Instance Method Details

#build?Boolean

Whether we’re in build mode

Returns:

  • (Boolean)

    If we’re in build mode



187
# File 'lib/middleman-core/application.rb', line 187

def build?; config[:environment] == :build; end

#build_dirString

Where to build output files

Returns:



106
# File 'lib/middleman-core/application.rb', line 106

config.define_setting :build_dir,   "build", 'Where to build output files'

#css_dirString

Location of stylesheets within source. Used by Compass.

Returns:



86
# File 'lib/middleman-core/application.rb', line 86

config.define_setting :css_dir,     "stylesheets", 'Location of stylesheets within source'

#development?Boolean

Whether we’re in development mode

Returns:

  • (Boolean)

    If we’re in dev mode



183
# File 'lib/middleman-core/application.rb', line 183

def development?; config[:environment] == :development; end

#encodingString

Default string encoding for templates and output.

Returns:



118
# File 'lib/middleman-core/application.rb', line 118

config.define_setting :encoding, "utf-8", 'Default string encoding for templates and output'

#environmentString

Middleman environment. Defaults to :development, set to :build by the build process

Returns:



66
# File 'lib/middleman-core/application.rb', line 66

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'

#fonts_dirString

Location of fonts within source. Used by Compass.

Returns:



94
# File 'lib/middleman-core/application.rb', line 94

config.define_setting :fonts_dir,   "fonts", 'Location of fonts within source'

#full_path(path) ⇒ String

Expand a path to include the index file if it’s a directory

Parameters:

  • path (String)

    Request path

Returns:

  • (String)

    Path with index file if necessary



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/middleman-core/application.rb', line 212

def full_path(path)
  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{/$}, ''), config[:index_file])
    resource = sitemap.find_resource_by_destination_path(indexed_path)
  end

  if resource
    '/' + resource.destination_path
  else
    '/' + Middleman::Util.normalize_path(path)
  end
end

#http_prefixString

Default prefix for building paths. Used by HTML helpers and Compass.

Returns:



110
# File 'lib/middleman-core/application.rb', line 110

config.define_setting :http_prefix, "/", 'Default prefix for building paths'

#images_dirString

Location of images within source. Used by HTML helpers and Compass.

Returns:



90
# File 'lib/middleman-core/application.rb', line 90

config.define_setting :images_dir,  "images", 'Location of images within source'

#index_fileString

Which file should be used for directory indexes

Returns:



70
# File 'lib/middleman-core/application.rb', line 70

config.define_setting :index_file,  "index.html", 'Which file should be used for directory indexes'

#js_dirString

Location of javascripts within source.

Returns:



82
# File 'lib/middleman-core/application.rb', line 82

config.define_setting :js_dir,      "javascripts", 'Location of javascripts within source'

#layoutString, Symbold

Default layout name

Returns:



114
# File 'lib/middleman-core/application.rb', line 114

config.define_setting :layout, :_auto_layout, 'Default layout name'

#layouts_dirString

Location of layouts within source. Used by renderers.

Returns:



102
# File 'lib/middleman-core/application.rb', line 102

config.define_setting :layouts_dir, "layouts", 'Location of layouts within source'

#partials_dirString

Location of partials within source. Used by renderers.

Returns:



98
# File 'lib/middleman-core/application.rb', line 98

config.define_setting :partials_dir,   "", 'Location of partials within source'

#sourceString

Name of the source directory

Returns:



62
# File 'lib/middleman-core/application.rb', line 62

config.define_setting :source,      "source", 'Name of the source directory'

#source_dirString

The full path to the source directory

Returns:



192
193
194
# File 'lib/middleman-core/application.rb', line 192

def source_dir
  File.join(root, config[:source])
end

#strip_index_fileBoolean

Whether to strip the index file name off links to directory indexes

Returns:

  • (Boolean)


74
# File 'lib/middleman-core/application.rb', line 74

config.define_setting :strip_index_file, true, 'Whether to strip the index file name off links to directory indexes'

#to_sObject Also known as: inspect

Work around this bug: 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.



202
203
204
# File 'lib/middleman-core/application.rb', line 202

def to_s
  "#<Middleman::Application:0x#{object_id}>"
end

#trailing_slashBoolean

Whether to include a trailing slash when stripping the index file

Returns:

  • (Boolean)


78
# File 'lib/middleman-core/application.rb', line 78

config.define_setting :trailing_slash, true, 'Whether to include a trailing slash when stripping the index file'