Move ExternalHelpers into a real extension

This commit is contained in:
Ben Hollis 2014-05-25 20:05:29 -07:00
parent fed95f9c5e
commit 70b3b87905
3 changed files with 16 additions and 15 deletions

View file

@ -148,9 +148,6 @@ module Middleman
# Sitemap Config options and public api # Sitemap Config options and public api
include Middleman::Sitemap include Middleman::Sitemap
# Setup external helpers
include Middleman::CoreExtensions::ExternalHelpers
# Reference to Logger singleton # Reference to Logger singleton
def logger def logger
::Middleman::Logger.singleton ::Middleman::Logger.singleton

View file

@ -18,7 +18,10 @@ Middleman::Extensions.register :front_matter do
end end
# External helpers looks in the helpers/ folder for helper modules # External helpers looks in the helpers/ folder for helper modules
Middleman::Extensions.register :external_helpers, auto_activate_before_configuration: true do
require 'middleman-core/core_extensions/external_helpers' require 'middleman-core/core_extensions/external_helpers'
Middleman::CoreExtensions::ExternalHelpers
end
# Extended version of Padrino's rendering # Extended version of Padrino's rendering
require 'middleman-core/core_extensions/rendering' require 'middleman-core/core_extensions/rendering'

View file

@ -1,9 +1,10 @@
# Load helpers in helpers/
module Middleman module Middleman
module CoreExtensions module CoreExtensions
module ExternalHelpers # Load helpers in `helpers/`
# once registered class ExternalHelpers < Extension
def self.included(app) def initialize(app, options_hash={}, &block)
super
# Setup a default helpers paths # Setup a default helpers paths
app.config.define_setting :helpers_dir, 'helpers', 'Directory to autoload helper modules from' app.config.define_setting :helpers_dir, 'helpers', 'Directory to autoload helper modules from'
app.config.define_setting :helpers_filename_glob, '**.rb', 'Glob pattern for matching helper ruby files' app.config.define_setting :helpers_filename_glob, '**.rb', 'Glob pattern for matching helper ruby files'
@ -11,20 +12,20 @@ module Middleman
basename = File.basename(filename, File.extname(filename)) basename = File.basename(filename, File.extname(filename))
basename.camelcase basename.camelcase
}, 'Proc implementing the conversion from helper filename to module name' }, 'Proc implementing the conversion from helper filename to module name'
end
# After config def after_configuration
app.after_configuration do helpers_path = File.join(app.root, app.config[:helpers_dir])
helpers_path = File.join(root, config[:helpers_dir])
next unless File.exist?(helpers_path)
Dir[File.join(helpers_path, config[:helpers_filename_glob])].each do |filename| if File.exist?(helpers_path)
module_name = config[:helpers_filename_to_module_name_proc].call(filename) Dir[File.join(helpers_path, app.config[:helpers_filename_glob])].each do |filename|
module_name = app.config[:helpers_filename_to_module_name_proc].call(filename)
next unless module_name next unless module_name
require filename require filename
next unless Object.const_defined?(module_name.to_sym) next unless Object.const_defined?(module_name.to_sym)
@template_context_class.send :include, Object.const_get(module_name.to_sym) app.template_context_class.send :include, Object.const_get(module_name.to_sym)
end end
end end
end end