Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
47 additions
and
44 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,66 @@ | ||
#!/usr/bin/env node | ||
var xml2js = require('xml2js'), | ||
fs = require('fs'), | ||
process = require('process'); | ||
fs = require('fs'), | ||
process = require('process'); | ||
|
||
var Converter = function(input, output){ | ||
if( input === undefined || input === null || !fs.existsSync(input) ) | ||
throw 'Snippet file not exist'; | ||
|
||
this.parser = new xml2js.Parser(); | ||
this.input = input; | ||
this.output = output || 'converted.snippets'; | ||
this.init(); | ||
if( input === undefined || input === null || !fs.existsSync(input) ) | ||
throw 'Snippet file not exist'; | ||
|
||
this.parser = new xml2js.Parser(); | ||
this.input = input; | ||
this.output = output || 'converted.snippets'; | ||
this.snippetExtensionPattern = /\.sublime-snippet$/; | ||
this.init(); | ||
}; | ||
Converter.prototype.init = function() { | ||
var stats = fs.lstatSync(this.input); | ||
if(stats.isDirectory()){ | ||
var files = fs.readdirSync(this.input); | ||
for(var i=0;i<files.length;i++){ | ||
this.parseXml(this.input+files[i], this.createSnippetFile); | ||
} | ||
}else{ | ||
this.parseXml(this.input, this.createSnippetFile); | ||
} | ||
var stats = fs.lstatSync(this.input); | ||
if(stats.isDirectory()){ | ||
var files = fs.readdirSync(this.input); | ||
for(var i=0;i<files.length;i++){ | ||
if(!this.snippetExtensionPattern.test(files[i])) continue; | ||
if(!/\/$/.test(this.input)) this.input += "/"; | ||
this.parseXml(this.input+files[i], this.createSnippetFile); | ||
} | ||
}else{ | ||
this.parseXml(this.input, this.createSnippetFile); | ||
} | ||
}; | ||
Converter.prototype.parseXml = function(filename, callback) { | ||
var t=this; | ||
fs.readFile(filename, function(err, data){ | ||
t.parser.parseString(data, callback.bind(t)); | ||
}); | ||
var t=this; | ||
fs.readFile(filename, function(err, data){ | ||
t.parser.parseString(data, callback.bind(t)); | ||
}); | ||
}; | ||
|
||
Converter.prototype.createSnippetFile = function(err, data) { | ||
if(err) throw err; | ||
if(data.snippet === undefined || | ||
data.snippet.tabTrigger === undefined || | ||
data.snippet.content === undefined) | ||
{ | ||
console.log('Failed to process snippet.'); | ||
return; | ||
} | ||
if(err) throw err; | ||
|
||
if(data.snippet === undefined || | ||
data.snippet.tabTrigger === undefined || | ||
data.snippet.content === undefined) | ||
{ | ||
console.log('Failed to process snippet.'); | ||
return; | ||
} | ||
|
||
var snippet = [ | ||
'snippet '+data.snippet.tabTrigger+ | ||
(data.snippet.description?' "'+data.snippet.description+'"':''), | ||
data.snippet.content.toString(), | ||
'endsnippet' | ||
].join('\n'); | ||
var snippet = [ | ||
'snippet '+data.snippet.tabTrigger+ | ||
(data.snippet.description?' "'+data.snippet.description+'"':''), | ||
data.snippet.content.toString(), | ||
'endsnippet' | ||
].join('\n'); | ||
|
||
fs.appendFile(this.output, snippet+"\n\n",function(err){ | ||
if(err) throw err; | ||
console.log(data.snippet.tabTrigger+' appended'); | ||
}); | ||
fs.appendFile(this.output, snippet+"\n\n",function(err){ | ||
if(err) throw err; | ||
console.log(data.snippet.tabTrigger+' appended'); | ||
}); | ||
}; | ||
|
||
var argv = process.argv.splice(2); | ||
if(argv[0] !== undefined){ | ||
var output = argv[1] || null; | ||
new Converter(argv[0], output); | ||
var output = argv[1] || null; | ||
new Converter(argv[0], output); | ||
} else { | ||
console.log('sublime2ultisnip [directory/file] [output file]'); | ||
console.log('sublime2ultisnip [directory/file] [output file]'); | ||
} |