Created
May 18, 2011 00:03
-
-
Save statianzo/977708 to your computer and use it in GitHub Desktop.
BinOfPaste
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' | |
require 'dm-core' | |
require 'dm-validations' | |
require 'dm-timestamps' | |
require 'dm-serializer' | |
require 'syntaxi' | |
require 'haml' | |
require 'sass' | |
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/pastebin.sqlite3") | |
class Snippet | |
include DataMapper::Resource | |
property :id, Serial | |
property :body, Text, :nullable => false | |
property :created_at, DateTime | |
property :updated_at, DateTime | |
Syntaxi.line_number_method = 'floating' | |
Syntaxi.wrap_at_column = 80 | |
def formatted_body | |
replacer = Time.now.strftime('[code-%d]') | |
html = Syntaxi.new("[code lang='ruby']#{self.body.gsub('[/code]', replacer)}[/code]").process | |
"<div class=\"syntax syntax_ruby\">#{html.gsub(replacer, '[/code]')}</div>" | |
end | |
end | |
DataMapper.auto_upgrade! | |
get '/' do | |
haml :new | |
end | |
post '/' do | |
@snippet = Snippet.new(:body => params[:snippet_body]) | |
if @snippet.save | |
redirect "/#{@snippet.id}" | |
else | |
redirect '/' | |
end | |
end | |
get '/application.css' do | |
content_type :css | |
sass :application | |
end | |
get '/:id.json' do | |
@snippet = Snippet.get(params[:id]) | |
content_type :json | |
if @snippet | |
@snippet.to_json | |
else | |
Snippet.new().to_hash.to_json | |
end | |
end | |
get '/:id' do | |
@snippet = Snippet.get(params[:id]) | |
if @snippet | |
haml :show | |
else | |
redirect '/' | |
end | |
end |
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
!!! strict | |
%html | |
%head | |
%title | |
= @title || 'Pastebin!' | |
%link{:rel => 'stylesheet', :type => 'text/css', :href => 'application.css'} | |
%body | |
= yield |
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
%div.snippet | |
%form{:action => '/', :method => 'POST'} | |
%textarea#snippet_body{:name => 'snippet_body', :rows => '20'} | |
%input{:type => 'submit', :value => 'Submit'} |
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
%div.snippet | |
%div.sbody#content | |
= @snippet.formatted_body | |
%div.sdate | |
Created On | |
= @snippet.created_at.strftime("%B %d, %Y at %I:%M %p") | |
%br | |
%a{:href => '/'} | |
New Paste! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment