Class: Middleman::Extension
- Inherits:
-
Object
- Object
- Middleman::Extension
- Extended by:
- Forwardable
- Includes:
- Contracts
- Defined in:
- lib/middleman-core/extension.rb
Overview
Direct Known Subclasses
CoreExtensions::Collections::CollectionsExtension, CoreExtensions::Data, CoreExtensions::DefaultHelpers, CoreExtensions::ExternalHelpers, CoreExtensions::FileWatcher, CoreExtensions::FrontMatter, CoreExtensions::Internationalization, CoreExtensions::Routing, CoreExtensions::ShowExceptions, Middleman::Extensions::AssetHash, Middleman::Extensions::AssetHost, Middleman::Extensions::AutomaticAltTags, Middleman::Extensions::AutomaticImageSizes, Middleman::Extensions::CacheBuster, Middleman::Extensions::DirectoryIndexes, Middleman::Extensions::ExternalPipeline, Middleman::Extensions::Gzip, Middleman::Extensions::Lorem, Middleman::Extensions::MinifyCss, Middleman::Extensions::MinifyJavascript, Middleman::Extensions::RelativeAssets, Renderers::CoffeeScript, Renderers::ERb, Renderers::Haml, Renderers::Less, Renderers::Liquid, Renderers::Markdown, Renderers::Sass, Renderers::Slim, Renderers::Stylus, Sitemap::Extensions::Ignores, Sitemap::Extensions::Import, Sitemap::Extensions::MoveFile, Sitemap::Extensions::OnDisk, Sitemap::Extensions::Proxies, Sitemap::Extensions::Redirects, Sitemap::Extensions::RequestEndpoints
Constant Summary
Constants included from Contracts
Class Attribute Summary collapse
-
.defined_helpers ⇒ Array<Module>
private
A list of all the helper modules this extension provides.
-
.exposed_to_application ⇒ Hash<Symbol, Symbol>
private
A list of all the methods modules this extension exposes to app.
-
.exposed_to_config ⇒ Hash<Symbol, Symbol>
private
A list of all the methods modules this extension exposes to config.
-
.exposed_to_template ⇒ Array<Any>
private
A list of method generators.
-
.ext_name ⇒ Symbol
The name this extension is registered under.
-
.resource_list_manipulator_priority ⇒ Numeric
The priority for this extension's
manipulate_resource_list
method, if it has one. -
.supports_multiple_instances ⇒ Boolean
By default extensions can only be activated once in a project.
Instance Attribute Summary collapse
-
#app ⇒ Middleman::Application
readonly
The Middleman application instance.
-
#options ⇒ Middleman::Configuration::ConfigurationManager
readonly
Options for this extension instance.
Class Method Summary collapse
-
.activated_extension(instance)
private
Notify that a particular extension has been activated and run all registered Extension.after_extension_activated callbacks.
-
.after_extension_activated(name, &block)
Register to run a block after a named extension is activated.
-
.clear_after_extension_callbacks
private
Reset all Extension.after_extension_activated callbacks.
-
.config ⇒ Middleman::Configuration::ConfigurationManager
private
The defined options for this extension.
-
.expose_to_application(*symbols)
Takes a method within this extension and exposes it globally on the main
app
instance. -
.expose_to_config(*symbols)
Takes a method within this extension and exposes it inside the scope of the config.rb sandbox.
-
.expose_to_template(*symbols)
Takes a method within this extension and exposes it inside the scope of the templating engine.
-
.helpers(*modules, &block)
Declare helpers to be added the global Middleman application.
-
.option(key, default = nil, description = nil, options = {}) ⇒ Object
Add an option to this extension.
-
.resources(*generators) ⇒ Object
Short-hand for simple Sitemap manipulation.
Instance Method Summary collapse
- #add_exposed_to_context(context) ⇒ Object
-
#after_build ⇒ Object
Respond to the
after_build
event. -
#after_configuration ⇒ Object
Respond to the
after_configuration
event. -
#after_extension_activated(name, &block)
Register to run a block after a named extension is activated.
-
#before_build ⇒ Object
Respond to the
before_build
event. -
#before_configuration ⇒ Object
Respond to the
before_configuration
event. -
#initialize(app, options_hash = {}) {|options| ... } ⇒ Extension
constructor
Extensions are instantiated when they are activated.
-
#manipulate_resource_list(resources) ⇒ Array<Sitemap::Resource>
Manipulate the resource list by transforming or adding Sitemap::Resources.
-
#ready ⇒ Object
Respond to the
ready
event.
Methods included from Contracts
Constructor Details
#initialize(app, options_hash = {}) {|options| ... } ⇒ Extension
Extensions are instantiated when they are activated.
289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/middleman-core/extension.rb', line 289 def initialize(app, ={}, &block) @_helpers = [] @app = app expose_methods (, &block) # Bind app hooks to local methods bind_before_configuration bind_after_configuration bind_before_build bind_after_build bind_ready end |
Class Attribute Details
.defined_helpers ⇒ Array<Module>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a list of all the helper modules this extension provides. Set these using #helpers.
83 |
# File 'lib/middleman-core/extension.rb', line 83 class_attribute :defined_helpers, instance_reader: false, instance_writer: false |
.exposed_to_application ⇒ Hash<Symbol, Symbol>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a list of all the methods modules this extension exposes to app. Set these using #expose_to_application.
89 |
# File 'lib/middleman-core/extension.rb', line 89 class_attribute :exposed_to_application, instance_reader: false, instance_writer: false |
.exposed_to_config ⇒ Hash<Symbol, Symbol>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a list of all the methods modules this extension exposes to config. Set these using #expose_to_config.
95 |
# File 'lib/middleman-core/extension.rb', line 95 class_attribute :exposed_to_config, instance_reader: false, instance_writer: false |
.exposed_to_template ⇒ Array<Any>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a list of method generators.
101 |
# File 'lib/middleman-core/extension.rb', line 101 class_attribute :exposed_to_template, instance_reader: false, instance_writer: false |
.ext_name ⇒ Symbol
Returns the name this extension is registered under. This is the symbol used to activate the extension.
112 |
# File 'lib/middleman-core/extension.rb', line 112 class_attribute :ext_name, instance_reader: false, instance_writer: false |
.resource_list_manipulator_priority ⇒ Numeric
Returns the priority for this extension's manipulate_resource_list
method, if it has one.
118 |
# File 'lib/middleman-core/extension.rb', line 118 class_attribute :resource_list_manipulator_priority, instance_reader: false, instance_writer: false |
.supports_multiple_instances ⇒ Boolean
By default extensions can only be activated once in a project. This is an advanced option.
77 |
# File 'lib/middleman-core/extension.rb', line 77 class_attribute :supports_multiple_instances, instance_reader: false, instance_writer: false |
Instance Attribute Details
#app ⇒ Middleman::Application (readonly)
Returns the Middleman application instance.
275 276 277 |
# File 'lib/middleman-core/extension.rb', line 275 def app @app end |
#options ⇒ Middleman::Configuration::ConfigurationManager (readonly)
Returns options for this extension instance.
272 273 274 |
# File 'lib/middleman-core/extension.rb', line 272 def @options end |
Class Method Details
.activated_extension(instance)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Notify that a particular extension has been activated and run all registered after_extension_activated callbacks.
262 263 264 265 266 267 268 |
# File 'lib/middleman-core/extension.rb', line 262 def activated_extension(instance) name = instance.class.ext_name return unless @_extension_activation_callbacks && @_extension_activation_callbacks.key?(name) @_extension_activation_callbacks[name].each do |block| block.arity == 1 ? block.call(instance) : block.call end end |
.after_extension_activated(name, &block)
This method returns an undefined value.
Register to run a block after a named extension is activated.
251 252 253 254 255 |
# File 'lib/middleman-core/extension.rb', line 251 def after_extension_activated(name, &block) @_extension_activation_callbacks ||= {} @_extension_activation_callbacks[name] ||= [] @_extension_activation_callbacks[name] << block if block_given? end |
.clear_after_extension_callbacks
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Reset all after_extension_activated callbacks.
243 244 245 |
# File 'lib/middleman-core/extension.rb', line 243 def clear_after_extension_callbacks @_extension_activation_callbacks = {} end |
.config ⇒ Middleman::Configuration::ConfigurationManager
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns The defined options for this extension.
123 124 125 |
# File 'lib/middleman-core/extension.rb', line 123 def config @_config ||= ::Middleman::Configuration::ConfigurationManager.new end |
.expose_to_application(*symbols)
This method returns an undefined value.
Takes a method within this extension and exposes it globally
on the main app
instance. Used for very low-level extensions
which many other extensions depend upon. Such as Data and
File watching.
188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/middleman-core/extension.rb', line 188 def expose_to_application(*symbols) self.exposed_to_application ||= {} if symbols.first && symbols.first.is_a?(Hash) self.exposed_to_application.merge!(symbols.first) elsif symbols.is_a? Array symbols.each do |sym| self.exposed_to_application[sym] = sym end end end |
.expose_to_config(*symbols)
This method returns an undefined value.
Takes a method within this extension and exposes it inside the scope of the config.rb sandbox.
208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/middleman-core/extension.rb', line 208 def expose_to_config(*symbols) self.exposed_to_config ||= {} if symbols.first && symbols.first.is_a?(Hash) self.exposed_to_config.merge!(symbols.first) elsif symbols.is_a? Array symbols.each do |sym| self.exposed_to_config[sym] = sym end end end |
.expose_to_template(*symbols)
This method returns an undefined value.
Takes a method within this extension and exposes it inside the scope
of the templating engine. Like helpers
, but scoped.
228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/middleman-core/extension.rb', line 228 def expose_to_template(*symbols) self.exposed_to_template ||= {} if symbols.first && symbols.first.is_a?(Hash) self.exposed_to_template.merge!(symbols.first) elsif symbols.is_a? Array symbols.each do |sym| self.exposed_to_template[sym] = sym end end end |
.helpers(*modules, &block)
This method returns an undefined value.
Declare helpers to be added the global Middleman application. This accepts either a list of modules to add on behalf of this extension, or a block whose contents will all be used as helpers in a new module.
166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/middleman-core/extension.rb', line 166 def helpers(*modules, &block) self.defined_helpers ||= [] if block_given? mod = Module.new mod.module_eval(&block) modules = [mod] end self.defined_helpers += modules end |
.option(key, default = nil, description = nil, options = {}) ⇒ Object
Add an option to this extension.
134 135 136 |
# File 'lib/middleman-core/extension.rb', line 134 def option(key, default=nil, description=nil, ={}) config.define_setting(key, default, description, ) end |
.resources(*generators) ⇒ Object
Short-hand for simple Sitemap manipulation
146 147 148 149 |
# File 'lib/middleman-core/extension.rb', line 146 def resources(*generators) self.resources_generators ||= [] self.resources_generators += generators end |
Instance Method Details
#add_exposed_to_context(context) ⇒ Object
335 336 337 338 339 |
# File 'lib/middleman-core/extension.rb', line 335 def add_exposed_to_context(context) (self.class.exposed_to_template || {}).each do |k, v| context.define_singleton_method(k, &method(v)) end end |
#after_build ⇒ Object
Respond to the after_build
event.
If an after_build
method is implemented, that method will be run after the builder runs.
|
# File 'lib/middleman-core/extension.rb', line 317
|
#after_configuration ⇒ Object
Respond to the after_configuration
event.
If an after_configuration
method is implemented, that method will be run before config.rb
is run.
|
# File 'lib/middleman-core/extension.rb', line 309
|
#after_extension_activated(name, &block)
This method returns an undefined value.
Register to run a block after a named extension is activated.
282 |
# File 'lib/middleman-core/extension.rb', line 282 def_delegator :"::Middleman::Extension", :after_extension_activated |
#before_build ⇒ Object
Respond to the before_build
event.
If an before_build
method is implemented, that method will be run before the builder runs.
|
# File 'lib/middleman-core/extension.rb', line 313
|
#before_configuration ⇒ Object
Because most extensions are activated from within config.rb
, they will not run any before_configuration
hook.
Respond to the before_configuration
event.
If a before_configuration
method is implemented, that method will be run before config.rb
is run.
|
# File 'lib/middleman-core/extension.rb', line 304
|
#manipulate_resource_list(resources) ⇒ Array<Sitemap::Resource>
This method must return the full set of resources, because its return value will be used as the new sitemap.
Manipulate the resource list by transforming or adding Sitemap::Resources. Sitemap manipulation is a powerful way of interacting with a project, since it can modify each Sitemap::Resource or generate new Sitemap::Resources. This method is used in a pipeline where each sitemap manipulator is run in turn, with each one being fed the output of the previous manipulator. See the source of built-in Middleman extensions like Middleman::Extensions::DirectoryIndexes and Middleman::Extensions::AssetHash for examples of how to use this.
335 336 337 338 339 |
# File 'lib/middleman-core/extension.rb', line 335 def add_exposed_to_context(context) (self.class.exposed_to_template || {}).each do |k, v| context.define_singleton_method(k, &method(v)) end end |
#ready ⇒ Object
Respond to the ready
event.
If an ready
method is implemented, that method will be run after the app has finished booting up.
|
# File 'lib/middleman-core/extension.rb', line 321
|