amerine / pasteez forked from timmyc/pasteez

cowpu

This URL has Read+Write access

pasteez / pasteez.rb
100644 64 lines (52 sloc) 1.154 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
require 'rubygems'
require 'sinatra'
require 'sequel'
require 'syntaxi'
 
DB = Sequel.connect(ENV['DATABASE_URL'] || 'sqlite://my.db')
 
# Database Model
class Snippet < Sequel::Model
 
  Syntaxi.line_number_method = 'floating'
  Syntaxi.wrap_at_column = 80
  
  def self.create_table
    DB.create_table :snippets do
      primary_key :id
      text :body
      timestamp :timestamp
    end
    rescue Sequel::DatabaseError
      #Assume Database is created.
  end
 
  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
 
configure do
  Snippet.create_table
end
 
# new
get '/' do
  erb :new
end
 
# create
post '/' do
  @snippet = Snippet.new(:body => params[:snippet_body], :timestamp => (params[:timestamp] || Time.now))
  if @snippet.save
    redirect "/#{@snippet.id}"
  else
    redirect '/'
  end
end
 
# show
get '/:id' do
  @snippet = Snippet[params[:id]]
  if @snippet
    erb :show
  else
    redirect '/'
  end
end
 
get '*' do
  redirect '/'
end