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
include Middleman::Sitemap
# Setup external helpers
include Middleman::CoreExtensions::ExternalHelpers
# Reference to Logger singleton
def logger
::Middleman::Logger.singleton

View file

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

View file

@ -1,9 +1,10 @@
# Load helpers in helpers/
module Middleman
module CoreExtensions
module ExternalHelpers
# once registered
def self.included(app)
# Load helpers in `helpers/`
class ExternalHelpers < Extension
def initialize(app, options_hash={}, &block)
super
# Setup a default helpers paths
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'
@ -11,20 +12,20 @@ module Middleman
basename = File.basename(filename, File.extname(filename))
basename.camelcase
}, 'Proc implementing the conversion from helper filename to module name'
end
# After config
app.after_configuration do
helpers_path = File.join(root, config[:helpers_dir])
next unless File.exist?(helpers_path)
def after_configuration
helpers_path = File.join(app.root, app.config[:helpers_dir])
Dir[File.join(helpers_path, config[:helpers_filename_glob])].each do |filename|
module_name = config[:helpers_filename_to_module_name_proc].call(filename)
if File.exist?(helpers_path)
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
require filename
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