module MasterView module Directives # creates a image_tag. # if path is specified in attr_value then it will be used exactly as listed, otherwise if attr_value is empty # then masterview will attempt to find the public/images in the string and infer the path from what follows. # otherwise it will simply use the src path. # If both width and height attr values are specified then it will build the :size option from them. # Other html attributes will be passed into image_tag options. class Image_tag < MasterView::DirectiveBase # /public\/images\/(.*)/ IMAGE_SRC_EXTRACT_REGEX = MasterView::ConfigSettings.template_asset_base_ref_pattern[:images] def stag(dcs) end def etag(dcs) src = attrs_lck['src'] if attr_value.empty? && src match = IMAGE_SRC_EXTRACT_REGEX.match(src) image_loc = (match) ? match[1] : src else image_loc = attr_value end image_tag_params = quote(image_loc) attrs.delete('src') # delete this if exists, path has already been specified width = attrs_lck['width'] height = attrs_lck['height'] if width && height attrs.delete('width') #delete these since being used in size attrs.delete('height') attrs[:size] = "#{width}x#{height}" end options = symbolize_sort_and_serialize_hash_to_str(attrs) image_tag_params += (', '+options) unless options.empty? erb_content('image_tag ' + image_tag_params) end end end end