public
Description: Textmate bundle for www.attachr.com
Homepage: http://attachr.com
Clone URL: git://github.com/juretta/attachr-textmate-bundle.git
Search Repo:
Initial import
juretta (author)
Thu Mar 27 22:55:39 -0700 2008
commit  1ae61d950f9796ea117b48d4ab483b769202175a
tree    8b079c4cb155ca8cf7062f7d862784bc2deac68a
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
0
@@ -0,0 +1,23 @@
0
+<?xml version="1.0" encoding="UTF-8"?>
0
+<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
0
+<plist version="1.0">
0
+<dict>
0
+ <key>beforeRunningCommand</key>
0
+ <string>nop</string>
0
+ <key>command</key>
0
+ <string>#!/usr/bin/env ruby
0
+require "#{ENV['TM_BUNDLE_SUPPORT']}/lib/textmate.rb"
0
+@textmate = Attachr::Textmate.new
0
+print @textmate.insert_snippet_from_attachr</string>
0
+ <key>input</key>
0
+ <string>none</string>
0
+ <key>keyEquivalent</key>
0
+ <string>@I</string>
0
+ <key>name</key>
0
+ <string>Insert snippet</string>
0
+ <key>output</key>
0
+ <string>afterSelectedText</string>
0
+ <key>uuid</key>
0
+ <string>77A669EC-E98C-4346-8DBC-704173739854</string>
0
+</dict>
0
+</plist>
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
0
@@ -0,0 +1,23 @@
0
+<?xml version="1.0" encoding="UTF-8"?>
0
+<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
0
+<plist version="1.0">
0
+<dict>
0
+ <key>beforeRunningCommand</key>
0
+ <string>nop</string>
0
+ <key>command</key>
0
+ <string>#!/usr/bin/env ruby
0
+require "#{ENV['TM_BUNDLE_SUPPORT']}/lib/textmate.rb"
0
+url = Attachr::Textmate.new.post_to_attachr
0
+print "Your text was posted to #{url}\n" unless url.nil?</string>
0
+ <key>input</key>
0
+ <string>selection</string>
0
+ <key>keyEquivalent</key>
0
+ <string>@A</string>
0
+ <key>name</key>
0
+ <string>Post to attachr.com</string>
0
+ <key>output</key>
0
+ <string>showAsTooltip</string>
0
+ <key>uuid</key>
0
+ <string>7784EB57-5A35-4978-AA2B-A53C80F14A61</string>
0
+</dict>
0
+</plist>
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
0
@@ -0,0 +1,23 @@
0
+<?xml version="1.0" encoding="UTF-8"?>
0
+<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
0
+<plist version="1.0">
0
+<dict>
0
+ <key>beforeRunningCommand</key>
0
+ <string>nop</string>
0
+ <key>command</key>
0
+ <string>#!/usr/bin/env ruby
0
+require "#{ENV['TM_BUNDLE_SUPPORT']}/lib/textmate.rb"
0
+url = Attachr::Textmate.new.post_to_attachr_account
0
+print "Your text was posted to #{url}\n" unless url.nil?</string>
0
+ <key>input</key>
0
+ <string>selection</string>
0
+ <key>keyEquivalent</key>
0
+ <string>@S</string>
0
+ <key>name</key>
0
+ <string>Post to your attachr.com account</string>
0
+ <key>output</key>
0
+ <string>showAsTooltip</string>
0
+ <key>uuid</key>
0
+ <string>38F70904-7E39-4648-9166-97863FDCEB85</string>
0
+</dict>
0
+</plist>
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
0
@@ -0,0 +1,25 @@
0
+<?xml version="1.0" encoding="UTF-8"?>
0
+<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
0
+<plist version="1.0">
0
+<dict>
0
+ <key>beforeRunningCommand</key>
0
+ <string>nop</string>
0
+ <key>command</key>
0
+ <string>#!/usr/bin/env ruby
0
+require "#{ENV['TM_BUNDLE_SUPPORT']}/lib/textmate.rb"
0
+result = Attachr::Textmate.new.set_api_key
0
+if result
0
+ print "Api-Key succesfuly set."
0
+else
0
+ print "An error occured."
0
+end</string>
0
+ <key>input</key>
0
+ <string>none</string>
0
+ <key>name</key>
0
+ <string>Set API-Key</string>
0
+ <key>output</key>
0
+ <string>showAsTooltip</string>
0
+ <key>uuid</key>
0
+ <string>5CA1C376-FBDD-48D5-BC63-AD9D96294B73</string>
0
+</dict>
0
+</plist>
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
0
@@ -0,0 +1,108 @@
0
+#!/usr/bin/env ruby -w
0
+# $Id$
0
+# Author: Stefan Saasen <s@juretta.com>
0
+
0
+require 'open-uri'
0
+require 'net/http'
0
+require 'cgi'
0
+require "rexml/document"
0
+
0
+module Attachr
0
+ module API
0
+ class Remote
0
+ HOST = "attachr.com"
0
+ PORT = 80
0
+ PROTO = "http"
0
+
0
+ attr_accessor :api_key
0
+
0
+ # class methods
0
+ class << self
0
+ # returns a valid Host with protocol and port
0
+ def url
0
+ PROTO + '://' + HOST + ((PORT != 80) ? ":#{PORT}" : '') + '/'
0
+ end
0
+ end # class << self
0
+
0
+ # set api_key
0
+ def initialize(api_key = nil)
0
+ @api_key = api_key
0
+ end
0
+
0
+ # post text to Attachr
0
+ # posts as anonymous if @api_key.nil?
0
+ def attach(text, filter = nil)
0
+ options = {}
0
+ options['entry[body]'] = CGI.escape(text).strip
0
+
0
+ # Filter?
0
+ if filter
0
+ options['filter_key'] = filter
0
+ else
0
+ # Default filter_id
0
+ options['entry[filter_id]'] = "11"
0
+ end
0
+ # Api-Key?
0
+ unless @api_key.nil?
0
+ options['api_key'] = @api_key
0
+ end
0
+
0
+ response = _post(options)
0
+ doc = REXML::Document.new response.body
0
+ doc.root.elements['url'].text
0
+ end
0
+
0
+ #
0
+ def get_titles_of_latest_entries
0
+ xml = nil
0
+ res = Net::HTTP.start(HOST, PORT) do |http|
0
+ xml = http.get('/list', {'Accept' => 'text/xml'}).body
0
+ end
0
+ doc = REXML::Document.new xml
0
+ doc.root.elements['entries'].collect{|e| e.elements["title"].to_a[1].to_s.strip if e.respond_to?(:elements) }.reject{|e| e.nil? }
0
+ end
0
+
0
+ # Fetch text from attachr.com URL
0
+ def fetch_plain_from_url(url)
0
+ return "" unless url.match(/attachr\.com/)
0
+ trash, remainder = url.split("attachr\.com/")
0
+ remainder = remainder + "/plain" unless remainder.match(/plain/)
0
+ url = self.class::url + remainder
0
+ _fetch(url)
0
+ end
0
+
0
+ # Fetch text with id
0
+ # id -> 1234 if private == false
0
+ # id _> quieziou2z1231378123123 if private == true
0
+ def fetch(id, private = false)
0
+ url = self.class::url
0
+ if private
0
+ url = url + "private/"
0
+ end
0
+ url = url + id + "/plain"
0
+ _fetch(url)
0
+ end
0
+
0
+ private
0
+ def _post(options)
0
+ res = Net::HTTP.start(HOST, PORT) do |http|
0
+ # Post to Attachr
0
+ http.post('/attachr/create', options.map {|k,v| "&#{k}=#{v}"}.to_s, {'Accept' => 'text/xml'})
0
+ end
0
+ res
0
+ end
0
+
0
+ # fetch content from url
0
+ def _fetch(url)
0
+ open(url).read.gsub(/\r\n/, "\n")
0
+ end
0
+
0
+ end # class Remote
0
+ end # module API
0
+end # module Attachr
0
+
0
+# cat code.txt | attachr.rb
0
+if __FILE__ == $0
0
+ url = Attachr::API::Remote.new.attach(STDIN.read)
0
+ print "Your text was posted to #{url}\n"
0
+end
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
0
@@ -0,0 +1,14 @@
0
+# Simple interface for reading and writing Internet passwords to
0
+# the KeyChain.
0
+
0
+module KeyChain
0
+ class << self
0
+ def add_internet_password(user, proto, host, path, pass)
0
+ %x{security add-internet-password -a "#{user}" -s "#{host}" -r "#{proto}" -p "#{path}" -w "#{pass}"}
0
+ end
0
+ def find_internet_password(user, proto, host, path)
0
+ result = %x{security find-internet-password -g -a "#{user}" -s "#{host}" -p "#{path}" -r #{proto} 2>&1 >/dev/null}
0
+ result =~ /^password: "(.*)"$/ ? $1 : nil
0
+ end
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
0
@@ -0,0 +1,75 @@
0
+require 'test/unit'
0
+require 'open-uri'
0
+
0
+require File.dirname(__FILE__) + '/textmate'
0
+
0
+class MockStringIO < StringIO
0
+ include OpenURI::Meta
0
+end
0
+
0
+class MockRemote
0
+ attr_reader :text, :filter, :api_key
0
+ attr_writer :api_key
0
+ def fetch_plain_from_url(url)
0
+
0
+ end
0
+
0
+ def fetch(id, private)
0
+
0
+ end
0
+
0
+ def attach(text, filter=nil, api_key=nil)
0
+ @text, @filter, @api_key = text, filter, api_key
0
+ "1234"
0
+ end
0
+end
0
+
0
+
0
+class TC_Textmate < Test::Unit::TestCase
0
+
0
+ def setup
0
+ @remote = MockRemote.new
0
+ @textmate = Attachr::Textmate.new(@remote)
0
+ end
0
+
0
+ def test_post_to_attachr_ruby
0
+ ENV['TM_SCOPE'] = "source.ruby.rails"
0
+ ENV['TM_SELECTED_TEXT'] = "require 'hpricot'"
0
+ @textmate.post_to_attachr
0
+
0
+ assert_equal ENV['TM_SELECTED_TEXT'], @remote.text
0
+ assert_equal "ruby", @remote.filter
0
+ end
0
+
0
+ def test_post_to_attachr_account
0
+
0
+ end
0
+
0
+
0
+ def test_insert_snippet_from_attachr
0
+ @textmate.insert_snippet_from_attachr
0
+ end
0
+
0
+ def test_filter
0
+ scope = {
0
+ "source.ruby" => "ruby",
0
+ "source.ruby.rails" => "ruby",
0
+ "text.xml" => "xml",
0
+ "text.xml.plist met.tag.preprocessor" => "xml"
0
+ }
0
+ scope.each do |scope, f|
0
+ _test_filter(scope, f)
0
+ end
0
+ end
0
+
0
+ private
0
+ def _test_filter(scope, f)
0
+ ENV['TM_SCOPE'] = scope
0
+ ENV['TM_SELECTED_TEXT'] = "some text"
0
+ @textmate.post_to_attachr
0
+ assert_equal f, @remote.filter
0
+ end
0
+
0
+
0
+
0
+end
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
0
@@ -0,0 +1,125 @@
0
+# $Id$
0
+# Author: Stefan Saasen <s@juretta.com>
0
+# Version: 0.1
0
+$: << File.join(File.split(__FILE__).first, '.')
0
+
0
+
0
+require 'attachr'
0
+require 'open-uri'
0
+require 'tm_dialog'
0
+
0
+module Attachr
0
+
0
+ class Textmate
0
+
0
+ class PrefsError < SystemCallError; end
0
+
0
+ class << self
0
+
0
+
0
+ end # class << self
0
+
0
+ # ============================================================================================
0
+
0
+ ATTACHR_PREFS = File.expand_path("~/Library/Preferences/com.macromates.textmate.attachr.txt")
0
+
0
+ attr_writer :remote_api
0
+
0
+ def initialize(remote_api = nil)
0
+ unless remote_api.nil?
0
+ @remote_api = remote_api
0
+ else
0
+ @remote_api = Attachr::API::Remote.new
0
+ end
0
+ end
0
+
0
+ # fetch snippet from attachr.com
0
+ def insert_snippet_from_attachr
0
+ options = TmDialog.new.open_insert_dialog
0
+ url_or_id = options['url_or_id']
0
+ selected_id = options['selectedSnippet']
0
+ url_or_id = selected_id.gsub(/#/, "") unless selected_id.nil?
0
+ return "" if url_or_id.nil?
0
+ # Verarbeitung
0
+ private = false
0
+ id = ""
0
+ # Private id
0
+ if url_or_id.match(/([0-9a-z]{32})/i)
0
+ id = $1.dup
0
+ private = true
0
+ # Public id
0
+ elsif url_or_id.match(/([0-9]+)/)
0
+ id = $1.dup
0
+ # Attachr url
0
+ elsif url_or_id.match(/attachr\.com\/.+/)
0
+ return @remote_api.fetch_plain_from_url(url_or_id)
0
+ else
0
+ return "invalid attachr URL/id"
0
+ end
0
+
0
+ begin
0
+ return @remote_api.fetch(id, private)
0
+ rescue; end
0
+ end # post_to_attchr
0
+
0
+ # set new Api-Key
0
+ def set_api_key
0
+ api_key = self.class::standard_input_box("Insert API-Key", "Please insert your Api-Key")
0
+ return false if api_key.nil?
0
+ write_api_key(api_key)
0
+ true
0
+ end
0
+
0
+ # Post snippet to attachr.com using the API-Key
0
+ def post_to_attachr_account
0
+ begin
0
+ api_key = get_api_key
0
+ rescue
0
+ api_key = self.class::standard_input_box("Insert API-Key", "Please insert your Api-Key")
0
+ return "" if api_key.nil?
0
+ write_api_key(api_key)
0
+ end
0
+ _post_to_attachr(api_key)
0
+ end
0
+
0
+ def post_to_attachr
0
+ _post_to_attachr
0
+ end
0
+
0
+ def _post_to_attachr(api_key=nil)
0
+ filter = nil
0
+ if ENV['TM_SCOPE'].scan(/(text|source)\.([^\.]+)/).flatten.first
0
+ filter = $2.dup
0
+ end
0
+ text = ENV['TM_SELECTED_TEXT']
0
+ return nil if text.nil? || text.empty?
0
+ @remote_api.api_key = api_key
0
+ @remote_api.attach(text, filter)
0
+ end
0
+
0
+ private
0
+
0
+ def get_api_key
0
+ raise("Can't get Api-Key.") unless File.file?(ATTACHR_PREFS)
0
+ IO.readlines(ATTACHR_PREFS).each do |line|
0
+ if line =~ /^API-KEY:(.+)/
0
+ return $1.dup
0
+ end
0
+ end
0
+ raise "Can't get Api-Key."
0
+ end
0
+
0
+ def write_api_key(api_key)
0
+ File.open(ATTACHR_PREFS, "w") do |f|
0
+ f << "API-KEY:#{api_key}"
0
+ end
0
+ end
0
+
0
+ end
0
+end
0
+
0
+
0
+if __FILE__ == $0
0
+ result = Attachr::Textmate.new.insert_snippet_from_attachr
0
+ p result
0
+end
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
0
@@ -0,0 +1,14 @@
0
+<?xml version="1.0" encoding="UTF-8"?>
0
+<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
0
+<plist version="1.0">
0
+<dict>
0
+ <key>name</key>
0
+ <string>Attachr</string>
0
+ <key>ordering</key>
0
+ <array>
0
+ <string>77A669EC-E98C-4346-8DBC-704173739854</string>
0
+ </array>
0
+ <key>uuid</key>
0
+ <string>06DF2AA3-6121-41AF-9F7E-8FC0349658B6</string>
0
+</dict>
0
+</plist>
...
 
0
...
1
2
0
@@ -0,0 +1 @@
0
+http://macromates.com/wiki/Bundles/StyleGuide
0
\ No newline at end of file

Comments

    No one has commented yet.