public
Description: A quick & dirty git-powered Sinatra wiki
Homepage: http://atonie.org/2008/02/git-wiki
Clone URL: git://github.com/sr/git-wiki.git
hiroshi (author)
Mon Jun 01 14:11:45 -0700 2009
sr (committer)
Mon Jun 01 14:11:45 -0700 2009
git-wiki / git-wiki.rb
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 1 require "sinatra/base"
e53d4ac4 » sr 2009-05-01 Explicitely require haml 2 require "haml"
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 3 require "grit"
6850e94f » sr 2009-01-31 Replace BlueCloth by rtomay... 4 require "rdiscount"
8e732dcc » sr 2008-08-24 decamelize page title and t... 5
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 6 module GitWiki
7 class << self
8 attr_accessor :homepage, :extension, :repository
9 end
10
11 def self.new(repository, extension, homepage)
12 self.homepage = homepage
13 self.extension = extension
14 self.repository = Grit::Repo.new(repository)
ea533a23 » sr 2008-08-28 little refactoring: page ta... 15
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 16 App
ea533a23 » sr 2008-08-28 little refactoring: page ta... 17 end
18
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 19 class PageNotFound < Sinatra::NotFound
20 attr_reader :name
21
22 def initialize(name)
23 @name = name
24 end
25 end
91e11a63 » sr 2008-03-08 $store is now Page.store 26
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 27 class Page
28 def self.find_all
d8e708df » sr 2009-01-21 get ride of monkey-patches 29 return [] if repository.tree.contents.empty?
30 repository.tree.contents.collect { |blob| new(blob) }
44e73c3e » sr 2008-08-26 cosmetics 31 end
ea533a23 » sr 2008-08-28 little refactoring: page ta... 32
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 33 def self.find(name)
ea533a23 » sr 2008-08-28 little refactoring: page ta... 34 page_blob = find_blob(name)
35 raise PageNotFound.new(name) unless page_blob
36 new(page_blob)
37 end
38
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 39 def self.find_or_create(name)
ea533a23 » sr 2008-08-28 little refactoring: page ta... 40 find(name)
41 rescue PageNotFound
42 new(create_blob_for(name))
43 end
44
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 45 def self.css_class_for(name)
6acef383 » sr 2008-09-21 replace Page#to_css_class b... 46 find(name)
e24139e9 » sr 2009-01-21 store page extension and re... 47 "exists"
6acef383 » sr 2008-09-21 replace Page#to_css_class b... 48 rescue PageNotFound
e24139e9 » sr 2009-01-21 store page extension and re... 49 "unknown"
6acef383 » sr 2008-09-21 replace Page#to_css_class b... 50 end
51
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 52 def self.repository
53 GitWiki.repository || raise
54 end
31259071 » sr 2008-09-21 cosmetics 55
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 56 def self.extension
57 GitWiki.extension || raise
58 end
31259071 » sr 2008-09-21 cosmetics 59
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 60 def self.find_blob(page_name)
61 repository.tree/(page_name + extension)
62 end
63 private_class_method :find_blob
3bdddd51 » sr 2008-01-29 Initial import 64
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 65 def self.create_blob_for(page_name)
66 Grit::Blob.create(repository, {
67 :name => page_name + extension,
68 :data => ""
69 })
70 end
71 private_class_method :create_blob_for
3bdddd51 » sr 2008-01-29 Initial import 72
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 73 def initialize(blob)
74 @blob = blob
75 end
3bdddd51 » sr 2008-01-29 Initial import 76
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 77 def to_html
6850e94f » sr 2009-01-31 Replace BlueCloth by rtomay... 78 RDiscount.new(wiki_link(content)).to_html
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 79 end
3bdddd51 » sr 2008-01-29 Initial import 80
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 81 def to_s
82 name
44e73c3e » sr 2008-08-26 cosmetics 83 end
84
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 85 def new?
86 @blob.id.nil?
44e73c3e » sr 2008-08-26 cosmetics 87 end
88
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 89 def name
90 @blob.name.gsub(/#{File.extname(@blob.name)}$/, '')
44e73c3e » sr 2008-08-26 cosmetics 91 end
d8e708df » sr 2009-01-21 get ride of monkey-patches 92
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 93 def content
94 @blob.data
d8e708df » sr 2009-01-21 get ride of monkey-patches 95 end
96
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 97 def update_content(new_content)
98 return if new_content == content
99 File.open(file_name, "w") { |f| f << new_content }
100 add_to_index_and_commit!
d8e708df » sr 2009-01-21 get ride of monkey-patches 101 end
3bdddd51 » sr 2008-01-29 Initial import 102
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 103 private
104 def add_to_index_and_commit!
105 Dir.chdir(self.class.repository.working_dir) {
106 self.class.repository.add(@blob.name)
107 }
108 self.class.repository.commit_index(commit_message)
109 end
5b5675c7 » sr 2008-03-28 output html4 with proper do... 110
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 111 def file_name
112 File.join(self.class.repository.working_dir, name + self.class.extension)
113 end
9c2e1ee5 » sr 2008-09-29 monkey-patch redcloth to pr... 114
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 115 def commit_message
116 new? ? "Created #{name}" : "Updated #{name}"
117 end
e8ace3b0 » sr 2008-03-28 take of advantage of edge s... 118
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 119 def wiki_link(str)
120 str.gsub(/([A-Z][a-z]+[A-Z][A-Za-z0-9]+)/) { |page|
121 %Q{<a class="#{self.class.css_class_for(page)}"} +
8791f20d » sr 2009-01-28 Back to the essences 122 %Q{href="/#{page}">#{page}</a>}
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 123 }
124 end
16928c6a » sr 2008-03-27 Take advantage of Sinatra's... 125 end
60dfcabc » sr 2008-03-28 Page#to_s now return page's... 126
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 127 class App < Sinatra::Base
128 set :app_file, __FILE__
129 set :haml, { :format => :html5,
130 :attr_wrapper => '"' }
131 use_in_file_templates!
16928c6a » sr 2008-03-27 Take advantage of Sinatra's... 132
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 133 error PageNotFound do
134 page = request.env["sinatra.error"].name
135 redirect "/#{page}/edit"
136 end
4a2e99ae » sr 2008-03-27 Set proper Content-Type hea... 137
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 138 before do
139 content_type "text/html", :charset => "utf-8"
140 end
4a2e99ae » sr 2008-03-27 Set proper Content-Type hea... 141
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 142 get "/" do
143 redirect "/" + GitWiki.homepage
144 end
3bdddd51 » sr 2008-01-29 Initial import 145
6c6ba014 » sr 2009-01-31 Clean up URIs a bit 146 get "/pages" do
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 147 @pages = Page.find_all
148 haml :list
149 end
3bdddd51 » sr 2008-01-29 Initial import 150
6c6ba014 » sr 2009-01-31 Clean up URIs a bit 151 get "/:page/edit" do
152 @page = Page.find_or_create(params[:page])
153 haml :edit
154 end
155
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 156 get "/:page" do
157 @page = Page.find(params[:page])
158 haml :show
159 end
160
6c6ba014 » sr 2009-01-31 Clean up URIs a bit 161 post "/:page" do
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 162 @page = Page.find_or_create(params[:page])
163 @page.update_content(params[:body])
164 redirect "/#{@page}"
165 end
166
167 private
168 def title(title=nil)
169 @title = title.to_s unless title.nil?
170 @title
171 end
172
173 def list_item(page)
8791f20d » sr 2009-01-28 Back to the essences 174 %Q{<a class="page_name" href="/#{page}">#{page.name}</a>}
b50336ac » sr 2009-01-21 Upgrade to Sinatra 0.9 175 end
176 end
3bdddd51 » sr 2008-01-29 Initial import 177 end
178
16928c6a » sr 2008-03-27 Take advantage of Sinatra's... 179 __END__
5108abaf » sr 2008-05-04 update sinatra 180 @@ layout
3f130b2d » sr 2009-01-21 use html5 181 !!!
3bdddd51 » sr 2008-01-29 Initial import 182 %html
183 %head
16928c6a » sr 2008-03-27 Take advantage of Sinatra's... 184 %title= title
3bdddd51 » sr 2008-01-29 Initial import 185 %body
8791f20d » sr 2009-01-28 Back to the essences 186 %ul
187 %li
188 %a{ :href => "/#{GitWiki.homepage}" } Home
189 %li
6c6ba014 » sr 2009-01-31 Clean up URIs a bit 190 %a{ :href => "/pages" } All pages
16928c6a » sr 2008-03-27 Take advantage of Sinatra's... 191 #content= yield
3bdddd51 » sr 2008-01-29 Initial import 192
5108abaf » sr 2008-05-04 update sinatra 193 @@ show
8791f20d » sr 2009-01-28 Back to the essences 194 - title @page.name
195 #edit
6c6ba014 » sr 2009-01-31 Clean up URIs a bit 196 %a{:href => "/#{@page}/edit"} Edit this page
8791f20d » sr 2009-01-28 Back to the essences 197 %h1= title
198 #content
ea533a23 » sr 2008-08-28 little refactoring: page ta... 199 ~"#{@page.to_html}"
3bdddd51 » sr 2008-01-29 Initial import 200
5108abaf » sr 2008-05-04 update sinatra 201 @@ edit
8791f20d » sr 2009-01-28 Back to the essences 202 - title "Editing #{@page.name}"
16928c6a » sr 2008-03-27 Take advantage of Sinatra's... 203 %h1= title
6c6ba014 » sr 2009-01-31 Clean up URIs a bit 204 %form{:method => 'POST', :action => "/#{@page}"}
d47f5f25 » sr 2008-03-27 Basic edit in place support 205 %p
8791f20d » sr 2009-01-28 Back to the essences 206 %textarea{:name => 'body', :rows => 30, :style => "width: 100%"}= @page.content
d47f5f25 » sr 2008-03-27 Basic edit in place support 207 %p
8791f20d » sr 2009-01-28 Back to the essences 208 %input.submit{:type => :submit, :value => "Save as the newest version"}
bdd34406 » sr 2008-03-28 Cleaner page listing and va... 209 or
c47205a1 » sr 2008-03-28 various typo fix 210 %a.cancel{:href=>"/#{@page}"} cancel
16928c6a » sr 2008-03-27 Take advantage of Sinatra's... 211
5108abaf » sr 2008-05-04 update sinatra 212 @@ list
16928c6a » sr 2008-03-27 Take advantage of Sinatra's... 213 - title "Listing pages"
8791f20d » sr 2009-01-28 Back to the essences 214 %h1 All pages
16928c6a » sr 2008-03-27 Take advantage of Sinatra's... 215 - if @pages.empty?
88d664e0 » Bryan T. Richardson 2008-08-20 fixed haml problem with lis... 216 %p No pages found.
16928c6a » sr 2008-03-27 Take advantage of Sinatra's... 217 - else
8791f20d » sr 2009-01-28 Back to the essences 218 %ul#list
219 - @pages.each do |page|
220 %li= list_item(page)