Skip to content

Commit ec2ac92

Browse files
committed
Redo routes, / for index, give pages url and edit_url
1 parent 2f6a137 commit ec2ac92

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

git-wiki.rb

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
module GitWiki
99
class << self
10-
attr_accessor :homepage, :extension, :repository, :link_pattern
10+
attr_accessor :root_page, :extension, :repository, :link_pattern
1111
end
1212
@repository = Grit::Repo.new(ARGV[0] || File.expand_path('~/wiki'))
1313
@extension = ARGV[1] || '.markdown'
14-
@homepage = ARGV[2] || 'Home'
14+
@root_page = ARGV[2] || 'index'
1515
@link_pattern = /\[\[(.*?)\]\]/
1616
end
1717

@@ -47,6 +47,14 @@ def to_s
4747
name
4848
end
4949

50+
def url
51+
to_s == GitWiki.root_page ? '/' : "/pages/#{to_s}"
52+
end
53+
54+
def edit_url
55+
"/pages/#{to_s}/edit"
56+
end
57+
5058
def css_class
5159
@blob.id ? 'existing' : 'new'
5260
end
@@ -70,27 +78,28 @@ def save!(data)
7078

7179
set :haml, :format => :html5, :attr_wrapper => '"'
7280

73-
get "/" do
74-
redirect "/" + GitWiki.homepage
81+
get '/' do
82+
@page = Page.find_or_create(GitWiki.root_page)
83+
haml :show
7584
end
7685

77-
get "/pages" do
86+
get '/pages/' do
7887
@pages = Page.find_all
7988
haml :list
8089
end
8190

82-
get "/:page/edit" do
91+
get '/pages/:page/?' do
8392
@page = Page.find_or_create(params[:page])
84-
haml :edit
93+
haml :show
8594
end
8695

87-
get "/:page" do
96+
get '/pages/:page/edit' do
8897
@page = Page.find_or_create(params[:page])
89-
haml :show
98+
haml :edit
9099
end
91100

92-
post "/:page" do
101+
post '/pages/:page/edit' do
93102
@page = Page.find_or_create(params[:page])
94103
@page.save!(params[:body])
95-
redirect "/#{@page}"
104+
redirect @page.url
96105
end

views/edit.haml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
- title "Editing #{@page.name}"
22
%h1= title
3-
%form{:method => 'POST', :action => "/#{@page}"}
3+
%form{:method => 'POST', :action => @page.edit_url}
44
%p
55
%textarea{:name => 'body', :rows => 30, :style => "width: 100%"}= @page.content
66
%p
77
%input.submit{:type => :submit, :value => "Save as the newest version"}
88
or
9-
%a.cancel{:href=>"/#{@page}"} cancel
9+
%a.cancel{:href=>@page.url} cancel

views/layout.haml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
%body
66
%ul
77
%li
8-
%a{ :href => "/#{GitWiki.homepage}" } Home
8+
%a{ :href => "/" } Home
99
%li
10-
%a{ :href => "/pages" } All pages
10+
%a{ :href => "/pages/" } All pages
1111
#content= yield

views/show.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
- title @page.name
22
#edit
3-
%a{:href => "/#{@page}/edit"} Edit this page
3+
%a{:href => @page.edit_url} Edit this page
44
%h1= title
55
#content
66
~"#{@page.to_html}"

0 commit comments

Comments
 (0)