Permalink
Browse files

Move ASCII filename conversion to backend.

  • Loading branch information...
1 parent 55786e2 commit 503756165cc9b53c19f41951c0c793ca4c4e8929 @bootstraponline bootstraponline committed Jul 11, 2012
Showing with 48 additions and 33 deletions.
  1. +1 −2 lib/gollum/frontend/app.rb
  2. +14 −0 lib/gollum/wiki.rb
  3. +1 −1 test/test_app.rb
  4. +6 −6 test/test_markup.rb
  5. +3 −1 test/test_page.rb
  6. +23 −23 test/test_wiki.rb
@@ -3,7 +3,6 @@
require 'gollum'
require 'mustache/sinatra'
require 'useragent'
-require 'stringex'
require 'gollum/frontend/views/layout'
require 'gollum/frontend/views/editable'
@@ -143,7 +142,7 @@ def supported_useragent?(user_agent)
end
post '/create' do
- name = params[:page].to_url
+ name = params[:page]
path = sanitize_empty_params(params[:path])
format = params[:format].intern
View
@@ -1,3 +1,14 @@
+require 'stringex'
+
+class String
+ alias :upstream_to_url :to_url
+ # _Header => header which causes errors
+ def to_url
+ return self if ['_Header', '_Footer', '_Sidebar'].include? self
+ upstream_to_url
+ end
+end
+
module Gollum
class Wiki
include Pagination
@@ -196,6 +207,7 @@ def exist?
#
# Returns a Gollum::Page or nil if no matching page was found.
def page(name, version = @ref)
+ name = name.to_url
@page_class.new(self).find(name, version)
end
@@ -247,6 +259,7 @@ def preview_page(name, data, format)
# Returns the String SHA1 of the newly written version, or the
# Gollum::Committer instance if this is part of a batch update.
def write_page(name, format, data, commit = {})
+ name = name.to_url
multi_commit = false
committer = if obj = commit[:committer]
@@ -292,6 +305,7 @@ def write_page(name, format, data, commit = {})
# Gollum::Committer instance if this is part of a batch update.
def update_page(page, name, format, data, commit = {})
name ||= page.name
+ name = name.to_url
format ||= page.format
dir = ::File.dirname(page.path)
dir = '' if dir == '.'
View
@@ -88,7 +88,7 @@
:rename => "C", :page => 'B',
:format => page_1.format, :message => 'def'
follow_redirect!
- assert_equal "/C", last_request.fullpath
+ assert_equal "/c", last_request.fullpath
assert last_response.ok?
@wiki.clear_cache
View
@@ -102,7 +102,7 @@
page = @wiki.page("Bilbo Baggins")
output = page.formatted_data
assert_match /class="internal present"/, output
- assert_match /href="\/Bilbo-Baggins"/, output
+ assert_match /href="\/bilbo-baggins"/, output
assert_match /\>Bilbo Baggins\</, output
end
@@ -135,7 +135,7 @@
page = @wiki.page(name)
output = page.formatted_data
assert_match /class="internal present"/, output
- assert_match /href="\/wiki\/Bilbo-Baggins-\d"/, output
+ assert_match /href="\/wiki\/bilbo-baggins-\d"/, output
assert_match /\>Bilbo Baggins \d\</, output
end
end
@@ -145,15 +145,15 @@
page = @wiki.page('Precious #1')
output = page.formatted_data
assert_match /class="internal present"/, output
- assert_match /href="\/Precious-%231"/, output
+ assert_match /href="\/precious-number-1"/, output
end
test "page link with extra #" do
@wiki.write_page("Potato", :markdown, "a [[Potato#1]] b", commit_details)
page = @wiki.page('Potato')
output = page.formatted_data
assert_match /class="internal present"/, output
- assert_match /href="\/Potato#1"/, output
+ assert_match /href="\/potato#1"/, output
end
test "external page link" do
@@ -167,14 +167,14 @@
@wiki.write_page("Potato", :markdown, "a [[Potato Heaad|Potato]] ", commit_details)
page = @wiki.page("Potato")
output = page.formatted_data
- assert_equal "<p>a<aclass=\"internalpresent\"href=\"/Potato\">PotatoHeaad</a></p>", normal(output)
+ assert_equal "<p>a<aclass=\"internalpresent\"href=\"/potato\">PotatoHeaad</a></p>", normal(output)
end
test "page link with different text on mediawiki" do
@wiki.write_page("Potato", :mediawiki, "a [[Potato|Potato Heaad]] ", commit_details)
page = @wiki.page("Potato")
output = page.formatted_data
- assert_equal normal("<p>\na <a class=\"internal present\" href=\"/Potato\">Potato Heaad</a> </p>
+ assert_equal normal("<p>\na <a class=\"internal present\" href=\"/potato\">Potato Heaad</a> </p>
"), normal(output)
end
View
@@ -31,7 +31,9 @@
end
test "get existing page with underscore" do
- assert_nil @wiki.page('Bilbo_Baggins')
+ # underscores are now converted to dashes by stringex.to_url
+ # Bilbo_Baggins maps to Bilbo-Baggins which exists
+ assert_equal @wiki.page('Bilbo_Baggins').path, @wiki.page('Bilbo-Baggins').path
end
test "get existing page where filename contains whitespace, with hypen" do
View
@@ -219,7 +219,7 @@
@wiki.update_page(page, page.name, :textile, "h1. Gollum", commit_details)
page = @wiki.page("Gollum")
- assert_equal "lotr/Gollum.textile", page.path
+ assert_equal "lotr/gollum.textile", page.path
assert_equal :textile, page.format
assert_equal "h1. Gollum", page.raw_data
end
@@ -270,7 +270,7 @@
assert_equal :textile, @wiki.page("Samwise Gamgee").format
assert_equal "h1. Samwise Gamgee2", @wiki.page("Samwise Gamgee").raw_data
- assert_equal "Samwise Gamgee.textile", @wiki.page("Samwise Gamgee").filename
+ assert_equal "samwise-gamgee.textile", @wiki.page("Samwise Gamgee").filename
end
test "update page with format change, verify non-canonicalization of filename, where filename contains Whitespace" do
@@ -282,7 +282,7 @@
assert_equal :textile, @wiki.page("Samwise Gamgee").format
assert_equal "h1. Samwise Gamgee", @wiki.page("Samwise Gamgee").raw_data
- assert_equal "Samwise Gamgee.textile", @wiki.page("Samwise Gamgee").filename
+ assert_equal "samwise-gamgee.textile", @wiki.page("Samwise Gamgee").filename
end
test "update page with name change, verify canonicalization of filename, where filename contains Whitespace" do
@@ -293,7 +293,7 @@
@wiki.update_page(page, 'Sam Gamgee', :textile, "h1. Samwise Gamgee", commit_details)
assert_equal "h1. Samwise Gamgee", @wiki.page("Sam Gamgee").raw_data
- assert_equal "Sam-Gamgee.textile", @wiki.page("Sam Gamgee").filename
+ assert_equal "sam-gamgee.textile", @wiki.page("Sam Gamgee").filename
end
test "update page with name and format change, verify canonicalization of filename, where filename contains Whitespace" do
@@ -305,7 +305,7 @@
assert_equal :textile, @wiki.page("Sam Gamgee").format
assert_equal "h1. Samwise Gamgee", @wiki.page("Sam Gamgee").raw_data
- assert_equal "Sam-Gamgee.textile", @wiki.page("Sam Gamgee").filename
+ assert_equal "sam-gamgee.textile", @wiki.page("Sam Gamgee").filename
end
teardown do
@@ -322,45 +322,45 @@
test "write a page" do
@wiki.write_page("New Page", :markdown, "Hi", commit_details)
- assert_equal "Hi", File.read(File.join(@path, "New-Page.md"))
+ assert_equal "Hi", File.read(File.join(@path, "new-page.md"))
end
test "update a page with same name and format" do
@wiki.write_page("New Page", :markdown, "Hi", commit_details)
page = @wiki.page("New Page")
@wiki.update_page(page, page.name, page.format, "Bye", commit_details)
- assert_equal "Bye", File.read(File.join(@path, "New-Page.md"))
+ assert_equal "Bye", File.read(File.join(@path, "new-page.md"))
end
test "update a page with different name and same format" do
@wiki.write_page("New Page", :markdown, "Hi", commit_details)
page = @wiki.page("New Page")
@wiki.update_page(page, "New Page 2", page.format, "Bye", commit_details)
- assert_equal "Bye", File.read(File.join(@path, "New-Page-2.md"))
- assert !File.exist?(File.join(@path, "New-Page.md"))
+ assert_equal "Bye", File.read(File.join(@path, "new-page-2.md"))
+ assert !File.exist?(File.join(@path, "new-page.md"))
end
test "update a page with same name and different format" do
@wiki.write_page("New Page", :markdown, "Hi", commit_details)
page = @wiki.page("New Page")
@wiki.update_page(page, page.name, :textile, "Bye", commit_details)
- assert_equal "Bye", File.read(File.join(@path, "New-Page.textile"))
- assert !File.exist?(File.join(@path, "New-Page.md"))
+ assert_equal "Bye", File.read(File.join(@path, "new-page.textile"))
+ assert !File.exist?(File.join(@path, "new-page.md"))
end
test "update a page with different name and different format" do
@wiki.write_page("New Page", :markdown, "Hi", commit_details)
page = @wiki.page("New Page")
@wiki.update_page(page, "New Page 2", :textile, "Bye", commit_details)
- assert_equal "Bye", File.read(File.join(@path, "New-Page-2.textile"))
- assert !File.exist?(File.join(@path, "New-Page.md"))
+ assert_equal "Bye", File.read(File.join(@path, "new-page-2.textile"))
+ assert !File.exist?(File.join(@path, "new-page.md"))
end
test "delete a page" do
@wiki.write_page("New Page", :markdown, "Hi", commit_details)
page = @wiki.page("New Page")
@wiki.delete_page(page, commit_details)
- assert !File.exist?(File.join(@path, "New-Page.md"))
+ assert !File.exist?(File.join(@path, "new-page.md"))
end
teardown do
@@ -376,34 +376,34 @@
test "update a page with same name and format" do
page = @wiki.page("Samwise Gamgee")
@wiki.update_page(page, page.name, page.format, "What we need is a few good taters.", commit_details)
- assert_equal "What we need is a few good taters.", File.read(File.join(@path, "Samwise Gamgee.mediawiki"))
+ assert_equal "What we need is a few good taters.", File.read(File.join(@path, "samwise-gamgee.mediawiki"))
end
test "update a page with different name and same format" do
page = @wiki.page("Samwise Gamgee")
@wiki.update_page(page, "Sam Gamgee", page.format, "What we need is a few good taters.", commit_details)
- assert_equal "What we need is a few good taters.", File.read(File.join(@path, "Sam-Gamgee.mediawiki"))
+ assert_equal "What we need is a few good taters.", File.read(File.join(@path, "sam-gamgee.mediawiki"))
assert !File.exist?(File.join(@path, "Samwise Gamgee"))
end
test "update a page with same name and different format" do
page = @wiki.page("Samwise Gamgee")
@wiki.update_page(page, page.name, :textile, "What we need is a few good taters.", commit_details)
- assert_equal "What we need is a few good taters.", File.read(File.join(@path, "Samwise Gamgee.textile"))
- assert !File.exist?(File.join(@path, "Samwise Gamgee.mediawiki"))
+ assert_equal "What we need is a few good taters.", File.read(File.join(@path, "samwise-gamgee.textile"))
+ assert !File.exist?(File.join(@path, "samwise-gamgee.mediawiki"))
end
test "update a page with different name and different format" do
page = @wiki.page("Samwise Gamgee")
@wiki.update_page(page, "Sam Gamgee", :textile, "What we need is a few good taters.", commit_details)
- assert_equal "What we need is a few good taters.", File.read(File.join(@path, "Sam-Gamgee.textile"))
- assert !File.exist?(File.join(@path, "Samwise Gamgee.mediawiki"))
+ assert_equal "What we need is a few good taters.", File.read(File.join(@path, "sam-gamgee.textile"))
+ assert !File.exist?(File.join(@path, "samwise gamgee.mediawiki"))
end
test "delete a page" do
page = @wiki.page("Samwise Gamgee")
@wiki.delete_page(page, commit_details)
- assert !File.exist?(File.join(@path, "Samwise Gamgee.mediawiki"))
+ assert !File.exist?(File.join(@path, "samwise gamgee.mediawiki"))
end
teardown do
@@ -421,8 +421,8 @@
test "write a page in sub directory" do
@wiki.write_page("New Page", :markdown, "Hi", commit_details)
- assert_equal "Hi", File.read(File.join(@path, @page_file_dir, "New-Page.md"))
- assert !File.exist?(File.join(@path, "New-Page.md"))
+ assert_equal "Hi", File.read(File.join(@path, @page_file_dir, "new-page.md"))
+ assert !File.exist?(File.join(@path, "new-page.md"))
end
test "edit a page in a sub directory" do

0 comments on commit 5037561

Please sign in to comment.