-
Notifications
You must be signed in to change notification settings - Fork 2
/
weaky.rb
144 lines (120 loc) · 2.64 KB
/
weaky.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require 'rubygems'
require 'sinatra/base'
require 'couchrest'
require 'couchrest_extended_document'
require 'maruku'
require 'haml'
require 'sass'
$couch_url = nil
if ENV['COUCH_URL']
$couch_url = ENV['COUCH_URL']
elsif ARGV[0]
$couch_url = 'http://' + ARGV[0].to_s.gsub(/^http:\/\//,"")
else
puts 'Usage:'
puts ' ruby weaky.rb http://localhost:5984/weaky'
puts ' ruby weaky.rb localhost:5984/weaky'
puts
puts ' Or set the environment variable COUCH_URL to the database URL'
exit 1
end
$weaky = CouchRest.database!($couch_url)
class Item < CouchRest::ExtendedDocument
use_database $weaky
view_by :name
property :name
property :body
WIKI_LINK_REGEX = /\[\[[A-Za-z0-9_\- ]+\]\]/
ESCAPE_FOR_MARUKU = /[\[\]]/
def escape(markdown)
markdown.gsub(ESCAPE_FOR_MARUKU) { |s| '\\' + s }
end
def linkify(html)
html.gsub(WIKI_LINK_REGEX) do |name_with_brackets|
name = name_with_brackets[2..-3]
items = Item.by_name(:key => name)
cls = items.count == 0 && 'missing' || ''
"<a href=\"/#{ name }\" class=\"#{ cls }\">#{ name }</a>"
end
end
def body_html
linkify(Maruku.new(escape(body)).to_html)
end
def new_url
'/new/' + name
end
def url
'/' + name
end
def id_url
"/id/#{id}"
end
def edit_url
'/edit/' + id
end
def delete_url
id && '/delete/' + id
end
end
class Weaky < Sinatra::Base
get '/' do
redirect '/home'
end
get '/:name.css' do
content_type 'text/css', :charset => 'utf-8'
sass :"/#{params[:name]}"
end
get '/items/all' do
@items = Item.all
haml :all
end
get '/new/:name' do
@item = Item.new(:name => params[:name])
@action = '/save'
haml :edit
end
post '/save' do
item = Item.new(:name => params[:name], :body => params[:body])
item.save
redirect item.url
end
get '/:name' do
items = Item.by_name(:key => params[:name])
if items.count == 1 then
@item = items.first
haml :show
elsif items.count == 0 then
@item = Item.new(:name => params[:name])
redirect @item.new_url
else
@items = items
@title = "disambiguation"
haml :disambiguation
end
end
get '/id/:id' do
@item = Item.get(params[:id])
haml :show
end
get '/edit/:id' do
@item = Item.get(params[:id])
@action = @item.id_url
haml :edit
end
post '/id/:id' do
item = Item.get(params[:id])
item.name = params[:name]
item.body = params[:body]
item.save
redirect item.url
end
post '/delete/:id' do
item = Item.get(params[:id])
url = item.url
item.destroy
redirect url
end
end
if __FILE__ == $0
Weaky.run!
end