forked from sr/git-wiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-wiki.rb
executable file
·220 lines (182 loc) · 4.35 KB
/
git-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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
require "sinatra/base"
require "haml"
require "grit"
require "rdiscount"
module GitWiki
class << self
attr_accessor :homepage, :extension, :repository
end
def self.new(repository, extension, homepage)
self.homepage = homepage
self.extension = extension
self.repository = Grit::Repo.new(repository)
App
end
class PageNotFound < Sinatra::NotFound
attr_reader :name
def initialize(name)
@name = name
end
end
class Page
def self.find_all
return [] if repository.tree.contents.empty?
repository.tree.contents.collect { |blob| new(blob) }
end
def self.find(name)
page_blob = find_blob(name)
raise PageNotFound.new(name) unless page_blob
new(page_blob)
end
def self.find_or_create(name)
find(name)
rescue PageNotFound
new(create_blob_for(name))
end
def self.css_class_for(name)
find(name)
"exists"
rescue PageNotFound
"unknown"
end
def self.repository
GitWiki.repository || raise
end
def self.extension
GitWiki.extension || raise
end
def self.find_blob(page_name)
repository.tree/(page_name + extension)
end
private_class_method :find_blob
def self.create_blob_for(page_name)
Grit::Blob.create(repository, {
:name => page_name + extension,
:data => ""
})
end
private_class_method :create_blob_for
def initialize(blob)
@blob = blob
end
def to_html
RDiscount.new(wiki_link(content)).to_html
end
def to_s
name
end
def new?
@blob.id.nil?
end
def name
@blob.name.gsub(/#{File.extname(@blob.name)}$/, '')
end
def content
@blob.data
end
def update_content(new_content)
return if new_content == content
File.open(file_name, "w") { |f| f << new_content }
add_to_index_and_commit!
end
private
def add_to_index_and_commit!
Dir.chdir(self.class.repository.working_dir) {
self.class.repository.add(@blob.name)
}
self.class.repository.commit_index(commit_message)
end
def file_name
File.join(self.class.repository.working_dir, name + self.class.extension)
end
def commit_message
new? ? "Created #{name}" : "Updated #{name}"
end
def wiki_link(str)
str.gsub(/([A-Z][a-z]+[A-Z][A-Za-z0-9]+)/) { |page|
%Q{<a class="#{self.class.css_class_for(page)}"} +
%Q{href="/#{page}">#{page}</a>}
}
end
end
class App < Sinatra::Base
set :app_file, __FILE__
set :haml, { :format => :html5,
:attr_wrapper => '"' }
enable :inline_templates
error PageNotFound do
page = request.env["sinatra.error"].name
redirect "/#{page}/edit"
end
before do
content_type "text/html", :charset => "utf-8"
end
get "/" do
redirect "/" + GitWiki.homepage
end
get "/pages" do
@pages = Page.find_all
haml :list
end
get "/:page/edit" do
@page = Page.find_or_create(params[:page])
haml :edit
end
get "/:page" do
@page = Page.find(params[:page])
haml :show
end
post "/:page" do
@page = Page.find_or_create(params[:page])
@page.update_content(params[:body])
redirect "/#{@page}"
end
private
def title(title=nil)
@title = title.to_s unless title.nil?
@title
end
def list_item(page)
%Q{<a class="page_name" href="/#{page}">#{page.name}</a>}
end
end
end
__END__
@@ layout
!!!
%html
%head
%title= title
%body
%ul
%li
%a{ :href => "/#{GitWiki.homepage}" } Home
%li
%a{ :href => "/pages" } All pages
#content= yield
@@ show
- title @page.name
#edit
%a{:href => "/#{@page}/edit"} Edit this page
%h1= title
#content
~"#{@page.to_html}"
@@ edit
- title "Editing #{@page.name}"
%h1= title
%form{:method => 'POST', :action => "/#{@page}"}
%p
%textarea{:name => 'body', :rows => 30, :style => "width: 100%"}= @page.content
%p
%input.submit{:type => :submit, :value => "Save as the newest version"}
or
%a.cancel{:href=>"/#{@page}"} cancel
@@ list
- title "Listing pages"
%h1 All pages
- if @pages.empty?
%p No pages found.
- else
%ul#list
- @pages.each do |page|
%li= list_item(page)