Class: Middleman::Application

Inherits:
Object
  • Object
show all
Includes:
Hooks, CoreExtensions::Extensions, CoreExtensions::RubyEncoding
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::RubyEncoding

registered

Methods included from CoreExtensions::Extensions

registered

Methods included from Hooks

included, #run_hook

Constructor Details

#initialize(&block) ⇒ Application

Initialize the Middleman project



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/middleman-core/application.rb', line 176

def initialize(&block)
  # Clear the static class cache
  cache.clear

  # Setup the default values from calls to set before initialization
  self.class.superclass.defaults.each { |k,v| set(k,v) }

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

  set :source, ENV["MM_SOURCE"] if ENV["MM_SOURCE"]

  super
end

Class Method Details

.cacheMiddleman::Util::Cache

Shared cache instance

Returns:



195
196
197
# File 'lib/middleman-core/application.rb', line 195

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

.defaultsHash

Access class-wide defaults

Returns:

  • (Hash)

    Hash of default values



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

def defaults
  @defaults ||= {}
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



33
34
35
36
# File 'lib/middleman-core/application.rb', line 33

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

.set(key, value = nil, &block) ⇒ void

This method returns an undefined value.

Set class-wide defaults

Parameters:

  • key (Symbol)

    Unique key name

  • value (defaults to: nil)

    Default value



51
52
53
54
55
56
# File 'lib/middleman-core/application.rb', line 51

def set(key, value=nil, &block)
  @defaults ||= {}
  @defaults[key] = value

  @inst.set(key, value, &block) if @inst
end

Instance Method Details

#build?Boolean

Whether we’re in build mode

Returns:

  • (Boolean)

    If we’re in build mode



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

def build?; environment == :build; end

#build_dirString

Where to build output files

Returns:

  • (String)


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

set :build_dir,   "build"

#css_dirString

Location of stylesheets within source. Used by Compass.

Returns:

  • (String)


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

set :css_dir,     "stylesheets"

#development?Boolean

Whether we’re in development mode

Returns:

  • (Boolean)

    If we’re in dev mode



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

def development?; environment == :development; end

#encodingString

Default string encoding for templates and output.

Returns:

  • (String)


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

set :encoding,    "utf-8"

#environmentString

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

Returns:

  • (String)


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

set :environment, (ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development

#fonts_dirString

Location of fonts within source. Used by Compass.

Returns:

  • (String)


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

set :fonts_dir,   "fonts"

#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



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/middleman-core/application.rb', line 237

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{/$}, ''), 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:

  • (String)


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

set :http_prefix, "/"

#images_dirString

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

Returns:

  • (String)


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

set :images_dir,  "images"

#index_fileString

Which file should be used for directory indexes

Returns:

  • (String)


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

set :index_file,  "index.html"

#js_dirString

Location of javascripts within source.

Returns:

  • (String)


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

set :js_dir,      "javascripts"

#layoutString, Symbold

Default layout name

Returns:

  • (String, Symbold)


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

set :layout, :_auto_layout

#rootString

Root project directory (overwritten in middleman build/server)

Returns:

  • (String)


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

set :root,        ENV["MM_ROOT"] || Dir.pwd

#root_pathObject

Pathname-addressed root



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

def root_path
  @_root_path ||= Pathname.new(root)
end

#set(key, value = nil, &block) ⇒ void

This method returns an undefined value.

Set attributes (global variables)

Parameters:

  • key (Symbol)

    Name of the attribue

  • value (defaults to: nil)

    Attribute value



66
67
68
69
70
71
# File 'lib/middleman-core/application.rb', line 66

def set(key, value=nil, &block)
  setter = "#{key}=".to_sym
  self.class.send(:attr_accessor, key) if !respond_to?(setter)
  value = block if block_given?
  send(setter, value)
end

#settingsMiddleman::Application

Backwards compatibilty with old Sinatra template interface



211
212
213
# File 'lib/middleman-core/application.rb', line 211

def settings
  self
end

#show_exceptionsBoolean

Whether to catch and display exceptions

Returns:

  • (Boolean)


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

set :show_exceptions, true

#sourceString

Name of the source directory

Returns:

  • (String)


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

set :source,      "source"

#source_dirString

The full path to the source directory

Returns:

  • (String)


218
219
220
# File 'lib/middleman-core/application.rb', line 218

def source_dir
  File.join(root, source)
end

#strip_index_fileBoolean

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

Returns:

  • (Boolean)


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

set :strip_index_file, true

#to_sObject

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.



228
229
230
# File 'lib/middleman-core/application.rb', line 228

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

#trailing_slashBoolean

Whether to include a trailing slash when stripping the index file

Returns:

  • (Boolean)


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

set :trailing_slash, true