├── .gitignore ├── .gemified ├── README.rdoc ├── bin └── gistr └── lib └── gistr.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | -------------------------------------------------------------------------------- /.gemified: -------------------------------------------------------------------------------- 1 | --- 2 | :author: Eric Lindvall 3 | :dependencies: 4 | - hpricot 5 | - activesupport 6 | :name: gistr 7 | :email: eric@5stops.com 8 | -------------------------------------------------------------------------------- /README.rdoc: -------------------------------------------------------------------------------- 1 | = Gistr 2 | 3 | A simple tool to post gists to tumblr. 4 | 5 | 6 | == The Problem 7 | 8 | Tumblr does not have a way to post code snippets simply and the current solutions are lacking. 9 | 10 | 11 | == CSS changes 12 | 13 | To make everything pretty, I have added the following CSS rules to my tumblr 14 | 15 | .gist { 16 | line-height: 1.3em !important; 17 | color: #ffffff !important; 18 | } 19 | 20 | .gist .gist-file { 21 | border: 0 !important; 22 | } 23 | 24 | .gist .gist-file .gist-data { 25 | background-color: transparent !important; 26 | padding-bottom: 1em; 27 | } 28 | -------------------------------------------------------------------------------- /bin/gistr: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require File.expand_path(File.dirname(__FILE__) + '/../lib/gistr') 4 | 5 | require 'optparse' 6 | require 'ostruct' 7 | 8 | opts = OpenStruct.new 9 | 10 | op = OptionParser.new do |o| 11 | o.on "-e", "--email=EMAIL", "Tumblr email address" do |email| 12 | opts.email = email 13 | end 14 | 15 | o.on "-p", "--password=PASSWORD", "Tumblr password" do |password| 16 | opts.password = password 17 | end 18 | 19 | o.on "-t", "--title=TITLE", "Tumblr post title" do |title| 20 | opts.title = title 21 | end 22 | 23 | o.on "-b", "--blog=BLOG", "Tumblr blog" do |blog| 24 | opts.blog = blog 25 | end 26 | end 27 | 28 | gist_id = *op.parse!(ARGV) 29 | 30 | unless gist_id 31 | puts op 32 | exit(1) 33 | end 34 | 35 | g = Gistr.new(gist_id) 36 | 37 | g.post opts.email, opts.password, opts.title, opts.blog 38 | 39 | -------------------------------------------------------------------------------- /lib/gistr.rb: -------------------------------------------------------------------------------- 1 | require 'open-uri' 2 | require 'hpricot' 3 | 4 | class Gistr 5 | def initialize(gist_id) 6 | @gist_id = gist_id 7 | end 8 | 9 | def post(email, password, title, blog) 10 | post_to_tumblr(email, password, title, blog, gist_code) 11 | end 12 | 13 | def gist_code 14 | return @gist_code if @gist_code 15 | 16 | # Use the secret .pibb format 17 | code = open("http://gist.github.com/#{@gist_id}.pibb").read 18 | 19 | # Remove the
tag
20 | #
21 | # Tumblr wrap lines at of HTML at times and having the additional
22 | # whitespace in a is very undesirable.
23 | h = Hpricot(code)
24 |
25 | h.search('pre').each do |pre|
26 | pre.swap(pre.inner_html) if pre.inner_html.length > 0
27 | end
28 |
29 | @gist_code = h.to_html
30 | end
31 |
32 | private
33 | def post_to_tumblr(email, password, title, blog, body)
34 | post_private = 1
35 | Net::HTTP.start("www.tumblr.com") do |http|
36 | req = Net::HTTP::Post.new("/api/write")
37 | req.set_form_data :email => email, :password => password,
38 | :title => title, :body => body, :format => 'html',
39 | :private => post_private, :group => blog
40 |
41 | http.request(req)
42 | end
43 | end
44 | end
45 |
--------------------------------------------------------------------------------