From b1b33395979f5059e732b760524de1e73fa3646d Mon Sep 17 00:00:00 2001 From: Mark-Simulacrum Date: Sun, 18 Aug 2013 20:06:39 -0600 Subject: [PATCH] Create automated alt tag addition, based on image name. Needs documentation. --- .../extensions/automatic-alt-tags.rb | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 middleman-core/lib/middleman-more/extensions/automatic-alt-tags.rb diff --git a/middleman-core/lib/middleman-more/extensions/automatic-alt-tags.rb b/middleman-core/lib/middleman-more/extensions/automatic-alt-tags.rb new file mode 100644 index 00000000..50df3b54 --- /dev/null +++ b/middleman-core/lib/middleman-more/extensions/automatic-alt-tags.rb @@ -0,0 +1,32 @@ +# Automatic Image alt tags from image names extension +class Middleman::Extensions::AutomaticAltTags < ::Middleman::Extension + + def initialize(app, options_hash={}, &block) + super + end + + helpers do + # Override default image_tag helper to automatically insert alt tag + # containing image name. + + def image_tag(path) + if !path.include?("://") + params[:alt] ||= "" + + real_path = path + real_path = File.join(images_dir, real_path) unless real_path.start_with?('/') + full_path = File.join(source_dir, real_path) + + if File.exists?(full_path) + begin + alt_text = File.basename(full_path, ".*") + alt_text.capitalize! + params[:alt] = alt_text + end + end + end + + super(path) + end + end +end