Refactor app.rb to use paged.

Create new pages relative to current directory.
This commit is contained in:
bootstraponline
2012-08-01 00:14:26 -06:00
parent 2261cfabf3
commit 72e26fc90b
6 changed files with 68 additions and 52 deletions
+49 -49
View File
@@ -90,23 +90,33 @@ module Precious
show_page_or_file('Home')
end
# name, path, version
def wiki_page( name, path = nil, version = nil )
path = name if path.nil?
name = extract_name(name)
path = extract_path(path)
wiki = wiki_new
wiki.paged(name, path, version)
end
def wiki_new
Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
end
get '/data/*' 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)
if page = wiki.page(@name)
if page = wiki_page(params[:splat].first)
page.raw_data
end
end
get '/edit/*' 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)
if page = wiki.page(@name)
splat = params[:splat].first
@name = extract_name(splat)
@path = extract_path(splat)
wiki = wiki_new
if page = wiki.paged(@name, @path)
if wiki.live_preview && page.format.to_s.include?('markdown') && supported_useragent?(request.user_agent)
live_preview_url = '/livepreview/index.html?page=' + encodeURIComponent(@name)
if @path
@@ -147,23 +157,20 @@ module Precious
end
get '/delete/*' 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)
wiki.delete_page(@page, { :message => "Destroyed #{@name} (#{@page.format})" })
name = params[:splat].first
page = wiki_page(params[:splat].first)
wiki = wiki_new
wiki.delete_page(page, { :message => "Destroyed #{name} (#{page.format})" })
redirect to('/')
end
get '/create/*' do
@path = extract_path(params[:splat].first)
@name = extract_name(params[:splat].first).to_url
wiki_options = settings.wiki_options.merge({ :page_file_dir => @path })
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)
@name = extract_name(params[:splat].first).to_url
@path = extract_path(params[:splat].first)
wiki = wiki_new
page = wiki.page(@name)
page = wiki.paged(@name,@path)
if page
redirect to("/#{page.escaped_url_path}")
else
@@ -191,10 +198,9 @@ module Precious
post '/revert/:page/*' do
@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)
@name = extract_name(params[:page])
wiki = wiki_new
@page = wiki.paged(@name,@path)
shas = params[:splat].first.split("/")
sha1 = shas.shift
sha2 = shas.shift
@@ -203,10 +209,10 @@ module Precious
redirect to("/#{@page.escaped_url_path}")
else
sha2, sha1 = sha1, "#{sha1}^" if !sha2
@versions = [sha1, sha2]
diffs = wiki.repo.diff(@versions.first, @versions.last, @page.path)
@diff = diffs.first
@message = "The patch does not apply."
@versions = [sha1, sha2]
diffs = wiki.repo.diff(@versions.first, @versions.last, @page.path)
@diff = diffs.first
@message = "The patch does not apply."
mustache :compare
end
end
@@ -223,11 +229,7 @@ module Precious
end
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 = wiki_page(params[:splat].first)
@page_num = [params[:page].to_i, 1].max
@versions = @page.versions :page => @page_num
mustache :history
@@ -258,9 +260,8 @@ module Precious
@path = extract_path(path)
@name = extract_name(path)
@versions = [start_version, end_version]
wiki_options = settings.wiki_options.merge({ :page_file_dir => @path })
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)
@page = wiki.page(@name)
wiki = wiki_new
@page = wiki.paged(@name,@path)
diffs = wiki.repo.diff(@versions.first, @versions.last, @page.path)
@diff = diffs.first
mustache :compare
@@ -277,12 +278,11 @@ module Precious
end
get %r{/(.+?)/([0-9a-f]{40})} do
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])
file_path = params[:captures][0]
name = extract_name(file_path)
path = extract_path(file_path)
version = params[:captures][1]
if page = wiki_page(name, path, version)
@page = page
@name = name
@content = page.formatted_data
@@ -327,19 +327,19 @@ module Precious
end
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)
path = extract_path(fullpath)
wiki = wiki_new
if page = wiki.page(name)
if page = wiki.paged(name, path)
@page = page
@name = name
@editable = true
@content = page.formatted_data
@toc_content = wiki.universal_toc ? @page.toc_data : nil
@mathjax = wiki.mathjax
# Set @path for mustache :page new_page_data_variables
@path = path
mustache :page
elsif file = wiki.file(fullpath)
content_type file.mime_type
+1 -1
View File
@@ -10,7 +10,7 @@
<li class="minibutton"><a href="{{base_url}}fileview"
class="action-all-pages">Files</a></li>
<li class="minibutton" class="jaws">
<a href="#" id="minibutton-new-page">New</a></li>
<a href="#" id="minibutton-new-page"{{{new_page_data_variables}}}>New</a></li>
<li class="minibutton" class="jaws">
<a href="#" id="minibutton-rename-page">Rename</a></li>
{{#editable}}
+1 -1
View File
@@ -8,7 +8,7 @@
<li class="minibutton"><a href="{{base_url}}"
class="action-edit-page">Home</a></li>
<li class="minibutton" class="jaws">
<a href="#" id="minibutton-new-page"{{{new_page_data_variables}}}>New Page</a>
<a href="#" id="minibutton-new-page"{{{new_page_data_variables}}}>New</a>
</li>
</ul>
</div>
+4
View File
@@ -7,6 +7,10 @@ module Precious
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
DEFAULT_AUTHOR = 'you'
def new_page_data_variables
%{ data-path="#{@path}"} if @path
end
def title
@page.url_path.gsub("-", " ")
end
+11
View File
@@ -201,6 +201,17 @@ module Gollum
@page_class.new(self).find(name, version, dir)
end
# Public: Convenience method instead of calling page(name, nil, dir).
#
# name - The human or canonical String page name of the wiki page.
# version - The String version ID to find (default: @ref).
# dir - The directory String relative to the repo.
#
# Returns a Gollum::Page or nil if no matching page was found.
def paged(name, dir = nil, version = @ref)
page(name, version, dir)
end
# Public: Get the static file for a given name.
#
# name - The full String pathname to the file.
+2 -1
View File
@@ -105,7 +105,8 @@ context "Wiki" do
index.commit 'Add Foobar/Elrond.', [wiki.repo.commits.last], Grit::Actor.new('Tom Preston-Werner', 'tom@github.com')
assert_equal 'Rivendell/Elrond.md', wiki.page('Elrond', nil, 'Rivendell').path
assert_equal 'Foobar/Elrond.md', wiki.page('Elrond', nil, 'Foobar').path
# test paged as well.
assert_equal 'Foobar/Elrond.md', wiki.paged('Elrond', 'Foobar').path
ensure
FileUtils.rm_rf(@path)
end