2013-11-14 17:44:37 +01:00
|
|
|
# Automatic Image alt tags from image names extension
|
|
|
|
class Middleman::Extensions::AutomaticAltTags < ::Middleman::Extension
|
|
|
|
helpers do
|
|
|
|
# Override default image_tag helper to automatically insert alt tag
|
|
|
|
# containing image name.
|
2013-12-28 19:14:15 +01:00
|
|
|
|
2014-08-03 17:29:29 +02:00
|
|
|
def image_tag(path, params={})
|
2014-04-29 01:02:18 +02:00
|
|
|
unless path.include?('://')
|
2013-12-28 01:26:31 +01:00
|
|
|
params[:alt] ||= ''
|
2013-11-14 17:44:37 +01:00
|
|
|
|
|
|
|
real_path = path
|
|
|
|
real_path = File.join(images_dir, real_path) unless real_path.start_with?('/')
|
|
|
|
full_path = File.join(source_dir, real_path)
|
|
|
|
|
2014-04-29 01:02:18 +02:00
|
|
|
if File.exist?(full_path)
|
2013-11-14 17:44:37 +01:00
|
|
|
begin
|
2013-12-28 01:26:31 +01:00
|
|
|
alt_text = File.basename(full_path, '.*')
|
2013-11-14 17:44:37 +01:00
|
|
|
alt_text.capitalize!
|
|
|
|
params[:alt] = alt_text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-03 17:29:29 +02:00
|
|
|
super(path, params)
|
2013-11-14 17:44:37 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|