35 lines
1.3 KiB
Ruby
35 lines
1.3 KiB
Ruby
module Middleman
|
|
module CoreExtensions
|
|
# 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'
|
|
app.config.define_setting :helpers_filename_to_module_name_proc, proc { |filename|
|
|
basename = File.basename(filename, File.extname(filename))
|
|
basename.camelcase
|
|
}, 'Proc implementing the conversion from helper filename to module name'
|
|
end
|
|
|
|
def after_configuration
|
|
helpers_path = File.join(app.root, app.config[:helpers_dir])
|
|
|
|
return unless 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)
|
|
|
|
app.template_context_class.send :include, Object.const_get(module_name.to_sym)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|