Permalink
Browse files

Re-implemented Update Image Size action

  • Loading branch information...
1 parent 30c8e91 commit 3bfec9a0bfa4a250d2c46c4866b5ffe134a716fb @onecrayon committed Mar 8, 2012
Showing with 36 additions and 69 deletions.
  1. +1 −0 Contents/Resources/English.lproj/Localizable.strings
  2. +30 −62 Scripts/image_size.py
  3. +5 −7 TextActions/Actions.xml
@@ -5,6 +5,7 @@
"com.onecrayon.sugar.htmlbundle.encrypt-rot13.title" = "Encrypt Selection / Line (ROT 13)";
"com.onecrayon.sugar.htmlbundle.url-escape.title" = "URL Escape Selection / Line";
"com.onecrayon.sugar.htmlbundle.url-unescape.title" = "URL Unescape Selection / Line";
+"com.onecrayon.sugar.htmlbundle.update-image.title" = "Update <img> Size";
"com.onecrayon.sugar.htmlbundle.wrap-in-php-echo.title" = "Wrap In <?= ... ?>";
"com.onecrayon.sugar.htmlbundle.strip-html.title" = "Strip HTML From Document / Selection";
"com.onecrayon.sugar.htmlbundle.validate-html.title" = "Validate Document (W3C)";
View
92 Scripts/image_size.py 100644 → 100755
@@ -1,12 +1,12 @@
+#!/usr/bin/env python
+
'''
@author: Sergey Chikuyonok (serge.che@gmail.com)
'''
from subprocess import Popen, PIPE
-import urllib
-import os
+import os, sys
import re
-import tea_actions as tea
def replace_or_append(img_tag, attr_name, attr_value):
"""
@@ -22,62 +22,36 @@ def replace_or_append(img_tag, attr_name, attr_value):
else:
return re.sub(r'\s*(\/?>)$', ' ' + attr_name + '="' + attr_value + r'" \1', img_tag)
-def find_image(context):
+def find_images(text):
"""
- Find image tag under caret
- @return Image tag and its indexes inside editor source
+ Find image tags in selection
+ @return Image tags and their indexes inside editor source
"""
- rng = tea.get_ranges(context)[0]
- caret_pos = rng.location
- text = context.string()
- start_ix = -1
- end_ix = -1
-
- # find the beginning of the tag
- while caret_pos >= 0:
- if text[caret_pos] == '<':
- if text[caret_pos:caret_pos + 4].lower() == '<img':
- # found the beginning of the image tag
- start_ix = caret_pos
- break
- else:
- # found some other tag
- return None
- caret_pos -= 1
-
- # find the end of the tag
- caret_pos = rng.location
- ln = len(text)
- while caret_pos <= ln:
- if text[caret_pos] == '>':
- end_ix = caret_pos + 1
- break
- caret_pos += 1
-
+ imgRE = re.compile(r'<img (?:[-_a-zA-Z0-9:]+=(?:".*?(?<!\\)"|\'.*?(?<!\\)\'|\S+)\s*)+/?>', re.I)
- if start_ix != -1 and end_ix != -1:
- return {
- 'start': start_ix,
- 'end': end_ix,
- 'tag': text[start_ix:end_ix]
- }
+ images = []
+ for match in imgRE.finditer(text):
+ images.append({
+ 'start': match.start(),
+ 'end': match.end(),
+ 'tag': match.group(0)
+ })
- return None
+ return images
-def get_image_size(context, img):
+def get_image_size(img):
"""
Returns size of image in <img>; tag
@param img: Image tag
@return Dictionary with <code>width</code> and <code>height</code> attributes
"""
m = re.search(r'src=(["\'])(.+?)\1', img, re.IGNORECASE)
if m:
- src = get_absolute_uri(context, m.group(2))
+ src = get_absolute_uri(m.group(2))
if not src:
return None
try:
raw_output = Popen('sips -g pixelWidth -g pixelHeight "%s"' % src, stdout=PIPE, shell=True).communicate()[0]
- tea.log(raw_output)
return {
'width': re.search(r'pixelWidth:\s*(\d+)', raw_output).group(1),
'height': re.search(r'pixelHeight:\s*(\d+)', raw_output).group(1)
@@ -87,10 +61,8 @@ def get_image_size(context, img):
return None
-def get_absolute_uri(context, img_path):
- file_uri = urllib.unquote(context.documentContext().fileURL().absoluteString())
- # remove protocol
- file_uri = re.sub(r'^\w+://\w+', '', file_uri)
+def get_absolute_uri(img_path):
+ file_uri = os.environ['EDITOR_PATH']
if img_path[0] == '/':
img_path = img_path[1:]
@@ -101,7 +73,6 @@ def get_absolute_uri(context, img_path):
abs_image_path = os.path.join(head, img_path)
abs_image_path = os.path.normpath(abs_image_path)
- tea.log('testing ' + abs_image_path)
if os.path.exists(abs_image_path):
return os.path.join(abs_image_path)
@@ -110,18 +81,15 @@ def get_absolute_uri(context, img_path):
return None
-def act(context):
- image = find_image(context)
- if image:
- size = get_image_size(context, image['tag'])
- if size:
- new_tag = replace_or_append(image['tag'], 'width', size['width'])
- new_tag = replace_or_append(new_tag, 'height', size['height'])
-
- rng = tea.get_ranges(context)[0]
- tea.insert_snippet_over_range(context, new_tag, tea.new_range(image['start'], image['end'] - image['start']), 'Update Image Size')
- tea.set_selected_range(context, rng)
- return True
-
- return False
+# Actual script
+text = sys.stdin.read()
+images = find_images(text)
+for image in reversed(images):
+ size = get_image_size(image['tag'])
+ if size:
+ new_tag = replace_or_append(image['tag'], 'width', size['width'])
+ new_tag = replace_or_append(new_tag, 'height', size['height'])
+ text = text[0:image['start']] + new_tag + text[image['end']:]
+
+sys.stdout.write(text)
View
@@ -74,15 +74,13 @@
</setup>
</action>
- <!-- TODO: figure out if I can make this work again
- <action id="com.onecrayon.sugar.htmlbundle.UpdateImage" category="actions.text.HTML">
- <class>TEAforEspresso</class>
- <title>Update &lt;img&gt; Size</title>
- <key-equivalent>control i</key-equivalent>
+ <action id="com.onecrayon.sugar.htmlbundle.update-image" category="actions.htmlbundle/tea.wrapping">
+ <class>OCShellAction</class>
<setup>
- <script>image_size</script>
+ <script>image_size.py</script>
<input>selection</input>
+ <alternate>line</alternate>
</setup>
- </action>-->
+ </action>
</action-recipes>

0 comments on commit 3bfec9a

Please sign in to comment.