/ Published in: Ruby
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#!/usr/bin/ruby $: << ENV['TM_SUPPORT_PATH'] + '/lib' require "cgi" require "ui" require "xmlrpc/client" unless ENV['SNIPPLR_KEY'] TextMate::UI.alert(:critical, "Snipplr error", "Please first set your 'SNIPPLR_KEY' in Preferences > Advanced > Shell Variables.") exit end TextMate::UI.request_string(:title => "Get Snippet from Snipplr", :prompt => "Enter tags to search for:", :button1 => "Search") do |tag| begin server = XMLRPC::Client.new2("http://snipplr.com/xml-rpc.php") snippet_list = server.call('snippet.list', ENV['SNIPPLR_KEY'], tag) rescue # fall through end unless snippet_list and snippet_list.length > 0 TextMate::UI.alert(:warning, "Snipplr result", "No snippets for '#{tag}' found.") else items = {} snippet_list.each { |r| items[r['title']] = r['id'] } TextMate::UI.request_item(:items => items.keys.sort, :title => "Snipplr result", :prompt => "Choose", :string => "Select which snippet to paste") do |item| begin snippet = server.call('snippet.get', items[item]) puts CGI.unescapeHTML(snippet['source']) rescue TextMate::UI.alert(:critical, "Snipplr error", "Could not retrieve '#{item}'.") end end end end