From f9b8b4e8d3bfdc0adcc7b3f23c886aad2d9fe199 Mon Sep 17 00:00:00 2001 From: Dawa Ometto Date: Mon, 10 Apr 2017 14:50:18 +0200 Subject: [PATCH] Remove ws subs (#1220) * remove unwanted whitespace substitution * end repression of file extensions * see also https://github.com/gollum/gollum-lib/pull/249 --- lib/gollum/app.rb | 38 ++++++++++++-------------- lib/gollum/helpers.rb | 7 ++++- lib/gollum/templates/layout.mustache | 2 +- test/test_app.rb | 41 ++++++---------------------- test/test_unicode.rb | 10 ++++--- 5 files changed, 39 insertions(+), 59 deletions(-) diff --git a/lib/gollum/app.rb b/lib/gollum/app.rb index 2f0611f4..7a951d43 100644 --- a/lib/gollum/app.rb +++ b/lib/gollum/app.rb @@ -18,20 +18,16 @@ require File.expand_path '../helpers', __FILE__ Gollum::set_git_timeout(120) Gollum::set_git_max_filesize(190 * 10**6) -# Fix to_url +# Use stringex #to_url only to leverage its #to_ascii method when using grit class String - alias :upstream_to_url :to_url - if defined?(Gollum::GIT_ADAPTER) && Gollum::GIT_ADAPTER != 'grit' def to_ascii self # Do not transliterate utf-8 url's unless using Grit end end - # _Header => header which causes errors def to_url - return nil if self.nil? - upstream_to_url :exclude => ['_Header', '_Footer', '_Sidebar'], :force_downcase => false + to_ascii end end @@ -120,12 +116,12 @@ module Precious def wiki_page(name, path = nil, version = nil, exact = true) wiki = wiki_new path = name if path.nil? - name = extract_name(name) || wiki.index_page + name, ext = extract_name(name) || wiki.index_page path = extract_path(path) path = '/' if exact && path.nil? - OpenStruct.new(:wiki => wiki, :page => wiki.paged(name, path, exact, version), - :name => name, :path => path) + OpenStruct.new(:wiki => wiki, :page => wiki.paged(join_page_name(name, ext), path, exact, version), + :name => name, :path => path, :ext => ext) end def wiki_new @@ -157,7 +153,7 @@ module Precious get '/edit/*' do forbid unless @allow_editing wikip = wiki_page(params[:splat].first) - @name = wikip.name + @name = join_page_name(wikip.name, wikip.ext) @path = wikip.path @upload_dest = find_upload_dest(@path) @@ -247,7 +243,7 @@ module Precious wikip = wiki_page(params[:splat].first) halt 500 if wikip.nil? wiki = wikip.wiki - page = wiki.paged(wikip.name, wikip.path, exact = true) + page = wiki.paged(join_page_name(wikip.name, wikip.ext), wikip.path, exact = true) rename = params[:rename] halt 500 if page.nil? halt 500 if rename.nil? or rename.empty? @@ -274,7 +270,7 @@ module Precious committer.commit wikip = wiki_page(rename) - page = wiki.paged(wikip.name, wikip.path, exact = true) + page = wiki.paged(join_page_name(wikip.name, wikip.ext), wikip.path, exact = true) return if page.nil? redirect to("/#{page.escaped_url_path}") end @@ -300,7 +296,7 @@ module Precious get '/delete/*' do forbid unless @allow_editing wikip = wiki_page(params[:splat].first) - name = wikip.name + name = join_page_name(wikip.name, wikip.ext) wiki = wikip.wiki page = wikip.page unless page.nil? @@ -319,7 +315,7 @@ module Precious @template_page = (temppage.page != nil) ? temppage.page.raw_data : "Template page option is set, but no /_Template page is present or committed." end wikip = wiki_page(params[:splat].first.gsub('+', '-')) - @name = wikip.name.to_url + @name, ext = wikip.name.to_url @path = wikip.path @allow_uploads = wikip.wiki.allow_uploads @upload_dest = find_upload_dest(@path) @@ -354,7 +350,7 @@ module Precious wiki.write_page(name, format, params[:content], commit_message, path) page_dir = settings.wiki_options[:page_file_dir].to_s - redirect to("/#{clean_url(::File.join(page_dir, path, encodeURIComponent(name)))}") + redirect to("/#{clean_url(::File.join(encodeURIComponent(page_dir), encodeURIComponent(path), encodeURIComponent(wiki.page_file_name(name, format))))}") rescue Gollum::DuplicatePageError => e @message = "Duplicate page: #{e.message}" mustache :error @@ -364,7 +360,7 @@ module Precious post '/revert/*/:sha1/:sha2' do wikip = wiki_page(params[:splat].first) @path = wikip.path - @name = wikip.name + @name = join_page_name(wikip.name, wikip.ext) wiki = wikip.wiki @page = wiki.paged(@name, @path) sha1 = params[:sha1] @@ -446,7 +442,7 @@ module Precious }x do |path, start_version, end_version| wikip = wiki_page(path) @path = wikip.path - @name = wikip.name + @name = join_page_name(wikip.name, wikip.ext) @versions = [start_version, end_version] wiki = wikip.wiki @page = wikip.page @@ -459,7 +455,7 @@ module Precious file_path = params[:captures][0] version = params[:captures][1] wikip = wiki_page(file_path, file_path, version) - name = wikip.name + name = join_page_name(wikip.name, wikip.ext) path = wikip.path if page = wikip.page @page = page @@ -520,9 +516,9 @@ module Precious def show_page_or_file(fullpath) wiki = wiki_new - name = extract_name(fullpath) || wiki.index_page + name, ext = extract_name(fullpath) || wiki.index_page path = extract_path(fullpath) || '/' - if page = wiki.paged(name, path, exact = true) + if page = wiki.paged(join_page_name(name, ext), path, exact = true) @page = page @name = name @content = page.formatted_data @@ -541,7 +537,7 @@ module Precious show_file(file) else not_found unless @allow_editing - page_path = [path, name].compact.join('/') + page_path = [path, join_page_name(name, ext)].compact.join('/') redirect to("/create/#{clean_url(encodeURIComponent(page_path))}") end end diff --git a/lib/gollum/helpers.rb b/lib/gollum/helpers.rb index b5fdb030..b83d56ce 100644 --- a/lib/gollum/helpers.rb +++ b/lib/gollum/helpers.rb @@ -6,6 +6,10 @@ module Precious EMOJI_PATHNAME = Pathname.new(Gemojione.images_path).freeze + def join_page_name(name, ext) + "#{name}#{ext}" + end + # Extract the path string that Gollum::Wiki expects def extract_path(file_path) return nil if file_path.nil? @@ -23,7 +27,8 @@ module Precious # File.basename is too eager to please and will return the last # component of the path even if it ends with a directory separator. - ::File.basename(file_path) + ext = ::File.extname(file_path) + return ::File.basename(file_path, ext), ext end def sanitize_empty_params(param) diff --git a/lib/gollum/templates/layout.mustache b/lib/gollum/templates/layout.mustache index 7efba435..e46a6e03 100644 --- a/lib/gollum/templates/layout.mustache +++ b/lib/gollum/templates/layout.mustache @@ -21,7 +21,7 @@ var baseUrl = '{{base_url}}'; var uploadDest = '{{upload_dest}}'; {{#page}} - var pageFullPath = '{{url_path_display}}'; + var pageFullPath = '{{url_path}}'; {{/page}} diff --git a/test/test_app.rb b/test/test_app.rb index da912de7..ec884f5e 100644 --- a/test/test_app.rb +++ b/test/test_app.rb @@ -15,29 +15,6 @@ context "Frontend" do FileUtils.rm_rf(@path) end - test "urls transform unicode" do - header = '_Header' - footer = '_Footer' - sidebar = '_Sidebar' - - # header, footer, and sidebar must be preserved - # or gollum will not recognize them - assert_equal header, header.to_url - assert_equal footer, footer.to_url - assert_equal sidebar, sidebar.to_url - - # spaces are converted to dashes in URLs - # and in file names saved to disk - # urls are not case sensitive - assert_equal 'Title-Space', 'Title Space'.to_url - - # ascii only file names prevent UTF8 issues - # when using git repos across operating systems - # as this test demonstrates, translation is not - # great - assert_equal 'm-plus-F', 'μ†ℱ'.to_url - end - test "utf-8 kcode" do assert_equal 'μ†ℱ'.scan(/./), ["μ", "†", "ℱ"] end @@ -255,8 +232,8 @@ context "Frontend" do test "creates pages with escaped characters in title" do post "/create", :content => 'abc', :page => 'Title with spaces', :format => 'markdown', :message => 'foo' - assert_equal 'http://example.org/Title-with-spaces', last_response.headers['Location'] - get "/Title-with-spaces" + assert_equal 'http://example.org/Title%20with%20spaces.md', last_response.headers['Location'] + get "/Title%20with%20spaces" assert_match /abc/, last_response.body end @@ -280,7 +257,7 @@ context "Frontend" do post "/create", :content => 'abc', :page => 'Home', :path => '/foo/', :format => 'markdown', :message => 'foo' - assert_equal "http://example.org/foo/Home", last_response.headers['Location'] + assert_equal "http://example.org/foo/Home.md", last_response.headers['Location'] follow_redirect! assert last_response.ok? @@ -672,7 +649,7 @@ context "Frontend with lotr" do post "/create", :content => '123', :page => page, :path => 'Mordor', :format => 'markdown', :message => 'oooh, scary' # should be wiki/Mordor/path - assert_equal 'http://example.org/Mordor/' + page, last_response.headers['Location'] + assert_equal 'http://example.org/Mordor/' + page + '.md', last_response.headers['Location'] get '/Mordor/' + page assert_match /123/, last_response.body @@ -683,7 +660,7 @@ context "Frontend with lotr" do test "create pages within sub-directories using page file dir" do post "/create", :content => 'one two', :page => 'base', :path => 'wiki/Mordor', :format => 'markdown', :message => 'oooh, scary' - assert_equal 'http://example.org/wiki/Mordor/base', last_response.headers['Location'] + assert_equal 'http://example.org/wiki/Mordor/base.md', last_response.headers['Location'] get "/wiki/Mordor/base" assert_match /one two/, last_response.body @@ -693,14 +670,14 @@ context "Frontend with lotr" do test "create pages within sub-directories" do post "/create", :content => 'big smelly creatures', :page => 'Orc', :path => 'Mordor', :format => 'markdown', :message => 'oooh, scary' - assert_equal 'http://example.org/Mordor/Orc', last_response.headers['Location'] + assert_equal 'http://example.org/Mordor/Orc.md', last_response.headers['Location'] get "/Mordor/Orc" assert_match /big smelly creatures/, last_response.body post "/create", :content => 'really big smelly creatures', :page => 'Uruk Hai', :path => 'Mordor', :format => 'markdown', :message => 'oooh, very scary' - assert_equal 'http://example.org/Mordor/Uruk-Hai', last_response.headers['Location'] - get "/Mordor/Uruk-Hai" + assert_equal 'http://example.org/Mordor/Uruk%20Hai.md', last_response.headers['Location'] + get "/Mordor/Uruk%20Hai" assert_match /really big smelly creatures/, last_response.body end @@ -708,7 +685,7 @@ context "Frontend with lotr" do post "/create", :content => 'big smelly creatures', :page => 'Orc', :path => 'Mordor', :format => 'markdown', :message => 'oooh, scary' - assert_equal 'http://example.org/Mordor/Orc', last_response.headers['Location'] + assert_equal 'http://example.org/Mordor/Orc.md', last_response.headers['Location'] post "/edit/Mordor/Orc", :content => 'not so big smelly creatures', :page => 'Orc', :path => 'Mordor', :message => 'minor edit' diff --git a/test/test_unicode.rb b/test/test_unicode.rb index 3dac8900..23826928 100644 --- a/test/test_unicode.rb +++ b/test/test_unicode.rb @@ -89,10 +89,12 @@ context "Frontend Unicode support" do assert_equal 'ghi', page.version.message end - test 'transliteration' do - # TODO: Remove to_url once write_page changes are merged. - @wiki.write_page('ééééé'.to_url, :markdown, '한글 text', { :name => '', :email => '' }) - page = @wiki.page('eeeee') + test 'unicode filenames' do + # we transliterate only when adapter is grit + return if defined?(Gollum::GIT_ADAPTER) && Gollum::GIT_ADAPTER != 'grit' + + @wiki.write_page("ééééé".to_url, :markdown, '한글 text', { :name => '', :email => '' }) + page = @wiki.page("eeeee".to_url) assert_equal '한글 text', utf8(page.raw_data) end