Created
May 7, 2016 10:06
-
-
Save evanslify/3d561d4ae29b750719ce43d7f5583752 to your computer and use it in GitHub Desktop.
Snippet conversion from UltiSnips to Atom.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
import re | |
import sys | |
def split_snippets(raw_string): | |
"""Split UltiSnip snippets to lists. | |
:raw_string: file.read() | |
:returns: list of individual snippets | |
""" | |
snippet_list = [] | |
one_snippet_list = [] | |
for line in raw_string.splitlines(): | |
if line is not end_string: | |
one_snippet_list.append(line) | |
if line == end_string: | |
snippet_list.append('\n'.join(one_snippet_list)) | |
one_snippet_list = [] | |
return snippet_list | |
def parse_one_snippet(snippet): | |
"""Parses one snippet. | |
:snippet: list of one snippet | |
:returns: (desc, prefix, content) | |
""" | |
snippet_l = snippet.splitlines() | |
if snippet_l[0] == '': | |
snippet_l.pop(0) | |
prefix = snippet_l[0].split()[1] | |
desc = prefix | |
content = '\n'.join(snippet_l[2:-2]) | |
return desc, prefix, content | |
def gen_atom_snippet(snippet_tuple): | |
"""TODO: Docstring for gen_atom_snippet. | |
:desc: TODO | |
:prefix: TODO | |
:content: TODO | |
:returns: TODO | |
""" | |
desc = snippet_tuple[0] | |
prefix = snippet_tuple[1] | |
content = re.sub(r'(\${.*})', r'(\1)', snippet_tuple[2]) | |
template = ''' | |
'%s': | |
'prefix': '%s' | |
'body': """ | |
%s | |
""" | |
''' % (desc, prefix, content) | |
return template | |
start_string = 'snippet' | |
end_string = 'endsnippet' | |
input_file = sys.argv[1] | |
output_file = sys.argv[2] | |
f = open(input_file, 'r').read() | |
res = split_snippets(f) | |
with open(output_file, 'w') as output_file: | |
for i in res: | |
result = gen_atom_snippet(parse_one_snippet(i)) | |
output_file.write(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment