Skip to content
This repository has been archived by the owner on Feb 28, 2022. It is now read-only.

Commit

Permalink
Small refactor following Chris's comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
dazoakley committed May 24, 2012
1 parent c629abb commit eb85c5d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 29 deletions.
45 changes: 24 additions & 21 deletions lib/gollum/frontend/app.rb
Expand Up @@ -40,9 +40,8 @@ class App < Sinatra::Base
enable :logging, :raise_errors, :dump_errors
end

# Deagol helper function - this allows us to manage repositories
# with directories etc...
def set_the_path(file_path)
# Deagol helper - extract the path string that Gollum::Wiki expects
def extract_path(file_path)
path = file_path.dup
if path != '' && path != '/' && path.include?('/')
path.sub!(/\/[\w\-\_\.]*$/,'')
Expand All @@ -53,13 +52,18 @@ def set_the_path(file_path)
path
end

# Deagol helper - extract the 'page' name from the file_path
def extract_name(file_path)
file_path.split("/").last
end

get '/' do
redirect '/pages'
end

get '/data/*' do
@path = set_the_path(params[:splat].first)
@name = params[:splat].first.split("/").last
@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)
Expand All @@ -68,14 +72,14 @@ def set_the_path(file_path)
end

get '/edit/*' do
@path = set_the_path(params[:splat].first)
@name = params[:splat].first.split("/").last
@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)

@format = nil
if match = /^.+\.(\w+)$/.match(@name)
@format = match[1]
if @name =~ /^.+\.(\w+)$/
@format = $1
end

if page = wiki.page(@name)
Expand All @@ -93,10 +97,10 @@ def set_the_path(file_path)
end

post '/edit/*' do
@path = set_the_path(params[:splat].first)
@path = extract_path(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(params[:splat].first.split("/").last)
page = wiki.page(extract_name(params[:splat].first))
name = params[:rename] || page.name
committer = Gollum::Committer.new(wiki, commit_message)
commit = {:committer => committer}
Expand All @@ -112,13 +116,12 @@ def set_the_path(file_path)

post '/create' do
name = params[:page].split('/').last
path = set_the_path(params[:page].dup)
path = extract_path(params[:page].dup)
wiki_options = settings.wiki_options.merge({ :page_file_dir => path })
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)

filename_to_write = name.dup
format = params[:format].intern
@format = format
if filename_to_write =~ /^(.+)\.#{format}$/
filename_to_write = $1
end
Expand All @@ -134,7 +137,7 @@ def set_the_path(file_path)
end

post '/revert/:page/:version_list' do
@path = set_the_path(params[:page].dup)
@path = extract_path(params[:page].dup)
@name = params[:page].split('/').last
wiki_options = settings.wiki_options.merge({ :page_file_dir => @path })
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)
Expand Down Expand Up @@ -165,8 +168,8 @@ def set_the_path(file_path)
end

get '/history/*' do
@path = set_the_path(params[:splat].first)
@name = params[:splat].first.split("/").last
@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)
Expand All @@ -189,7 +192,7 @@ def set_the_path(file_path)
end

get '/compare/:name/:version_list' do
@path = set_the_path(params[:name].dup)
@path = extract_path(params[:name].dup)
@name = params[:name].split('/').last
@versions = params[:version_list].split(/\.{2,3}/)
wiki_options = settings.wiki_options.merge({ :page_file_dir => @path })
Expand All @@ -212,7 +215,7 @@ def set_the_path(file_path)

get %r{/(.+?)/([0-9a-f]{40})} do
file_path = params[:captures][0]
path = set_the_path(file_path.dup)
path = extract_path(file_path.dup)
name = file_path.split('/').last
wiki_options = settings.wiki_options.merge({ :page_file_dir => path })
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)
Expand All @@ -237,7 +240,7 @@ def set_the_path(file_path)
end

get '/pages*' do
@path = set_the_path(params[:splat].first)
@path = extract_path(params[:splat].first)
wiki_options = settings.wiki_options.merge({ :page_file_dir => @path })
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)
@results = wiki.pages
Expand All @@ -263,8 +266,8 @@ def show_page_or_file(name)
else
@name = name
@format = nil
if match = /^.+\.(\w+)$/.match(@name)
@format = match[1]
if name =~ /^.+\.(\w+)$/
@format = $1
end
mustache :create
end
Expand Down
3 changes: 1 addition & 2 deletions lib/gollum/frontend/public/gollum/javascript/gollum.js
Expand Up @@ -101,15 +101,14 @@ $(document).ready(function() {
$('#minibutton-new-page').removeClass('jaws');
$('#minibutton-new-page').click(function(e) {
e.preventDefault();
var path = $(this).attr('data-path') ? $(this).attr('data-path') : '';
$.GollumDialog.init({
title: 'Create A New Page',
fields: [
{
id: 'name',
name: 'Page Name',
type: 'text',
defaultValue: path
defaultValue: $(this).data('path') || ''
}
],
OK: function( res ) {
Expand Down
2 changes: 1 addition & 1 deletion lib/gollum/frontend/views/create.rb
Expand Up @@ -42,7 +42,7 @@ def page_name
end

def formats
super(format().to_sym)
super(format.to_sym)
end

def default_markup
Expand Down
7 changes: 4 additions & 3 deletions lib/gollum/frontend/views/pages.rb
Expand Up @@ -8,9 +8,10 @@ def title
end

def data_path
if @path && @path != '' && @path != '/'
" data-path=\"#{@path}/\""
end
@path =~ /^\/?(.*?)\/?$/
path = $1 || ''
path << '/' unless path.empty?
" data-path=\"#{path}\""
end

def breadcrumb
Expand Down
5 changes: 3 additions & 2 deletions lib/gollum/wiki.rb
Expand Up @@ -242,11 +242,12 @@ def write_page(name, format, data, commit = {})
end

filename = Gollum::Page.cname(name)
dir = page_file_dir || ''

committer.add_to_index(page_file_dir, filename, format, data)
committer.add_to_index(dir, filename, format, data)
committer.after_commit do |index, sha|
@access.refresh
index.update_working_dir(page_file_dir, filename, format)
index.update_working_dir(dir, filename, format)
end

multi_commit ? committer : committer.commit
Expand Down

0 comments on commit eb85c5d

Please sign in to comment.