-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Tag.py
executable file
·149 lines (129 loc) · 4.7 KB
/
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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import re, sublime
ST2 = int(sublime.version()) < 3000
if ST2:
try:
sublime.error_message("TAG Package Message:\n\nThis Package does NOT WORK in Sublime Text 2\n\n Use Sublime Text 3 instead.")
except:
try:
sublime.message_dialog("TAG Package Message:\n\nThis Package does NOT WORK in Sublime Text 2\n\n Use Sublime Text 3 instead.")
except:
pass
class Tag():
def __init__(self):
Tag.regexp_is_valid = re.compile("^[a-z0-9#\:\-_]+$", re.I);
Tag.regexp_self_closing_optional = re.compile("^<?(\?xml|\!|area|base|br|col|frame|hr|img|input|link|meta|param|command|embed|source|/?li|/?p)[^a-z]", re.I);
Tag.regexp_self_closing = re.compile("^<?(\?xml|\!|%|area|base|br|col|frame|hr|img|input|link|meta|param|command|embed|source)[^a-z]", re.I);
Tag.regexp_self_closing_xml = re.compile("^<?(\?xml|\!)[^a-z]", re.I);
Tag.regexp_is_closing = re.compile("^<?[^><]+/>", re.I);
Tag.xml_files = [item.lower() for item in ['xhtml', 'xml', 'rdf', 'xul', 'svg', 'xsd', 'xslt','tmTheme', 'tmPreferences', 'tmLanguage', 'sublime-snippet', 'opf', 'ncx', 'jsx', 'js']]
def is_valid(self, content):
return Tag.regexp_is_valid.match(content)
def is_self_closing(self, content, return_optional_tags = True, is_xml= False):
if return_optional_tags:
if is_xml == False:
return Tag.regexp_self_closing.match(content) or Tag.regexp_is_closing.match(content)
else:
return Tag.regexp_is_closing.match(content) or Tag.regexp_self_closing_xml.match(content)
else:
if is_xml == False:
return Tag.regexp_self_closing_optional.match(content) or Tag.regexp_is_closing.match(content)
else:
return Tag.regexp_is_closing.match(content) or Tag.regexp_self_closing_xml.match(content)
def name(self, content, return_optional_tags = True, is_xml = False):
if content[:1] == '/' or content[:2] == '\\/':
tag_name = content.split('/')[1].split('>')[0].strip();
else:
tag_name = content.split(' ')[0].split('>')[0].strip();
if self.is_valid(tag_name) and not self.is_self_closing(content, return_optional_tags, is_xml):
return tag_name
else:
return ''
def is_closing(self, content):
if content[:1] == '/' or content[:2] == '\\/' or Tag.regexp_is_closing.match(content):
return True
else:
return False
def view_is_xml(self, view):
if view.settings().get('is_xml'):
return True
else:
name = view.file_name()
if not name:
is_xml = '<?xml' in view.substr(sublime.Region(0, 50))
else:
name = ('name.'+name).split('.')
name.reverse()
name = name.pop(0).lower()
is_xml = name in Tag.xml_files or '<?xml' in view.substr(sublime.Region(0, 50))
view.settings().set('is_xml', is_xml)
return is_xml
def clean_html(self, content):
# normalize
content = content.replace('\r', '\n').replace('\t', ' ')
# comments
unparseable = content.split('<!--')
content = unparseable.pop(0)
l = len(unparseable)
i = 0
while i < l:
tmp = unparseable[i].split('-->')
content += '....'
content += len(tmp.pop(0))*'.'
content += '...'
content += "...".join(tmp)
i += 1
# multiline line comments /* */
if content.count('/*') == content.count('*/'):
unparseable = content.split('/*')
content = unparseable.pop(0)
l = len(unparseable)
i = 0
while i < l:
tmp = unparseable[i].split('*/')
content += '..'
content += len(tmp.pop(0))*'.'
content += '..'
content += "..".join(tmp)
i += 1
# one line comments //
unparseable = re.split('(\s\/\/[^\n]+\n)', content)
for comment in unparseable:
if comment[:3] == '\n//' or comment[:3] == ' //':
content = content.replace(comment, (len(comment))*'.')
# one line comments #
unparseable = re.split('(\s\#[^\n]+\n)', content)
for comment in unparseable:
if comment[:3] == '\n#' or comment[:3] == ' #':
content = content.replace(comment, (len(comment))*'.')
# script
if content.count('<script') == content.count('</script'):
unparseable = content.split('<script')
content = unparseable.pop(0)
l = len(unparseable)
i = 0
while i < l:
tmp = unparseable[i].split('</script>')
content += '.......'
content += len(tmp.pop(0))*'.'
content += '.........'
content += ".........".join(tmp)
i += 1
# style
if content.count('<style') == content.count('</style'):
unparseable = content.split('<style')
content = unparseable.pop(0)
l = len(unparseable)
i = 0
while i < l:
tmp = unparseable[i].split('</style>')
content += '......'
content += len(tmp.pop(0))*'.'
content += '........'
content += "........".join(tmp)
i += 1
# here-doc
while '<<<' in content:
content = content.replace('<<<', '...')
while '<<' in content:
content = content.replace('<<', '..')
return content