-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathgist.rb
More file actions
executable file
·152 lines (133 loc) · 4.18 KB
/
gist.rb
File metadata and controls
executable file
·152 lines (133 loc) · 4.18 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Copyright 2008 Chris Wanstrath
# Taken from defunkt's gist repository: http://github.com/defunkt/gist/tree/master
require 'open-uri'
require 'net/http'
require 'net/https'
module Gist
extend self
@@gist_url = 'https://gist.github.com/%s.txt'
@@files = []
def read(gist_id)
return help if gist_id == '-h' || gist_id.nil? || gist_id[/help/]
open(@@gist_url % gist_id).read
end
def add_file(name, content)
load_files
@@files << {'name' => name, 'content' => content}
puts "#{name} added."
save_files
end
def send(private_gist)
load_files
url = URI.parse('https://gist.github.com/gists')
req = Net::HTTP::Post.new(url.path)
req.set_form_data(data(private_gist))
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_PEER
https.ca_file = File.join(File.dirname(__FILE__), "cacert.pem")
res = https.start {|http| http.request(req) }
case res
when Net::HTTPBadRequest
print "Ewww, not your fault, but something bad happened. No gist created."
when Net::HTTPFound
url = copy res['Location']
print "Created gist at #{url}. URL copied to clipboard."
end
clear
end
def clear
@@files = []
save_files
end
def process_selection
selection = nil
gistname = nil
if ENV['TM_SELECTED_TEXT']
selection = ENV['TM_SELECTED_TEXT']
gistname = "snippet" << "." << get_extension
else
selection = STDIN.read
gistname = get_gist_name
end
add_file(gistname, selection)
end
# Add extension for supported modes based on TM_SCOPE
# Cribbed from http://github.com/defunkt/gist.el/tree/master/gist.el
def get_extension
scope = ENV["TM_SCOPE"].split[0]
case scope
when /source\.actionscript$/ ; "as"
when /source\.c$/, "source.objc" ; "c"
when /source\.c\+\+$/, "source.objc++" ; "cpp"
# common-lisp-mode ; "el"
when /source\.css$/ ; "css"
when /source\.diff$/, "meta.diff.range" ; "diff"
# emacs-lisp-mode ; "el"
when /source\.erlang$/ ; "erl"
when /source\.haskell$/, "text.tex.latex.haskel" ; "hs"
when /text\.html\.markdown$/ ; "md"
when /text\.html$/ ; "html"
when /source\.io$/ ; "io"
when /source\.java$/ ; "java"
when /source\.js$/ ; "js"
# jde-mode ; "java"
# js2-mode ; "js"
when /source\.lua$/ ; "lua"
when /source\.ocaml$/ ; "ml"
when /source\.objc$/, "source.objc++" ; "m"
when /source\.perl$/ ; "pl"
when /source\.php$/ ; "php"
when /source\.python$/ ; "sc"
when /source\.ruby$/ ; "rb" # Emacs bundle uses rbx
when /text\.plain$/ ; "txt"
when /source\.sql$/ ; "sql"
when /source\.scheme$/ ; "scm"
when /source\.smalltalk$/ ; "st"
when /source\.shell$/ ; "sh"
when /source\.tcl$/, "text.html.tcl" ; "tcl"
when /source\.lex$/ ; "tex"
when /text\.xml$/, /text.xml.xsl$/, /source.plist$/, /text.xml.plist$/ ; "xml"
else "txt"
end
end
def get_gist_name
if filepath = ENV['TM_FILEPATH']
ENV['TM_PROJECT_DIRECTORY'] ? filepath.sub(ENV['TM_PROJECT_DIRECTORY'], '') : File.basename(filepath)
else
"file" << "." << get_extension
end
end
private
def load_files
path = File.join(File.dirname(__FILE__), 'tmp_gists')
save_files unless File.exists?(path)
@@files = Marshal.load(File.read(path))
@@files ||= []
end
def save_files
path = File.join(File.dirname(__FILE__), 'tmp_gists')
File.open(path, 'w') {|f| f.puts Marshal.dump(@@files) }
end
def copy(content)
return content if `which pbcopy`.strip == ''
IO.popen('pbcopy', 'r+') { |clip| clip.puts content }
content
end
def data(private_gist)
params = {}
@@files.each_with_index do |file, i|
params.merge!({
"file_ext[gistfile#{i+1}]" => '',
"file_name[gistfile#{i+1}]" => file['name'],
"file_contents[gistfile#{i+1}]" => file['content']
})
end
params.merge(private_gist ? { 'private' => 'on' } : {}).merge(auth)
end
def auth
user = `git config --global github.user`.strip
token = `git config --global github.token`.strip
user.empty? ? {} : { 'login' => user, 'token' => token }
end
end