TextMate bundle searching and pasting code


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #!/usr/bin/env ruby -wKU
  2. # Copyright 2008 Hirotsugu Asari
  3.  
  4. require "xmlrpc/client"
  5. require 'cgi'
  6.  
  7. CMD = [ ENV['TM_SUPPORT_PATH'] +
  8. '/bin/CocoaDialog.app/Contents/MacOS/CocoaDialog' ]
  9.  
  10. srv = XMLRPC::Client.new2("http://snipplr.com/xml-rpc.php")
  11.  
  12.  
  13. if ENV['SNIPPLR_KEY'].nil?
  14. args = [
  15. 'ok-msgbox', '--no-cancel', '--title "Snipplr"',
  16. '--text "Environment variable SNIPPLR_KEY missing."', '2>/dev/null'
  17. ]
  18. %x{ #{ (CMD + args).join(" ") } }
  19. exit
  20. end
  21.  
  22.  
  23. # get tags from user
  24. args = [
  25. 'standard-inputbox', '--title "Get Snippet from Snipplr"',
  26. '--informative-text "Enter tags to search for"', '--string-output',
  27. '2>/dev/null'
  28. ]
  29.  
  30. user_response_for_tags = %x{#{(CMD + args).join(" ")}}.split("\n")
  31. if user_response_for_tags[0] == 'Cancel'
  32. exit
  33. end
  34.  
  35. # grab snippets from server
  36. matches = []
  37. begin
  38. matches = srv.call('snippet.list',ENV['SNIPPLR_KEY'],user_response_for_tags[1],'date')
  39. rescue
  40. args = [
  41. 'ok-msgbox', '--no-cancel', '--title "Snipplr"',
  42. "--text 'No matching snippet found.'", '2>/dev/null'
  43. ]
  44. %x{ #{ (CMD + args).join(" ") } }
  45. exit
  46. end
  47.  
  48. ## XML-RPC call may return multiple instances of identical item, so trim
  49. ## the hash to our needs
  50. snippet_title = {}
  51. matches.each do |h|
  52. if !snippet_title.has_key?(h['id'])
  53. snippet_title[h['id']] = h['title']
  54. end
  55. end
  56.  
  57. item_string = '' # for CocoaDialog option
  58. snippet_title.each_value do |s|
  59. item_string += %Q{ '#{s}' }
  60. end
  61.  
  62. args = [
  63. 'standard-dropdown', '--title "Snipplr"', '--text "Select Snippet"',
  64. '--exit-onchange', '--string-output', '--items ' + item_string,
  65. '2>/dev/null'
  66. ]
  67.  
  68. user_choice_for_snippet = %x{#{(CMD + args).join(" ")}}.split("\n")
  69. if user_choice_for_snippet[0] == 'Cancel'
  70. exit
  71. end
  72.  
  73. begin
  74. puts CGI.unescapeHTML(srv.call('snippet.get',snippet_title.index(user_choice_for_snippet[1]))["source"])
  75. rescue
  76. args = [
  77. 'ok-msgbox', '--no-cancel', '--title "Snipplr"',
  78. "--text 'No matching snippet found.'", '2>/dev/null'
  79. ]
  80. %x{ #{ (CMD + args).join(" ") } }
  81. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.