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

Commit

Permalink
Merge pull request #5 from nature/create_new_file
Browse files Browse the repository at this point in the history
Create New Files
  • Loading branch information
dazoakley committed May 25, 2012
2 parents 9c78699 + eb85c5d commit 27fbd79
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/gollum/committer.rb
Expand Up @@ -88,7 +88,7 @@ def add_to_index(dir, name, format, data, allow_same_ext = false)

dir = '/' if dir.strip.empty?

fullpath = ::File.join(*[@wiki.page_file_dir, dir, path].compact)
fullpath = ::File.join(*[@wiki.page_file_dir, path].compact)
fullpath = fullpath[1..-1] if fullpath =~ /^\//

if index.current_tree && tree = index.current_tree / dir
Expand Down
62 changes: 42 additions & 20 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,10 +72,16 @@ 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 @name =~ /^.+\.(\w+)$/
@format = $1
end

if page = wiki.page(@name)
if page.format.to_s.include?('markdown')
redirect '/livepreview/index.html?page=' + encodeURIComponent(@name)
Expand All @@ -87,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 @@ -105,21 +115,29 @@ def set_the_path(file_path)
end

post '/create' do
name = params[:page]
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
format = params[:format].intern
name = params[:page].split('/').last
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
if filename_to_write =~ /^(.+)\.#{format}$/
filename_to_write = $1
end

begin
wiki.write_page(name, format, params[:content], commit_message)
redirect "/#{CGI.escape(page.path)}"
wiki.write_page(filename_to_write, format, params[:content], commit_message)
page = wiki.page(name)
redirect "/#{CGI.escape(page.path).gsub('%2F','/')}"
rescue Gollum::DuplicatePageError => e
@message = "Duplicate page: #{e.message}"
mustache :error
end
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 @@ -150,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 @@ -174,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 @@ -197,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 @@ -222,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 @@ -247,6 +265,10 @@ def show_page_or_file(name)
file.raw_data
else
@name = name
@format = nil
if name =~ /^.+\.(\w+)$/
@format = $1
end
mustache :create
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/gollum/frontend/public/gollum/javascript/gollum.js
Expand Up @@ -102,12 +102,13 @@ $(document).ready(function() {
$('#minibutton-new-page').click(function(e) {
e.preventDefault();
$.GollumDialog.init({
title: 'Create New Page',
title: 'Create A New Page',
fields: [
{
id: 'name',
name: 'Page Name',
type: 'text'
type: 'text',
defaultValue: $(this).data('path') || ''
}
],
OK: function( res ) {
Expand Down
3 changes: 3 additions & 0 deletions lib/gollum/frontend/templates/pages.mustache
Expand Up @@ -7,6 +7,9 @@
</li>
<li class="minibutton"><a href="/"
class="action-edit-page">Home</a></li>
<li class="minibutton" class="jaws">
<a href="#" id="minibutton-new-page"{{{data_path}}}">New Page</a>
</li>
</ul>
</div>
<div id="pages">
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(:markdown)
super(format.to_sym)
end

def default_markup
Expand Down
4 changes: 4 additions & 0 deletions lib/gollum/frontend/views/edit.rb
Expand Up @@ -58,6 +58,10 @@ def format
@format = (@page.format || false) if @format.nil?
@format.to_s.downcase
end

def formats
super(format().to_sym)
end
end
end
end
7 changes: 7 additions & 0 deletions lib/gollum/frontend/views/pages.rb
Expand Up @@ -7,6 +7,13 @@ def title
"Browse Files"
end

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

def breadcrumb
breadcrumb = []
slugs = "All Files/#{@path}".split('/')
Expand Down
2 changes: 1 addition & 1 deletion lib/gollum/page.rb
Expand Up @@ -242,7 +242,7 @@ def historical?
# Returns the String canonical name.
def self.cname(name, char_white_sub = '-', char_other_sub = '-')
name.respond_to?(:gsub) ?
name.gsub(%r{\s},char_white_sub).gsub(%r{[/<>+]}, char_other_sub) :
name.gsub(%r{\s},char_white_sub).gsub(%r{[<>+]}, char_other_sub) :
''
end

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

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

committer.add_to_index('', filename, format, data)

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

multi_commit ? committer : committer.commit
Expand Down

0 comments on commit 27fbd79

Please sign in to comment.