TextMate bundle posting 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.  
  6. CMD = [ ENV['TM_SUPPORT_PATH'] +
  7. '/bin/CocoaDialog.app/Contents/MacOS/CocoaDialog' ]
  8.  
  9. def ask_for(item='')
  10. if item.to_s.empty?
  11. return []
  12. end
  13. args = [
  14. 'standard-inputbox','--title "Post Snippet to Snipplr"',
  15. "--informative-text 'Enter the new snippets #{item}'",
  16. '--string-output', '2>/dev/null'
  17. ]
  18. return %x{#{(CMD + args).join(" ")}}.split("\n")
  19. end
  20.  
  21. srv = XMLRPC::Client.new2("http://snipplr.com/xml-rpc.php")
  22.  
  23.  
  24. if ENV['SNIPPLR_KEY'].nil?
  25. args = [
  26. 'ok-msgbox', '--no-cancel', '--title "Snipplr"',
  27. '--text "Environment variable SNIPPLR_KEY missing."', '2>/dev/null'
  28. ]
  29. %x{ #{ (CMD + args).join(" ") } }
  30. exit
  31. end
  32.  
  33. # Title
  34. user_response_for_title = ask_for('title')
  35. if user_response_for_title[0] == 'Cancel'
  36. exit
  37. end
  38.  
  39. # Tags
  40. user_response_for_tags = ask_for('tags')
  41. if user_response_for_tags[0] == 'Cancel'
  42. exit
  43. end
  44. user_response_for_tags[1] += ' textmatebundle' # shameless self-promotion
  45.  
  46. # Languages
  47. user_response_for_langs = nil
  48. while user_response_for_langs.nil?
  49. args = [
  50. 'inputbox', '--title "Snipplr"', '--informative-text "Choose Language for this snippet"',
  51. '--button1 "OK"', '--button2 "Cancel"', '--button3 "Show Legal Languages"',
  52. '--string-output', '2>/dev/null'
  53. ]
  54. user_response_for_langs = %x{ #{ (CMD + args).join(" ") } }.split("\n")
  55.  
  56.  
  57. if user_response_for_langs[0] == 'Cancel'
  58. exit
  59. elsif user_response_for_langs[0] == 'Show Legal Languages'
  60. informative_text = ''
  61. begin
  62. known_langs = srv.call('languages.list',ENV['SNIPPLR_KEY']).invert
  63. known_langs.sort {|a,b| a[1] <=> b[1]}.each do |k,v|
  64. informative_text += "#{k} (#{v})\n"
  65. end
  66. rescue
  67. informative_text << 'Failed to obtain known language list'
  68. end
  69. args = [
  70. 'msgbox', '--title "Snipplr"', '--text "Currently known languages"',
  71. '--informative-text "' + informative_text + '"', '--debug',
  72. '--button1 "OK"','2>/dev/null'
  73. ]
  74. %x{ #{ (CMD + args).join(" ") } }
  75. user_response_for_langs = nil
  76. redo
  77. end
  78. end
  79.  
  80.  
  81. # finally ready to post
  82. begin
  83. srv_response = srv.call('snippet.post',ENV['SNIPPLR_KEY'],
  84. user_response_for_title[1],
  85. ARGF.to_a.to_s,
  86. user_response_for_tags[1], user_response_for_langs[1])
  87. print "Successfully posted '#{user_response_for_title[1]}'"
  88. rescue
  89. args = [
  90. 'ok-msgbox', '--no-cancel', '--title "Snipplr"',
  91. "--text 'Posting new snippet failed: #{$!}'" , '2>/dev/null'
  92. ]
  93. %x{#{(CMD + args).join(" ")}}
  94. end
  95.  

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.