This repository is private.
All pages are served over SSL and all pushing and pulling is done over SSH.
No one may fork, clone, or view it unless they are added as a member.
Every repository with this icon (
) is private.
Every repository with this icon (
This repository is public.
Anyone may fork, clone, or view it.
Every repository with this icon (
) is public.
Every repository with this icon (
git-wiki / git-wiki.rb
| b50336ac » | sr | 2009-01-21 | 1 | require "sinatra/base" | |
| e53d4ac4 » | sr | 2009-05-01 | 2 | require "haml" | |
| b50336ac » | sr | 2009-01-21 | 3 | require "grit" | |
| 6850e94f » | sr | 2009-01-31 | 4 | require "rdiscount" | |
| 8e732dcc » | sr | 2008-08-24 | 5 | ||
| b50336ac » | sr | 2009-01-21 | 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 | 15 | ||
| b50336ac » | sr | 2009-01-21 | 16 | App | |
| ea533a23 » | sr | 2008-08-28 | 17 | end | |
| 18 | |||||
| b50336ac » | sr | 2009-01-21 | 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 | 26 | ||
| b50336ac » | sr | 2009-01-21 | 27 | class Page | |
| 28 | def self.find_all | ||||
| d8e708df » | sr | 2009-01-21 | 29 | return [] if repository.tree.contents.empty? | |
| 30 | repository.tree.contents.collect { |blob| new(blob) } | ||||
| 44e73c3e » | sr | 2008-08-26 | 31 | end | |
| ea533a23 » | sr | 2008-08-28 | 32 | ||
| b50336ac » | sr | 2009-01-21 | 33 | def self.find(name) | |
| ea533a23 » | sr | 2008-08-28 | 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 | 39 | def self.find_or_create(name) | |
| ea533a23 » | sr | 2008-08-28 | 40 | find(name) | |
| 41 | rescue PageNotFound | ||||
| 42 | new(create_blob_for(name)) | ||||
| 43 | end | ||||
| 44 | |||||
| b50336ac » | sr | 2009-01-21 | 45 | def self.css_class_for(name) | |
| 6acef383 » | sr | 2008-09-21 | 46 | find(name) | |
| e24139e9 » | sr | 2009-01-21 | 47 | "exists" | |
| 6acef383 » | sr | 2008-09-21 | 48 | rescue PageNotFound | |
| e24139e9 » | sr | 2009-01-21 | 49 | "unknown" | |
| 6acef383 » | sr | 2008-09-21 | 50 | end | |
| 51 | |||||
| b50336ac » | sr | 2009-01-21 | 52 | def self.repository | |
| 53 | GitWiki.repository || raise | ||||
| 54 | end | ||||
| 31259071 » | sr | 2008-09-21 | 55 | ||
| b50336ac » | sr | 2009-01-21 | 56 | def self.extension | |
| 57 | GitWiki.extension || raise | ||||
| 58 | end | ||||
| 31259071 » | sr | 2008-09-21 | 59 | ||
| b50336ac » | sr | 2009-01-21 | 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 | 64 | ||
| b50336ac » | sr | 2009-01-21 | 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 | 72 | ||
| b50336ac » | sr | 2009-01-21 | 73 | def initialize(blob) | |
| 74 | @blob = blob | ||||
| 75 | end | ||||
| 3bdddd51 » | sr | 2008-01-29 | 76 | ||
| b50336ac » | sr | 2009-01-21 | 77 | def to_html | |
| 6850e94f » | sr | 2009-01-31 | 78 | RDiscount.new(wiki_link(content)).to_html | |
| b50336ac » | sr | 2009-01-21 | 79 | end | |
| 3bdddd51 » | sr | 2008-01-29 | 80 | ||
| b50336ac » | sr | 2009-01-21 | 81 | def to_s | |
| 82 | name | ||||
| 44e73c3e » | sr | 2008-08-26 | 83 | end | |
| 84 | |||||
| b50336ac » | sr | 2009-01-21 | 85 | def new? | |
| 86 | @blob.id.nil? | ||||
| 44e73c3e » | sr | 2008-08-26 | 87 | end | |
| 88 | |||||
| b50336ac » | sr | 2009-01-21 | 89 | def name | |
| 90 | @blob.name.gsub(/#{File.extname(@blob.name)}$/, '') | ||||
| 44e73c3e » | sr | 2008-08-26 | 91 | end | |
| d8e708df » | sr | 2009-01-21 | 92 | ||
| b50336ac » | sr | 2009-01-21 | 93 | def content | |
| 94 | @blob.data | ||||
| d8e708df » | sr | 2009-01-21 | 95 | end | |
| 96 | |||||
| b50336ac » | sr | 2009-01-21 | 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 | 101 | end | |
| 3bdddd51 » | sr | 2008-01-29 | 102 | ||
| b50336ac » | sr | 2009-01-21 | 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 | 110 | ||
| b50336ac » | sr | 2009-01-21 | 111 | def file_name | |
| 112 | File.join(self.class.repository.working_dir, name + self.class.extension) | ||||
| 113 | end | ||||
| 9c2e1ee5 » | sr | 2008-09-29 | 114 | ||
| b50336ac » | sr | 2009-01-21 | 115 | def commit_message | |
| 116 | new? ? "Created #{name}" : "Updated #{name}" | ||||
| 117 | end | ||||
| e8ace3b0 » | sr | 2008-03-28 | 118 | ||
| b50336ac » | sr | 2009-01-21 | 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 | 122 | %Q{href="/#{page}">#{page}</a>} | |
| b50336ac » | sr | 2009-01-21 | 123 | } | |
| 124 | end | ||||
| 16928c6a » | sr | 2008-03-27 | 125 | end | |
| 60dfcabc » | sr | 2008-03-28 | 126 | ||
| b50336ac » | sr | 2009-01-21 | 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 | 132 | ||
| b50336ac » | sr | 2009-01-21 | 133 | error PageNotFound do | |
| 134 | page = request.env["sinatra.error"].name | ||||
| 135 | redirect "/#{page}/edit" | ||||
| 136 | end | ||||
| 4a2e99ae » | sr | 2008-03-27 | 137 | ||
| b50336ac » | sr | 2009-01-21 | 138 | before do | |
| 139 | content_type "text/html", :charset => "utf-8" | ||||
| 140 | end | ||||
| 4a2e99ae » | sr | 2008-03-27 | 141 | ||
| b50336ac » | sr | 2009-01-21 | 142 | get "/" do | |
| 143 | redirect "/" + GitWiki.homepage | ||||
| 144 | end | ||||
| 3bdddd51 » | sr | 2008-01-29 | 145 | ||
| 6c6ba014 » | sr | 2009-01-31 | 146 | get "/pages" do | |
| b50336ac » | sr | 2009-01-21 | 147 | @pages = Page.find_all | |
| 148 | haml :list | ||||
| 149 | end | ||||
| 3bdddd51 » | sr | 2008-01-29 | 150 | ||
| 6c6ba014 » | sr | 2009-01-31 | 151 | get "/:page/edit" do | |
| 152 | @page = Page.find_or_create(params[:page]) | ||||
| 153 | haml :edit | ||||
| 154 | end | ||||
| 155 | |||||
| b50336ac » | sr | 2009-01-21 | 156 | get "/:page" do | |
| 157 | @page = Page.find(params[:page]) | ||||
| 158 | haml :show | ||||
| 159 | end | ||||
| 160 | |||||
| 6c6ba014 » | sr | 2009-01-31 | 161 | post "/:page" do | |
| b50336ac » | sr | 2009-01-21 | 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 | 174 | %Q{<a class="page_name" href="/#{page}">#{page.name}</a>} | |
| b50336ac » | sr | 2009-01-21 | 175 | end | |
| 176 | end | ||||
| 3bdddd51 » | sr | 2008-01-29 | 177 | end | |
| 178 | |||||
| 16928c6a » | sr | 2008-03-27 | 179 | __END__ | |
| 5108abaf » | sr | 2008-05-04 | 180 | @@ layout | |
| 3f130b2d » | sr | 2009-01-21 | 181 | !!! | |
| 3bdddd51 » | sr | 2008-01-29 | 182 | %html | |
| 183 | %head | ||||
| 16928c6a » | sr | 2008-03-27 | 184 | %title= title | |
| 3bdddd51 » | sr | 2008-01-29 | 185 | %body | |
| 8791f20d » | sr | 2009-01-28 | 186 | %ul | |
| 187 | %li | ||||
| 188 | %a{ :href => "/#{GitWiki.homepage}" } Home | ||||
| 189 | %li | ||||
| 6c6ba014 » | sr | 2009-01-31 | 190 | %a{ :href => "/pages" } All pages | |
| 16928c6a » | sr | 2008-03-27 | 191 | #content= yield | |
| 3bdddd51 » | sr | 2008-01-29 | 192 | ||
| 5108abaf » | sr | 2008-05-04 | 193 | @@ show | |
| 8791f20d » | sr | 2009-01-28 | 194 | - title @page.name | |
| 195 | #edit | ||||
| 6c6ba014 » | sr | 2009-01-31 | 196 | %a{:href => "/#{@page}/edit"} Edit this page | |
| 8791f20d » | sr | 2009-01-28 | 197 | %h1= title | |
| 198 | #content | ||||
| ea533a23 » | sr | 2008-08-28 | 199 | ~"#{@page.to_html}" | |
| 3bdddd51 » | sr | 2008-01-29 | 200 | ||
| 5108abaf » | sr | 2008-05-04 | 201 | @@ edit | |
| 8791f20d » | sr | 2009-01-28 | 202 | - title "Editing #{@page.name}" | |
| 16928c6a » | sr | 2008-03-27 | 203 | %h1= title | |
| 6c6ba014 » | sr | 2009-01-31 | 204 | %form{:method => 'POST', :action => "/#{@page}"} | |
| d47f5f25 » | sr | 2008-03-27 | 205 | %p | |
| 8791f20d » | sr | 2009-01-28 | 206 | %textarea{:name => 'body', :rows => 30, :style => "width: 100%"}= @page.content | |
| d47f5f25 » | sr | 2008-03-27 | 207 | %p | |
| 8791f20d » | sr | 2009-01-28 | 208 | %input.submit{:type => :submit, :value => "Save as the newest version"} | |
| bdd34406 » | sr | 2008-03-28 | 209 | or | |
| c47205a1 » | sr | 2008-03-28 | 210 | %a.cancel{:href=>"/#{@page}"} cancel | |
| 16928c6a » | sr | 2008-03-27 | 211 | ||
| 5108abaf » | sr | 2008-05-04 | 212 | @@ list | |
| 16928c6a » | sr | 2008-03-27 | 213 | - title "Listing pages" | |
| 8791f20d » | sr | 2009-01-28 | 214 | %h1 All pages | |
| 16928c6a » | sr | 2008-03-27 | 215 | - if @pages.empty? | |
| 88d664e0 » | Bryan T. Richardson | 2008-08-20 | 216 | %p No pages found. | |
| 16928c6a » | sr | 2008-03-27 | 217 | - else | |
| 8791f20d » | sr | 2009-01-28 | 218 | %ul#list | |
| 219 | - @pages.each do |page| | ||||
| 220 | %li= list_item(page) | ||||







