Refactor app.rb to use paged.
Create new pages relative to current directory.
This commit is contained in:
+49
-49
@@ -90,23 +90,33 @@ module Precious
|
|||||||
show_page_or_file('Home')
|
show_page_or_file('Home')
|
||||||
end
|
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
|
get '/data/*' do
|
||||||
@path = extract_path(params[:splat].first)
|
if page = wiki_page(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
|
page.raw_data
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/edit/*' do
|
get '/edit/*' do
|
||||||
@path = extract_path(params[:splat].first)
|
splat = params[:splat].first
|
||||||
@name = extract_name(params[:splat].first)
|
@name = extract_name(splat)
|
||||||
wiki_options = settings.wiki_options.merge({ :page_file_dir => @path })
|
@path = extract_path(splat)
|
||||||
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)
|
wiki = wiki_new
|
||||||
|
if page = wiki.paged(@name, @path)
|
||||||
if page = wiki.page(@name)
|
|
||||||
if wiki.live_preview && page.format.to_s.include?('markdown') && supported_useragent?(request.user_agent)
|
if wiki.live_preview && page.format.to_s.include?('markdown') && supported_useragent?(request.user_agent)
|
||||||
live_preview_url = '/livepreview/index.html?page=' + encodeURIComponent(@name)
|
live_preview_url = '/livepreview/index.html?page=' + encodeURIComponent(@name)
|
||||||
if @path
|
if @path
|
||||||
@@ -147,23 +157,20 @@ module Precious
|
|||||||
end
|
end
|
||||||
|
|
||||||
get '/delete/*' do
|
get '/delete/*' do
|
||||||
@path = extract_path(params[:splat].first)
|
name = params[:splat].first
|
||||||
@name = extract_name(params[:splat].first)
|
page = wiki_page(params[:splat].first)
|
||||||
wiki_options = settings.wiki_options.merge({ :page_file_dir => @path })
|
wiki = wiki_new
|
||||||
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)
|
wiki.delete_page(page, { :message => "Destroyed #{name} (#{page.format})" })
|
||||||
@page = wiki.page(@name)
|
|
||||||
wiki.delete_page(@page, { :message => "Destroyed #{@name} (#{@page.format})" })
|
|
||||||
|
|
||||||
redirect to('/')
|
redirect to('/')
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/create/*' do
|
get '/create/*' do
|
||||||
@path = extract_path(params[:splat].first)
|
@name = extract_name(params[:splat].first).to_url
|
||||||
@name = extract_name(params[:splat].first).to_url
|
@path = extract_path(params[:splat].first)
|
||||||
wiki_options = settings.wiki_options.merge({ :page_file_dir => @path })
|
wiki = wiki_new
|
||||||
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)
|
|
||||||
|
|
||||||
page = wiki.page(@name)
|
page = wiki.paged(@name,@path)
|
||||||
if page
|
if page
|
||||||
redirect to("/#{page.escaped_url_path}")
|
redirect to("/#{page.escaped_url_path}")
|
||||||
else
|
else
|
||||||
@@ -191,10 +198,9 @@ module Precious
|
|||||||
|
|
||||||
post '/revert/:page/*' do
|
post '/revert/:page/*' do
|
||||||
@path = extract_path(params[:page])
|
@path = extract_path(params[:page])
|
||||||
@name = params[:page]
|
@name = extract_name(params[:page])
|
||||||
wiki_options = settings.wiki_options.merge({ :page_file_dir => @path })
|
wiki = wiki_new
|
||||||
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)
|
@page = wiki.paged(@name,@path)
|
||||||
@page = wiki.page(@name)
|
|
||||||
shas = params[:splat].first.split("/")
|
shas = params[:splat].first.split("/")
|
||||||
sha1 = shas.shift
|
sha1 = shas.shift
|
||||||
sha2 = shas.shift
|
sha2 = shas.shift
|
||||||
@@ -203,10 +209,10 @@ module Precious
|
|||||||
redirect to("/#{@page.escaped_url_path}")
|
redirect to("/#{@page.escaped_url_path}")
|
||||||
else
|
else
|
||||||
sha2, sha1 = sha1, "#{sha1}^" if !sha2
|
sha2, sha1 = sha1, "#{sha1}^" if !sha2
|
||||||
@versions = [sha1, sha2]
|
@versions = [sha1, sha2]
|
||||||
diffs = wiki.repo.diff(@versions.first, @versions.last, @page.path)
|
diffs = wiki.repo.diff(@versions.first, @versions.last, @page.path)
|
||||||
@diff = diffs.first
|
@diff = diffs.first
|
||||||
@message = "The patch does not apply."
|
@message = "The patch does not apply."
|
||||||
mustache :compare
|
mustache :compare
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -223,11 +229,7 @@ module Precious
|
|||||||
end
|
end
|
||||||
|
|
||||||
get '/history/*' do
|
get '/history/*' do
|
||||||
@path = extract_path(params[:splat].first)
|
@page = wiki_page(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
|
@page_num = [params[:page].to_i, 1].max
|
||||||
@versions = @page.versions :page => @page_num
|
@versions = @page.versions :page => @page_num
|
||||||
mustache :history
|
mustache :history
|
||||||
@@ -258,9 +260,8 @@ module Precious
|
|||||||
@path = extract_path(path)
|
@path = extract_path(path)
|
||||||
@name = extract_name(path)
|
@name = extract_name(path)
|
||||||
@versions = [start_version, end_version]
|
@versions = [start_version, end_version]
|
||||||
wiki_options = settings.wiki_options.merge({ :page_file_dir => @path })
|
wiki = wiki_new
|
||||||
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)
|
@page = wiki.paged(@name,@path)
|
||||||
@page = wiki.page(@name)
|
|
||||||
diffs = wiki.repo.diff(@versions.first, @versions.last, @page.path)
|
diffs = wiki.repo.diff(@versions.first, @versions.last, @page.path)
|
||||||
@diff = diffs.first
|
@diff = diffs.first
|
||||||
mustache :compare
|
mustache :compare
|
||||||
@@ -277,12 +278,11 @@ module Precious
|
|||||||
end
|
end
|
||||||
|
|
||||||
get %r{/(.+?)/([0-9a-f]{40})} do
|
get %r{/(.+?)/([0-9a-f]{40})} do
|
||||||
file_path = params[:captures][0]
|
file_path = params[:captures][0]
|
||||||
path = extract_path(file_path)
|
name = extract_name(file_path)
|
||||||
name = extract_name(file_path)
|
path = extract_path(file_path)
|
||||||
wiki_options = settings.wiki_options.merge({ :page_file_dir => path })
|
version = params[:captures][1]
|
||||||
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)
|
if page = wiki_page(name, path, version)
|
||||||
if page = wiki.page(name, params[:captures][1])
|
|
||||||
@page = page
|
@page = page
|
||||||
@name = name
|
@name = name
|
||||||
@content = page.formatted_data
|
@content = page.formatted_data
|
||||||
@@ -327,19 +327,19 @@ module Precious
|
|||||||
end
|
end
|
||||||
|
|
||||||
def show_page_or_file(fullpath)
|
def show_page_or_file(fullpath)
|
||||||
path = extract_path(fullpath)
|
|
||||||
name = extract_name(fullpath)
|
name = extract_name(fullpath)
|
||||||
wiki_options = settings.wiki_options.merge({ :page_file_dir => path })
|
path = extract_path(fullpath)
|
||||||
wiki = Gollum::Wiki.new(settings.gollum_path, wiki_options)
|
wiki = wiki_new
|
||||||
|
|
||||||
if page = wiki.page(name)
|
if page = wiki.paged(name, path)
|
||||||
@page = page
|
@page = page
|
||||||
@name = name
|
@name = name
|
||||||
@editable = true
|
@editable = true
|
||||||
@content = page.formatted_data
|
@content = page.formatted_data
|
||||||
@toc_content = wiki.universal_toc ? @page.toc_data : nil
|
@toc_content = wiki.universal_toc ? @page.toc_data : nil
|
||||||
@mathjax = wiki.mathjax
|
@mathjax = wiki.mathjax
|
||||||
|
# Set @path for mustache :page new_page_data_variables
|
||||||
|
@path = path
|
||||||
mustache :page
|
mustache :page
|
||||||
elsif file = wiki.file(fullpath)
|
elsif file = wiki.file(fullpath)
|
||||||
content_type file.mime_type
|
content_type file.mime_type
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
<li class="minibutton"><a href="{{base_url}}fileview"
|
<li class="minibutton"><a href="{{base_url}}fileview"
|
||||||
class="action-all-pages">Files</a></li>
|
class="action-all-pages">Files</a></li>
|
||||||
<li class="minibutton" class="jaws">
|
<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">
|
<li class="minibutton" class="jaws">
|
||||||
<a href="#" id="minibutton-rename-page">Rename</a></li>
|
<a href="#" id="minibutton-rename-page">Rename</a></li>
|
||||||
{{#editable}}
|
{{#editable}}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
<li class="minibutton"><a href="{{base_url}}"
|
<li class="minibutton"><a href="{{base_url}}"
|
||||||
class="action-edit-page">Home</a></li>
|
class="action-edit-page">Home</a></li>
|
||||||
<li class="minibutton" class="jaws">
|
<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>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -7,6 +7,10 @@ module Precious
|
|||||||
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
|
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
|
||||||
DEFAULT_AUTHOR = 'you'
|
DEFAULT_AUTHOR = 'you'
|
||||||
|
|
||||||
|
def new_page_data_variables
|
||||||
|
%{ data-path="#{@path}"} if @path
|
||||||
|
end
|
||||||
|
|
||||||
def title
|
def title
|
||||||
@page.url_path.gsub("-", " ")
|
@page.url_path.gsub("-", " ")
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -201,6 +201,17 @@ module Gollum
|
|||||||
@page_class.new(self).find(name, version, dir)
|
@page_class.new(self).find(name, version, dir)
|
||||||
end
|
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.
|
# Public: Get the static file for a given name.
|
||||||
#
|
#
|
||||||
# name - The full String pathname to the file.
|
# name - The full String pathname to the file.
|
||||||
|
|||||||
+2
-1
@@ -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')
|
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 '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
|
ensure
|
||||||
FileUtils.rm_rf(@path)
|
FileUtils.rm_rf(@path)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user