-
Notifications
You must be signed in to change notification settings - Fork 4
/
image_viewer.py
75 lines (52 loc) · 2.2 KB
/
image_viewer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'''
Image-Viewer v0.1.2
[
{"keys": ["super+i"], "command": "image_viewer"}
]
view from CSS:
background: url(../img/image.png);
|
^ cursor
view from HTML:
<img src="img/image.png" />
|
^ cursor
Copyright (c) 2012 Razumenko Maksim <[email protected]>
MIT License, see http://opensource.org/licenses/MIT
'''
import sublime, sublime_plugin, re, os, desktop
class ImageViewerCommand(sublime_plugin.TextCommand):
def run(self, edit):\
settings = sublime.load_settings(__name__ + '.sublime-settings')
image_types = settings.get("image_types", "[]")
def is_image(file_name):
'''Return True if file_name is image'''
file_type = re.match('.*(\..*)$', file_name or "")
file_type = file_type.group(1) if file_type else ""
return file_type in image_types
view = self.view
file_name = view.file_name()
if is_image(file_name):
self.view_image(file_name)
else:
for reg in self.view.sel():
scope_reg = view.extract_scope(reg.a) if reg.a == reg.b else reg
file_string = view.substr(scope_reg)
with_quotes = re.match('^[\"\']{1}(.*)[\"\']{1}$', file_string)
file_string = with_quotes.group(1) if with_quotes else file_string
if is_image(file_string):
dir_name = os.path.dirname(view.file_name())
self.view_image(dir_name, file_string)
else:
sublime.error_message("Image-Viewer\n\nERROR: Image type is not recognized:\n\n" + file_string)
def view_image(self, dir_name, file_name = ""):
'''View image with default OS viewer'''
full_name = dir_name + "/" + file_name if file_name else dir_name
if (os.path.isfile(full_name)):
desktop.open(full_name)
else:
full_name2 = dir_name + "../" + file_name
if (os.path.isfile(full_name2)):
desktop.open(full_name2)
else:
sublime.error_message("Image-Viewer\n\nERROR: File not found:\n\n" + full_name)