Skip to content

Instantly share code, notes, and snippets.

@DivineDominion DivineDominion/Gemfile forked from erikpukinskis/lilwiki.rb
Last active May 11, 2016

Embed
What would you like to do?
Super short & minimal wiki using Sinatra and DataMapper
# A sample Gemfile
source "https://rubygems.org"
gem 'sinatra'
gem 'sinatra-authentication'
gem 'data_mapper'
gem 'dm-sqlite-adapter'
gem 'rdiscount'
gem 'haml'
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
You can’t perform that action at this time.