middleman/middleman-core/lib/middleman-more/extensions/automatic_alt_tags.rb

28 lines
758 B
Ruby
Raw Normal View History

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
def image_tag(path, params={})
2014-04-29 01:02:18 +02:00
unless path.include?('://')
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
alt_text = File.basename(full_path, '.*')
2013-11-14 17:44:37 +01:00
alt_text.capitalize!
params[:alt] = alt_text
end
end
end
super(path, params)
2013-11-14 17:44:37 +01:00
end
end
end