-
-
Save DivineDominion/b08613cebed5deff4268c6a345ef0633 to your computer and use it in GitHub Desktop.
Super short & minimal wiki using Sinatra and DataMapper
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
# A sample Gemfile | |
source "https://rubygems.org" | |
gem 'sinatra' | |
gem 'sinatra-authentication' | |
gem 'data_mapper' | |
gem 'dm-sqlite-adapter' | |
gem 'rdiscount' | |
gem 'haml' |
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 'bundler' | |
require 'sinatra' | |
require 'rdiscount' | |
require 'haml' | |
require 'data_mapper' | |
require 'sinatra-authentication' | |
use Rack::Session::Cookie, 'shhhhhhhhhhhhhhh~' | |
class Page | |
include DataMapper::Resource | |
property :id, Serial | |
property :title, String, :unique => true | |
property :body, Text | |
def html | |
RDiscount.new(body).to_html | |
end | |
def slug | |
title ? title.downcase.gsub(/[^a-z0-9 ]*/, '').gsub(/ /, '_') : 'home' | |
end | |
end | |
class DmUser | |
property :approved, Boolean, :default => false | |
property :admin, Boolean, :default => false | |
end | |
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/test.db") | |
DataMapper.auto_upgrade! | |
unless Page.first(:title => 'home') | |
Page.create(:title => 'home', :body => "Welcome to Lil' Wiki!\n\n[A Page To Create](/A Page To Create)") | |
end | |
get '/' do | |
show_page('home') | |
end | |
get '/:title' do | |
show_page(params[:title]) | |
end | |
get '/:title/edit' do | |
if approved? | |
@page = Page.first(:title => params[:title]) | |
haml :edit | |
else | |
haml :error | |
end | |
end | |
post '/posts/:title' do | |
if approved? | |
@page = Page.first(:title => params[:title]) || Page.create(:title => params[:title]) | |
@page.update(params) | |
redirect "/#{params[:title]}" | |
else | |
haml :error | |
end | |
end | |
def approved? | |
current_user && current_user.approved? | |
end | |
def show_page(title) | |
@page = Page.first(:title => title) | |
if @page | |
haml :page | |
else | |
@page = Page.new(:title => title) | |
haml :edit | |
end | |
end | |
__END__ | |
@@ layout | |
!!! XML | |
!!! | |
%html | |
%head | |
%title Wiki | |
%body | |
%ul | |
%li | |
%a{:href => '/'} Home | |
- if @page | |
%li | |
%a{:href => "/#{@page.slug}/edit"} Edit this page | |
- if logged_in? | |
%li | |
%a{:href => '/logout'} Log out | |
- else | |
%li | |
%a{:href => '/login'} Log in | |
%li | |
%a{:href => '/signup'} Sign up | |
#page= yield | |
@@ page | |
%h1= @page.title | |
#body= @page.html | |
@@ edit | |
%h1= "Editing #{@page.title}" | |
%form{:method => "post", :action => "/posts/#{@page.title}"} | |
%textarea{:name => "body"}= @page.body | |
%br | |
%input{:type => "submit"} | |
@@ error | |
You need to be approved before you can edit. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment