Skip to content

Commit

Permalink
v0.1 - just works!
Browse files Browse the repository at this point in the history
  • Loading branch information
juhasz committed Jan 21, 2012
1 parent d4a9f87 commit 75b0b78
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions vim-snippet2sublime-snippet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/python
# Filename: cat.py

import sys
import re

def convert(filename):
snippets = file(filename)
snippet = ''
name = ''
while True:
line = snippets.readline()

''' If a new snippet starts, write the previous one if any. '''
if line.startswith('snippet '):
if name:
to_xml(name, snippet)
name = line.split(),
name = name[0][1]
snippet = ''

elif not line.startswith('#'):
''' Remove the first tab characters for right indention. '''
if line.startswith('\t'):
line = line[1:]
snippet += line

if len(line) == 0:
break
snippets.close()

def to_xml(name, snippet):
xml = '<snippet>\n'
xml += ' <tabTrigger>' + name + '</tabTrigger>\n'
xml += ' <content><![CDATA[' + snippet + ']]></content>\n'
xml += '</snippet>'

''' Escape the right '$' characters. '''
xml = re.sub('(\$[a-zA-Z0-9_])', '\\\\\\1', xml)

''' Pattern for project name. '''
project_name = '${TM_FILENAME/([^.]*)\..*$/$1/}'
xml = xml.replace('`Filename()`', project_name)

f = file(name + '.sublime-snippet', 'w')
f.write(xml)
f.close()

# Script starts from here
if len(sys.argv) < 2:
print 'No action specified.'
sys.exit()

for filename in sys.argv[1:]:
convert(filename)

0 comments on commit 75b0b78

Please sign in to comment.