Skip to content

Instantly share code, notes, and snippets.

@nowhereman
Created April 23, 2010 15:08
Show Gist options
  • Save nowhereman/376634 to your computer and use it in GitHub Desktop.
Save nowhereman/376634 to your computer and use it in GitHub Desktop.
Transform TextMate Snippets into NetBeans Code Templates

!!! Obselete Gist !!! Go to http://github.com/nowhereman/snippets-converter

Convert TextMate Snippets into NetBeans Code Templates.

How to use (for Ubuntu):

Copy your TextMate Snippets directory into the tmsnippets2netbeans folder, run ruby tmsnippets2netbeans.rb This will convert all you need and create a XML file.

Put the org-netbeans-modules-editor-settings-CustomCodeTemplates.xml file into

/home/username/.netbeans/6.8/config/Editors/text/x-ruby/CodeTemplates folder

Requirement:

you'll need Nokogiri gem in order to use this script, so : sudo gem install nokogiri

Follow the original author, Nicolas Alpi, on twitter http://www.twitter.com/spyou

And his blog http://www.notgeeklycorrect.com

#!/usr/bin/env ruby
#
# !!! Obselete Gist !!!
# Go to http://github.com/nowhereman/snippets-converter
# TmSnippets2NetBeans
# Quick and dirty code to transform TextMate Snippets into NetBeans Code Templates
# Based on tmsnippets2gedit (http://github.com/spyou/tmsnippets2gedit)
#
# Copyright (c) 2010 Nowhere Man
#
# Last update 29 April 2010
#
# TmSnippets2NetBeans is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
#
# TmSnippets2NetBeans is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with TmSnippets2NetBeans; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
require 'test/unit'
class TestSnippetsFormat < Test::Unit::TestCase
def setup
@snippets = []
@snippets << [ "${1: ${2: cool} }", " ${2: cool} "]
@snippets << [ "${1: cool $2 }", " cool $2 "]
@snippets << [ "${1: ${2: $cool} }", " ${2: $cool} "]
@snippets << [ "${1: [ ${2: cool} ] }", " [ ${2: cool} ] "]
@snippets << [ "${1: ${2: cool} ${3: guy} }", " ${2: cool} ${3: guy} "]
@snippets << [ "${1: [ ${2: cool} ${3: guy} ] }", " [ ${2: cool} ${3: guy} ] "]
@snippets << [ "${1: [ ${2: cool} ${3: guy ${4:tralala}} ] }", " [ ${2: cool} guy ${4:tralala} ] "]
@snippets << [ "${1: [ ${2: cool} ${3: guy { ${4:tralala} } } ] }", " [ ${2: cool} guy { ${4:tralala} } ] "]
@snippets << [ "should_have_db_indices :${1:object_id}${2:, [:${3:commentable_type}, :${4:commentable_id}]}$0", "should_have_db_indices :${1:object_id}, [:${3:commentable_type}, :${4:commentable_id}]$0" ]
snippet = 'context "${1:description}" do
${2:setup do
${3: cool}
end
}should "${4:description}" do
${5: guy}
end
end'
expected_snippet = 'context "${1:description}" do
setup do
${3: cool}
end
should "${4:description}" do
${5: guy}
end
end'
@snippets << [ snippet, expected_snippet ]
@snippets << [ "[ ${3::${4:key} =&gt; ${5:value}${6:, :${7:key} =&gt; ${8:value} } } ]", "[ :${4:key} =&gt; ${5:value}, :${7:key} =&gt; ${8:value} ]" ]
@snippets << [ " { ${3::${4:key} =&gt; ${5:value}${6:, :${7:key} =&gt; ${8:value}}} } ", " { :${4:key} =&gt; ${5:value}, :${7:key} =&gt; ${8:value} } " ]
snippet = 'context "on POST to :${1:create}" do
setup do
post :$1, :${2:user} =&gt; { ${3::${4:key} =&gt; ${5:value}${6:, :${7:key} =&gt; ${8:value} } } }
end
${9:should "${10:description}" do
$0
end}
end'
expected_snippet = 'context "on POST to :${1:create}" do
setup do
post :$1, :${2:user} =&gt; { :${4:key} =&gt; ${5:value}, :${7:key} =&gt; ${8:value} }
end
should "${10:description}" do
$0
end
end'
@snippets << [ snippet, expected_snippet ]
snippet = 'context "${1:description}" do
${2:setup do
$3
end
}should "${4:description}" do
$0
end
end'
expected_snippet = 'context "${1:description}" do
setup do
$3
end
should "${4:description}" do
$0
end
end'
@snippets << [ snippet, expected_snippet ]
end
def test_snippets_conversion
@snippets.each do |snippet, expected_snippet|
# Nested tab stops, e.g '${1: $2 }'
nested_tab_stop = /((\$\{[0-9]{1,5}:[^${}]*)\$([0-9]{1,5})([^${}]*\}))+/m
snippet.gsub!(nested_tab_stop, '\2:nested_tab_stop\3:\4')
snippet.gsub!(/\$([0-9]{1,5})/, ':tab_stop\1:') # Tab Stop, e.g '$0'
snippet.gsub!(/\$([^{][^0-9]+[^:])/, ':dollar:\1') # Dollar, e.g. '$titi'
# Place holders, e.g. '${1: cool }'
place_holders = /\$\{((?>[^${}]+)|(\1))+\}/m
place_holders_matches = snippet.scan(place_holders)
place_holders_matches.flatten.each_with_index do |place_holder, idx|
snippet.gsub!(/\$\{#{place_holder}\}/m, ":place_holders#{idx}:") if place_holder
end
# Nested place holders, e.g. '${1: ${3:cool} }'
nested_place_holders = /(\$\{[0-9]{1,5}:(([^${}]*(\$\{[0-9]{1,5}:|\{)[^${}]+\}[^${}]*)|[^${}]+)\})+/m
i = 0
loop do
i += 1
# puts i # debug
break if !snippet.gsub!(nested_place_holders, '\2') || i > 20
end
place_holders_matches.flatten.each_with_index do |place_holder, idx|
snippet.gsub!(/:place_holders#{idx}:/m, "\$\{#{place_holder}\}") if place_holder
end
# Nested tab stops
snippet.gsub!(/:nested_tab_stop([0-9]{1,5}):/, '$\1')
nested_tab_stop = /(\$\{[0-9]{1,5}:([^${}]*\$[0-9]{1,5}[^${}]*)\})+/m
i = 0
loop do
i += 1
# puts i # debug
break if !snippet.gsub!(nested_tab_stop, '\2') || i > 20
end
snippet.gsub!(/:tab_stop([0-9]{1,5}):/, '$\1')
snippet.gsub!(/:dollar:/, '$')
assert_equal expected_snippet, snippet
end
end
end
#!/usr/bin/env ruby
#
# !!! Obselete Gist !!!
# Go to http://github.com/nowhereman/snippets-converter
#
# TmSnippets2NetBeans
# Quick and dirty code to transform TextMate Snippets into NetBeans Code Templates
# Based on tmsnippets2gedit (http://github.com/spyou/tmsnippets2gedit)
#
# Copyright (c) 2009 Nicolas Alpi
# Copyright (c) 2010 Nowhere Man
#
# Last update 29 April 2010
#
# TmSnippets2NetBeans is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
#
# TmSnippets2NetBeans is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with TmSnippets2NetBeans; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
require 'rubygems'
require 'nokogiri'
def convert(file)
xml = File.read(file)
doc = Nokogiri::XML(xml)
i = 0
j = 0
arrKey = Array.new
arrString = Array.new
# Transform the key and string node into arrays
doc.xpath('//key','//string').each do |key|
if key.name == "key"
arrKey[i] = key.text
i+=1
else
arrString[j] = key.text
j+=1
end
end
code = arrString[arrKey.index('content')]
# Nested tab stops and place holders remover, e.g '${1: ${3:cool} $2 }' become ' ${3:cool} $2 '
# Nested tab stops, e.g '${1: $2 }'
nested_tab_stop = /((\$\{[0-9]{1,5}:[^${}]*)\$([0-9]{1,5})([^${}]*\}))+/m
code.gsub!(nested_tab_stop, '\2:nested_tab_stop\3:\4')
code.gsub!(/\$([0-9]{1,5})/, ':tab_stop\1:') # Tab Stop, e.g '$0'
code.gsub!(/\$([^{][^0-9]+[^:])/, ':dollar:\1') # Dollar, e.g. '$titi'
# Place holders, e.g. '${1: cool }'
place_holders = /\$\{((?>[^${}]+)|(\1))+\}/m
place_holders_matches = code.scan(place_holders)
place_holders_matches.flatten.each_with_index do |place_holder, idx|
code.gsub!(/\$\{#{place_holder}\}/m, ":place_holders#{idx}:") if place_holder
end
# Nested place holders, e.g. '${1: ${3:cool} }'
nested_place_holders = /(\$\{[0-9]{1,5}:(([^${}]*(\$\{[0-9]{1,5}:|\{)[^${}]+\}[^${}]*)|[^${}]+)\})+/m
i = 0
loop do
i += 1
# puts i # debug
break if !code.gsub!(nested_place_holders, '\2') || i > 20
end
place_holders_matches.flatten.each_with_index do |place_holder, idx|
code.gsub!(/:place_holders#{idx}:/m, "\$\{#{place_holder}\}") if place_holder
end
# Nested tab stops
code.gsub!(/:nested_tab_stop([0-9]{1,5}):/, '$\1')
nested_tab_stop = /(\$\{[0-9]{1,5}:([^${}]*\$[0-9]{1,5}[^${}]*)\})+/m
i = 0
loop do
i += 1
# puts i # debug
break if !code.gsub!(nested_tab_stop, '\2') || i > 20
end
code.gsub!(/:tab_stop([0-9]{1,5}):/, '$\1')
code.gsub!(/:dollar:/, '$')
# NetBeans specifics
code.gsub!('$0', '${cursor}')
code.gsub!(/\$\{([0-9]{1,5}):((?>[^{}]+)|(\1))+\}/m, '${\1 default="\2"}')
code.gsub!(/\$([0-9]{1,5})/m, '${tabStop\1 default=""}')
# Output to NetBeans format
return <<-CODE
<codetemplate abbreviation='#{arrString[arrKey.index('tabTrigger')]}' xml:space='preserve'>
<code><![CDATA[#{code}]]></code>
<description><![CDATA[#{arrString[arrKey.index('name')]}]]></description>
</codetemplate>
CODE
end
output = ""
output += <<-CODE
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE codetemplates PUBLIC "-//NetBeans//DTD Editor Code Templates settings 1.0//EN" "http://www.netbeans.org/dtds/EditorCodeTemplates-1_0.dtd">
<codetemplates>
CODE
for file in Dir.glob("Snippets/*.tmSnippet")
puts "Converting #{file} ..."
output += convert(file)
end
output += "</codetemplates>"
File.open("org-netbeans-modules-editor-settings-CustomCodeTemplates.xml", "w") do |f|
f.write(output)
end
puts "**** Done, result stored in org-netbeans-modules-editor-settings-CustomCodeTemplates.xml ****"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment