-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathtag_insert_as_tag.py
36 lines (32 loc) · 1.19 KB
/
tag_insert_as_tag.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
import sublime, sublime_plugin, re
from Tag import Tag
Tag = Tag.Tag()
class TagInsertAsTagCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
new_selections = []
for region in view.sel():
source = view.substr(region)
if not source.strip():
region = view.word(region)
source = view.substr(region)
if not source.strip():
new_selections.append(sublime.Region(region.a, region.b))
pass
else:
if re.match("^\s", source):
view.replace(edit, region, '<p>'+source+'</p>')
new_selections.append(sublime.Region(region.end()+3, region.end()+3))
elif Tag.is_self_closing(source):
view.replace(edit, region, '<'+source+'/>')
new_selections.append(sublime.Region(region.end()+3, region.end()+3))
else:
tag = source.split('\r')[0].split('\n')[0].split(' ')[0]
if tag and Tag.is_valid(tag) and tag != '<' and tag != '</' and tag != '>':
view.replace(edit, region, '<'+source+'></'+tag+'>')
new_selections.append(sublime.Region(region.end()+2, region.end()+2))
else:
new_selections.append(sublime.Region(region.end(), region.end()))
view.sel().clear()
for sel in new_selections:
view.sel().add(sel)