Created
July 24, 2009 17:54
-
-
Save rwc9u/154445 to your computer and use it in GitHub Desktop.
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 ruby | |
# This is a quick script to generate YASnippets from TextMate Snippets. | |
# | |
# I based the script off of a python script of a similar nature by | |
# Jeff Wheeler: http://nokrev.com | |
# http://code.nokrev.com/?p=snippet-copier.git;a=blob_plain;f=snippet_copier.py | |
# | |
# Usage | |
# | |
# Make sure you have the plist and the choice gem installed | |
# $ sudo gem install plist | |
# $ sudo gem install choice | |
# | |
# Usage: snippet_copier.rb [-dofp] | |
# | |
# Standard Options: | |
# -d, --snippet-dir=PATH Tells the program the directory to find the TextMate Snippets | |
# -o, --output-dir=PATH What directory to write the new YASnippets to | |
# -f, --file=SNIPPET FILE NAME A specific snippet that you want to copy or a glob for various files | |
# -p, --pretty-print Pretty prints multiple snippets when printing to standard out | |
# | |
# Common options: | |
# --help Show this message | |
require 'rubygems' | |
require 'plist' | |
require 'choice' | |
Choice.options do | |
header '' | |
header 'Standard Options:' | |
option :snippet_dir do | |
short '-d' | |
long '--snippet-dir=PATH' | |
desc 'Tells the program the directory to find the TextMate Snippets' | |
default '.' | |
end | |
option :output_dir do | |
short '-o' | |
long '--output-dir=PATH' | |
desc 'What directory to write the new YASnippets to' | |
end | |
option :snippet do | |
short '-f' | |
long '--file=SNIPPET FILE NAME' | |
desc 'A specific snippet that you want to copy or a glob for various files' | |
default '*.tmSnippet' | |
end | |
option :pretty_print do | |
short '-p' | |
long '--pretty-print' | |
desc 'Pretty prints multiple snippets when printing to standard out' | |
end | |
separator '' | |
separator 'Common options: ' | |
option :help do | |
long '--help' | |
desc 'Show this message' | |
end | |
end | |
class TmSnippet | |
def initialize(file) | |
@snippet = Plist::parse_xml(file) | |
end | |
def name | |
@snippet["name"] | |
end | |
def tab_trigger | |
@snippet["tabTrigger"] | |
end | |
def content | |
@snippet["content"] | |
end | |
def to_yasnippet | |
doc = "#tab-trigger: #{self.tab_trigger}\n" | |
doc << "#contributor : Translated from TextMate Snippet\n" | |
doc << "#name : #{self.name}\n" | |
doc << "# --\n" | |
doc << "#{self.content}" | |
end | |
end | |
def yasnippet_file_name(dir, name, ext=nil) | |
file = "" | |
if ext | |
file = File.join(dir, name + "." + ext.to_s) | |
else | |
file = File.join(dir, name) | |
end | |
if File.exist?(file) | |
if ext | |
file = yasnippet_file_name(dir,name, ext+1) | |
else | |
file = yasnippet_file_name(dir,name, 1) | |
end | |
end | |
file | |
end | |
snippet_files_glob = File.join(Choice.choices.snippet_dir, Choice.choices.snippet) | |
snippet_files = Dir.glob(snippet_files_glob) | |
snippet_files.each do |file| | |
snippet = TmSnippet.new(file) | |
if Choice.choices.output_dir | |
FileUtils.mkdir_p(Choice.choices.output_dir) | |
snippet_file = yasnippet_file_name(Choice.choices.output_dir, snippet.tab_trigger) | |
File.open(snippet_file, 'w') do |f| | |
f.write(snippet.to_yasnippet) | |
end | |
else | |
if Choice.choices.pretty_print | |
puts "--------------------------------------------" | |
end | |
puts snippet.to_yasnippet | |
if Choice.choices.pretty_print | |
puts "--------------------------------------------" | |
end | |
puts "\n\n" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment