Skip to content

Commit 75b0b78

Browse files
committed
v0.1 - just works!
1 parent d4a9f87 commit 75b0b78

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

vim-snippet2sublime-snippet.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/python
2+
# Filename: cat.py
3+
4+
import sys
5+
import re
6+
7+
def convert(filename):
8+
snippets = file(filename)
9+
snippet = ''
10+
name = ''
11+
while True:
12+
line = snippets.readline()
13+
14+
''' If a new snippet starts, write the previous one if any. '''
15+
if line.startswith('snippet '):
16+
if name:
17+
to_xml(name, snippet)
18+
name = line.split(),
19+
name = name[0][1]
20+
snippet = ''
21+
22+
elif not line.startswith('#'):
23+
''' Remove the first tab characters for right indention. '''
24+
if line.startswith('\t'):
25+
line = line[1:]
26+
snippet += line
27+
28+
if len(line) == 0:
29+
break
30+
snippets.close()
31+
32+
def to_xml(name, snippet):
33+
xml = '<snippet>\n'
34+
xml += ' <tabTrigger>' + name + '</tabTrigger>\n'
35+
xml += ' <content><![CDATA[' + snippet + ']]></content>\n'
36+
xml += '</snippet>'
37+
38+
''' Escape the right '$' characters. '''
39+
xml = re.sub('(\$[a-zA-Z0-9_])', '\\\\\\1', xml)
40+
41+
''' Pattern for project name. '''
42+
project_name = '${TM_FILENAME/([^.]*)\..*$/$1/}'
43+
xml = xml.replace('`Filename()`', project_name)
44+
45+
f = file(name + '.sublime-snippet', 'w')
46+
f.write(xml)
47+
f.close()
48+
49+
# Script starts from here
50+
if len(sys.argv) < 2:
51+
print 'No action specified.'
52+
sys.exit()
53+
54+
for filename in sys.argv[1:]:
55+
convert(filename)

0 commit comments

Comments
 (0)