Skip to content

Instantly share code, notes, and snippets.

@sandboxws
Created March 7, 2012 10:07
Show Gist options
  • Save sandboxws/1992352 to your computer and use it in GitHub Desktop.
Save sandboxws/1992352 to your computer and use it in GitHub Desktop.
Convert TextMate snippets to Sublime Text 2 snippets
require 'nokogiri'
require 'fileutils'
require 'active_support/inflector'
def friendly_filename(filename)
filename.gsub(/[^\w\s_-]+/, '')
.gsub(/(^|\b\s)\s+($|\s?\b)/, '\\1\\2')
.gsub(/\s/, '_')
end
# allowed keys from tm snippets
valid_keys = ['content', 'name', 'tabTrigger', 'scope']
# maps tm tags to subl tags
keys_mapping = {
'content' => 'content',
'name' => 'description',
'tabTrigger' => 'tabTrigger',
'scope' => 'scope'
}
path = File.expand_path '.'
collections = Dir.entries('tm_snippets').reject {|e| e.length < 3}
collections.each do |collection|
# create directories for Sublime snippets, remove exsiting ones
FileUtils.rm_rf("#{path}/subl_snippets/#{collection}")
Dir.mkdir("#{path}/subl_snippets/#{collection}")
# snippets file names
tm_names = Dir.entries("#{path}/tm_snippets/#{collection}").reject {|f| f.length < 3}
tm_names.each do |name|
# hash to containt tm snippet data
dict = {}
# parse tm snippets
doc = Nokogiri::XML(File.open("#{path}/tm_snippets/#{collection}/#{name}"))
# get all keys tags
keys = doc.xpath '//key'
# read next element of each tag which is the string tag
keys.each do |key|
dict[key.text] = key.next_element.text if valid_keys.include?(key.text)
end
# build subl xml doc
builder = Nokogiri::XML::Builder.new do |xml|
xml.snippet do
dict.each do |key,value|
xml.send(keys_mapping[key], value)
end
end
end
# create subl xml document
doc = builder.doc
# get xml doc without the xml declaration element
xml = doc.root.to_s
file_name = name
# save xml data to file
File.open("#{path}/subl_snippets/#{collection}/#{file_name}.sublime-snippet", 'w') {|file| file.write(xml)}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment