Skip to content

Instantly share code, notes, and snippets.

@uchida
Last active October 1, 2015 09:38
Show Gist options
  • Select an option

  • Save uchida/1965345 to your computer and use it in GitHub Desktop.

Select an option

Save uchida/1965345 to your computer and use it in GitHub Desktop.
convert snippet file for neosnippet into snippet files for yasnippet
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# convert snippet file for neosnippet into snippet files for yasnippet
# by Akihiro Uchida, CC0 dedicated to the public domain
# see http://creativecommons.org/publicdomain/zero/1.0/
import argparse
import os.path
import textwrap
YASNIPPET_TEMPLATE = """# -*- mode: snippet -*-
# name: {key}
# key: {key}
# --
{definition}
"""
class SnippetParser(object):
def __init__(self):
self.items = []
self.item = {}
self.snippet = ''
return
def feed(self, snip_file):
for line in snip_file:
if line.startswith('snippet'):
if 'key' in self.item:
self.add_snippet()
self.item['key'] = "".join(line.lstrip('snippet').strip())
elif line.startswith(('#', 'abbr', 'alias', 'prev_word')):
continue
else:
contents = line
for s in ['snippet', ' ' * 4]:
if line.startswith(s):
contents = line.lstrip(s)
self.snippet += contents
return
def add_snippet(self):
self.snippet = self.snippet.rstrip('\n')
for paren in ('\\{', '\\}'):
self.snippet = self.snippet.replace(paren, paren.strip("\\"))
self.item['definition'] = textwrap.dedent(self.snippet)
self.items.append(self.item)
self.snippet = ''
self.item = {}
return
def finish(self):
if 'key' in self.item:
self.add_snippet()
self.snippet = ''
self.item = {}
return self.items
def check_dir(outdir):
if not os.path.exists(outdir):
msg = '{} does not exist.'.format(outdir)
raise argparse.ArgumentTypeError(msg)
elif not os.path.isdir(outdir):
msg = '{} is not a directory.'.format(outdir)
raise argparse.ArgumentTypeError(msg)
return outdir
if __name__ == '__main__':
# parse command line arguments
description = 'convert snippet file for neosnippet into snippet files for yasnippet'
arg_parser = argparse.ArgumentParser(description=description)
arg_parser.add_argument('infile', type=argparse.FileType('r'),
help='a neocomplcache snippet file')
arg_parser.add_argument('--overwrite', action='store_true',
help='overwrite yasnippet files')
arg_parser.add_argument('-o', '--outdir', required=True, type=check_dir,
help='output directory for yasnippet')
args = arg_parser.parse_args()
# parse neocomplcache snippet file
snip_parser = SnippetParser()
snip_parser.feed(args.infile)
items = snip_parser.finish()
args.infile.close()
# write yasnippet snippet files
for item in items:
fpath = os.path.join(args.outdir, item['key'])
if os.path.exists(fpath) and not args.overwrite:
overwrite_warn = "file {} already exists. overwrite? "
res = raw_input(overwrite_warn.format(fpath))
if res.lower() not in ('y', 'yes'):
continue
with open(fpath, 'w') as f:
f.write(YASNIPPET_TEMPLATE.format(**item))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment