Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 325 lines (279 sloc) 6.696 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
#!/usr/bin/env ruby
#
# This file, gist, is generated code.
# Please DO NOT EDIT or send patches for it.
#
# Please take a look at the source from
# http://github.com/defunkt/gist
# and submit patches against the individual files
# that build gist.
#

module Gist
  module Manpage
    extend self

    def display(name)
      puts manpage(name)
    end

    def manpage(name)
      return "** Can't find groff(1)" unless groff?

      require 'open3'
      out = nil
      Open3.popen3(groff_command) do |stdin, stdout, _|
        stdin.puts raw_manpage(name)
        stdin.close
        out = stdout.read.strip
      end
      out
    end

    def raw_manpage(name)
      if File.exists? file = File.dirname(__FILE__) + "/../../man/#{name}.1"
        File.read(file)
      else
        DATA.read
      end
    end

    def groff?
      system("which groff > /dev/null")
    end

    def groff_command
      "groff -Wall -mtty-char -mandoc -Tascii"
    end

    def puts(*args)
      page_stdout
      super
    end

    def page_stdout
      return unless $stdout.tty?

      read, write = IO.pipe

      if Kernel.fork
        $stdin.reopen(read)
        read.close
        write.close

        ENV['LESS'] = 'FSRX'

        Kernel.select [STDIN]

        pager = ENV['PAGER'] || 'less -isr'
        pager = 'cat' if pager.empty?

        exec pager rescue exec "/bin/sh", "-c", pager
      else
        $stdout.reopen(write)
        $stderr.reopen(write) if $stderr.tty?
        read.close
        write.close
      end
    end
  end
end
module Gist
  VERSION = Version = '1.0.3'
end
require 'open-uri'
require 'net/http'
require 'optparse'

require 'gist/manpage' unless defined?(Gist::Manpage)
require 'gist/version' unless defined?(Gist::Version)

module Gist
  extend self

  GIST_URL = 'http://gist.github.com/%s.txt'
  CREATE_URL = 'http://gist.github.com/gists'

  PROXY = ENV['HTTP_PROXY'] ? URI(ENV['HTTP_PROXY']) : nil
  PROXY_HOST = PROXY ? PROXY.host : nil
  PROXY_PORT = PROXY ? PROXY.port : nil

  def execute(*args)
    private_gist = false
    gist_extension = nil

    opts = OptionParser.new do |opts|
      opts.banner = "Usage: gist [options] [filename or stdin]"

      opts.on('-p', '--private', 'Make the gist private') do
        private_gist = true
      end

      t_desc = 'Set syntax highlighting of the Gist by file extension'
      opts.on('-t', '--type [EXTENSION]', t_desc) do |extension|
        gist_extension = '.' + extension
      end

      opts.on('-m', '--man', 'Print manual') do
        Gist::Manpage.display("gist")
      end

      opts.on('-h', '--help', 'Display this screen') do
        puts opts
        exit
      end
    end

    opts.parse!(args)

    begin
      if $stdin.tty?

        if args.empty?
          puts opts
          exit
        end

        if File.exists?(file = args[0])
          input = File.read(file)
          gist_extension = File.extname(file) if file.include?('.')
        else
          abort "Can't find #{file}"
        end
      else
        input = $stdin.read
      end

      url = write(input, private_gist, gist_extension)
      browse(url)
      puts copy(url)
    rescue => e
      warn e
      puts opts
    end
  end

  def write(content, private_gist = false, gist_extension = nil)
    url = URI.parse(CREATE_URL)

    proxy = Net::HTTP::Proxy(PROXY_HOST, PROXY_PORT)
    req = proxy.post_form(url, data(nil, gist_extension, content, private_gist))

    req['Location']
  end

  def read(gist_id)
    open(GIST_URL % gist_id).read
  end

  def browse(url)
    if RUBY_PLATFORM =~ /darwin/
      `open #{url}`
    end
  end

  def copy(content)
    cmd = case true
    when system("which pbcopy &> /dev/null")
      :pbcopy
    when system("which xclip &> /dev/null")
      :xclip
    when system("which putclip &> /dev/null")
      :putclip
    end

    if cmd
      IO.popen(cmd.to_s, 'r+') { |clip| clip.print content }
    end

    content
  end

private
  def data(name, ext, content, private_gist)
    return {
      'file_ext[gistfile1]' => ext ? ext : '.txt',
      'file_name[gistfile1]' => name,
      'file_contents[gistfile1]' => content
    }.merge(private_gist ? { 'action_button' => 'private' } : {}).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
Gist.execute(*ARGV)
__END__
.\" generated with Ron/v0.3
.\" http://github.com/rtomayko/ron/
.
.TH "GIST" "1" "February 2010" "GITHUB" "Gist Manual"
.
.SH "NAME"
\fBgist\fR \-\- gist on the command line
.
.SH "SYNOPSIS"
\fBgist\fR [\fB\-p\fR] [\fB\-t extension\fR] \fIFILE\fR
.
.SH "DESCRIPTION"
\fBgist\fR can be used to create gists on gist.github.com from the command
line. There are two primary methods of creating gists.
.
.P
If standard input is supplied, it will be used as the content of the
new gist. If \fIFILE\fR is provided, the content of that file will be used
to create the gist.
.
.P
Once your gist is successfully created, the URL will be copied to your
clipboard. If you are on OS X, \fBgist\fR will open the gist in your
browser, too.
.
.SH "OPTIONS"
\fBgist\fR's default mode of operation is to read content from standard
input and create a public, text gist from it, tied to your GitHub
account if you user and token are provided (see \fBCONFIGURATION\fR).
.
.P
These options can be used to change this behavior:
.
.TP
\fB\-p\fR, \fB\-\-private\fR
Create a private gist instead of a public gist.
.
.TP
\fB\-t\fR, \fB\-\-type\fR
Set the file extension explicitly. Passing a type of \fBrb\fR ensure
the gist is created as a Ruby file.
.
.P
You may additionally ask for help:
.
.TP
\fB\-h\fR, \fB\-\-help\fR
Print help.
.
.TP
\fB\-m\fR, \fB\-\-man\fR
Display this man page.
.
.SH "CONFIGURATION"
Use git\-config(1) to display the currently configured GitHub username:
.
.IP "" 4
.
.nf

$ git config \-\-global github.user
.
.fi
.
.IP "" 0
.
.P
Or, set the GitHub username with:
.
.IP "" 4
.
.nf

$ git config \-\-global github.user <username>
.
.fi
.
.IP "" 0
.
.P
See \fIhttp://github.com/guides/local\-github\-config\fR for more
information.
.
.P
\fBgist\fR will check the \fBHTTP_PROXY\fR env variable if supplied:
.
.IP "" 4
.
.nf

$ HTTP_PROXY=http://host:port/ gist script.py
.
.fi
.
.IP "" 0
.
.SH "EXAMPLES"
.
.nf

$ gist < file.txt
$ echo secret | gist \-\-private
$ echo "puts :hi" | gist \-t rb
$ gist script.py
.
.fi
.
.SH "BUGS"
\fIhttp://github.com/defunkt/gist/issues\fR
.
.SH "AUTHOR"
Chris Wanstrath :: chris@ozmm.org
.
.SH "SEE ALSO"
hub(1), git(1), git\-clone(1),\fIhttp://github.com\fR, \fIhttp://github.com/defunkt/gist\fR
Something went wrong with that request. Please try again.