forked from sr/git-wiki
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-wiki.rb
executable file
·148 lines (123 loc) · 3.31 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
module GitWiki
class << self
attr_accessor :extension, :repository, :default_branch
end
def self.new(repository, extension, default_branch)
self.extension = extension
self.repository = Grit::Repo.new(repository)
self.default_branch = default_branch
App
end
class PageNotFound < Sinatra::NotFound
attr_reader :name
def initialize(name)
@name = name
end
end
class Page
def self.find_all(branch = default_branch)
return [] if repository.tree(branch).contents.empty?
repository.tree(branch).contents.collect { |blob| new(blob) }
end
def self.find(name, branch = default_branch)
page_blob = find_blob(name, branch)
raise PageNotFound.new(name) unless page_blob
new(page_blob)
end
def self.repository
GitWiki.repository || raise
end
def self.extension
GitWiki.extension || raise
end
def self.find_blob(page_name, branch = default_branch)
repository.tree(branch)/(page_name + extension)
end
private_class_method :find_blob
def initialize(blob)
@blob = blob
end
def to_html
RDiscount.new(content).to_html
end
def to_s
name
end
def name
@blob.name.gsub(/#{File.extname(@blob.name)}$/, '')
end
def content
@blob.data
end
end
class App < Sinatra::Base
set :app_file, __FILE__
set :haml, { :format => :html5, :attr_wrapper => '"' }
use_in_file_templates!
error PageNotFound do
"404 #{request.env["sinatra.error"].message}"
end
before do
content_type "text/html", :charset => "utf-8"
@branches = GitWiki.repository.branches.collect{|br| br.name}
end
get "/" do
@branch = params[:branch] || GitWiki.default_branch
redirect "/pages/#{@branch}"
end
get "/pages/:branch" do
@branch = params[:branch] || GitWiki.default_branch
@pages = Page.find_all(@branch)
haml :list
end
get "/:branch/:page" do
@branch = params[:branch] || GitWiki.default_branch
@page = Page.find(params[:page], @branch)
haml :show
end
private
def title(title=nil)
@title = title.to_s unless title.nil?
@title
end
def list_item(page, branch)
%Q{<a class="page_name" href="/#{branch}/#{page}">#{page.name}</a>}
end
end
end
__END__
@@ layout
!!!
%html
%head
%title= title
%link(rel='stylesheet' href="/css/application.css" type='text/css')
%link(rel='stylesheet' href="/css/css3-github-buttons.css" type ='text/css')
%script(src="/javascripts/jquery.js")
%script(src="/javascripts/application.js")
%body
%div{ :id => "container" }
%a.button.icon.home{:href => "/pages/#{@branch}"} Home
#content= yield
@@ show
- title @page.name
%br
%select{:name => "branch[name]", :id => "branchName"}
- for br in @branches
%option{:value => "/#{br}/#{@page}", :selected => (br==@branch)? true : false} #{br}
%h1= title
#content
~"#{@page.to_html}"
@@ list
- title "Listing pages"
%br
%select{:name => "branch[name]", :id => "branchName"}
- for br in @branches
%option{:value => "/pages/#{br}", :selected => (br==@branch)? true : false} #{br}
%h1 All pages
- if @pages.empty?
%p No pages found.
- else
%ul#list
- @pages.each do |page|
%li= list_item(page, @branch)