Created
May 18, 2011 07:43
-
-
Save ssig33/978152 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 'active_record' | |
require 'haml' | |
require 'bluecloth' | |
require 'cgi' | |
require 'rack/csrf' | |
require 'logger' | |
ActiveRecord::Base.establish_connection( | |
:adapter => 'sqlite3', | |
:database => 'production.sqlite3' | |
) | |
ActiveRecord::Base.logger = Logger.new("./database.log") | |
#ActiveRecord::Migrator.migrate("migrate/", nil) | |
class Page < ActiveRecord::Base | |
def html | |
BlueCloth.new(self.body).to_html rescue "<pre>#{self.body}</pre>" | |
end | |
def haml | |
ary = self.body.split("\n") | |
str = "" | |
ary.shift | |
ary.each{|a| str += a} | |
Haml::Engine.new(str).render | |
end | |
end | |
configure do | |
set :logging, false | |
set :app_file, __FILE__ | |
use Rack::Session::Cookie, :secret => '' | |
use Rack::Csrf, :raise => true | |
end | |
get '/' do | |
@page = Page.where(:name => "index").order("created_at desc").first | |
redirect '/edit/index' unless @page | |
haml :page | |
end | |
get '/edit/:id' do | |
@page = Page.where(:name => params[:id]).order("created_at desc").first | |
@page = Page.new if @page == nil | |
haml :edit | |
end | |
get '/:id' do | |
file = open("public/#{params[:id]}/index.html").read rescue nil | |
return file if file | |
@page = Page.where(:name => params[:id]).order("created_at desc").first | |
redirect "/edit/#{params[:id]}" unless @page | |
return @page.haml if @page.body.split("\n").first.chop == "haml" | |
return haml :page | |
end | |
post '/update' do | |
raise if params[:password] != "CHANGE ME" | |
page = Page.new | |
page.name = params[:id] | |
page.body = params[:body] | |
page.save | |
redirect "/#{params[:id]}" | |
end | |
post '/destroy' do | |
raise if params[:password] != "CHANGE ME" | |
Page.where(:name => params[:id]).delete_all | |
redirect "/" | |
end | |
helpers do | |
def h str | |
CGI.escapeHTML str.to_s | |
end | |
def title | |
if request.path_info == "/" or request.path_info == "/index" | |
return "ssig33.com" | |
else | |
return "ssig33.com - #{@page.name}" | |
end | |
end | |
end | |
__END__ | |
@@ page | |
<!DOCTYPE html> | |
%meta{:charset => "UTF-8"} | |
%title=h title | |
%link{:href => "http://ssig33.com/common.css", :media => "screen", :rel => "stylesheet", :type => "text/css"} | |
%meta{:name => "viewport", :content => "width=320, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"} | |
%div#[email protected] | |
@@ edit | |
<!DOCTYPE html> | |
%meta{:charset => "UTF-8"} | |
%title=h "Edit - #{params[:id]}" | |
%link{:href => "http://ssig33.com/common.css", :media => "screen", :rel => "stylesheet", :type => "text/css"} | |
%meta{:name => "viewport", :content => "width=320, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"} | |
%div#all | |
%form{:action => "/update", :method => "post"} | |
=Rack::Csrf.csrf_tag(env) | |
%input{:id => "id", :name => "id", :type => "hidden", :value => "#{params[:id]}"} | |
%p | |
%textarea{:cols => "80", :id => "bodY", :name => "body", :rows => "30"}[email protected] #rescue "" | |
%p | |
%input{:id => "password", :name => "password", :type => "password"} | |
%p | |
%input{:name => "commit", :type => "submit", :value => "Save changes"} | |
%form{:action => "/destroy", :method => "post"} | |
=Rack::Csrf.csrf_tag(env) | |
%input{:id => "id", :name => "id", :type => "hidden", :value => "#{params[:id]}"} | |
%p | |
%input{:id => "password", :name => "password", :type => "password"} | |
%p | |
%input{:name => "commit", :type => "submit", :value => "Destroy"} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment