Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 99 lines (82 sloc) 2.82 KB
# Converts command-based snippets to new file-based snippet syntax
# NOTE: This is only meant to help, it is not perfect! Check the file
# afterwards to make sure it's correct.
import sys
import re
import os
def Usage():
print """\
Usage: -h or --help Print this help and exit
or: inputfile Print .snippets file
or: inputfile outputfile Output to file"""
def FindSnippet(line):
Try to find a snippet in the given line. If it is found, return the
converted snippet; otherwise, return -1.\
snippet ="exe ['\"](GlobalSnip|Snipp)(!)? (\S+) (.*)['\"]", line)
if not snippet: return -1
trigger =
text = "\t" +
if # Process multi-snippet
endSnip ='\s*\\"(.*?)\\"\s*(.*)', text)
if not endSnip:
endSnip ='\s*"(.*?)"\s*(.*)', text)
if not endSnip: return -1
trigger += ' ' + # Add name to snippet declaration
text = "\t" +
return trigger + "\n" + text
newLines = []
def ProcessLine(line):
Search the line for a snippet or comment, and append it to newLines[]
if it is found.\
snippet = FindSnippet(line)
if snippet == -1:
comment = re.match('^"(.*)', line)
if comment: newLines.append('#' +
newLines.append('snippet ' + snippet)
return snippet
def Output(lines, file = None):
outputLines = ''
for snippet in lines:
for line in snippet.split("\\n"):
line = re.sub(r'\\t', '\t', line)
line = re.sub(r'\\\\', r'\\', line)
if not re.match('^(\#|snippet)', line):
line = "\t" + line
outputLines += line + "\n"
if file:
output = open(file, 'w')
except IOError, e:
raise SystemExit(' %s' % e)
print outputLines,
def main(argv = None):
if argv is None: argv = sys.argv[1:]
if not argv or '-h' in argv or '--help' in argv:
return 1
input = open(argv[0], 'r')
except IOError, e:
raise SystemExit(' %s' % e)
snippet = -1
for line in input.readlines():
if snippet == -1:
snippet = ProcessLine(line)
concat ="^\s+(\\\s*)?(\.\s*)?['\"](.*)['\"]", line)
if concat:
newLines[-1] += "\n" + # Add concatenated lines
snippet = ProcessLine(line)
if len(argv) == 1: Output(newLines)
else: Output(newLines, argv[1])
if __name__ == '__main__':