From a6c37f3dd3516e56e82c85e32f131f20ea035598 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 29 Mar 2014 14:29:42 -0700 Subject: [PATCH 1/3] Clean up extensions a bit. Removes newest form of registering extensions, more consistently sets and uses an extension's ext_name, and makes a lot of things errors instead of just log messages in hopes that people can't get too far with a messed-up config. --- .../fixtures/feature-params-app/config.rb | 2 +- .../fixtures/v4-extension-callbacks/config.rb | 6 +- .../lib/middleman-core/application.rb | 4 +- .../lib/middleman-core/core_extensions.rb | 81 +++++++++++++------ .../middleman-core/core_extensions/compass.rb | 2 - .../core_extensions/default_helpers.rb | 2 - .../core_extensions/extensions.rb | 29 +++---- .../middleman-core/core_extensions/i18n.rb | 2 - .../lib/middleman-core/extension.rb | 12 +-- .../lib/middleman-core/extensions.rb | 40 +++++++-- .../lib/middleman-core/extensions/lorem.rb | 2 - 11 files changed, 107 insertions(+), 75 deletions(-) diff --git a/middleman-core/fixtures/feature-params-app/config.rb b/middleman-core/fixtures/feature-params-app/config.rb index fda7e5a0..7e2bc2ef 100644 --- a/middleman-core/fixtures/feature-params-app/config.rb +++ b/middleman-core/fixtures/feature-params-app/config.rb @@ -11,6 +11,6 @@ class ExtensionA < ::Middleman::Extension option :hola, '', '' end -ExtensionA.register +Middleman::Extensions.register :extension_a, ExtensionA activate :extension_a, :hello => "world", :hola => "mundo" diff --git a/middleman-core/fixtures/v4-extension-callbacks/config.rb b/middleman-core/fixtures/v4-extension-callbacks/config.rb index c9c1c5d4..f62745bc 100644 --- a/middleman-core/fixtures/v4-extension-callbacks/config.rb +++ b/middleman-core/fixtures/v4-extension-callbacks/config.rb @@ -16,7 +16,7 @@ class ExtensionOne < ::Middleman::Extension end end -ExtensionOne.register +Middleman::Extensions.register :extension_one, ExtensionOne class ExtensionTwo < ::Middleman::Extension helpers do @@ -36,7 +36,7 @@ class ExtensionTwo < ::Middleman::Extension end end -ExtensionTwo.register +Middleman::Extensions.register :extension_two, ExtensionTwo activate :extension_one -activate :extension_two \ No newline at end of file +activate :extension_two diff --git a/middleman-core/lib/middleman-core/application.rb b/middleman-core/lib/middleman-core/application.rb index 83cb0b75..9a19ba93 100644 --- a/middleman-core/lib/middleman-core/application.rb +++ b/middleman-core/lib/middleman-core/application.rb @@ -196,8 +196,10 @@ module Middleman activate :default_helpers activate :lorem - if defined?(Middleman::CoreExtensions::Compass) + begin activate :compass + rescue LoadError + # Compass is not available, don't complain about it end # Evaluate a passed block if given diff --git a/middleman-core/lib/middleman-core/core_extensions.rb b/middleman-core/lib/middleman-core/core_extensions.rb index b241c9c5..d61e90a7 100644 --- a/middleman-core/lib/middleman-core/core_extensions.rb +++ b/middleman-core/lib/middleman-core/core_extensions.rb @@ -27,68 +27,97 @@ require 'middleman-core/core_extensions/routing' require 'middleman-core/core_extensions/show_exceptions' # Setup default helpers -require 'middleman-core/core_extensions/default_helpers' - -require 'middleman-core/core_extensions/i18n' +Middleman::Extensions.register :default_helpers do + require 'middleman-core/core_extensions/default_helpers' + Middleman::CoreExtensions::DefaultHelpers +end # Compass framework -begin +Middleman::Extensions.register :compass do require 'middleman-core/core_extensions/compass' -rescue LoadError + Middleman::CoreExtensions::Compass end ### # Setup Optional Extensions ### +Middleman::Extensions.register :i18n do + require 'middleman-core/core_extensions/i18n' + Middleman::CoreExtensions::Internationalization +end + # CacheBuster adds a query string to assets in dynamic templates to # avoid browser caches failing to update to your new content. -require 'middleman-core/extensions/cache_buster' -Middleman::Extensions::CacheBuster.register +Middleman::Extensions.register :cache_buster do + require 'middleman-core/extensions/cache_buster' + Middleman::Extensions::CacheBuster +end # RelativeAssets allow any asset path in dynamic templates to be either # relative to the root of the project or use an absolute URL. -require 'middleman-core/extensions/relative_assets' -Middleman::Extensions::RelativeAssets.register +Middleman::Extensions.register :relative_assets do + require 'middleman-core/extensions/relative_assets' + Middleman::Extensions::RelativeAssets +end # AssetHost allows you to setup multiple domains to host your static # assets. Calls to asset paths in dynamic templates will then rotate # through each of the asset servers to better spread the load. -require 'middleman-core/extensions/asset_host' -Middleman::Extensions::AssetHost.register +Middleman::Extensions.register :asset_host do + require 'middleman-core/extensions/asset_host' + Middleman::Extensions::AssetHost +end # MinifyCss compresses CSS -require 'middleman-core/extensions/minify_css' -Middleman::Extensions::MinifyCss.register +Middleman::Extensions.register :minify_css do + require 'middleman-core/extensions/minify_css' + Middleman::Extensions::MinifyCss +end # MinifyJavascript compresses JS -require 'middleman-core/extensions/minify_javascript' -Middleman::Extensions::MinifyJavascript.register +Middleman::Extensions.register :minify_javascript do + require 'middleman-core/extensions/minify_javascript' + Middleman::Extensions::MinifyJavascript +end # GZIP assets and pages during build -require 'middleman-core/extensions/gzip' -Middleman::Extensions::Gzip.register +Middleman::Extensions.register :gzip do + require 'middleman-core/extensions/gzip' + Middleman::Extensions::Gzip +end # AssetHash appends a hash of the file contents to the assets filename # to avoid browser caches failing to update to your new content. -require 'middleman-core/extensions/asset_hash' -Middleman::Extensions::AssetHash.register +Middleman::Extensions.register :asset_hash do + require 'middleman-core/extensions/asset_hash' + Middleman::Extensions::AssetHash +end # Provide Apache-style index.html files for directories -require 'middleman-core/extensions/directory_indexes' -Middleman::Extensions::DirectoryIndexes.register +Middleman::Extensions.register :directory_indexes do + require 'middleman-core/extensions/directory_indexes' + Middleman::Extensions::DirectoryIndexes +end # Lorem provides a handful of helpful prototyping methods to generate # words, paragraphs, fake images, names and email addresses. -require 'middleman-core/extensions/lorem' +Middleman::Extensions.register :lorem do + require 'middleman-core/extensions/lorem' + Middleman::Extensions::Lorem +end # AutomaticImageSizes inspects the images used in your dynamic templates # and automatically adds width and height attributes to their HTML # elements. -require 'middleman-core/extensions/automatic_image_sizes' -Middleman::Extensions::AutomaticImageSizes.register +Middleman::Extensions.register :automatic_image_sizes do + require 'middleman-core/extensions/automatic_image_sizes' + Middleman::Extensions::AutomaticImageSizes +end # AutomaticAltTags uses the file name of the `image_tag` to generate # a default `:alt` value. -require 'middleman-core/extensions/automatic_alt_tags' -Middleman::Extensions::AutomaticAltTags.register +Middleman::Extensions.register :automatic_alt_tags do + require 'middleman-core/extensions/automatic_alt_tags' + Middleman::Extensions::AutomaticAltTags +end diff --git a/middleman-core/lib/middleman-core/core_extensions/compass.rb b/middleman-core/lib/middleman-core/core_extensions/compass.rb index 914169c6..75b04ab3 100644 --- a/middleman-core/lib/middleman-core/core_extensions/compass.rb +++ b/middleman-core/lib/middleman-core/core_extensions/compass.rb @@ -73,5 +73,3 @@ class Middleman::CoreExtensions::Compass < ::Middleman::Extension end end end - -Middleman::CoreExtensions::Compass.register diff --git a/middleman-core/lib/middleman-core/core_extensions/default_helpers.rb b/middleman-core/lib/middleman-core/core_extensions/default_helpers.rb index 288c99b4..1e76674f 100644 --- a/middleman-core/lib/middleman-core/core_extensions/default_helpers.rb +++ b/middleman-core/lib/middleman-core/core_extensions/default_helpers.rb @@ -259,5 +259,3 @@ class Middleman::CoreExtensions::DefaultHelpers < ::Middleman::Extension end end end - -Middleman::CoreExtensions::DefaultHelpers.register diff --git a/middleman-core/lib/middleman-core/core_extensions/extensions.rb b/middleman-core/lib/middleman-core/core_extensions/extensions.rb index 32404273..da366272 100644 --- a/middleman-core/lib/middleman-core/core_extensions/extensions.rb +++ b/middleman-core/lib/middleman-core/core_extensions/extensions.rb @@ -88,26 +88,19 @@ module Middleman # @param [Symbol, Module] ext Which extension to activate # @return [void] def activate(ext, options={}, &block) - if extension = ::Middleman::Extensions.load(ext) - if extension.ancestors.include?(::Middleman::Extension) - logger.debug "== Activating: #{ext}" + extension = ::Middleman::Extensions.load(ext) + logger.debug "== Activating: #{ext}" - if extension.supports_multiple_instances? - extensions[ext] ||= {} - key = "instance_#{extensions[ext].keys.length}" - extensions[ext][key] = extension.new(self.class, options, &block) - else - if extensions[ext] - logger.error "== #{ext} already activated." - else - extensions[ext] = extension.new(self.class, options, &block) - end - end - else - logger.error "!! Tried to activate old-style extension: #{ext}" - end + if extension.supports_multiple_instances? + extensions[ext] ||= {} + key = "instance_#{extensions[ext].keys.length}" + extensions[ext][key] = extension.new(self.class, options, &block) else - logger.error "!! Unknown Extension: #{ext}" + if extensions[ext] + raise "#{ext} has already been activated and cannot be re-activated." + else + extensions[ext] = extension.new(self.class, options, &block) + end end end diff --git a/middleman-core/lib/middleman-core/core_extensions/i18n.rb b/middleman-core/lib/middleman-core/core_extensions/i18n.rb index 8047462b..f500d953 100644 --- a/middleman-core/lib/middleman-core/core_extensions/i18n.rb +++ b/middleman-core/lib/middleman-core/core_extensions/i18n.rb @@ -201,5 +201,3 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension end end end - -Middleman::CoreExtensions::Internationalization.register(:i18n) diff --git a/middleman-core/lib/middleman-core/extension.rb b/middleman-core/lib/middleman-core/extension.rb index 25a8a22c..6219f258 100644 --- a/middleman-core/lib/middleman-core/extension.rb +++ b/middleman-core/lib/middleman-core/extension.rb @@ -33,14 +33,6 @@ module Middleman self.defined_helpers += m end - def extension_name - self.ext_name || self.name.underscore.split('/').last.to_sym - end - - def register(n=self.extension_name) - ::Middleman::Extensions.register(n, self) - end - def activate new(::Middleman::Application) end @@ -56,7 +48,7 @@ module Middleman end def activated_extension(instance) - name = instance.class.extension_name + name = instance.class.ext_name return unless @_extension_activation_callbacks && @_extension_activation_callbacks[name] @_extension_activation_callbacks[name].each do |block| block.arity == 1 ? block.call(instance) : block.call() @@ -136,7 +128,7 @@ module Middleman end if ext.respond_to?(:manipulate_resource_list) - ext.app.sitemap.register_resource_list_manipulator(ext.class.extension_name, ext) + ext.app.sitemap.register_resource_list_manipulator(ext.class.ext_name, ext) end end end diff --git a/middleman-core/lib/middleman-core/extensions.rb b/middleman-core/lib/middleman-core/extensions.rb index 0c6fe6d7..c147230d 100644 --- a/middleman-core/lib/middleman-core/extensions.rb +++ b/middleman-core/lib/middleman-core/extensions.rb @@ -13,7 +13,16 @@ module Middleman # activate :my_extension # # Provide your extension module either as the namespace - # parameter, or return it from the block: + # parameter: + # + # Middleman::Extensions.register(:my_extension, MyExtension) + # + # Or return it from a block: + # + # Middleman::Extensions.register(:my_extension) do + # require 'my_extension' + # MyExtension + # end # # @param [Symbol] name The name of the extension # @param [Module] namespace The extension module @@ -22,34 +31,49 @@ module Middleman # you the ability to require other files only when the # extension is activated. def register(name, namespace=nil, &block) - # If we've already got a matching extension that passed the - # version check, bail out. - return if registered.has_key?(name.to_sym) && !registered[name.to_sym].is_a?(String) + # If we've already got an extension registered under this name, bail out + if registered.has_key?(name.to_sym) + raise "There is already an extension registered with the name '#{name}'" + end registered[name.to_sym] = if block_given? block - elsif namespace + elsif namespace && namespace.ancestors.include?(::Middleman::Extension) namespace + else + raise "You must provide a Middleman::Extension or a block that returns a Middleman::Extension" end end # Load an extension by name, evaluating block definition if necessary. def load(name) name = name.to_sym - return nil unless registered.has_key?(name) + + unless registered.has_key?(name) + raise "Unknown Extension: #{name}. Check the name and make sure you have referenced the extension's gem in your Gemfile." + end extension = registered[name] if extension.is_a?(Proc) - extension = extension.call() || nil + extension = extension.call() registered[name] = extension end + if !extension.ancestors.include?(::Middleman::Extension) + raise "Tried to activate old-style extension: #{name}. They are no longer supported." + end + + # Set the extension's name to whatever it was registered as. + extension.ext_name = name + extension end end end - # Where to look in gems for extensions to auto-register + # Where to look in gems for extensions to auto-register. Since most extensions are + # called out in a Gemfile, this is really only useful for template extensions that get + # used by "middleman init". EXTENSION_FILE = File.join('lib', 'middleman_extension.rb') unless const_defined?(:EXTENSION_FILE) class << self diff --git a/middleman-core/lib/middleman-core/extensions/lorem.rb b/middleman-core/lib/middleman-core/extensions/lorem.rb index a69454c6..d13abeeb 100644 --- a/middleman-core/lib/middleman-core/extensions/lorem.rb +++ b/middleman-core/lib/middleman-core/extensions/lorem.rb @@ -174,5 +174,3 @@ class Middleman::Extensions::Lorem < ::Middleman::Extension end end end - -Middleman::Extensions::Lorem.register \ No newline at end of file From abeee38126ff908589626e4805b6d6995e2f2acc Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 29 Mar 2014 17:17:00 -0700 Subject: [PATCH 2/3] Remove yet another way to register extensions and register/activate FrontMatter like a normal extension. --- middleman-core/lib/middleman-core/application.rb | 8 ++++---- .../lib/middleman-core/core_extensions.rb | 5 ++++- .../middleman-core/core_extensions/extensions.rb | 13 ------------- middleman-core/lib/middleman-core/sitemap.rb | 1 - 4 files changed, 8 insertions(+), 19 deletions(-) diff --git a/middleman-core/lib/middleman-core/application.rb b/middleman-core/lib/middleman-core/application.rb index 9a19ba93..8c2c9352 100644 --- a/middleman-core/lib/middleman-core/application.rb +++ b/middleman-core/lib/middleman-core/application.rb @@ -142,10 +142,6 @@ module Middleman # Setup custom rendering include Middleman::CoreExtensions::Rendering - # Parse YAML from templates. Must be before sitemap so sitemap - # extensions see updated frontmatter! - register Middleman::CoreExtensions::FrontMatter - # Sitemap Config options and public api include Middleman::Sitemap @@ -182,6 +178,10 @@ module Middleman # Setup the default values from calls to set before initialization self.class.config.load_settings(self.class.superclass.config.all_settings) + # Parse YAML from templates. Must be before sitemap so sitemap + # extensions see updated frontmatter! + activate :front_matter + # Initialize the Sitemap @sitemap = ::Middleman::Sitemap::Store.new(self) diff --git a/middleman-core/lib/middleman-core/core_extensions.rb b/middleman-core/lib/middleman-core/core_extensions.rb index d61e90a7..340ca2fd 100644 --- a/middleman-core/lib/middleman-core/core_extensions.rb +++ b/middleman-core/lib/middleman-core/core_extensions.rb @@ -12,7 +12,10 @@ require 'middleman-core/core_extensions/extensions' require 'middleman-core/core_extensions/data' # Parse YAML from templates -require 'middleman-core/core_extensions/front_matter' +Middleman::Extensions.register :front_matter do + require 'middleman-core/core_extensions/front_matter' + Middleman::CoreExtensions::FrontMatter +end # External helpers looks in the helpers/ folder for helper modules require 'middleman-core/core_extensions/external_helpers' diff --git a/middleman-core/lib/middleman-core/core_extensions/extensions.rb b/middleman-core/lib/middleman-core/core_extensions/extensions.rb index da366272..cdb0ed87 100644 --- a/middleman-core/lib/middleman-core/core_extensions/extensions.rb +++ b/middleman-core/lib/middleman-core/core_extensions/extensions.rb @@ -62,19 +62,6 @@ module Middleman def configure(env, &block) send("#{env}_config", &block) end - - # Register a new extension - # - # @param [Module] extension Extension modules to register - # @param [Hash] options Per-extension options hash - # @return [void] - def register(extension, options={}, &block) - if extension.ancestors.include?(::Middleman::Extension) - extension.new(self, options, &block) - else - $stderr.puts "!! Tried to register old-style extension: #{extension}" - end - end end # Instance methods diff --git a/middleman-core/lib/middleman-core/sitemap.rb b/middleman-core/lib/middleman-core/sitemap.rb index fbda5b2d..658dc7c5 100644 --- a/middleman-core/lib/middleman-core/sitemap.rb +++ b/middleman-core/lib/middleman-core/sitemap.rb @@ -1,6 +1,5 @@ # Core Sitemap Extensions module Middleman - module Sitemap # Setup Extension From c1f7299cfd8a52bda3616b8b2f2b178b7d95037c Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 29 Mar 2014 17:21:49 -0700 Subject: [PATCH 3/3] Fix some references to extensions[:frontmatter] --- .../lib/middleman-core/core_extensions/front_matter.rb | 5 ++--- middleman-core/lib/middleman-core/file_renderer.rb | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb index cffd234f..41ec27ac 100644 --- a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb +++ b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb @@ -32,7 +32,6 @@ module Middleman::CoreExtensions end def after_configuration - app.extensions[:frontmatter] = self app.ignore %r{\.frontmatter$} ::Middleman::Sitemap::Resource.send :include, ResourceInstanceMethods @@ -45,7 +44,7 @@ module Middleman::CoreExtensions [:layout, :layout_engine].each do |opt| data[opt] = fmdata[opt] unless fmdata[opt].nil? end - + if fmdata[:renderer_options] data[:renderer_options] = {} fmdata[:renderer_options].each do |k, v| @@ -71,7 +70,7 @@ module Middleman::CoreExtensions # @private # @return [Hash] def raw_data - app.extensions[:frontmatter].data(source_file).first + app.extensions[:front_matter].data(source_file).first end # This page's frontmatter diff --git a/middleman-core/lib/middleman-core/file_renderer.rb b/middleman-core/lib/middleman-core/file_renderer.rb index 0393ad83..6d3e4dd1 100644 --- a/middleman-core/lib/middleman-core/file_renderer.rb +++ b/middleman-core/lib/middleman-core/file_renderer.rb @@ -84,8 +84,8 @@ module Middleman # @param [String] path # @return [String] def get_template_data_for_file - if @app.extensions[:frontmatter] - @app.extensions[:frontmatter].template_data_for_file(@path) + if @app.extensions[:front_matter] + @app.extensions[:front_matter].template_data_for_file(@path) else File.read(File.expand_path(@path, source_dir)) end @@ -116,4 +116,4 @@ module Middleman end end end -end \ No newline at end of file +end