Skip to content

Instantly share code, notes, and snippets.

@youpy
Created February 17, 2012 14:34
Show Gist options
  • Save youpy/1853822 to your computer and use it in GitHub Desktop.
Save youpy/1853822 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
%w|ubygems pathname pit erb|.each {|g| require g }
def read_sections
sections = {}
section = nil
DATA.readlines.each do |line|
if line =~ /^@@\s+(.+)/
section = $1
elsif section
sections[section] ||= ''
sections[section] += line
end
end
return sections
end
def wait_for_upload(url)
until(`curl -I -X GET '#{url}'`.match(/200 OK/)) do
sleep 1
end
end
def main
name = ARGV.shift || 'sketch_%i' % Time.now.to_i
dir = (Pathname('~/Dropbox/Public/sketches/') + name).expand_path
dir.mkpath
dropbox_id = Pit.get('dropbox.com', :require => {
'id' => 'your Dropbox ID (http://dl.dropbox.com/u/XXXXX)'
})['id']
url = 'http://dl.dropbox.com/u/%i/sketches/%s/' % [dropbox_id, name]
read_sections.each do |filename, template|
open(dir + filename, 'w') do |f|
f.write(ERB.new(template).result(binding))
end
end
wait_for_upload(url + 'index.html')
print <<EOM
sketch is created.
url: #{url + 'index.html'}
path: #{dir}
to use guard-livereload:
$ gem install bundler
$ cd #{dir}
$ bundle install
$ bundle exec guard
EOM
system('open %s &' % (url + 'index.html'))
end
main
__END__
@@ index.html
<html>
<head>
<title><%= name %></title>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>
<script src='https://github.com/jashkenas/coffee-script/raw/master/extras/coffee-script.js'></script>
<script type="text/coffeescript" src='app.coffee'></script>
<link rel="stylesheet/tass" type="text/x-tass" href="styles.tass"/>
<script type="text/javascript" src="https://raw.github.com/cho45/tasscss/master/lib/tass.js"></script>
</head>
<body>
<h1><%= name %></h1>
<div></div>
</body>
</html>
@@ app.coffee
$ () ->
alert('hello')
@@ styles.tass
$base-color : #333;
$base-back : #fff;
body {
color: $base-color;
background: $base-back;
}
@@ Guardfile
url = '<%= url %>'
$etags = {}
def get_etag(url)
puts 'get etag: ' + url
`curl -s -I -X HEAD '#{url}'`.match(/etag: (.+)/i)[1]
end
def wait_for_upload(url)
count = 0
while((etag = get_etag(url)) == $etags[url] && count < 30) do
sleep 1
count += 1
end
$etags[url] = etag
end
guard :livereload do
watch(%r{.+.(tass|html|coffee)}) {|m|
full_url = url + m[0]
unless $etags[full_url]
$etags[full_url] = get_etag(full_url)
end
wait_for_upload(full_url)
m[0]
}
end
@@ Gemfile
source :rubygems
gem 'guard-livereload'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment