diff --git a/README b/README new file mode 100644 index 0000000..f50c0ff --- /dev/null +++ b/README @@ -0,0 +1,4 @@ +Toopaste +======== + +A simple (and tiny) Pastie clone written using Sinatra and DataMapper. diff --git a/toopaste.rb b/toopaste.rb new file mode 100644 index 0000000..3c0b047 --- /dev/null +++ b/toopaste.rb @@ -0,0 +1,53 @@ +require 'sinatra' +require 'dm-core' +require 'dm-validations' +require 'dm-timestamps' +require 'syntaxi' + +DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/toopaste.sqlite3") + +class Snippet + include DataMapper::Resource + + property :id, Integer, :serial => true # primary serial key + property :body, Text, :nullable => false # cannot be null + property :created_at, DateTime + property :updated_at, DateTime + + validates_present :body + validates_length :body, :minimum => 1 + + Syntaxi.line_number_method = 'floating' + Syntaxi.wrap_at_column = 80 + #Syntaxi.wrap_enabled = false + + def formatted_body + replacer = Time.now.strftime('[code-%d]') + html = Syntaxi.new("[code lang='ruby']#{self.body.gsub('[/code]', replacer)}[/code]").process + "
#{html.gsub(replacer, '[/code]')}
" + end +end + +DataMapper.auto_upgrade! +#File.open('toopaste.pid', 'w') { |f| f.write(Process.pid) } + +# new +get '/' do + erb :new +end + +# create +post '/' do + @snippet = Snippet.new(:body => params[:snippet_body]) + if @snippet.save + redirect "/#{@snippet.id}" + else + redirect '/' + end +end + +# show +get '/:id' do + @snippet = Snippet.get(params[:id]) + erb :show +end diff --git a/views/layout.erb b/views/layout.erb new file mode 100644 index 0000000..c2fe590 --- /dev/null +++ b/views/layout.erb @@ -0,0 +1,63 @@ + + + + <%= @title || 'Toopaste!' %> + + + + <%= yield %> + + diff --git a/views/new.erb b/views/new.erb new file mode 100644 index 0000000..0606fd5 --- /dev/null +++ b/views/new.erb @@ -0,0 +1,6 @@ +
+
+ +
+
+
diff --git a/views/show.erb b/views/show.erb new file mode 100644 index 0000000..ecf6633 --- /dev/null +++ b/views/show.erb @@ -0,0 +1,5 @@ +
+
<%= @snippet.formatted_body %>
+
Created on <%= @snippet.created_at.strftime("%B %d, %Y at %I:%M %p") %>
+
New Paste! +