require 'cgi' require 'sinatra' require 'gollum' require 'mustache/sinatra' require 'gollum/frontend/views/layout' module Precious class App < Sinatra::Base register Mustache::Sinatra dir = File.dirname(File.expand_path(__FILE__)) # We want to serve public assets for now set :public_folder, "#{dir}/public" set :static, true set :mustache, { # Tell mustache where the Views constant lives :namespace => Precious, # Mustache templates live here :templates => "#{dir}/templates", # Tell mustache where the views are :views => "#{dir}/views" } # Sinatra error handling configure :development, :staging do enable :show_exceptions, :dump_errors disable :raise_errors, :clean_trace end configure :test do enable :logging, :raise_errors, :dump_errors end get '/' do show_page_or_file('Home', 'README') end get %r{^/(javascript|css|images)} do halt 404 end get %r{/(.+?)/([0-9a-f]{40})} do name = params[:captures][0] wiki = Gollum::Wiki.new(settings.roots[request.script_name][:gollum_path], settings.roots[request.script_name][:wiki_options]) if page = wiki.page(name, params[:captures][1]) @page = page @name = name @content = page.formatted_data @base_path = wiki.base_path mustache :page else halt 404 end end get '/pages' do wiki = Gollum::Wiki.new(settings.roots[request.script_name][:gollum_path], settings.roots[request.script_name][:wiki_options]) @results = wiki.pages @ref = wiki.ref @base_path = wiki.base_path mustache :pages end get '/*' do show_page_or_file(params[:splat].first) end def show_page_or_file(*names) ps = names.collect do |name| wiki = Gollum::Wiki.new(settings.roots[request.script_name][:gollum_path], settings.roots[request.script_name][:wiki_options]) if page = wiki.page(name) @page = page @name = name @content = page.formatted_data @base_path = wiki.base_path mustache :page elsif file = wiki.file(name) content_type file.mime_type file.raw_data else nil end end.select {|p| p} ps.empty? ? nil : ps.first end end end