Created
September 30, 2012 16:24
-
-
Save jhollinger/3807389 to your computer and use it in GitHub Desktop.
A simple clipboard in Sinatra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
CLIPBOARD = ENV['CLIPBOARD'] || '/tmp/www-clipboard' | |
get '/' do | |
@data = File.readlines(CLIPBOARD) rescue [] | |
erb :form | |
end | |
post '/paste' do | |
unless params[:data].to_s.empty? | |
mode = params[:clear] ? 'w+' : 'a+' | |
File.open(CLIPBOARD, mode) { |f| f.puts params[:data] } | |
end | |
redirect '/' | |
end | |
__END__ | |
@@layout | |
<!doctype html> | |
<html> | |
<head> | |
<title>Clipboard</title> | |
</head> | |
<body> | |
<%= yield %> | |
</body> | |
</html> | |
@@form | |
<form action="/paste" method="post"> | |
<label for="data">Data to paste:</label><br /> | |
<textarea id="data" name="data" rows="10" cols="55" autofocus="autofocus"></textarea> | |
<input type="checkbox" id="clear" name="clear" /><label for="clear">Clear existing pastes</label><br /> | |
<input type="submit" value="Paste" /> | |
</form> | |
<h3>Previously pasted data</h3> | |
<%= @data.reverse.map { |data| "<p>#{data.chomp}</p>" }.join "\n" %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment