From 72e26fc90b567d2b5ffa46d7c06e8433adb9db87 Mon Sep 17 00:00:00 2001 From: bootstraponline Date: Wed, 1 Aug 2012 00:14:26 -0600 Subject: [PATCH] Refactor app.rb to use paged. Create new pages relative to current directory. --- lib/gollum/frontend/app.rb | 98 ++++++++++---------- lib/gollum/frontend/templates/page.mustache | 2 +- lib/gollum/frontend/templates/pages.mustache | 2 +- lib/gollum/frontend/views/page.rb | 4 + lib/gollum/wiki.rb | 11 +++ test/test_wiki.rb | 3 +- 6 files changed, 68 insertions(+), 52 deletions(-) diff --git a/lib/gollum/frontend/app.rb b/lib/gollum/frontend/app.rb index 11f7ad6c..675a9036 100644 --- a/lib/gollum/frontend/app.rb +++ b/lib/gollum/frontend/app.rb @@ -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 diff --git a/lib/gollum/frontend/templates/page.mustache b/lib/gollum/frontend/templates/page.mustache index 1d108a64..1936c17d 100644 --- a/lib/gollum/frontend/templates/page.mustache +++ b/lib/gollum/frontend/templates/page.mustache @@ -10,7 +10,7 @@
  • Files
  • - New
  • + New
  • Rename
  • {{#editable}} diff --git a/lib/gollum/frontend/templates/pages.mustache b/lib/gollum/frontend/templates/pages.mustache index cc1103b1..6a94e4e1 100644 --- a/lib/gollum/frontend/templates/pages.mustache +++ b/lib/gollum/frontend/templates/pages.mustache @@ -8,7 +8,7 @@
  • Home
  • - New Page + New
  • diff --git a/lib/gollum/frontend/views/page.rb b/lib/gollum/frontend/views/page.rb index 84246a9d..8afac5f0 100644 --- a/lib/gollum/frontend/views/page.rb +++ b/lib/gollum/frontend/views/page.rb @@ -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 diff --git a/lib/gollum/wiki.rb b/lib/gollum/wiki.rb index af6e164c..70337a0d 100644 --- a/lib/gollum/wiki.rb +++ b/lib/gollum/wiki.rb @@ -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. diff --git a/test/test_wiki.rb b/test/test_wiki.rb index 7e68ab16..9e2c2176 100644 --- a/test/test_wiki.rb +++ b/test/test_wiki.rb @@ -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