Make the Sinatra app directory aware.

This commit is contained in:
Darren Oakley
2012-06-18 16:00:14 +01:00
parent b53c961db2
commit 971cbb94f6
25 changed files with 468 additions and 127 deletions
+89 -50
View File
@@ -6,11 +6,13 @@ require 'useragent'
require 'gollum/frontend/views/layout'
require 'gollum/frontend/views/editable'
require 'gollum/frontend/views/has_page'
require File.expand_path '../uri_encode_component', __FILE__
require File.expand_path '../helpers', __FILE__
# Run the frontend, based on Sinatra
#
#
# There are a number of wiki options that can be set for the frontend
#
# Example
@@ -23,6 +25,7 @@ require File.expand_path '../uri_encode_component', __FILE__
module Precious
class App < Sinatra::Base
register Mustache::Sinatra
include Precious::Helpers
dir = File.dirname(File.expand_path(__FILE__))
@@ -30,7 +33,7 @@ module Precious
@@supported_browsers = ['Firefox', 'Chrome', 'Safari']
Browser = Struct.new(:browser, :version)
@@ie9 = Browser.new('Internet Explorer', '9.0')
def supported_useragent?(user_agent)
ua = UserAgent.parse(user_agent)
return true if ua >= @@ie9
@@ -69,19 +72,28 @@ module Precious
end
get '/data/*' do
@name = params[:splat].first
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
@path = extract_path(params[:splat].first)
@name = extract_name(params[:splat].first)
wiki_options = settings.wiki_options.merge({ :page_file_dir => @path })
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)
if page = wiki.page(@name)
page.raw_data
end
end
get '/edit/*' do
@name = params[:splat].first
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
@path = extract_path(params[:splat].first)
@name = extract_name(params[:splat].first)
wiki_options = settings.wiki_options.merge({ :page_file_dir => @path })
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)
if page = wiki.page(@name)
if wiki.live_preview && page.format.to_s.include?('markdown') && supported_useragent?(request.user_agent)
redirect '/livepreview/index.html?page=' + encodeURIComponent(@name)
live_preview_url = '/livepreview/index.html?page=' + encodeURIComponent(@name)
if @path
live_preview_url << '&path=' + encodeURIComponent(@path)
end
redirect live_preview_url
else
@page = page
@page.version = wiki.repo.log(wiki.ref, @page.path).first
@@ -94,31 +106,37 @@ module Precious
end
post '/edit/*' do
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
page = wiki.page(params[:splat].first)
name = params[:rename] || page.name
committer = Gollum::Committer.new(wiki, commit_message)
commit = {:committer => committer}
path = sanitize_empty_params(params[:path])
wiki_options = settings.wiki_options.merge({ :page_file_dir => path })
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)
page = wiki.page(CGI.unescape(params[:page]))
name = params[:rename] || page.name
committer = Gollum::Committer.new(wiki, commit_message)
commit = {:committer => committer}
update_wiki_page(wiki, page, params[:content], commit, name,
params[:format])
update_wiki_page(wiki, page, params[:content], commit, name, params[:format])
update_wiki_page(wiki, page.header, params[:header], commit) if params[:header]
update_wiki_page(wiki, page.footer, params[:footer], commit) if params[:footer]
update_wiki_page(wiki, page.sidebar, params[:sidebar], commit) if params[:sidebar]
committer.commit
redirect "/#{CGI.escape(Gollum::Page.cname(name))}"
page = wiki.page(params[:rename]) if params[:rename]
redirect "/#{page.escaped_url_path}"
end
post '/create' do
name = params[:page]
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
name = CGI.unescape(params[:page])
path = sanitize_empty_params(params[:path])
format = params[:format].intern
format = params[:format].intern
wiki_options = settings.wiki_options.merge({ :page_file_dir => path })
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)
begin
wiki.write_page(name, format, params[:content], commit_message)
redirect "/#{CGI.escape(Gollum::Page.cname(name))}"
page = wiki.page(name)
redirect "/#{page.escaped_url_path}"
rescue Gollum::DuplicatePageError => e
@message = "Duplicate page: #{e.message}"
mustache :error
@@ -126,15 +144,17 @@ module Precious
end
post '/revert/:page/*' do
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
@name = params[:page]
@page = wiki.page(@name)
shas = params[:splat].first.split("/")
sha1 = shas.shift
sha2 = shas.shift
@path = extract_path(params[:page])
@name = params[:page]
wiki_options = settings.wiki_options.merge({ :page_file_dir => @path })
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)
@page = wiki.page(@name)
shas = params[:splat].first.split("/")
sha1 = shas.shift
sha2 = shas.shift
if wiki.revert_page(@page, sha1, sha2, commit_message)
redirect "/#{CGI.escape(@name)}"
redirect "/#{@page.escaped_url_path}"
else
sha2, sha1 = sha1, "#{sha1}^" if !sha2
@versions = [sha1, sha2]
@@ -156,34 +176,39 @@ module Precious
mustache :page
end
get '/history/:name' do
@name = params[:name]
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
@page = wiki.page(@name)
@page_num = [params[:page].to_i, 1].max
@versions = @page.versions :page => @page_num
get '/history/*' do
@path = extract_path(params[:splat].first)
@name = extract_name(params[:splat].first)
wiki_options = settings.wiki_options.merge({ :page_file_dir => @path })
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)
@page = wiki.page(@name)
@page_num = [params[:page].to_i, 1].max
@versions = @page.versions :page => @page_num
mustache :history
end
post '/compare/:name' do
post '/compare/*' do
@file = params[:splat].first
@versions = params[:versions] || []
if @versions.size < 2
redirect "/history/#{CGI.escape(params[:name])}"
redirect "/history/#{CGI.escape(@file)}"
else
redirect "/compare/%s/%s...%s" % [
CGI.escape(params[:name]),
CGI.escape(@file),
@versions.last,
@versions.first]
end
end
get '/compare/:name/:version_list' do
@name = params[:name]
@versions = params[:version_list].split(/\.{2,3}/)
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
@page = wiki.page(@name)
diffs = wiki.repo.diff(@versions.first, @versions.last, @page.path)
@diff = diffs.first
@path = extract_path(params[:name].dup)
@name = extract_name(params[:name])
@versions = params[:version_list].split(/\.{2,3}/)
wiki_options = settings.wiki_options.merge({ :page_file_dir => @path })
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)
@page = wiki.page(@name)
diffs = wiki.repo.diff(@versions.first, @versions.last, @page.path)
@diff = diffs.first
mustache :compare
end
@@ -198,8 +223,11 @@ module Precious
end
get %r{/(.+?)/([0-9a-f]{40})} do
name = params[:captures][0]
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
file_path = params[:captures][0]
path = extract_path(file_path)
name = extract_name(file_path)
wiki_options = settings.wiki_options.merge({ :page_file_dir => path })
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)
if page = wiki.page(name, params[:captures][1])
@page = page
@name = name
@@ -219,10 +247,17 @@ module Precious
mustache :search
end
get '/pages' do
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
@results = wiki.pages
@ref = wiki.ref
get %r{
/pages # match any URL beginning with /pages
(?: # begin an optional non-capturing group
/(.+) # capture any path after the "/pages" excluding the leading slash
)? # end the optional non-capturing group
}x do |path|
@path = extract_path(path) if path
wiki_options = settings.wiki_options.merge({ :page_file_dir => @path })
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)
@results = wiki.pages
@ref = wiki.ref
mustache :pages
end
@@ -237,8 +272,12 @@ module Precious
show_page_or_file(params[:splat].first)
end
def show_page_or_file(name)
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
def show_page_or_file(fullpath)
path = extract_path(fullpath)
name = extract_name(fullpath)
wiki_options = settings.wiki_options.merge({ :page_file_dir => path })
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)
if page = wiki.page(name)
@page = page
@name = name
@@ -248,7 +287,7 @@ module Precious
@mathjax = wiki.mathjax
mustache :page
elsif file = wiki.file(name)
elsif file = wiki.file(fullpath)
content_type file.mime_type
file.raw_data
else
+20
View File
@@ -0,0 +1,20 @@
module Precious
module Helpers
# Extract the path string that Gollum::Wiki expects
def extract_path(file_path)
last_slash = file_path.rindex("/")
if last_slash
file_path[0, last_slash]
end
end
# Extract the 'page' name from the file_path
def extract_name(file_path)
::File.basename(file_path)
end
def sanitize_empty_params(param)
[nil,''].include?(param) ? nil : CGI.unescape(param)
end
end
end
@@ -94,7 +94,7 @@ a:hover, a:visited {
float:left;
margin-bottom: 20px;
min-width: 33%;
border-radius: 0.5em;
-moz-border-radius: 0.5em;
-webkit-border-radius: 0.5em;
@@ -681,3 +681,36 @@ ul.actions {
background-position: -431px -28px;
padding: 0;
}
/* @section pages */
#pages {
font-size: 1.2em;
margin-bottom: 20px;
}
#pages ul {
list-style: none;
margin: 0;
padding: 0;
}
#pages li a.file,
#pages li a.folder {
background-image: url(/images/fileview/document.png);
background-position: 0 1px;
background-repeat: no-repeat;
padding-left: 20px;
}
#pages li a.folder {
background-image: url(/images/fileview/folder-horizontal.png);
}
#pages .breadcrumb {
border-top: 1px solid #ddd;
border-bottom: 1px solid #ddd;
margin: 1em 0;
padding: 0.25em;
}
@@ -65,8 +65,13 @@ $.key = function( key ) {
// True if &create=true
var create = $.key( 'create' );
// The name of the page being edited.
// The path and name of the page being edited.
var pageName = $.key( 'page' );
var pathName = $.key( 'path' );
if (pathName === 0) {
pathName = undefined;
}
defaultCommitMessage = function() {
var msg = pageName + ' (markdown)';
@@ -88,14 +93,19 @@ $.save = function( commitMessage ) {
var markdown = 'markdown';
var txt = editorSession.getValue();
var msg = defaultCommitMessage();
var newLocation = location.protocol + '//' + location.host + '/' + pageName;
var newLocation = location.protocol + '//' + location.host;
if (pathName) {
newLocation += '/' + pathName;
}
newLocation += '/' + pageName;
// if &create=true then handle create instead of edit.
if ( create ) {
jQuery.ajax( {
type: POST,
url: '/create',
data: { page: pageName, format: markdown, content: txt, message: commitMessage || msg },
data: { path: pathName, page: pageName, format: markdown, content: txt, message: commitMessage || msg },
success: function() {
win.location = newLocation;
}
@@ -104,7 +114,7 @@ $.save = function( commitMessage ) {
jQuery.ajax( {
type: POST,
url: '/edit/' + pageName,
data: { format: markdown, content: txt, message: commitMessage || msg },
data: { path: pathName, page: pageName, format: markdown, content: txt, message: commitMessage || msg },
success: function() {
win.location = newLocation;
}
@@ -325,7 +335,7 @@ var applyTimeout = function () {
editorSession.setValue( data );
}
});
$( '#save' ).click( function() {
$.save();
});
@@ -378,10 +388,10 @@ var applyTimeout = function () {
var widthFourth = widthHalf / 2;
var height = $( win ).height();
var heightHalf = height / 2;
// height minus 50 so the end of document text doesn't flow off the page.
var editorContainerStyle = 'width:' + widthHalf + 'px;' +
'height:' + (height - 50) + 'px;' +
'height:' + (height - 50) + 'px;' +
'left:' + (leftRight === false ? widthHalf + 'px;' : '0px;') +
'top:' + '40px;'; // use 40px for tool menu
cssSet( editorContainer, editorContainerStyle );
@@ -1,16 +1,16 @@
<div id="wiki-wrapper" class="compare">
<div id="head">
<h1>History for <strong>{{path}}</strong></h1>
<ul class="actions">
<li class="minibutton">
{{>searchbar}}
</li>
<li class="minibutton"><a href="/{{escaped_name}}"
<li class="minibutton"><a href="/{{escaped_url_path}}"
class="action-view-page">View Page</a></li>
<li class="minibutton"><a href="/edit/{{escaped_name}}"
<li class="minibutton"><a href="/edit/{{escaped_url_path}}"
class="action-edit-page">Edit Page</a></li>
<li class="minibutton"><a href="/history/{{escaped_name}}"
<li class="minibutton"><a href="/history/{{escaped_url_path}}"
class="action-page-history">Page History</a></li>
</ul>
</div>
@@ -20,19 +20,19 @@
{{/message}}
<div id="compare-content">
{{#show_revert}}
<ul class="actions">
<li class="minibutton"><a href="/history/{{escaped_name}}"
<li class="minibutton"><a href="/history/{{escaped_url_path}}"
class="action-page-history">Back to Page History</a></li>
<li class="minibutton">
<form name="gollum-revert" action="/revert/{{escaped_name}}/{{before}}/{{after}}" method="post" id="gollum-revert-form">
<form name="gollum-revert" action="/revert/{{escaped_url_path}}/{{before}}/{{after}}" method="post" id="gollum-revert-form">
<a href="#" class="gollum-revert-button">Revert Changes</a>
</form>
</li>
</ul>
{{/show_revert}}
<div class="data highlight">
<table cellpadding="0" cellspacing="0">
{{#lines}}
@@ -49,7 +49,7 @@
</div>
<div id="footer">
<ul class="actions">
<li class="minibutton"><a href="/history/{{escaped_name}}"
<li class="minibutton"><a href="/history/{{escaped_url_path}}"
class="action-page-history">Back to Page History</a></li>
{{#show_revert}}
<li class="minibutton">
+2 -2
View File
@@ -2,9 +2,9 @@
<div id="head">
<h1>Editing <strong>{{title}}</strong></h1>
<ul class="actions">
<li class="minibutton"><a href="/{{escaped_name}}"
<li class="minibutton"><a href="/{{escaped_url_path}}"
class="action-view-page">View Page</a></li>
<li class="minibutton"><a href="/history/{{escaped_name}}"
<li class="minibutton"><a href="/history/{{escaped_url_path}}"
class="action-page-history">Page History</a></li>
</ul>
</div>
@@ -15,6 +15,7 @@
{{#is_edit_page}}
<input type="hidden" name="page" id="gollum-editor-page-title" value="{{page_name}}">
{{/is_edit_page}}
<input type="hidden" name="path" id="gollum-editor-page-path" value="{{path}}">
<div id="gollum-editor-function-bar">
<div id="gollum-editor-function-buttons">
<a href="#" id="function-bold" class="function-button">
@@ -30,7 +31,7 @@
<span>Ordered List</span></a>
<a href="#" id="function-blockquote" class="function-button">
<span>Blockquote</span></a>
<a href="#" id="function-hr" class="function-button">
<span>Horizontal Rule</span></a>
<span class="function-divider">&nbsp;</span>
@@ -49,8 +50,8 @@
<a href="#" id="function-help" class="function-button">
<span>Help</span></a>
</div>
<div id="gollum-editor-format-selector">
<div id="gollum-editor-format-selector">
<select id="wiki_format" name="format">
{{#formats}}
<option {{#selected}}selected="selected" {{/selected}}value="{{id}}">
@@ -85,7 +86,7 @@
</div>
<textarea id="gollum-editor-body"
data-markup-lang="{{format}}" name="content">{{content}}</textarea>
{{#header}}
<div id="gollum-editor-edit-header" class="collapsed">
<a href="#" class="button"><span>Expand/Collapse</span></a>
@@ -93,7 +94,7 @@
<textarea id="gollum-editor-header" name="header">{{header}}</textarea>
</div>
{{/header}}
{{#footer}}
<div id="gollum-editor-edit-footer" class="collapsed">
<a href="#" class="button"><span>Expand/Collapse</span></a>
@@ -101,7 +102,7 @@
<textarea id="gollum-editor-footer" name="footer">{{footer}}</textarea>
</div>
{{/footer}}
{{#sidebar}}
<div id="gollum-editor-edit-sidebar" class="collapsed">
<a href="#" class="button"><span>Expand/Collapse</span></a>
@@ -109,7 +110,7 @@
<textarea id="gollum-editor-sidebar" name="sidebar">{{sidebar}}</textarea>
</div>
{{/sidebar}}
<div id="gollum-editor-edit-summary" class="singleline">
<label for="message" class="jaws">Edit message:</label>
{{#is_create_page}}
@@ -119,7 +120,7 @@
<input type="text" name="message" id="gollum-editor-message-field" value="Updated {{page_name}} ({{format}})">
{{/is_edit_page}}
</div>
<span class="jaws"><br></span>
<input type="submit" id="gollum-editor-submit" value="Save" title="Save current changes">
<a href="/preview" id="gollum-editor-preview" class="minibutton" title="Preview this Page">Preview</a>
@@ -5,25 +5,25 @@
<li class="minibutton">
{{>searchbar}}
</li>
<li class="minibutton"><a href="/{{escaped_name}}"
<li class="minibutton"><a href="/{{escaped_url_path}}"
class="action-view-page">View Page</a></li>
<li class="minibutton"><a href="/edit/{{escaped_name}}"
<li class="minibutton"><a href="/edit/{{escaped_url_path}}"
class="action-edit-page">Edit Page</a></li>
</ul>
</div>
<div id="wiki-history">
<ul class="actions">
<li class="minibutton"><a href="javascript:void(0);"
class="action-compare-revision">Compare Revisions</a></li>
</ul>
<form name="compare-versions" id="version-form" method="post"
action="/compare/{{escaped_name}}">
action="/compare/{{escaped_url_path}}">
<fieldset>
<table>
<tbody>
{{#versions}}
<tr>
<td class="checkbox">
@@ -39,11 +39,11 @@
<td class="commit-name">
<span class="time-elapsed">{{date}}:</span>&nbsp;
{{message}}
[<a href="/{{escaped_name}}/{{id}}" title="View commit">{{id7}}</a>]
[<a href="/{{escaped_url_path}}/{{id}}" title="View commit">{{id7}}</a>]
</td>
</tr>
{{/versions}}
</tbody>
</table>
</fieldset>
@@ -53,7 +53,7 @@
<ul class="actions">
<li class="minibutton"><a href="javascript:void(0);"
class="action-compare-revision">Compare Revisions</a></li>
<!-- only show this button if we have more than 20 revisions -->
<li class="minibutton"><a href="#"
class="action-back-to-top">Back to Top</a></li>
+2 -2
View File
@@ -12,10 +12,10 @@
<li class="minibutton" class="jaws">
<a href="#" id="minibutton-new-page">New Page</a></li>
{{#editable}}
<li class="minibutton"><a href="/edit/{{escaped_name}}"
<li class="minibutton"><a href="/edit/{{escaped_url_path}}"
class="action-edit-page">Edit Page</a></li>
{{/editable}}
<li class="minibutton"><a href="/history/{{escaped_name}}"
<li class="minibutton"><a href="/history/{{escaped_url_path}}"
class="action-page-history">Page History</a></li>
</ul>
</div>
+9 -8
View File
@@ -9,16 +9,17 @@
class="action-edit-page">Home</a></li>
</ul>
</div>
<div id="results">
<div id="pages">
{{#has_results}}
<ul>
{{#results}}
<li>
<a href="/{{name}}">{{name}}</a>
</li>
{{/results}}
</ul>
<div id="file-browser">
<div class="breadcrumb">
{{{breadcrumb}}}
</div>
<ul>
{{{files_folders}}}
</ul>
</div>
{{/has_results}}
{{#no_results}}
+2 -4
View File
@@ -1,16 +1,14 @@
module Precious
module Views
class Compare < Layout
include HasPage
attr_reader :page, :diff, :versions, :message
def title
"Comparison of #{@page.title}"
end
def path
@page.path
end
def before
@versions[0][0..6]
end
+1
View File
@@ -2,6 +2,7 @@ module Precious
module Views
class Edit < Layout
include Editable
include HasPage
attr_reader :page, :content
+15
View File
@@ -0,0 +1,15 @@
module Precious
module HasPage
def path
@page.path
end
def escaped_url_path
@page.escaped_url_path
end
def format
@page.format.to_s
end
end
end
+2
View File
@@ -1,6 +1,8 @@
module Precious
module Views
class History < Layout
include HasPage
attr_reader :page, :page_num
def title
+1 -1
View File
@@ -6,7 +6,7 @@ module Precious
include Rack::Utils
alias_method :h, :escape_html
attr_reader :name
attr_reader :name, :path
def escaped_name
CGI.escape(@name)
+3 -5
View File
@@ -1,16 +1,14 @@
module Precious
module Views
class Page < Layout
include HasPage
attr_reader :content, :page, :header, :footer
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
DEFAULT_AUTHOR = 'you'
def title
@page.title
end
def format
@page.format.to_s
@page.url_path.gsub("-", " ")
end
def author
+48
View File
@@ -1,3 +1,5 @@
require "pathname"
module Precious
module Views
class Pages < Layout
@@ -7,6 +9,52 @@ module Precious
"All pages in #{@ref}"
end
def breadcrumb
if @path
path = Pathname.new(@path)
breadcrumb = [%{<a href="/pages/">Home</a>}]
path.descend do |crumb|
title = crumb.basename
if title == path.basename
breadcrumb << title
else
breadcrumb << %{<a href="/pages/#{crumb}/">#{title}</a>}
end
end
breadcrumb.join(" / ")
else
"Home"
end
end
def files_folders
if has_results
folder_links = []
@results.map { |page|
page_path = page.path.sub(/^#{@path}\//,'')
if page_path.include?('/')
folder = page_path.split('/').first
folder_path = @path ? "#{@path}/#{folder}" : folder
folder_link = %{<li><a href="/pages/#{folder_path}/" class="folder">#{folder}</a></li>}
unless folder_links.include?(folder_link)
folder_links << folder_link
folder_link
end
elsif page_path != ".gitkeep"
%{<li><a href="/#{page.escaped_url_path}" class="file">#{page.name}</a></li>}
end
}.compact.join("\n")
else
""
end
end
def has_results
!@results.empty?
end
+22 -1
View File
@@ -153,6 +153,27 @@ module Gollum
# Returns the String path.
attr_reader :path
# Public: The url path required to reach this page within the repo.
#
# Returns the String url_path
def url_path
path = if self.path.include?('/')
self.path.sub(/\/.+$/, '/')
else
''
end
path << Page.cname(self.name, '-', '-')
path
end
# Public: The url_path, but CGI escaped.
#
# Returns the String url_path
def escaped_url_path
CGI.escape(self.url_path).gsub('%2F','/')
end
# Public: The raw contents of the page.
#
# Returns the String data.
@@ -179,7 +200,7 @@ module Gollum
#
# Returns the String data.
def formatted_data(encoding = nil, &block)
@blob && markup_class.render(historical?, encoding) do |doc|
@blob && markup_class.render(historical?, encoding) do |doc|
@doc = doc
yield doc if block_given?
end
+1 -1
View File
@@ -470,7 +470,7 @@ module Gollum
@repo.git.grep(*args).split("\n").map! do |line|
result = line.split(':')
file_name = Gollum::Page.canonicalize_filename(::File.basename(result[1]))
file_name = result[1].gsub( ::File.extname(result[1]), '' )
{
:count => result[2].to_i,