Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for single quotes and fix edge cases that cause duplicate attributes and incorrect ordering. #55

Open
wants to merge 1 commit into
base: st3
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions autofilename.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,13 @@ def run(self, edit):
class InsertDimensionsCommand(sublime_plugin.TextCommand):
this_dir = ''

# inserts the dimension after the closing quotation in the src attribute
def insert_dimension(self,edit,dim,name,tag_scope):
view = self.view
sel = view.sel()[0].a

if name in view.substr(tag_scope):
reg = view.find('(?<='+name+'\=)\s*\"\d{1,5}', tag_scope.a)
view.replace(edit, reg, '"'+str(dim))
else:
dimension = str(dim)
view.insert(edit, sel+1, ' '+name+'="'+dimension+'"')
view.insert(edit, sel+1, ' '+name+'="'+str(dim)+'"')


def get_setting(self,string,view=None):
if view and view.settings().get(string):
Expand All @@ -77,15 +74,34 @@ def insert_dimensions(self, edit, scope, w, h):
self.insert_dimension(edit,h,'height', scope)


# determines if there is a template tag in a given region. supports HTML and template languages.
# Determines if there is an img tag in a given region. Supports HTML and template languages.
def img_tag_in_region(self, region):
view = self.view

# handle template languages but template languages like slim may also contain HTML so
# we do a check for that as well
return view.substr(region).strip().startswith('img') | ('<img' in view.substr(region))


def clear_dimensions(self, edit, tag_scope):
view = self.view
for dim in ['width', 'height']:
if dim in view.substr(tag_scope):
reg = view.find(dim + '\=\s*[\"\']\d{1,5}[\"\'] ?', tag_scope.a)
view.erase(edit, reg)


# Refine scope to contain exactly the image tag (default extracted scopes may include other tags and images that
# negatively affect dimension insertion).
def get_tag_scope(self, src):
view = self.view
sel = view.sel()[0].a
scope = view.extract_scope(sel-1)

tag_scope = view.line(sel) if self.get_setting('afn_template_languages',view) else view.extract_scope(scope.a-1)
tag_scope = view.find("<?img((?!img).)*?src=[\'\"]" + src + "[\'\"].*?[>\r\n]", tag_scope.a)

return tag_scope


def run(self, edit):
view = self.view
view.run_command("commit_completion")
Expand All @@ -94,21 +110,22 @@ def run(self, edit):
if not 'html' in view.scope_name(sel): return
scope = view.extract_scope(sel-1)

# if using a template language, the scope is set to the current line
tag_scope = view.line(sel) if self.get_setting('afn_template_languages',view) else view.extract_scope(scope.a-1)

path = view.substr(scope)
if path.startswith(("'","\"","(")):
path = path[1:-1]

src = path

path = path[path.rfind(FileNameComplete.sep):] if FileNameComplete.sep in path else path
full_path = self.this_dir + path

tag_scope = self.get_tag_scope(src)
if self.img_tag_in_region(tag_scope) and path.endswith(('.png','.jpg','.jpeg','.gif')):
with open(full_path,'rb') as r:
read_data = r.read() if path.endswith(('.jpg','.jpeg')) else r.read(24)
w, h = getImageInfo(read_data)

self.clear_dimensions(edit, tag_scope)
self.insert_dimensions(edit, tag_scope, w, h)


Expand Down