forked from semanticart/hoboken
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki.rb
74 lines (62 loc) · 2.08 KB
/
wiki.rb
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
65
66
67
68
69
70
71
72
73
74
require 'rubygems'
require 'sinatra'
configure do
%w(dm-core dm-is-versioned dm-timestamps dm-tags haml wikitext dm-migrations).each { |lib| require lib }
require "./article"
ROOT = File.expand_path(File.dirname(__FILE__))
DataMapper.setup(:default, ENV['DATABASE_URL'] || 'postgres://localhost/mydb')
DataMapper.auto_upgrade!
PARSER = Wikitext::Parser.new(:external_link_class => 'external', :internal_link_prefix => nil)
end
helpers do
# break up a CamelCased word into something more readable
# this is used when you create a new page by visiting /NewItem
def de_wikify(phrase)
phrase.gsub(/(\w)([A-Z])/, "\\1 \\2")
end
def friendly_time(time)
time.strftime("%a. %b. %d, %Y, %I:%M%p")
end
end
get '/' do
@article = Article.first_or_create(:slug => 'Index')
@recent = Article.all(:order => [:updated_at.desc], :limit => 10)
haml :show
end
post '/' do
@article = Article.first_or_create(:slug => params[:slug])
unless params[:preview] == '1'
@article.update(:title => params[:title], :body => params[:body], :slug => params[:slug], :tag_list => params[:tag_list])
redirect "/#{params[:slug].gsub(/^index$/i, '')}"
else
haml :edit, :locals => {:action => ["Editing", "Edit"]}
end
end
get '/tags/:tag_name' do
tag = Tag.first(:name => params[:tag_name])
@tagged_articles = Tagging.all(:tag_id => tag.id, :order => [:id.desc]).map{|t| t.taggable}
@title = "Items tagged "#{params[:tag_name]}""
haml :show_tag
end
get '/:slug' do
@article = Article.first(:slug => params[:slug])
if @article
haml :show
else
@article = Article.new(:slug => params[:slug], :title => de_wikify(params[:slug]))
haml :edit, :locals => {:action => ["Creating", "Create"]}
end
end
get '/:slug/history' do
@article = Article.first(:slug => params[:slug])
haml :history
end
get '/:slug/edit' do
@article = Article.first(:slug => params[:slug])
haml :edit, :locals => {:action => ["Editing", "Edit"]}
end
post '/:slug/edit' do
@article = Article.first(:slug => params[:slug])
@article.body = params[:body] if params[:body]
haml :revert
end