Created
February 23, 2009 09:23
-
-
Save avdgaag/68870 to your computer and use it in GitHub Desktop.
TextMate command to reset the dimensions in a HTML IMG element
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby -w | |
# This TextMate command takes an HTML IMG element and re-calculates its size. | |
# The currently selected image will then get the original dimensions. | |
# This is useful when you have changed an image and need to reset its dimensions | |
# in your HTML. | |
# | |
# This should work on the selected text or current line and replace the current | |
# selection. Scope: text.html. Assign a nice keyboard shortcut of your choosing. | |
# | |
# I'm sure this might be done easier or quicker, but for now, this works. | |
# | |
# Author: Arjan van der Gaag | |
# E-mail: [email protected] | |
# Date: 23 feb 2009 | |
# Version: 1.0 | |
# | |
# Licensed under the MIT license. | |
################################################################################# | |
# Exit the current script with a tool tip, rather | |
# than replacing the current selection as should be default. | |
def exit_show_tool_tip(msg) | |
puts msg | |
exit 206 # forces TextMate to output as tooltip | |
end | |
# Scan an HTML img element for its SRC, WIDTH and HEIGHT attributes. | |
# All three attribute values are returned. | |
def get_properties_for(img) | |
image = img.scan(/src="(.+?)"/)[0][0] | |
width = img.scan(/width="(\d+?)"/)[0][0] | |
height = img.scan(/height="(\d+?)"/)[0][0] | |
return image, width, height | |
rescue NoMethodError | |
exit_show_tool_tip 'No proper image found.' | |
end | |
# Invoke sips to try and find the image's original dimensions | |
# This returns both the widht and height. | |
def get_original_size_for(image) | |
output = %x{sips -g pixelWidth -g pixelHeight "#{image}"} | |
if output.scan(/pixelWidth:[^\d]*(\d+).*pixelHeight:[^\d](\d+)/im) | |
return $1, $2 | |
else | |
exit_show_tool_tip "Could not get image size from #{output.inspect}" | |
end | |
end | |
def reformat_images(text) | |
# find our image in the current line | |
if text =~ /<img(.+?)\/>/ | |
img = $1 | |
# then find its properties | |
image, width, height = get_properties_for(img) | |
# try to re-calculate the image dimensions | |
new_width, new_height = get_original_size_for(image) | |
# If they don't match we rewrite them | |
if new_width != width || new_height != height | |
new_img = img.gsub("width=\"#{width}\"", "width=\"#{new_width}\"").gsub("height=\"#{height}\"", "height=\"#{new_height}\"") | |
return text.gsub(img, new_img) | |
else | |
exit_show_tool_tip 'Nothing changed' | |
end | |
else | |
exit_show_tool_tip 'No image found.' | |
end | |
end | |
print reformat_images(STDIN.read) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment